forked from ConSol/appserver-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-appserver.js
executable file
·277 lines (244 loc) · 8.7 KB
/
docker-appserver.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/local/bin/node
var dot = require('dot');
dot.templateSettings.strip = false;
var fs = require('fs');
require('colors');
var _ = require('underscore');
var Docker = require('dockerode');
var tarCmd = "tar";
var child = require('child_process');
var stream = require('stream');
JSON.minify = JSON.minify || require("node-json-minify");
(function() {
var opts = parseOpts();
// All supported servers which must be present as a sub-directory
var servers = getServers(opts);
// Create build files
createAutomatedBuilds(servers, opts);
// If desired create Docker images
if (opts.options.build) {
buildImages(servers, opts);
}
})();
// ===============================================================================
function createAutomatedBuilds(servers, opts) {
console.log("Creating Automated Builds\n".cyan);
var globalConfig = {};
if (fs.existsSync("config.json")) {
globalConfig = JSON.parse(JSON.minify(fs.readFileSync("config.json", "utf8")));
}
servers.forEach(function (server) {
console.log(server.magenta);
var config =
_.extend({},
globalConfig,
getServersConfig(server));
var versions = extractVersions(config,opts.options.version);
execWithTemplates(server, function (templates) {
versions.forEach(function (version) {
console.log(" " + version.green);
ensureDir(__dirname + "/" + server + "/" + version);
var changed = false;
templates.forEach(function (template) {
var file = checkForMapping(config, version, template.file);
if (!file) {
// Skip any file flagged as being mapped but no mapping was found
return;
}
var templateHasChanged =
fillTemplate(
server + "/" + version + "/" + file,
template.templ,
_.extend(
{},
config,
{ "version": version,
"config": _.extend({}, config.meta['default'], config.meta[version])}
));
changed = changed || templateHasChanged;
});
if (!changed) {
console.log(" UNCHANGED".yellow);
} else {
}
});
});
});
}
function buildImages(servers,opts) {
console.log("\n\nBuilding Images\n".cyan);
var docker = new Docker(getDockerConnectionsParams(opts));
servers.forEach(function(server) {
console.log(server.magenta);
var versions = extractVersions(getServersConfig(server),opts.options.version);
doBuildImages(docker,server,versions);
});
}
// ===================================================================================
function checkForMapping(config,version,file) {
if (/^__.*$/.test(file)) {
var mappings = config.mappings || {};
var mapping = mappings[version] || {};
return mapping[file];
} else {
return file;
}
}
function execWithTemplates(server,templFunc) {
var templ_dir = server + "/templates";
var templates = fs.readdirSync(templ_dir);
var ret = [];
templates.forEach(function (template) {
ret.push({
"templ" : dot.template(fs.readFileSync(templ_dir + "/" + template)),
"file" : template
});
});
templFunc(ret);
}
function fillTemplate(file,template,config) {
var newContent = template(config).trim() + "\n";
var label = file.replace(/.*\/([^\/]+)$/,"$1");
if (!newContent.length) {
console.log(" " + label + ": " + "SKIPPED".grey);
return false;
} else {
var exists = fs.existsSync(file);
var oldContent = exists ? fs.readFileSync(file, "utf8") : undefined;
if (newContent.trim() !== oldContent.trim()) {
console.log(" " + label + ": " + (exists ? "CHANGED".green : "NEW".yellow));
fs.writeFileSync(file,newContent,{ "encoding" : "utf8"});
return true;
}
}
return false;
}
function ensureDir(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir,0755);
}
var stat = fs.statSync(dir);
if (!stat.isDirectory()) {
throw new Error(dir + " is not a directory");
}
}
function getServers(opts) {
if (opts.options.server) {
return _.filter(getAllServers(), function (server) {
return _.contains(opts.options.server,server);
});
} else {
return getAllServers();
}
}
function getAllServers() {
return _.filter(fs.readdirSync(__dirname), function (f) {
return fs.existsSync(f + "/servers.json");
});
}
function getServersConfig(server) {
return JSON.parse(JSON.minify(fs.readFileSync(__dirname + "/" + server + "/servers.json", "utf8")));
}
function extractVersions(config,versionsFromOpts) {
if (versionsFromOpts) {
return _.filter(config.versions, function (version) {
return _.contains(versionsFromOpts,version);
});
} else {
return config.versions;
}
}
function getFullVersion(server,version) {
var config = getServersConfig(server);
return config.meta[version].version;
}
function doBuildImages(docker,server,versions) {
if (versions.length > 0) {
var version = versions.shift();
console.log(" " + version.green);
var tar = child.spawn(tarCmd, ['-c', '.'], { cwd: __dirname + "/" + server + "/" + version });
var name = "consol/" + server + "-" + version;
var fullName = name + ":" + getFullVersion(server,version);
docker.buildImage(
tar.stdout, { "t": fullName, "forcerm": true, "q": true },
function (error, stream) {
if (error) {
throw error;
}
stream.pipe(getResponseStream());
stream.on('end', function () {
docker.getImage(fullName).tag({repo: name }, function (error, result) {
if (error) { throw error; }
console.log(result);
});
doBuildImages(docker,server,versions);
});
});
}
}
function getResponseStream() {
var buildResponseStream = new stream.Writable();
buildResponseStream._write = function (chunk, encoding, done) {
var resp = JSON.parse(chunk.toString());
if (resp.stream) {
process.stdout.write(resp.stream);
}
if (resp.errorDetail) {
process.stderr.write(resp.stream);
}
done();
};
return buildResponseStream;
}
function getDockerConnectionsParams(opts) {
if (opts.options.host) {
return {
"host": "http://" + opts.options.host,
"port": opts.options.port || 2375
};
} else if (process.env.DOCKER_HOST) {
var parts = process.env.DOCKER_HOST.match(/^tcp:\/\/(.+?)\:?(\d+)?$/i);
if (parts !== null) {
return {
"host" : "http://" + parts[1],
"port" : parts[2] || 2375
};
} else {
return {
"socketPath" : process.env.DOCKER_HOST
};
}
} else {
return {
"host" : "http://localhost",
"port" : 2375
};
}
}
function parseOpts() {
var Getopt = require('node-getopt');
var getopt = new Getopt([
['s' , 'server=ARG+', 'Servers for which to create container images (e.g. "tomcat")'],
['v' , 'version=ARG+', 'Versions of a given server to create (e.g. "7.0" for tomcat)'],
['b' , 'build', 'Build image(s)'],
['d' , 'host', 'Docker hostname (default: localhost)'],
['p' , 'port', 'Docker port (default: 2375)'],
['h' , 'help', 'display this help']
]);
var help =
"Usage: node docker-appserver.js [OPTION]\n" +
"Generator for automated Docker builds.\n" +
"\n" +
"[[OPTIONS]]\n" +
"\n" +
"This script creates so called 'automated builds' for Java application server\n" +
"which can be registered at hub.docker.io\n\n" +
"It uses templates for covering multiple version of appserver.\n\n" +
"Supported servers:\n\n";
var servers = getAllServers();
servers.forEach(function (server) {
var config = getServersConfig(server);
help += " " + server + ": " + config.versions.join(", ") + "\n";
});
return getopt.bindHelp(help).parseSystem();
}