serverless/lib/defaults/actions/ResourcesDeploy.js
2015-12-01 21:10:40 +02:00

222 lines
5.8 KiB
JavaScript

'use strict';
/**
* Action: ResourcesDeploy
*/
const JawsPlugin = require('../../JawsPlugin'),
JawsError = require('../../jaws-error'),
JawsCLI = require('../../utils/cli'),
BbPromise = require('bluebird'),
awsMisc = require('../../utils/aws/Misc'),
JawsUtils = require('../../utils/index');
class ResourcesDeploy extends JawsPlugin {
/**
* @param Jaws class object
* @param config object
*/
constructor(Jaws, config) {
super(Jaws, config);
this.evt = {};
}
/**
* Define your plugins name
*
* @returns {string}
*/
static getName() {
return 'jaws.core.' + ResourcesDeploy.name;
}
/**
* @returns {Promise} upon completion of all registrations
*/
registerActions() {
this.Jaws.addAction(this.resourcesDeploy.bind(this), {
handler: 'resourcesDeploy',
description: `Provision AWS resources (resources-cf.json).
usage: jaws resources deploy`,
context: 'resources',
contextAction: 'deploy',
options: [
{
option: 'region',
shortcut: 'r',
description: 'region you want to deploy to'
},
{
option: 'stage',
shortcut: 's',
description: 'stage you want to deploy to'
},
{
option: 'nonInteractive',
shortcut: 'i',
description: 'Optional - Turn off CLI interactivity if true. Default: false'
},
],
});
return BbPromise.resolve();
}
/**
* Action
*/
resourcesDeploy(evt) {
let _this = this;
if(evt) {
_this.evt = evt;
_this.Jaws._interactive = false;
}
// If CLI, parse arguments
if (_this.Jaws.cli) {
_this.evt = _this.Jaws.cli.options;
if (_this.Jaws.cli.options.nonInteractive) {
_this.Jaws._interactive = false;
}
}
return this.Jaws.validateProject()
.bind(_this)
.then(_this._promptStage)
.then(_this._promptRegion)
.then(_this._validateAndPrepare)
.then(_this._updateResources)
.then(() => {
_this._spinner.stop(true);
JawsCLI.log('Resource Deployer: Successfully deployed ' + _this.evt.stage + ' resources to ' + _this.evt.region);
});
}
/**
* Prompt stage if it's missing
*/
_promptStage(){
let _this = this;
let stages = Object.keys(_this.Jaws._projectJson.stages);
// Skip if non-interactive
if (!_this.Jaws._interactive || _this.evt.stage) return BbPromise.resolve();
// if project has 1 stage, skip prompt
if (stages.length === 1) {
_this.evt.stage = stages[0];
return BbPromise.resolve();
}
// Create Choices
let choices = [];
for (let i = 0; i < stages.length; i++) {
choices.push({
key: (i + 1) + ') ',
value: stages[i],
label: stages[i],
});
}
return JawsCLI.select('Which stage are you deploying to: ', choices, false)
.then(function(results) {
_this.evt.stage = results[0].value;
});
}
/**
* Prompt region if it's missing
*/
_promptRegion(){
let _this = this;
// skip region prompt if selected stage is 'local'
if (_this.evt.stage === 'local') {
_this.evt.region = 'local';
return BbPromise.resolve();
}
if (!_this.Jaws._interactive || _this.evt.region) return BbPromise.resolve();
// TODO: list only regions defined in the provided stage
// this assumes that the provided stage is valid, we'll have to validate before getting here
let choices = awsMisc.validLambdaRegions.map(r => {
return {
key: '',
value: r,
label: r,
};
});
return _this.selectInput('Which region are you deploying to: ', choices, false)
.then(results => {
_this.evt.region = results[0].value;
});
}
_validateAndPrepare(){
let _this = this;
// non interactive validation
if (!_this.Jaws._interactive) {
// Check API Keys
if (!_this.Jaws._awsProfile) {
if (!_this.Jaws._awsAdminKeyId || !_this.Jaws._awsAdminSecretKey) {
return BbPromise.reject(new JawsError('Missing AWS Profile and/or API Key and/or AWS Secret Key'));
}
}
// Check Params
if (!_this.evt.stage || !_this.evt.region) {
return BbPromise.reject(new JawsError('Missing stage and/or region and/or key'));
}
}
// validate stage: make sure stage exists
if (!_this.Jaws._projectJson.stages[_this.evt.stage] && _this.evt.stage != 'local') {
return BbPromise.reject(new JawsError('Stage ' + _this.evt.stage + ' does not exist in your project', JawsError.errorCodes.UNKNOWN));
}
// validate region: make sure region exists in stage
if (!_this.Jaws._projectJson.stages[_this.evt.stage].some(function(r) {
return r.region == _this.evt.region;
})) {
return BbPromise.reject(new JawsError('Region "' + _this.evt.region + '" does not exist in stage "' + _this.evt.stage + '"'));
}
}
_updateResources(){
let _this = this;
JawsCLI.log('Deploying resources to stage "'
+ _this.evt.stage
+ ' and region '
+ _this.evt.region
+ '" via Cloudformation. This could take a while depending on how many resources you are updating...');
// Start spinner
_this._spinner = JawsCLI.spinner();
_this._spinner.start();
let config = {
profile: _this._awsProfile,
region : _this.evt.region
};
_this.CF = require('../../utils/aws/CloudFormation')(config);
return _this.CF.sUpdateResourcesStack(
_this.Jaws,
_this.evt.stage,
_this.evt.region)
.then(cfData => {
return _this.CF.sMonitorCf(cfData, 'update');
});
}
}
module.exports = ResourcesDeploy;