-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |