Skip to content

Commit

Permalink
Merge pull request #316 from AdTechMedia/test
Browse files Browse the repository at this point in the history
Test to stage 2017-08-08
  • Loading branch information
AlexanderC authored Aug 8, 2017
2 parents b25cce7 + 723a71d commit b0484d1
Show file tree
Hide file tree
Showing 27 changed files with 332 additions and 158 deletions.
2 changes: 2 additions & 0 deletions .recink.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ $:
pattern:
- /.+\.js$/i
ignore:
- /.+\.cfg\.js$/i
- /.+\.po\.js$/i
- /.+\.e2e\.js$/i
- /.+\.spec\.js$/i
- /^(.*\/)?node_modules(\/?$)?/i
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
after_failure:
- deepify undeploy src --loglevel=debug

# Temporarily turned off (Electron issue)
## Temporarily turned off (Electron issue)
# - stage: "Run e2e Tests :clipboard:"
# node_js: 8
# script:
Expand Down
48 changes: 25 additions & 23 deletions bin/deploy/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
const { runChildCmd } = require('../helpers/utils');

const env = process.env.DEPLOY_ENV || 'test';
const bucket = 'atm-deploy-caches';
const prefix = 'atm-website/npm-registry';
const appPath = path.join(__dirname, '../../');
const cacheDir = path.join(process.env.HOME, '.npm_lazy');
const configPath = path.join(appPath, 'npm_lazy.config.js');
const configPath = path.join(__dirname, '../../', 'npm_lazy.config.js');

/**
* Parent message handler
Expand All @@ -20,7 +20,7 @@ process.on('message', (event) => {
case 'get-config': getConfig(); break;
case 'configure': configure(); break;
case 'run-registry': runRegistry(); break;
case 'exit': process.exit(0); break;
case 'exit': onExit(); break;
}
});

Expand All @@ -31,7 +31,7 @@ function getConfig() {
process.send({
cacheDir: cacheDir,
configPath: configPath,
pullCommand: `aws s3 sync s3://${bucket}/${prefix} ${cacheDir}`,
pullCommand: `aws s3 sync s3://${bucket}/${prefix} ${cacheDir} --delete`,
pushCommand: `aws s3 sync ${cacheDir} s3://${bucket}/${prefix} --delete`
});
}
Expand All @@ -40,28 +40,21 @@ function getConfig() {
* Run local npm registry
*/
function runRegistry() {
const childCmd = spawn(`npm_lazy --config ${configPath}`, { shell: true, cwd: appPath });

childCmd.stdout.on('data', data => {
let str = data.toString();

if (/.*(Request|Reusing).*/.test(str)) {
console.log(str.trim());
}
});

childCmd.stderr.on('data', error => {
console.error(error.toString());
});
runChildCmd(`npm_lazy --config ${configPath}`, /.*(Request|Reusing).*/).then(() => {
console.log('npm_lazy server stopped');
})
}

