-
Notifications
You must be signed in to change notification settings - Fork 33
/
import.js
82 lines (68 loc) · 2.2 KB
/
import.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
#! /usr/bin/env node
console.log('importing.')
var fs = require('fs')
var path = require('path')
var os = require('os')
var fse = require('fs.extra')
var Zip = require('node-zip')
var util = require('./util')
var DM_CERTS_DIR = '/.docker/machine/certs/'
var DM_MACHINE_DIR = '/.docker/machine/machines'
var HOME = os.homedir()
var TMP = os.tmpdir()
var args = process.argv.slice(2)
var machineArg = args[0]
if (!machineArg) {
console.log('machine-import <config-zip>')
process.exit(1)
}
var machine = path.basename(machineArg, path.extname(machineArg))
console.log('Using ', path.basename(machineArg), ' as file name')
var configDir = path.join(HOME, DM_MACHINE_DIR, machine)
try {
fs.statSync(configDir)
console.log('that machine already exists')
process.exit(1)
} catch (e) {}
var tmp = path.join(TMP, machine)
fse.rmrfSync(tmp)
unzip()
processConfig()
util.copyDir(tmp, configDir)
util.copyDir(path.join(tmp, 'certs'), path.join(HOME, DM_CERTS_DIR, machine))
// Fix file permissions for id_rsa key, if present
util.permissions(path.join(configDir, 'id_rsa'), '0600')
fse.rmrfSync(tmp)
function unzip () {
var zip = new Zip()
zip.load(fs.readFileSync(machine + '.zip'))
for (var f in zip.files) {
var file = zip.files[f]
if (!file.dir) {
util.mkdir(path.dirname(path.join(tmp, file.name)))
fs.writeFileSync(path.join(tmp, file.name), file.asNodeBuffer())
}
}
}
function processConfig () {
var configName = path.join(tmp, 'config.json')
var configFile = fs.readFileSync(configName)
var config = JSON.parse(configFile.toString())
util.recurseJson(config, function (parent, key, value) {
if (typeof value === 'string' && value.indexOf('{{HOME}}') > -1) {
// path.join fixes windows/unix paths
parent[key] = path.join(value.replace('{{HOME}}', HOME))
}
})
var raw = config.RawDriver
if (raw) {
var decoded = new Buffer(raw, 'base64').toString()
var driver = JSON.parse(decoded)
// update store path
driver.StorePath = path.join(HOME, '/.docker/machine')
var updatedBlob = new Buffer(JSON.stringify(driver)).toString('base64')
// update old config
config.RawDriver = updatedBlob
}
fs.writeFileSync(configName, JSON.stringify(config))
}