-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-stack-on-swarm.js
138 lines (117 loc) · 2.99 KB
/
run-stack-on-swarm.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
#!/usr/bin/env node
// Run a Docker Cloud stack file on a Docker 1.12 or higher swarm
const fs = require('fs');
const yaml = require('js-yaml');
var doc = null;
// Get document, or throw exception on error
try {
doc = yaml.safeLoad(fs.readFileSync(process.argv[2], 'utf8'));
} catch (e) {
console.error(e);
process.exit(-1);
}
var network;
if (process.argv.length > 3) {
network = process.argv[3];
}
console.log("#!/bin/sh");
var addOption = (options, key, value) => {
if (options[key]) {
options[key].push(value);
} else {
options[key] = [value];
}
return options;
};
var toEnvStr = (key, str) => {
let envstr = null;
if (typeof(str) == "string" && str.match(/\s+/)) {
if (str.indexOf('"') !== -1) {
envstr = `'${key}=${str}'`;
} else {
envstr = `"${key}=${str}"`;
}
} else {
envstr = `${key}=${str}`;
}
return envstr;
};
for (name in doc) {
let service = doc[name];
let options = {"name": [name]},
image = null,
command = null;
if (network) {
addOption(options, "network", network);
}
for (key in service) {
let value = service[key];
switch (key) {
case "image":
image = value;
break;
case "command":
command = value;
break;
case "volumes":
process.stderr.write(`WARN: ignoring volumes for ${name}\n`);
break;
case "volumes_from":
process.stderr.write(`WARN: ignoring volumes for ${name}\n`);
break;
case "tags":
for (j in value) {
// addOption(options, "constraint", `node.label.${value[j]}==${value[j]}`);
}
break;
case "ports":
for (j in value) {
addOption(options, "publish", value[j]);
}
break;
case "environment":
if (Array.isArray(value)) {
for (j in value) {
let parts = value[j].split("=", 2);
let str = toEnvStr(parts[0], parts[1]);
addOption(options, "env", str);
}
} else {
for (key in value) {
let str = toEnvStr(key, value[key]);
addOption(options, "env", str);
}
}
break;
case "restart":
addOption(options, "restart-condition", value);
break;
case "target_num_containers":
addOption(options, "replicas", value);
break;
case "sequential_deployment":
addOption(options, "update-parallelism", "2");
addOption(options, "update-delay", "10s");
break;
default:
process.stderr.write(`WARN: ignoring unknown option ${key} for ${name}\n`);
break;
}
}
var optstrs = [];
for (key in options) {
let values = options[key];
for (i in values) {
let value = values[i];
optstrs.push(`--${key} ${value}`);
}
}
var cmd = "docker service create \\\n ";
cmd += optstrs.join(" \\\n ");
cmd += ` \\\n ${image}`;
if (command) {
cmd += ` \\\n ${command}`;
}
cmd += "\n\n";
console.log(cmd);
}