Skip to content

Commit

Permalink
Merge pull request #90 from allmywant/master
Browse files Browse the repository at this point in the history
Featrue: nuke container / test
  • Loading branch information
allmywant authored Mar 15, 2021
2 parents e394074 + 8e14774 commit 1f91560
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 27 deletions.
36 changes: 36 additions & 0 deletions bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
image: atlassian/default-image:2

pipelines:
custom:
nuke:
- step:
name: Nuke the container
script:
- echo "" >> nuke.lock
- git add nuke.lock
- git commit --allow-empty -m "Adding the nuke.lock file."
- git push

restore:
- step:
name: restore the container
script:
- git rm --ignore-unmatch nuke.lock
- git commit --allow-empty -m "Deleting the nuke.lock file."
- git push

start-stop-archive-test:
- variables:
- name: WaveId
- name: State
- name: SampleRate
- name: RecipeToDivertTo
- step:
name: Change the state of a test
caches:
- node
script:
- npm install
- npm install -g gulp-cli
- gulp ssa --state $State --waveId $WaveId --traffic $SampleRate --recipe $RecipeToDivertTo
- git add --all
- git commit --allow-empty -m "Updating $WaveId test to $State"
- git push

branches:
master:
- step:
Expand Down
40 changes: 40 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const gulp = require('gulp'),
babel = require('gulp-babel'),
newCli = require('./scripts/cli-new'),
setCli = require('./scripts/cli-set'),
ssaCli = require('./scripts/cli-ssa'),
parallelize = require('concurrent-transform');

// Check whether config exists & create it if not.
Expand Down Expand Up @@ -67,6 +68,39 @@ function build()
del(['dist/assets/js']);
let containerName = config.containerName;
let modularResult = {liveList: [], stagingList: [], divertList: [], inactive: 0};

// is there a nuke.lock file?
const fileName = 'nuke.lock';
if (fs.existsSync(fileName))
{
// generate the container without test objects
return (
gulp.src('license.txt')
.pipe(addsrc.append(['lib/mojito.js', 'lib/shared-code.js']))
.pipe(concat(containerName + '.pretty.js'))
.pipe(babel({presets: [['@babel/preset-env', { "modules": false, exclude:['@babel/plugin-transform-typeof-symbol'] }]]}))
.pipe(gulp.dest('dist/assets/js'))
.pipe(uglify())
.pipe(addsrc.prepend(['license.txt']))
.pipe(concat(containerName + '.js'))
.pipe(gulp.dest('dist/assets/js'))
.pipe(through.obj(function(file, enc, callback)
{
let gzippedSize = ((zlib.gzipSync(file.contents, {level: 9}).length)/1024).toFixed(2) + ' KB',
colorCyan = '\x1b[36m',
colorReset = '\x1b[0m';

setTimeout(function(){
console.log(
`%s${colorCyan}%s${colorReset}%s`,
'Mojito container built (', gzippedSize, ')');
});

callback(null, file);
}))
);
}

return (
gulp.src('lib/waves/**/config.yml')
.pipe(yaml())
Expand Down Expand Up @@ -188,11 +222,17 @@ function cliSet(cb)
setCli(getCLIArgs(true), cb);
}

function cliSSA(cb)
{
ssaCli(getCLIArgs(true), cb);
}

exports.test = test;
exports.build = build;
exports.scripts = build;
exports.publish = publish;
exports.default = build;
exports.new = cliNew;
exports.set = cliSet;
exports.ssa = cliSSA;
exports.refresh = gulp.series([build, publish]);
45 changes: 18 additions & 27 deletions scripts/cli-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function setTestState(waveId, cmd, args, cb)
{
console.error(`${colorRed}%s${colorReset}`, `Failed to read lib/waves/${waveId}/config.yml: ${e.message}`);
});
cb();
cb(new Error(`Failed to read lib/waves/${waveId}/config.yml: ${e.message}`));
}

test.state = cmd;
Expand All @@ -50,7 +50,7 @@ function setTestState(waveId, cmd, args, cb)
{
console.error(`${colorRed}%s${colorReset}`, `Traffic must be in range (0, 1].`);
});
cb();
cb(new Error(`Traffic must be in range (0, 1].`));

