Skip to content

Commit

Permalink
Added command to publish and deploy --tag to bucket the published ver…
Browse files Browse the repository at this point in the history
…sion to a specific tag

Added support for NoEcho cloudformation parameters
Added support to add Resources in a microservice package.json to be build into the final cloudformation.json
Added support for Auto Scaling of Dynamodb tables
  • Loading branch information
zirkerc committed Mar 1, 2018
1 parent d3dcf25 commit 3837a7e
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 71 deletions.
7 changes: 5 additions & 2 deletions leo-cli-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ program
.option("--region [region]", "Region to run cloudformation")
.option("--url [url]", "s3 url to cloudformation.json")
.option("--awsprofile [awsprofile]", "AWS Profile to use")
.option("--tag [tag]", "Tag for publish directory. eg. prod")
.usage('<dir> <stack> [options]')
.action(function (dir, stack) {
let rootDir = path.resolve(process.cwd(), dir);
Expand All @@ -27,6 +28,7 @@ program
}
program.region = program.region || (configure.regions || [])[0] || "us-west-2";

program.tag = (program.tag ? (program.tag.match(/^[\/\\]/) ? program.tag : `/${program.tag}`) : "").replace(/\\/g, "/");
if (stack && typeof stack === "string") {
cloudformation.getBuckets([program.region], {}, (err, buckets) => {
const cloudFormationFile = path.resolve(path.resolve(dir, "cloudformation.json"));
Expand All @@ -40,7 +42,7 @@ program
let s3region = program.region == "us-east-1" ? "" : "-" + program.region;
let bucket = {
region: program.region,
url: program.url || `https://s3${s3region}.amazonaws.com/${buckets[0].bucket}/${microservice.name}/${version}/`,
url: program.url || `https://s3${s3region}.amazonaws.com/${buckets[0].bucket}/${microservice.name}${program.tag}/${version}/`,
cloudFormation: JSON.parse(fs.readFileSync(cloudFormationFile))
};
let url = bucket.url + "cloudformation.json"
Expand All @@ -54,7 +56,8 @@ program
Parameters: Object.keys(bucket.cloudFormation.Parameters || {}).map(key => {
return {
ParameterKey: key,
UsePreviousValue: true
UsePreviousValue: true,
NoEcho: bucket.cloudFormation.Parameters[key].NoEcho
}
})
}).then(data => {
Expand Down
12 changes: 8 additions & 4 deletions leo-cli-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ program
.option("--force [force]", "Force bots to publish")
.option("--filter [filter]", "Filter bots to publish")
.option("--awsprofile [awsprofile]", "AWS Profile to use")
.option("--tag [tag]", "Tag for publish directory. eg. prod")
.usage('<dir> [options]')
.action(function (dir) {
let env = program.env || "dev";
Expand Down Expand Up @@ -66,17 +67,19 @@ program
});
}

let regions = Array.from(new Set([].concat(program.region).concat(configure.regions))).filter(a => !!a);
start.then(cf => createCloudFormation(configure._meta.microserviceDir, {
config: configure,
filter: filter,
publish: program.run || !program.build,
force: force,
regions: program.region ? [].concat(program.region) : configure.regions,
regions: regions.length && regions, // program.region ? [].concat(program.region) : configure.regions,
public: program.public,
cloudformation: cf,
overrideCloudFormationFile: !cf,
overrideCloudFormationFile: !cf && !program.build,
alias: process.env.LEO_ENV,
region: process.env.LEO_REGION
region: process.env.LEO_REGION,
tag: program.tag
}).then((data) => {

if (program.run || !program.build) {
Expand All @@ -97,7 +100,8 @@ program
Parameters: Object.keys(bucket.cloudFormation.Parameters || {}).map(key => {
return {
ParameterKey: key,
UsePreviousValue: true
UsePreviousValue: true,
NoEcho: bucket.cloudFormation.Parameters[key].NoEcho
}
})
}).then(data => {
Expand Down
114 changes: 57 additions & 57 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var showPagesTemplate = require("./showpages.js");
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
build: function(program, rootDir, opts = {}, callback) {
build: function (program, rootDir, opts = {}, callback) {
opts = Object.assign({
alias: program.env || 'dev',
region: program.region || 'us-west-2',
Expand Down Expand Up @@ -78,7 +78,7 @@ module.exports = {
dir: opts.buildDir,
basename: rootDir,
main: pkg.main
}, function(err, data) {
}, function (err, data) {
err && console.log(err);
callback(err, data)
});
Expand Down Expand Up @@ -126,25 +126,25 @@ module.exports = {
callback();
}
},
publish: function(rootDir, remoteDir, opts, callback) {
publish: function (rootDir, remoteDir, opts, callback) {
opts = Object.assign({
public: false

}, opts || {});

console.log("\n\n---------------Publishing files-------------------");
console.log(`\n\n---------------${opts.label || "Publishing files"}-------------------`);
console.log(`From ${rootDir} to ${remoteDir}`);
console.time("Published Files");

let args = ['s3', 'sync', rootDir, `${remoteDir}`];
let args = ['s3', opts.command || 'sync', rootDir, `${remoteDir}`];
if (process.env.LEO_AWS_PROFILE) {
args.push("--profile", process.env.LEO_AWS_PROFILE);
}
if (opts.public) {
args.push("--grants", "read=uri=http://acs.amazonaws.com/groups/global/AllUsers");
}
let upload = spawn('aws', args);
upload.stdout.on('data', (data) => {});
upload.stdout.on('data', (data) => { });
upload.stderr.on('data', (data) => {
console.log(data.toString());
});
Expand All @@ -158,7 +158,7 @@ module.exports = {
}
});
},
buildStaticAssets: function(rootDir, configure, newVersion, opts, callback) {
buildStaticAssets: function (rootDir, configure, newVersion, opts, callback) {
execsync("npm install", {
cwd: rootDir
});
Expand Down Expand Up @@ -200,7 +200,7 @@ module.exports = {

glob(jsDir + "/*.js", {
nodir: true
}, function(err, files) {
}, function (err, files) {
var entries = {};
files.map((file) => {
entries[path.basename(file, ".js")] = [file];
Expand Down Expand Up @@ -274,7 +274,7 @@ module.exports = {
},
debug: true
}];
webpack(config, function(err, stats) {
webpack(config, function (err, stats) {
if (err) {
console.log(err);
} else {
Expand All @@ -295,7 +295,7 @@ module.exports = {
.pipe(rename({
extname: ''
}))
.pipe(gulp.dest(viewDir)).on('end', function() {
.pipe(gulp.dest(viewDir)).on('end', function () {
callback(null, path.normalize(path.resolve(rootDir, "dist")));
});
});
Expand All @@ -304,7 +304,7 @@ module.exports = {
callback(null, path.normalize(path.resolve(rootDir, "static")));
}
},
createMicroserviceApp: function(rootDir, configure, version, opts) {
createMicroserviceApp: function (rootDir, configure, version, opts) {
//let's look for all views that need to be added
var viewDir = path.normalize(path.resolve(rootDir, "views"));

Expand All @@ -314,7 +314,7 @@ module.exports = {
nodir: true
});
console.log("Views", files)
files.forEach(function(file) {
files.forEach(function (file) {
var f = path.basename(file);

var p = path.relative(viewDir, path.dirname(file)).replace(/\\/g, '/');
Expand All @@ -332,7 +332,7 @@ module.exports = {
let swagger = {
paths: {}
};
showPagesFiles.forEach(function(file) {
showPagesFiles.forEach(function (file) {
var snippet = {
"get": {
"consumes": [
Expand Down Expand Up @@ -392,7 +392,7 @@ module.exports = {
version: version
};
},
buildMicroserviceApp: function(rootDir, configure, version, opts, callback) {
buildMicroserviceApp: function (rootDir, configure, version, opts, callback) {
let self = this;
this.buildStaticAssets(rootDir, configure, version, {}, (err, staticDir) => {

Expand All @@ -411,7 +411,7 @@ module.exports = {
archive.pipe(zip);
var showPagesFiles = [];
var files = opts.files;
files.forEach(function(file) {
files.forEach(function (file) {
var f = path.basename(file);

var p = path.relative(viewDir, path.dirname(file)).replace(/\\/g, '/');
Expand Down Expand Up @@ -439,7 +439,7 @@ module.exports = {
name: "index.js"
});

zip.on("close", function() {
zip.on("close", function () {
self.publish(staticDir, "s3://" + opts.static.replace(/^s3:\/\//, ""), {
public: opts.public
}, err => callback(err, {
Expand All @@ -449,7 +449,7 @@ module.exports = {
archive.finalize();
});
},
createMicroserviceApp2: function(rootDir, configure, version, opts, callback) {
createMicroserviceApp2: function (rootDir, configure, version, opts, callback) {
this.buildStaticAssets(rootDir, configure, version, {}, (err, staticDir) => {

if (err) {
Expand All @@ -471,9 +471,9 @@ module.exports = {
var showPagesFiles = [];
glob(path.resolve(rootDir, "views") + "/**/*", {
nodir: true
}, function(err, files) {
}, function (err, files) {
console.log("Views", files)
files.forEach(function(file) {
files.forEach(function (file) {
var f = path.basename(file);

var p = path.relative(viewDir, path.dirname(file)).replace(/\\/g, '/');
Expand Down Expand Up @@ -502,12 +502,12 @@ module.exports = {
});

console.log(`Build App showPages function`);
zip.on("close", function() {
zip.on("close", function () {
console.log("Zip Closed");
let swagger = {
paths: {}
};
showPagesFiles.forEach(function(file) {
showPagesFiles.forEach(function (file) {
var snippet = {
"get": {
"consumes": [
Expand Down Expand Up @@ -596,7 +596,7 @@ function buildLambdaDirectory(rootDir, opts, callback) {
commondir: false,
detectGlobals: true,
insertGlobalVars: {
process: function() {
process: function () {
return;
}
},
Expand Down Expand Up @@ -626,11 +626,11 @@ function buildLambdaDirectory(rootDir, opts, callback) {

}
}
b.transform(function(file) {
b.transform(function (file) {
if (file.match("leowrap")) {
return through(function(buf, enc, next) {
return through(function (buf, enc, next) {
next(null, "");
}, function(cb) {
}, function (cb) {
var type = config.type;

var wrapperFile = __dirname + "/wrappers/" + type + ".js";
Expand All @@ -644,16 +644,16 @@ function buildLambdaDirectory(rootDir, opts, callback) {
cb();
});
} else if (file.match("leoConfigure.js")) {
return through(function(buf, enc, next) {
return through(function (buf, enc, next) {
next(null, "");
}, function(cb) {
}, function (cb) {
this.push("module.exports = " + JSON.stringify(config));
cb();
});
} else if (file.match("leo-sdk-config.js")) {
return through(function(buf, enc, next) {
return through(function (buf, enc, next) {
next(null, "");
}, function(cb) {
}, function (cb) {
var sdkConfigData = {};
var sdkConfigPath = path.resolve(`${require('os').homedir()}/.leo`, "config.json");
if (fs.existsSync(sdkConfigPath) && !config.excludeProfiles) {
Expand Down Expand Up @@ -683,9 +683,9 @@ function buildLambdaDirectory(rootDir, opts, callback) {
// Match any sdks
let parts = path.basename(file).match(/(.*?)(?:-(.*?))?-config\.js$/)
if (parts) {
return through(function(buff, enc, next) {
return through(function (buff, enc, next) {
next(null, "");
}, function(cb) {
}, function (cb) {
let dirs = [".leo"];
let filenames = [];
if (parts[2]) {
Expand All @@ -706,31 +706,31 @@ function buildLambdaDirectory(rootDir, opts, callback) {
var sdkConfigData;

configloop:
for (let i in dirs) {
let dir = dirs[i]
for (let j in filenames) {
let filename = filenames[j];
let sdkConfigPath = path.resolve(`${require('os').homedir()}/${dir}`, filename);
//console.log(sdkConfigPath)
if (fs.existsSync(sdkConfigPath) && !config.excludeProfiles) {
sdkConfigData = JSON.parse(fs.readFileSync(sdkConfigPath) || sdkConfigData);
//console.log(config.profiles)
if (config.profiles) {
let profiles = config.profiles;
let tmp = {};
config.profiles.map((p => {
tmp[p] = sdkConfigData[p];
// Can't Change aws profile in lambda so remove the profile key
delete tmp[p].profile;
}))
sdkConfigData = tmp;
sdkConfigData.default = sdkConfigData.default || sdkConfigData[config.defaultProfile] || sdkConfigData[config.profiles[0]];
}
sdkConfigData.default = sdkConfigData.default || sdkConfigData[Object.keys(sdkConfigData)[0]];
break configloop;
for (let i in dirs) {
let dir = dirs[i]
for (let j in filenames) {
let filename = filenames[j];
let sdkConfigPath = path.resolve(`${require('os').homedir()}/${dir}`, filename);
//console.log(sdkConfigPath)
if (fs.existsSync(sdkConfigPath) && !config.excludeProfiles) {
sdkConfigData = JSON.parse(fs.readFileSync(sdkConfigPath) || sdkConfigData);
//console.log(config.profiles)
if (config.profiles) {
let profiles = config.profiles;
let tmp = {};
config.profiles.map((p => {
tmp[p] = sdkConfigData[p];
// Can't Change aws profile in lambda so remove the profile key
delete tmp[p].profile;
}))
sdkConfigData = tmp;
sdkConfigData.default = sdkConfigData.default || sdkConfigData[config.defaultProfile] || sdkConfigData[config.profiles[0]];
}
};
sdkConfigData.default = sdkConfigData.default || sdkConfigData[Object.keys(sdkConfigData)[0]];
break configloop;
}
};
};

if (sdkConfigData) {
this.push(`module.exports = ${JSON.stringify(sdkConfigData)};`)
Expand All @@ -745,8 +745,8 @@ function buildLambdaDirectory(rootDir, opts, callback) {
return through();
}
}, {
global: true
});
global: true
});
b.bundle().pipe(source(indexFilename)).pipe(buffer())
.pipe(gulp.dest(`${opts.dir}/`)).on("end", () => {
archive.file(`${opts.dir}/${indexFilename}`, {
Expand All @@ -759,7 +759,7 @@ function buildLambdaDirectory(rootDir, opts, callback) {
});
}
}
zip.on("close", function() {
zip.on("close", function () {
fs.unlinkSync(`${opts.dir}/${indexFilename}`);
console.timeEnd(`Zipped Lambda Function ${opts.basename}`);
callback(null, {
Expand Down
Loading

0 comments on commit 3837a7e

Please sign in to comment.