forked from bhurlow/machine-share
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.js
More file actions
executable file
·94 lines (70 loc) · 2.48 KB
/
Copy pathexport.js
File metadata and controls
executable file
·94 lines (70 loc) · 2.48 KB
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
#! /usr/bin/env node
var fs = require('fs');
var path = require('path');
var fse = require('fs-extra');
var tgz = require('tar.gz');
var util = require('./util');
var args = process.argv.slice(2);
var machine = args[0];
if (!machine) {
console.log('machine-export <machine-name>');
process.exit(1);
}
var dm = new util.dm(machine);
console.dir(dm);
fse.emptyDirSync(dm.tmpDir);
fse.mkdirsSync( dm.tmpCerts );
fse.mkdirsSync( dm.tmpExt );
fse.copySync(dm.configDir, dm.tmpDir);
processConfig(dm.tmpConfigFile);
createTgzFolder(dm.tmpDir, machine + '.tar.gz');
function toPortableConfig(key, value) {
if (typeof value === 'string' && value.length > 0) {
var orig_value = value;
// console.log(`Json: "${key}": "${value}"`);
if ( util.startsWith(value, dm.certsDir + path.sep ) ) {
var name = path.basename(value);
// console.log('Cert name: ' + name);
if(name.length > 0) {
util.copy(value, dm.tmpCerts);
// the copy of global certs from source machine, stored in a sub-folder on the target
value = path.join(dm.configDir, dm.certsName, name);
}
} else if(util.startsWith(value, dm.configDir + path.sep )) {
// normal file inside the machine config dir
} else if( util.isfile(value) ) {
console.log(`External file reference "${value}"`);
util.copy(value, dm.tmpExt);
value = path.join(dm.configDir, dm.extName, path.basename(value));
}
value = value.replace(dm.home, '{{HOME}}');
if(value != orig_value) {
if( path.sep == '\\') {
// convert all paths to forward slash
value = value.replace(/\\/g, '/');
}
// console.log(`"${orig_value}" -> "${value}"`);
}
}
return value;
}
function processConfig(configPath) {
var config = fse.readJsonSync(configPath);
var drv = config['DriverName'];
if( drv ) {
// TODO: not much sense to export 'virtualbox' etc
console.log(`Machine driver: "${drv}"`);
}
fs.writeFileSync(configPath, JSON.stringify(config, toPortableConfig, 4));
}
function createTgzFolder(srcFolder, dstFile) {
tgz().compress(srcFolder, dstFile)
.then(function() {
console.log(`Config saved to "${dstFile}"`);
process.exit(0);
})
.catch(function(err) {
console.log('Archive Error ', err.stack);
process.exit(3);
});
}