Skip to content

Commit

Permalink
Variations (#8)
Browse files Browse the repository at this point in the history
* new version

* bug fixes

* working on running microservices

* running microservices

* better local microservice running

* adding bot variation code

* Fixing name conflict for variations.  v.name must be unique
  • Loading branch information
roaringdev authored and zirkerc committed Mar 20, 2018
1 parent 2981c9c commit 83bf597
Show file tree
Hide file tree
Showing 10 changed files with 2,136 additions and 77 deletions.
11 changes: 7 additions & 4 deletions leo-cli-configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@ const generateProfile = require("leo-sdk/lib/generateProfile.js");
program
.version('0.0.1')
.option("-g, --global", "Install Globally")
.option("--region [region]", "Region to run cloudformation")
.option("--awsprofile [profile]", "Region to run cloudformation")
.usage('<stack> <region> <dir> [options]')
.action(function(stack, region, dir, options) {
if (typeof dir === "object") {
options = dir;
dir = ".";
}
options = Object.assign({
global: false
}, options || {});
if (options.global) {
dir = null;
}
generateProfile(stack, {
generateProfile(stack, Object.assign({
region: region
}, dir, (err) => {
}, options), dir, (err) => {
console.log("done");
});
})
.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp(colors.red);
}
}
98 changes: 53 additions & 45 deletions leo-cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,70 @@ program
.option("-e, --env [env]", "Environment")
.option("--region [region]", "Region to run cloudformation")
.usage('<dir> [options]')
.action(function (dir) {
.action(function(dir) {
let rootDir = path.resolve(process.cwd(), dir);
let watchDir = utils.findFirstPackageValue(rootDir, ["microservice"], "__directory");
var pkg = require(path.resolve(rootDir, "package.json"));

let child = null;
var reactRunner = require("./lib/react.js");

var buildConfig = require("./lib/build-config").build;

function run() {
function f() {
child = fork(__dirname + "/lib/runner.js", process.argv, {
cwd: rootDir,
env: Object.assign({}, process.env, {
LEO_ENV: program.env || "dev",
LEO_REGION: program.region,
LEO_CONFIG: JSON.stringify(buildConfig(rootDir)),
LEO_PREVENT_RUN_AGAIN: "true"
}),
//execArgv: ["--inspect", "--debug-brk"]
});
//process.kill(child.pid, 'USR1');
child.once("exit", () => {
child = null;
});
}
if (child) {
child.once("exit", f);
child.kill();
} else {
f();
}
}
process.on('SIGINT', () => {
if (child) {
console.log("closing child process. Ctrl-c again to cancel test");
child.kill();
} else {
watcher.close();
process.exit();
}
});
if (pkg.config && pkg.config.leo && pkg.config.leo.type == "microservice") {
let c = buildConfig(rootDir);
reactRunner(rootDir, c, c);
} else {
let child = null;

run();
let watchDirs = [watchDir].concat(pkg.config.test ? pkg.config.test.watch : []);
var watcher = watch(watchDirs, {
recursive: true,
filter: (f) => {
return !/node_modules/.test(f)
function run() {
function f() {
child = fork(__dirname + "/lib/runner.js", process.argv, {
cwd: rootDir,
env: Object.assign({}, process.env, {
LEO_ENV: program.env || "dev",
LEO_REGION: program.region,
LEO_CONFIG: JSON.stringify(buildConfig(rootDir)),
LEO_PREVENT_RUN_AGAIN: "true"
}),
//execArgv: ["--inspect", "--debug-brk"]
});
//process.kill(child.pid, 'USR1');
child.once("exit", () => {
child = null;
});
}
if (child) {
child.once("exit", f);
child.kill();
} else {
f();
}
}
}, (eventType, filename) => {
console.log("new file");
process.on('SIGINT', () => {
if (child) {
console.log("closing child process. Ctrl-c again to cancel test");
child.kill();
} else {
watcher.close();
process.exit();
}
});

run();
});
let watchDirs = (watchDir ? [watchDir] : []).concat(pkg.config.test ? pkg.config.test.watch : []);
console.log(watchDirs);
var watcher = watch(watchDirs, {
recursive: true,
filter: (f) => {
return !/node_modules/.test(f)
}
}, (eventType, filename) => {
console.log("new file");
run();
});
}
})
.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp(colors.red);
}
}
32 changes: 18 additions & 14 deletions lib/build-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var defaults = {};

function apply(config, override, path, triggers, depth) {
// console.log(depth, path, triggers)
if (override.global) {
if (override && override.global) {
// console.log(depth, "Global Appled")
//extend(true, config, override.global);
apply(config, override.global, path, triggers, depth + " ");
Expand Down Expand Up @@ -81,7 +81,7 @@ function fill(node, config, path) {
var child = node[key];
var file;

if (child && typeof (child) == "object" && Object.keys(child).length == 1 && Object.keys(child)[0].toLowerCase() == "ref") {
if (child && typeof(child) == "object" && Object.keys(child).length == 1 && Object.keys(child)[0].toLowerCase() == "ref") {
var ref = child[Object.keys(child)[0]];
var val = config.variables[ref.toLowerCase()];
if ((typeof val == "undefined" || val == null) && !currentOpts["ignore-missing-variables"]) {
Expand Down Expand Up @@ -142,7 +142,7 @@ function fill(node, config, path) {
throw `Reference File "${file[1]}" is missing`;
}
node[key] = val;
} else if (typeof (child) == "string") {
} else if (typeof(child) == "string") {

// var groups = getRegexGroups(child, "\\${(.*?)}", "g");
// for (var g in groups) {
Expand All @@ -164,11 +164,11 @@ function fill(node, config, path) {
}

module.exports = {
configure: function (config) {
configure: function(config) {
console.log("Configured", currentOpts);
currentOpts = extend(true, currentOpts, config);
},
"build": function build(rootDir, opts) {
"build": function build(rootDir, opts, pkgOverride) {

if (opts) {
currentOpts = Object.assign(currentOpts, opts);
Expand Down Expand Up @@ -220,10 +220,14 @@ module.exports = {
bot: {}
};

paths.slice(0).reverse().forEach(function (dir) {
paths.slice(0).reverse().forEach(function(dir, i) {
var packageFile = path.resolve(dir, "package.json");
if (fs.existsSync(packageFile)) {
var pkg = require(packageFile);
if (i == paths.length - 2 && pkgOverride) {
var pkg = pkgOverride;
} else {
var pkg = require(packageFile);
}
if (pkg.config && pkg.config.leo) {

var typeDefaults = {};
Expand All @@ -238,7 +242,7 @@ module.exports = {
// }

extend(true, config, typeDefaults, pkg.config.leo);
["version", "description", "name"].forEach(function (key) {
["version", "description", "name"].forEach(function(key) {
config[key] = pkg.config.leo[key] || pkg[key];
});
configs[config.type] = pkg.config.leo;
Expand Down Expand Up @@ -331,7 +335,7 @@ module.exports = {

var filelookups = {};
if (systemDirectory && fs.existsSync(path.resolve(systemDirectory, `config/`))) {
fs.readdirSync(path.resolve(systemDirectory, `config/`)).forEach(function (file) {
fs.readdirSync(path.resolve(systemDirectory, `config/`)).forEach(function(file) {
file = file.replace(/\.json$/, "");
filelookups[file.toLowerCase()] = file;
});
Expand All @@ -341,7 +345,7 @@ module.exports = {
config = {};
var objP = c.type == "system" ? [] : (c.group || "").split(/_/g).filter(e => !!e).map(e => "group:" + e);
apply(config, c, objP.length ? objP : null, triggers, "");
overrideFiles.forEach(function (c) {
overrideFiles.forEach(function(c) {
var name = c.type == "system" ? "system" : configs.microservice.name;
var postfix = c.postfix || "";
name = name + postfix;
Expand Down Expand Up @@ -442,19 +446,19 @@ module.exports = {
return config;

},
"fill": function (obj, repo, docClient) {
"fill": function(obj, repo, docClient) {
if (docClient) {
return module.exports.fillWithTableReferences(obj, repo, docClient)
} else {
fill(obj, repo);
return obj;
}
},
fillWithTableReferences: function (obj, repo, docClient) {
fillWithTableReferences: function(obj, repo, docClient) {
fill(obj, repo);
return module.exports.addTableReferences(obj, repo.registry._tableReferences, docClient)
},
addTableReferences: function (obj, tables, docClient) {
addTableReferences: function(obj, tables, docClient) {
var keys = Object.keys(tables || {});
if (docClient && keys.length) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -505,4 +509,4 @@ module.exports = {
}

}
};
};
65 changes: 59 additions & 6 deletions lib/cloud-formation.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,57 @@ module.exports = {
nodir: true
}, function(err, files) {
var entries = [];
let processFiles = [];
files.filter(f => !f.match(/\/node_modules\//)).map(f => {
var pkg = merge({
config: {
leo: {}
}
}, JSON.parse(fs.readFileSync(f)));
let leo = pkg.config.leo;

processFiles.push(f);
if (leo.variations) {
leo.variations.forEach((v, i) => {
if (!v.name){
// TODO: Add memory and time to name
v.name = i;
}
let name = pkg.name + "-var-" + v.name;
delete v.name;
let newPackage = merge({
config: {
leo: {
isVariation: true
}
}
}, pkg, {
name: name,
config: {
leo: v
}
});

processFiles.push({
file: f,
package: newPackage
});
});
}
});

let prevSwagger = merge({}, cloudFormation.Resources.RestApi && cloudFormation.Resources.RestApi.Properties.Body);
files.filter(f => !f.match(/\/node_modules\//)).map((file) => {
var package = JSON.parse(fs.readFileSync(file));
processFiles.map((file) => {
var package;
if (file.package) {
package = file.package;
file = file.file;
} else {
package = JSON.parse(fs.readFileSync(file));
}



const packageName = package.name.replace(/[^a-zA-Z0-9]/g, '');
const ID = package.logicalResource || package.name.replace(/[^a-zA-Z0-9]+/g, "_").replace(/(^\w|_\w)/g, function(txt) {
return txt.charAt(txt.length == 1 ? 0 : 1).toUpperCase();
Expand All @@ -237,10 +284,17 @@ module.exports = {
}

// TODO: this was for old config merging
let mergedConfig = buildConfig(file);
let mergedConfig = buildConfig(file, null, package);

// TODO: Don't delete if variation askes for a trigger/time
if (mergedConfig && mergedConfig.isVariation) {
delete mergedConfig.cron.time;
delete mergedConfig.cron.triggers;
}
package.config.leo = mergedConfig;



let version = package.version;
let botDirName = file.replace(/^.*(?:bots|api)[/\\](.*)[\\/]package\.json$/, "$1").replace(/\//g, "\\");
// console.log(opts.force, file, botDirName)
Expand All @@ -254,7 +308,6 @@ module.exports = {
const existingPath = existing && existing.Properties.Code.S3Key.replace && existing.Properties.Code.S3Key || '';



let entryData = createLambdaEntry(existing, package, newPath, file);
if (entryData) {
let prev_version = existingPath.replace(new RegExp(`${microservice.name}/.*?/${ID}_`), '').replace('.zip', '');
Expand Down Expand Up @@ -417,7 +470,7 @@ module.exports = {
let swagger = getSwagger(cloudFormation, microservice);
Object.assign(swagger.paths, data.swagger.paths);

const newPath = `${microservice.name}/${microservice.version}/${data.LogicalResourceId}_${version}.zip`;
const newPath = `${microservice.name}${opts.tag}/${microservice.version}/${data.LogicalResourceId}_${version}.zip`;
cloudFormation.Resources[data.LogicalResourceId] = createLambdaEntry(cloudFormation.Resources[data.LogicalResourceId], {
main: "index.js",
config: {
Expand Down Expand Up @@ -1184,4 +1237,4 @@ function getSwagger(cloudFormation, microservice) {
}
}
};
}
}
Loading

0 comments on commit 83bf597

Please sign in to comment.