2017-05-31 03:44:37 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const execa = require('execa');
|
|
|
|
const { parse } = require('dotenv');
|
2017-05-31 20:35:06 +03:00
|
|
|
const { name } = require('../package.json');
|
2017-05-31 03:44:37 +03:00
|
|
|
const main = require('apr-main');
|
|
|
|
const map = require('apr-map');
|
|
|
|
const series = require('apr-series');
|
|
|
|
const globby = require('globby');
|
|
|
|
const path = require('path');
|
|
|
|
const flatten = require('lodash.flatten');
|
|
|
|
const readPkg = require('read-pkg');
|
|
|
|
const figures = require('figures');
|
|
|
|
|
2017-06-01 01:55:41 +03:00
|
|
|
const COMPOSE_PROJECT_NAME = `${name}-${process.env.CIRCLE_BRANCH}`;
|
2017-05-31 03:44:37 +03:00
|
|
|
const NAMESPACE = 'quay.io/yldio';
|
|
|
|
const ROOT = path.join(__dirname, '..');
|
|
|
|
const LOG = console.log;
|
|
|
|
|
2017-05-31 18:08:29 +03:00
|
|
|
const createTritonProfile = async () => {
|
2017-05-31 19:57:57 +03:00
|
|
|
const { SDC_ACCOUNT = '', SDC_KEY_ID = '' } = process.env;
|
|
|
|
|
2017-05-31 13:46:58 +03:00
|
|
|
LOG(
|
|
|
|
`${figures.arrowRight} create triton profile SDC_ACCOUNT=${SDC_ACCOUNT.length} SDC_KEY_ID=${SDC_KEY_ID.length}`
|
|
|
|
);
|
2017-05-31 13:32:15 +03:00
|
|
|
|
|
|
|
const payload = JSON.stringify({
|
|
|
|
url: 'https://eu-ams-1.api.joyent.com',
|
|
|
|
account: SDC_ACCOUNT,
|
|
|
|
keyId: SDC_KEY_ID,
|
|
|
|
name: 'eu-ams-1',
|
|
|
|
curr: true
|
|
|
|
});
|
|
|
|
|
2017-05-31 13:46:58 +03:00
|
|
|
const cp = execa('triton', ['profile', 'create', '-f', '-'], {
|
|
|
|
input: payload
|
|
|
|
});
|
2017-05-31 13:32:15 +03:00
|
|
|
|
2017-05-31 13:46:58 +03:00
|
|
|
cp.stdout.pipe(process.stdout);
|
|
|
|
cp.stderr.pipe(process.stderr);
|
2017-05-31 13:32:15 +03:00
|
|
|
|
|
|
|
return cp;
|
|
|
|
};
|
|
|
|
|
2017-05-31 03:44:37 +03:00
|
|
|
const getEnv = async () => {
|
2017-05-31 18:08:29 +03:00
|
|
|
await createTritonProfile();
|
2017-05-31 03:44:37 +03:00
|
|
|
const tritonEnv = await execa.stdout('triton', ['env']);
|
|
|
|
const dotEnv = tritonEnv.replace(/^export /gim, '');
|
|
|
|
return Object.assign({}, process.env, parse(dotEnv));
|
|
|
|
};
|
|
|
|
|
2017-05-31 18:08:29 +03:00
|
|
|
const login = async () => {
|
2017-05-31 03:44:37 +03:00
|
|
|
LOG(`${figures.arrowRight} login`);
|
2017-05-31 18:08:29 +03:00
|
|
|
|
|
|
|
const {
|
|
|
|
_DOCKER_LOGIN_USERNAME,
|
|
|
|
_DOCKER_LOGIN_PASSWORD,
|
|
|
|
_DOCKER_REGISTRY
|
|
|
|
} = process.env;
|
|
|
|
|
2017-05-31 13:46:58 +03:00
|
|
|
return execa(
|
|
|
|
'docker',
|
|
|
|
[
|
|
|
|
'login',
|
2017-06-01 03:47:41 +03:00
|
|
|
'--email="."',
|
2017-05-31 13:46:58 +03:00
|
|
|
`--username="${_DOCKER_LOGIN_USERNAME}"`,
|
|
|
|
`--password="${_DOCKER_LOGIN_PASSWORD}"`,
|
|
|
|
_DOCKER_REGISTRY
|
|
|
|
],
|
|
|
|
{
|
|
|
|
stdio: 'inherit'
|
|
|
|
}
|
|
|
|
);
|
2017-05-31 03:44:37 +03:00
|
|
|
};
|
|
|
|
|
2017-05-31 18:08:29 +03:00
|
|
|
const build = async () => {
|
2017-05-31 03:44:37 +03:00
|
|
|
LOG(`${figures.arrowRight} build`);
|
|
|
|
|
2017-05-31 19:57:57 +03:00
|
|
|
const { CIRCLE_BRANCH } = process.env;
|
|
|
|
|
2017-05-31 03:44:37 +03:00
|
|
|
const dockerfiles = await globby(['packages/*/Dockerfile'], {
|
|
|
|
cwd: ROOT
|
|
|
|
});
|
|
|
|
|
|
|
|
LOG(`${figures.arrowRight} build.dockerfiles`);
|
|
|
|
LOG(JSON.stringify(dockerfiles, null, 2));
|
|
|
|
|
2017-05-31 18:08:29 +03:00
|
|
|
const images = await map(dockerfiles, async dockerfile => {
|
2017-05-31 03:44:37 +03:00
|
|
|
const folder = path.resolve(ROOT, path.dirname(dockerfile));
|
|
|
|
const { name } = await readPkg(folder);
|
|
|
|
const tags = [`${name}:${CIRCLE_BRANCH}`, `${name}:latest`];
|
|
|
|
|
|
|
|
LOG(`${figures.arrowRight} build.name ${name}`);
|
|
|
|
LOG(JSON.stringify(tags, null, 2));
|
|
|
|
|
|
|
|
await execa(
|
|
|
|
'docker',
|
|
|
|
flatten([
|
|
|
|
'build',
|
|
|
|
flatten(tags.map(name => ['-t', `${NAMESPACE}/${name}`])),
|
|
|
|
'-f',
|
|
|
|
dockerfile,
|
|
|
|
'.'
|
|
|
|
]),
|
|
|
|
{
|
|
|
|
stdio: 'inherit'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-05-31 18:08:29 +03:00
|
|
|
return `${NAMESPACE}/${name}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
await map(images, image => {
|
|
|
|
LOG(`${figures.arrowRight} build.push ${image}`);
|
2017-05-31 18:34:00 +03:00
|
|
|
return execa('docker', ['push', `${image}`], {
|
2017-05-31 03:44:37 +03:00
|
|
|
stdio: 'inherit'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-05-31 18:08:29 +03:00
|
|
|
const logout = () => {
|
2017-05-31 03:44:37 +03:00
|
|
|
LOG(`${figures.arrowRight} logout`);
|
2017-05-31 12:38:19 +03:00
|
|
|
return execa('docker', ['logout'], {
|
|
|
|
stdio: 'inherit'
|
|
|
|
});
|
2017-05-31 03:44:37 +03:00
|
|
|
};
|
|
|
|
|
2017-05-31 18:08:29 +03:00
|
|
|
const deploy = async () => {
|
|
|
|
const env = await getEnv();
|
|
|
|
|
2017-05-31 03:44:37 +03:00
|
|
|
LOG(`${figures.arrowRight} deploy`);
|
2017-06-01 01:55:41 +03:00
|
|
|
|
2017-06-01 02:32:12 +03:00
|
|
|
// kill containers
|
2017-06-01 01:55:41 +03:00
|
|
|
await execa('docker-compose', ['kill'], {
|
|
|
|
stdio: 'inherit',
|
|
|
|
env: Object.assign({}, env, {
|
|
|
|
COMPOSE_PROJECT_NAME
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-06-01 02:32:12 +03:00
|
|
|
// rm containers
|
2017-06-01 02:56:56 +03:00
|
|
|
await execa('docker-compose', ['rm', '--force'], {
|
2017-06-01 01:55:41 +03:00
|
|
|
stdio: 'inherit',
|
|
|
|
env: Object.assign({}, env, {
|
|
|
|
COMPOSE_PROJECT_NAME
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-06-01 02:32:12 +03:00
|
|
|
// pull new images
|
2017-06-01 02:56:56 +03:00
|
|
|
await execa('docker-compose', ['pull', '--parallel'], {
|
2017-06-01 02:32:12 +03:00
|
|
|
stdio: 'inherit',
|
|
|
|
env: Object.assign({}, env, {
|
|
|
|
COMPOSE_PROJECT_NAME
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
// up project
|
2017-06-01 01:19:26 +03:00
|
|
|
return execa('docker-compose', ['up', '-d', '--build', '--force-recreate'], {
|
2017-05-31 12:38:19 +03:00
|
|
|
stdio: 'inherit',
|
2017-05-31 20:35:06 +03:00
|
|
|
env: Object.assign({}, env, {
|
2017-06-01 01:55:41 +03:00
|
|
|
COMPOSE_PROJECT_NAME
|
2017-05-31 20:35:06 +03:00
|
|
|
})
|
2017-05-31 03:44:37 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const run = async () => {
|
|
|
|
LOG(`${figures.arrowRight} .env`);
|
2017-05-31 18:08:29 +03:00
|
|
|
LOG(JSON.stringify(Object.keys(process.env), null, 2));
|
2017-05-31 03:44:37 +03:00
|
|
|
|
2017-05-31 18:08:29 +03:00
|
|
|
return series([login, build, logout, deploy]);
|
2017-05-31 03:44:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
LOG(`${figures.arrowRight} DEPLOY`);
|
|
|
|
main(run());
|