UNPKG

2.2 kBJavaScriptView Raw
1const shell = require("shelljs");
2const crypto = require("crypto");
3const program = require("commander");
4const path = require("path");
5
6build_run_docker();
7
8function build_run_docker() {
9 program
10 .version("0.1.0")
11 .option("-i, --imagename [imagename]", "IMAGE_NAME")
12 .option("-t, --target", "DOCKER_STAGE")
13 .option("-f, --dockerfile <dockerfile>", "DOCKER_FILE")
14 .option("-r, --home_root <home_root>", "HOME_ROOT")
15
16 .parse(process.argv);
17 const DOCKER_PARAMS_NAME = ["target"]; //Declare docker params will be used
18
19 program.parse(process.argv);
20
21 // if (typeof program.home_root === "undefined") {
22 // console.error("Must identify --home_root!!!");
23 // process.exit(1);
24 // }
25
26 if (typeof program.imagename === "undefined") {
27 console.error("Must identify --imagename!!!");
28 process.exit(1);
29 }
30
31 function exec() {
32 console.log(arguments[0]);
33
34 shell.exec(...arguments);
35 }
36
37 //set default home_root is the current working directory
38 program.home_root = program.home_root || process.cwd();
39
40 const image_name = program.imagename;
41 const buf = Buffer.alloc(3); //create random 6 characters
42 const image_tag = crypto.randomFillSync(buf).toString("hex");
43
44 shell.pushd(`${program.home_root}`);
45
46 console.log(`Creating image ${image_name}:${image_tag}`);
47
48 //Docker build params
49 var docker_params = [];
50 for (let docker_param_name of DOCKER_PARAMS_NAME) {
51 if (program[docker_param_name]) {
52 docker_params.push(
53 `--${docker_param_name} ${program[docker_param_name]}`
54 );
55 }
56 }
57
58 var dockerfile_name = program.dockerfile || 'Dockerfile';
59 var dockerfile_path = path.resolve(program.home_root, dockerfile_name);
60 exec_wrapper(
61 `docker build ${docker_params.join(
62 " "
63 )} -t ${image_name}:${image_tag} -f ${dockerfile_path} .`
64 );
65
66 exec_wrapper(
67 `docker images | grep ${image_tag} | awk '{print $3}' | xargs -I {} docker run -it -d --name ${image_name}-${image_tag} -v /Users:/Users -p 49173:8080 -p 15672:15672 -p 5672:5672 -p 6379:6379 -p 5044:5044 -p 3001:3000 {} tail -f /dev/null`
68 );
69
70 function exec_wrapper(command) {
71 console.log(`Will run: ${command}`);
72 exec(command);
73 }
74};
75
76