/**
* Configure local registry for environment
*/
function configure() {
if (!fs.existsSync(cacheDir)){
fs.mkdirSync(cacheDir);
}
Promise.all([
runChildCmd('npm config set registry http://localhost:8080/'),
runChildCmd(`rm -rf ${cacheDir} && mkdir ${cacheDir}`)
]).then(() => {
console.log('Local npm registry configured');
});

fs.writeFileSync(
configPath,
Expand All @@ -70,9 +63,9 @@ function configure() {
logRequesterIP: true,
logToConsole: true
},
readOnly: ${(process.env.DEPLOY_ENV === 'master')},
readOnly: ${['master', 'stage'].includes(env)},
cacheDirectory: '${cacheDir}',
cacheAge: 0,
cacheAge: 9999999999,
httpTimeout: 4000,
maxRetries: 2,
externalUrl: 'http://localhost:8080',
Expand All @@ -83,3 +76,12 @@ function configure() {
};`
);
}

/**
* Reset registry config and exit
*/
function onExit() {
runChildCmd('npm config delete registry').then(() => {
process.exit(0);
});
}
136 changes: 136 additions & 0 deletions bin/deploy/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/usr/bin/env node

'use strict';

const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const AwsHelper = require('../helpers/aws');
const { runChildCmd } = require('../helpers/utils');

const deepifyRegexp = /\d{2}:\d{2}:\d{2}/;
const env = process.env.DEPLOY_ENV || 'test';
const srcPath = path.join(__dirname, '../../', 'src');
const awsh = new AwsHelper('atm-deploy-caches');
const s3Prefix = `atm-website/lambdas/${env}`;
const compareBranch = process.env.DEPLOY_ENV ? `origin/${env}` : '';

let deployBackend = false;

/**
* Compile microservise
* @param microApp
* @returns {Promise}
*/
function compileMicroservice(microApp) {
return awsh.listS3Objects(`${s3Prefix}/${microApp}`).then(res => {
if (res.KeyCount === 0) {
return Promise.resolve(true);
} else {
return checkForBackendChanges(microApp).then(res => {
return Promise.resolve(res);
});
}
}).then(compileBackend => {
deployBackend = compileBackend;

return compileBackend
? runChildCmd(`cd ${srcPath} && deepify compile prod ${microApp}`, deepifyRegexp)
: reuseCompiledLambdas(microApp);
});
}

exports.compileMicroservice = compileMicroservice;

/**
* Upload microservice lambdas
* @param microApp
* @returns {Promise}
*/
function cacheMicroserviceLambdas(microApp) {
return Promise.all(
findLambdasByMicroAppName(microApp).map(lambdaPath => {
let stream = fs.createReadStream(lambdaPath);
return awsh.uploadZipToS3(lambdaPath.replace(srcPath, s3Prefix), stream);
})
);
}

exports.cacheMicroserviceLambdas = cacheMicroserviceLambdas;

/**
* Deploy frontend and/or backend
* @returns {Promise}
*/
function deployApplication() {
let deployCommand = deployBackend ? 'deepify deploy' : 'deepify deploy --frontend';

return runChildCmd(`cd ${srcPath} && ${deployCommand}`, deepifyRegexp);
}

exports.deployApplication = deployApplication;

/**
* Download compiled lambdas
* @param microApp
* @returns {Promise}
*/
function reuseCompiledLambdas(microApp) {
return awsh.listS3Objects(`${s3Prefix}/${microApp}`).then(res => {
let keys = res.Contents.map(item => item.Key);

return Promise.all(keys.map(key => {
return awsh.getAndSaveS3Object(key, key.replace(s3Prefix, srcPath));
}));
});
}

/**
* Find lambdas by micro-application name
* @param microApp
* @returns {Array}
*/
function findLambdasByMicroAppName(microApp) {
let lambdas = [];
let searchDir = `${srcPath}/${microApp}/backend/src`;

fs.readdirSync(searchDir).map(item => {
let lambdaDir = `${searchDir}/${item}`;

if (fs.lstatSync(lambdaDir).isDirectory()) {
lambdas = lambdas.concat(
fs.readdirSync(lambdaDir).filter(item => /.*\.zip$/.test(item)).map(item => `${lambdaDir}/${item}`)
)
}
});

return lambdas;
}

/**
* Check microservice for backend changes
* @param microApp
* @returns {Promise}
*/
function checkForBackendChanges(microApp) {
return new Promise((resolve, reject) => {
exec(`git diff --name-only HEAD ${compareBranch}`, (error, stdout) => {
if (error) {
return reject(error);
}

let hasChanges = false;
let files = stdout.split('\n').filter(item => item.trim());
let regExp = new RegExp(`${microApp}/backend`, 'gi');

for (let i = 0, len = files.length; i < len; i++) {
if (regExp.test(files[i])) {
hasChanges = true;
break;
}
}

resolve(hasChanges);
});
});
}
Loading

0 comments on commit b0484d1

Please sign in to comment.