forked from old-m-projects/dmport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
264 lines (218 loc) · 8.18 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
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
#! /usr/bin/env node
var program = require('commander');
var pack = require('./package.json');
var exec = require('child_process').exec;
var fs = require('fs');
var path = require('path');
var exportCommand = "";
/**
* Docker Machine Import / Export
*
* @copyright 2016 Mumba Pty Ltd. All rights reserved.
* @license Apache-2.0
*/
//Setup our command line interactions
program
.version(pack.version)
.usage('[options]')
.description('Import/Export docker-machines')
.option('-x, --export [value]', 'export docker machine')
.option('-i, --import [value]', 'import json encoded output from dmport export. NOTE! use eval $(dmport -i $ENVVAR_W_EXPORT_JSON) to set env vars for machine, certificates will also be written to file')
.option('-s, --shell [value]', 'sets the shell for docker-machine to use')
.parse(process.argv);
//we require options so lets error out if we dont get them.
if (!program.export && !program.import) {
console.error('An option is required');
process.exit(9);
}
function getPaths(vars) {
var machinepath, certPathArray, storepath, certpath;
if (vars.indexOf('DOCKER_') === -1) {
machinepath = path.join(process.env.HOME, '.docker', 'machine', 'machines', vars, path.sep);
certpath = path.join(process.env.HOME, '.docker', 'machine', 'certs', path.sep);
storepath = path.join(process.env.HOME, '.docker', 'machine', path.sep);
if (!fs.existsSync(path.join(process.env.HOME, '.docker'))) {
fs.mkdirSync(path.join(process.env.HOME, '.docker'));
}
if (!fs.existsSync(storepath)) {
fs.mkdirSync(storepath);
}
if (!fs.existsSync(certpath)) {
fs.mkdirSync(certpath);
}
if (!fs.existsSync(path.join(storepath, 'machines'))) {
fs.mkdirSync(path.join(storepath, 'machines'));
}
if (!fs.existsSync(machinepath)) {
fs.mkdirSync(machinepath);
}
} else {
//this regex digs the path out of the environment variables
machinepath = /DOCKER_CERT_PATH=(.*)/g.exec(vars)[1].replace(/['"]+/g, '');
certPathArray = machinepath.split(path.sep);
certPathArray.splice(-2, 2);
storepath = certPathArray.join(path.sep);
certPathArray.push("certs");
certpath = certPathArray.join(path.sep);
}
return {"machine": machinepath, "cert": certpath, "store": storepath};
}
//start of the export method
if (program.export) {
exportCommand = 'docker-machine env ' + program.export;
if(program.shell){
exportCommand = exportCommand + ' --shell ' + program.shell;
}
//since this is specific to docker-machine we are assuming its installed for exporting.
exec(exportCommand, function (error, stdout, stderr) {
if (error) {
console.error('exec error', error);
return;
}
var config = {};
var paths = getPaths(stdout);
//this regex look for all the environment variables with docker in the name, we also grab the value to place into our output config
var r = /(DOCKER_[A-Z]\w+)=(.*)/g;
var m;
//loop through the values of the regex to build our config
while (m = r.exec(stdout)) {
if (m[1] === 'DOCKER_CERT_PATH') {
config[m[1]] = '__MACHINE_PATH__';
} else {
config[m[1]] = m[2].replace(/['"]+/g, '');
}
}
config["machines"] = {};
config["certs"] = {};
//next we grab the files in the cert path, not sure if these are needed but I suspect they are
fs.readdir(paths.cert, function (err, files) {
Promise.all(files.map(function (filename) {
return new Promise(function (resolve, reject) {
fs.readFile(path.join(paths.cert, filename), 'utf8', function (err, data) {
if (err) {
reject(err);
}
config["certs"][filename] = new Buffer(data).toString('base64');
resolve();
});
});
})).then(function () {
//next we grab the files in the machine export path, I added this into a machine key so we can later add exporting of multiple wihtout breaking anyting
config["machines"][program.export] = {};
fs.readdir(paths.machine, function (err, files) {
Promise.all(files.map(function (filename) {
return new Promise(function (resolve, reject) {
fs.readFile(path.join(paths.machine, filename), 'utf8', function (err, data) {
if (err) {
reject(err);
}
//now we sanitize our paths in the config.json
if (filename === 'config.json') {
//this just preps the paths for use as regex, Im sure there is a better way to do this.
//@todo test replace(/[\/]+/g, '\\/')
//sanitize our machines path
var jsonifiedMachinePath = JSON.stringify(paths.machine)
.replace(/['"]+/g, '')//remove quotes
.replace(/[\\\\]+/g, '\\\\\\\\')//add escaping slashes for windows paths
.replace(/[\/]+/g, '\\/');//add escaping slashes for linux paths
//build our regex
var jmpr = new RegExp(jsonifiedMachinePath + '(\\\\+)?', 'g');
//replace our paths with something easily identifiable
data = data.replace(jmpr, '__MACHINE_PATH__');
//sanitize our certificate path
var jsonifiedCertPath = JSON.stringify(paths.cert)
.replace(/['"]+/g, '')
.replace(/[\\\\]+/g, '\\\\\\\\')
.replace(/[\/]+/g, '\\/');
var jcpr = new RegExp(jsonifiedCertPath + '(\\\\+)?', 'g');
data = data.replace(jcpr, '__MACHINE_CERT_PATH__');
//santize our storepath
var jsonifiedStorePath = JSON.stringify(paths.store)
.replace(/['"]+/g, '')
.replace(/[\\\\]+/g, '\\\\\\\\')
.replace(/[\/]+/g, '\\/');
var jspr = new RegExp(jsonifiedStorePath + '(\\\\+)?', 'g');
data = data.replace(jspr, '__MACHINE_STORE_PATH__');
}
config["machines"][program.export][filename] = new Buffer(data).toString('base64');
resolve();
});
});
})).then(function () {
//once we are done just console log the config.
console.log(JSON.stringify(config));
}).catch(function (err) {
console.error(err)
});
});
}).catch(function (err) {
console.error(err)
});
});
});
}
//start of the import method
if (program.import) {
var input = JSON.parse(program.import);
var keys = Object.keys(input);
var env = '';
var paths;
Object.keys(input['machines']).map(function (machinename) {
//we do the machine first so we can get the paths setup for certs but also change paths in the future if we have multiple machines.
paths = getPaths(machinename);
//We do the certs now that we have the paths setup
Promise.all(Object.keys(input['certs']).map(function (filename) {
return new Promise(function (resolve, reject) {
fs.writeFile(path.join(paths.cert, filename), new Buffer(input['certs'][filename], 'base64'), {
'encoding': 'utf8',
'mode': '600'
}, function (err) {
if (err) {
reject(err);
}
resolve();
});
});
})).then(function () {
}).catch(function (err) {
console.error(err)
});
//Finally we do the machines files
Promise.all(Object.keys(input['machines'][machinename]).map(function (filename) {
return new Promise(function (resolve, reject) {
var cleartext = new Buffer(input['machines'][machinename][filename], 'base64');
//for the config.json we want to add the paths appropriate for the computer we are importing to
if (filename === 'config.json') {
cleartext = cleartext.toString();
cleartext = cleartext.replace(/__MACHINE_PATH__/g, JSON.stringify(paths.machine).replace(/['"]+/g, ''));
cleartext = cleartext.replace(/__MACHINE_CERT_PATH__/g, JSON.stringify(paths.cert).replace(/['"]+/g, ''));
cleartext = cleartext.replace(/__MACHINE_STORE_PATH__/g, JSON.stringify(paths.store).replace(/['"]+/g, ''));
}
fs.writeFile(path.join(paths.machine, filename), cleartext, {
'encoding': 'utf8',
'mode': '600'
}, function (err) {
if (err) {
reject(err);
}
resolve();
});
});
})).then(function () {
//we loop through once to get our paths && exports
keys.forEach(function (v) {
if (v.indexOf('DOCKER') !== -1) {
if (v === 'DOCKER_CERT_PATH') {
env += 'export ' + v + '="' + paths.machine + '"\n';
} else {
env += 'export ' + v + '="' + input[v] + '"\n';
}
}
});
//output our environment vars
console.log(env);
}).catch(function (err) {
console.error(err)
});
});
}