return;
}
Expand Down Expand Up @@ -81,7 +81,7 @@ function divertTest(waveId, recipe, cb)
{
console.error(`${colorRed}%s${colorReset}`, `Failed to read lib/waves/${waveId}/config.yml: ${e.message}`);
});
cb();
cb(new Error(`Failed to read lib/waves/${waveId}/config.yml: ${e.message}`));
return;
}

Expand All @@ -90,9 +90,9 @@ function divertTest(waveId, recipe, cb)
{
setTimeout(function ()
{
console.error(`${colorRed}%s${colorReset}`, `Test must live.`);
console.error(`${colorRed}%s${colorReset}`, `Test must be live.`);
});
cb();
cb(new Error(`Test must be live.`));
return;
}

Expand All @@ -109,7 +109,7 @@ function divertTest(waveId, recipe, cb)
{
console.error(`${colorRed}%s${colorReset}`, `The recipe ${recipe} doesn't exist.`);
});
cb();
cb(new Error(`The recipe ${recipe} doesn't exist.`));
return;
}

Expand All @@ -130,51 +130,42 @@ function checkArgs(args, cb)

if (cmd == 'divert' || cmd == 'live') {
if (keys.length > 2) {
setTimeout(usage);
cb();
usage();
cb(new Error(`Invalid parameters.`));
return false;
}
}
else if (keys.length > 1) {
setTimeout(usage);
cb();
usage();
cb(new Error(`Invalid parameters.`));
return false;
}

if (cmd != 'live' && cmd != 'staging' && cmd != 'inactive' && cmd != 'divert') {
setTimeout(usage);
cb();
usage();
cb(new Error(`Invalid state: ${cmd}.`));
return false;
}

// wave id validation
let waveId = args[cmd];
if (waveId == null || /[<>:"|?*]/.test(waveId)) {
setTimeout(function ()
{
console.warn(`${colorRed}%s${colorReset}`, 'Please specify an valid wave id.');
});
cb();
console.warn(`${colorRed}%s${colorReset}`, 'Please specify an valid wave id.');
cb(new Error(`Please specify an valid wave id.`));
return false;
}

// wave existence
if (!fs.existsSync(`lib/waves/${waveId}/config.yml`)) {
setTimeout(function ()
{
console.warn(`${colorRed}%s${colorReset}`, `Wave id ${waveId} doesn't exist.`);
});
cb();
console.warn(`${colorRed}%s${colorReset}`, `Wave id ${waveId} doesn't exist.`);
cb(new Error(`Wave id ${waveId} doesn't exist.`));
return false;
}

// divert recipe
if (cmd == 'divert' && args['recipe'] == null) {
setTimeout(function ()
{
console.warn(`${colorRed}%s${colorReset}`, 'Please specify a recipe id.');
});
cb();
console.warn(`${colorRed}%s${colorReset}`, 'Please specify a recipe id.');
cb(new Error(`Please specify a recipe id.`));
return false;
}

Expand Down
65 changes: 65 additions & 0 deletions scripts/cli-ssa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';
const setCli = require('./cli-set');
const colorRed = '\x1b[31m',
colorReset = '\x1b[0m';

// Start/Stop/Archive a test
module.exports = function (args, cb)
{
if (!checkArgs(args, cb)) {
return;
}

let argsSet = {};
argsSet[args.state] = args.waveId;

if (args.state == 'live')
{
argsSet.traffic = args.traffic
}

if (args.state == 'divert')
{
argsSet.recipe = args.recipe
}

setCli(argsSet, cb);
}

function checkArgs(args, cb)
{
let keys = Object.keys(args),
waveId = args.waveId;

if (!waveId)
{
usage();
cb(new Error('Invalid wave id.'));
return false;
}

let state = args.state;

if (state != 'live' && state != 'staging' && state != 'inactive' && state != 'divert') {
usage();
cb(new Error(`Invalid state: ${state}.`));
return false;
}

if (state == 'divert' || state == 'live') {
if (keys.length < 3) {
usage();
cb(new Error('Invalid parameters.'));
return false;
}
}

return true;
}

function usage()
{
console.log(`${colorRed}%s${colorReset}`, 'Invalid parameters.');
console.warn('Usage:');
console.warn(' gulp ssa --state staging|live|inactive|divert --waveId {{wave id}} --traffic {{simple rate}} --recipe {{recipe id}}');
}

0 comments on commit 1f91560

Please sign in to comment.