Skip to content

Commit

Permalink
Issue #37 : Add cloudformation stack events with colors
Browse files Browse the repository at this point in the history
  • Loading branch information
facundovictor committed Sep 19, 2018
1 parent 47bc87a commit 6ac2b28
Showing 1 changed file with 57 additions and 7 deletions.
64 changes: 57 additions & 7 deletions bin/capsule.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const commander = require('commander');
const chalk = require('chalk');
const aws = require('aws-sdk');
const path = require('path')
const Spinner = require('cli-spinner').Spinner;
let cf;
let s3;
let cfr;
Expand Down Expand Up @@ -167,6 +168,14 @@ const stack_states = [
'REVIEW_IN_PROGRESS'
];

const error_states = [
'CREATE_FAILED',
'DELETE_FAILED',
'UPDATE_FAILED',
'ROLLBACK_FAILED',
'UPDATE_ROLLBACK_FAILED'
];

const paths = {
base: `${__dirname}/../`,
ci_s3: 'ci/s3_cloudformation.cf',
Expand All @@ -179,10 +188,8 @@ const paths = {

let last_time = new Date(new Date() - 1000);

/*
* Helpers
*
*/
// Helpers ####################################################################

const logIfVerbose = (str, error) => {
if (commander.verbose){
if (error){
Expand All @@ -205,8 +212,7 @@ const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

const getRandomToken = () => Math.floor(Math.random() * 89999) + 10000;

// File Helpers ##############################################################

// File Helpers ###############################################################

/**
* Merge in commandline params into
Expand Down Expand Up @@ -394,6 +400,43 @@ const loadAWSConfiguration = async (config_path, aws_profile) => {

// AWS CF Helpers #############################################################

/**
* Given the cloud formation stack state, it returns the color red if it is a
* failure state, green if it is a success state, and yellow otherwise.
*
* @method getStackEventColor
*
* @param {String} state
*
* @return {String} color
*/
const getStackEventColor = (state) => {
switch (true) {
case error_states.includes(state): return 'red';
case stack_states.includes(state): return 'green';
default: return 'yellow';
}
}

/**
* Given the cloud formation stack event, it returns a string with a single
* line description for it.
*
* @method getStackEventColor
*
* @param {String} event
*
* @return {String} output_line
*/
const getStackEventOutputLine = (e) => {
let time = `${e.Timestamp.toLocaleString()}`;
let status = `${chalk[getStackEventColor(e.ResourceStatus)](e.ResourceStatus)}`;
let resource = `${e.ResourceType}`;
let id = `${e.PhysicalResourceId}`;

return `${time} ${status} ${resource} ${id}`;
}

/**
* Given an object of key->value, it will return the list of parameters in the
* format expected by AWS.
Expand Down Expand Up @@ -634,7 +677,10 @@ const getStackEvents = async (id) => {
const monitorStackProgress = async (id, token) => {
let in_progress = true;
let events_seen = []
let spinner = new Spinner();
spinner.setSpinnerString('|/-\\');
logIfVerbose(`Start monitoring stack ${id}`);
spinner.start();
while (in_progress) {
let events;
try {
Expand All @@ -655,6 +701,10 @@ const monitorStackProgress = async (id, token) => {
logIfVerbose(`Event ignored: ${e.EventId}`);
} else {
logIfVerbose(`NEW Event: ${e}`);
spinner.text = getStackEventOutputLine(e);
if (e.ResourceStatusReason !== 'User Initiated') {
process.stdout.write('\n');
}
events_seen.push(e.EventId);
}
if (e.ResourceType === 'AWS::CloudFormation::Stack' &&
Expand All @@ -671,6 +721,7 @@ const monitorStackProgress = async (id, token) => {
await delay(1000);
}
}
spinner.stop();
logIfVerbose(`End monitoring stack ${id} with token ${token}`);
}

Expand Down Expand Up @@ -1157,7 +1208,6 @@ const ciCmds = async(type) => {

// MAIN #######################################################################
(async () => {

global.cwd = process.cwd();
let type = commander.type;

Expand Down

0 comments on commit 6ac2b28

Please sign in to comment.