Skip to content

Commit

Permalink
docker scripts WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gandazgul committed Jan 19, 2019
1 parent d2c7550 commit d802024
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ helmfile.d/70-cronjobs.carlos.yaml
helmfile.d/values/ipaddress.yaml
helmfile.d/values/gogs.yaml
secondary-master.d/values/ipaddress.yaml
helmfile.d/70-cronjobs.yaml

# chart deps
charts/gogs/charts/

# secrets and private files
secrets.sh

#node
node_modules/
6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions docker/docker-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const execSync = require('child_process').execSync;
// const logger = require('../src/api/logger');
const logger = console;
logger.warning = console.warn;
const argv = require('minimist')(process.argv.slice(2));

const imageName = argv.image;
if (!imageName) {
logger.error('DOCKER:BUILD', 'Please specify an image name with --image=');
process.exit(1);
}

try {
process.chdir(`docker/${imageName}`);

console.log(`New directory: ${process.cwd()}`);
}
catch (err) {
logger.error('DOCKER:BUILD', 'Chdir failed', err);
}

const ioOptions = { stdio: [process.stdin, process.stdout, process.stderr] };

const username = execSync('whoami').toString('ascii').trim();
const version = execSync('git log -n 1 --pretty=format:%h').toString('ascii').trim();
const dockerImageName = `${username}/${imageName}:v${version}`;
const dockerImageNameLatest = `${username}/${imageName}:latest`;

function run() {
if (argv.force) {
build();
}
else {
logger.info('DOCKER:BUILD', `Checking docker image: ${dockerImageName}...`);

const imageID = execSync(`docker images -q ${dockerImageName}`).toString();

if (!imageID) {
build();
}
else {
logger.warning(
'DOCKER:BUILD',
'The tag already exists, commit something first to build new image or run with --force.'
);
}
}
}

function build() {
// Build and then tag the docker image with the current version AND "latest"
const dockerBuildCommand = `docker build -t docker.io/${dockerImageName} -t docker.io/${dockerImageNameLatest} .`;
const pushCommand = `docker push docker.io/${dockerImageName}`;
const pushCommandLatest = `docker push docker.io/${dockerImageNameLatest}`;

try {
logger.info('DOCKER:BUILD', `Running: ${dockerBuildCommand}`);
execSync(dockerBuildCommand, ioOptions);
logger.info('DOCKER:BUILD', `Running: ${pushCommand}`);
execSync(pushCommand, ioOptions);
logger.info('DOCKER:BUILD', `Running: ${pushCommandLatest}`);
execSync(pushCommandLatest, ioOptions);
}
catch (err) {
logger.error('DOCKER:BUILD', 'A fatal error has occurred.', err.message);
process.exit(1);
}
}

run();
52 changes: 52 additions & 0 deletions docker/docker-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const exec = require('child_process').spawn;
const execSync = require('child_process').execSync;
// const logger = require('../src/api/logger');
const logger = console;
logger.warning = console.warn;
const argv = require('minimist')(process.argv.slice(2));

console.log(argv);

// TODO: each docker folder can export a manifest of their params, docker-compose file?

const imageName = argv.image;
if (!imageName) {
logger.error('DOCKER:BUILD', 'Please specify an image name with --image=');
process.exit(1);
}

const stdio = [process.stdin, process.stdout, process.stderr];
const ioOptions = { detached: true, shell: true, stdio };
const username = execSync('whoami').toString('ascii').trim();
const dockerImageNameLatest = `${username}/${imageName}:latest`;

process.on('exit', (code) => {
if (code !== 0) {
logger.error('DOCKER:RUN', `Exiting with exit code: ${code}`);
}

logger.info('DOCKER:RUN', 'Shutting down');
execSync(`docker stop ${imageName}`, { stdio });
logger.info('DOCKER:RUN', 'Shut down complete');
});

process.on('SIGINT', () => {
process.exit(0);
});

const runCommand = `docker run --rm --name=${imageName} ${argv._.join(' ')} docker.io/${dockerImageNameLatest}`;

try {
logger.info('DOCKER:RUN', `Running: ${runCommand}`);
exec(runCommand, ioOptions);
}
catch (error) {
console.error('ERROR!');
console.error(error.status);

if (error.status > 0) {
console.error('A fatal error has occurred.');
console.error(error.message);
process.exit(1);
}
}
2 changes: 1 addition & 1 deletion helmfile.d/53-mqtt-smarttings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ releases:
chart: ../charts/nest-mqtt-bridge
values:
- image:
tag: "v1"
tag: "v5cd8cb"
- volumes:
- name: yasr-volume
persistentVolumeClaim:
Expand Down
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "k8s-infrastructure",
"version": "1.0.0",
"main": "index.js",
"repository": "[email protected]:gandazgul/my-infrastructure.git",
"author": "Carlos Ravelo <[email protected]>",
"license": "MIT",
"private": true,
"scripts": {
"docker:build": "node ./docker/docker-build.js",
"docker:run": "node ./docker/docker-run.js"
},
"dependencies": {
"minimist": "^1.2.0"
}
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


minimist@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=

0 comments on commit d802024

Please sign in to comment.