This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
165 lines (141 loc) · 4.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var fs = require('fs');
var yaml = require('js-yaml');
var path = require('path');
var execSync = require('execSync');
var tmp = require('tmp');
var rl = require('readline');
var path = require('path');
var readConfig = function(buildPath, opts) {
var playbookPath = path.join(buildPath, 'dockerflow.yml');
if (!fs.existsSync(playbookPath)) {
throw new Error("dockerflow.yml not found in " + buildPath);
}
var playbookYml = yaml.safeLoad(fs.readFileSync(playbookPath, 'utf8'));
var vars = playbookYml[0].vars;
var envSpec = opts["environment"] || vars["dockerflow-environment"];
var envKeys = [];
if (envSpec) {
envKeys = envSpec.split(" ");
}
var envOpts = [];
for (var i = 0; i < envKeys.length; i++) {
var k = envKeys[i];
envOpts.push("-e " + k + "=$" + k);
}
var host = opts.H || opts.host;
if (host) {
host = "-H " + host;
} else {
host = ""
}
return {
host: host,
base: opts["base"] || vars["dockerflow-base"],
tag: opts["tag"] || vars["dockerflow-tag"],
command: opts["command"] || vars["dockerflow-command"],
dockerOptions: "-v " + buildPath + ":/dockerflow:ro " + envOpts.join(" ") + " " + (opts["docker-options"] || vars["dockerflow-docker-options"] || "")
};
};
var cleanup = function(ctrId) {
console.log("Cleaning up.")
execSync.exec("docker kill " + ctrId);
execSync.exec("docker rm "+ ctrId);
process.exit();
};
var commitAndTag = function(ctrId, opts) {
if (opts.command) {
cmdPart = "--run='" + JSON.stringify({Cmd: opts.command.split(" ")}) + "'";
} else {
cmdPart = ""
}
var info = execSync.exec("docker commit " + cmdPart + " " + ctrId);
var imgId = info.stdout.trim();
console.log("Image id is ", imgId);
execSync.exec("docker tag " + imgId + " " + opts.tag);
cleanup(ctrId);
};
// Cribbed from https://coderwall.com/p/v16yja
var ask = function(question, callback) {
var r = rl.createInterface({
input: process.stdin,
output: process.stdout});
r.question(question + ' ', function(answer) {
r.close();
callback(null, answer);
});
};
var yesses = {y: true, Y: true, yes: true, Yes: true, YES: true}
var execDebugBuild = function(opts) {
tmp.dir(function(err, dirPath) {
if (err) {
throw err;
}
var ctrIdPath = path.join(dirPath, "ctrId");
fs.writeFileSync(ctrIdPath, "");
var rcFilePath = path.join(dirPath, "rcFile");
var ansibleCmd = 'ansible-playbook /dockerflow/dockerflow.yml -c local -i "127.0.0.1,"';
var rcFileContents = [
"echo $HOSTNAME > /tmp/dockerflow-ctrId",
"history -s '" + ansibleCmd + "'",
ansibleCmd
].join("\n");
fs.writeFileSync(rcFilePath, rcFileContents);
var extraMounts = "-v " + ctrIdPath + ":/tmp/dockerflow-ctrId" + " -v " + rcFilePath + ":/tmp/dockerflow-commands";
var ctrCmd = "/bin/bash -c '/bin/bash --rcfile /tmp/dockerflow-commands'";
console.log("Dropping you into the debug container. Build will begin immediately.");
var runCmd = ["docker", opts.host, "run -i -t", extraMounts, opts.dockerOptions, opts.base, ctrCmd].join(" ");
execSync.run(runCmd);
var ctrId = fs.readFileSync(ctrIdPath).toString().trim();
ask("Commit and tag this image? (default: no)", function(err, answer) {
if (err) {
throw err;
}
if (yesses[answer.trim()]) {
commitAndTag(ctrId, opts);
} else {
cleanup(ctrId);
}
});
});
};
var execBuild = function(opts) {
var ctrCmd = "ansible-playbook /dockerflow/dockerflow.yml -c local -i \"127.0.0.1,\"";
var runCmd = ["docker", opts.host, "run -i -t -d", opts.dockerOptions, opts.base, ctrCmd].join(" ");
var info = execSync.exec(runCmd);
if (info.code) {
console.log("Unable to start container, code " + info.code + ": " + info.stdout);
process.exit();
}
var ctrId = info.stdout.trim();
var interrupted = false;
process.on('SIGINT', function() {
interrupted = true;
cleanup(ctrId);
});
execSync.run("docker logs -f " + ctrId);
var exitCode = parseInt(execSync.exec("docker wait " + ctrId).stdout.trim());
if (exitCode || interrupted) {
console.log("Build failed or interrupted, not tagging image.");
cleanup(ctrId);
}
console.log("Build completed successfully.");
commitAndTag(ctrId, opts)
};
var build = exports.build = function build(buildPath, opts) {
var imgs = readConfig(buildPath, opts);
execBuild(imgs);
};
var rebuild = exports.rebuild = function rebuild(buildPath, opts) {
var imgs = readConfig(buildPath, opts);
imgs.base = imgs.tag;
execBuild(imgs);
};
var debugBuild = exports["debug-build"] = function debugBuild(buildPath, opts) {
var imgs = readConfig(buildPath, opts);
execDebugBuild(imgs);
};
var debugRebuild = exports["debug-rebuild"] = function debugRebuild(buildPath, opts) {
var imgs = readConfig(buildPath, opts);
imgs.base = imgs.tag;
execDebugBuild(imgs);
}