diff --git a/export.js b/export.js index 32c34d0..60fcac8 100755 --- a/export.js +++ b/export.js @@ -1,5 +1,4 @@ #! /usr/bin/env node -console.log('exporting.') var fs = require('fs') var path = require('path') @@ -9,23 +8,23 @@ var Zip = require('node-zip') var slash = require('slash') var util = require('./util') -var DM_CERTS_DIR = '/.docker/machine/certs/' -var DM_MACHINE_DIR = '/.docker/machine/machines' -var HOME = os.homedir() +var DM_CERTS_DIR = 'certs' +var DM_MACHINE_DIR = 'machines' var TMP = os.tmpdir() -var args = process.argv.slice(2) - -var machine = args[0] +var cli = util.cli() +var machine = cli.params[0] if (!machine) { console.log('machine-export ') process.exit(1) } +console.log('Exporting machine "' + machine + '" from', cli.storagePath) + var tmp = path.join(TMP, machine) fse.rmrfSync(tmp) -var configDir = path.join(HOME, DM_MACHINE_DIR, machine) +var configDir = path.join(cli.storagePath, DM_MACHINE_DIR, machine) util.copyDir(configDir, tmp) fs.mkdirSync(path.join(tmp, 'certs')) @@ -39,13 +38,13 @@ function processConfig () { util.recurseJson(config, function (parent, key, value) { if (typeof value === 'string') { - if (util.startsWith(value, path.join(HOME, DM_CERTS_DIR))) { + if (util.startsWith(value, path.join(cli.storagePath, DM_CERTS_DIR, '/'))) { var name = path.basename(value) util.copy(value, path.join(tmp, 'certs', name)) - value = path.join(HOME, DM_CERTS_DIR, machine, name) + value = path.join(cli.storagePath, DM_CERTS_DIR, machine, name) } - value = value.replace(HOME, '{{HOME}}') - // uniform windows/unix paths + value = value.replace(cli.storagePath, '{{STORAGE}}') + // uniform windows/unix paths value = slash(value) parent[key] = value } diff --git a/import.js b/import.js index e795c16..55ca1ed 100644 --- a/import.js +++ b/import.js @@ -1,5 +1,4 @@ #! /usr/bin/env node -console.log('importing.') var fs = require('fs') var path = require('path') @@ -8,33 +7,30 @@ 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 DM_CERTS_DIR = 'certs' +var DM_MACHINE_DIR = 'machines' var TMP = os.tmpdir() -var args = process.argv.slice(2) -var machineArg = args[0] +var cli = util.cli() +var machineArg = cli.params[0] if (!machineArg) { console.log('machine-import ') process.exit(1) } // Make sure command points to a zip or tar file -var file_type = machineArg.substring(machineArg.length - 4) -var machine = "" -if (!file_type.match(/^(.zip|.tar)$/)) { - machine = machineArg.substring(0, machineArg.length - 4) -} -machine = machineArg -console.log('Using ', machineArg, " as file name") +var fileType = machineArg.substring(machineArg.length - 4) +var machine = fileType.match(/^(.zip|.tar)$/) ? machineArg.substring(0, machineArg.length - 4) : machineArg + +console.log('Importing machine "' + machine + '" into', cli.storagePath) -var configDir = path.join(HOME, DM_MACHINE_DIR, machine) +var configDir = path.join(cli.storagePath, DM_MACHINE_DIR, machine) try { fs.statSync(configDir) console.log('that machine already exists') process.exit(1) -} catch (e) {} +} catch (e) { +} var tmp = path.join(TMP, machine) fse.rmrfSync(tmp) @@ -43,7 +39,7 @@ unzip() processConfig() util.copyDir(tmp, configDir) -util.copyDir(path.join(tmp, 'certs'), path.join(HOME, DM_CERTS_DIR, machine)) +util.copyDir(path.join(tmp, 'certs'), path.join(cli.storagePath, DM_CERTS_DIR, machine)) // Fix file permissions for id_rsa key, if present util.permissions(path.join(configDir, 'id_rsa'), '0600') fse.rmrfSync(tmp) @@ -66,9 +62,13 @@ function processConfig () { 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)) + if (typeof value === 'string') { + if (value.indexOf('{{HOME}}') >= 0) { + parent[key] = path.join(value.replace('{{HOME}}/.docker/machine', cli.storagePath)) + } + if (value.indexOf('{{STORAGE}}') >= 0) { + parent[key] = path.join(value.replace('{{STORAGE}}', cli.storagePath)) + } } }) @@ -77,7 +77,7 @@ function processConfig () { var decoded = new Buffer(raw, 'base64').toString() var driver = JSON.parse(decoded) // update store path - driver.StorePath = path.join(HOME, '/.docker/machine') + driver.StorePath = cli.storagePath var updatedBlob = new Buffer(JSON.stringify(driver)).toString('base64') // update old config diff --git a/util.js b/util.js index f37df36..d476ba0 100644 --- a/util.js +++ b/util.js @@ -1,4 +1,6 @@ var fs = require('fs') +var path = require('path') +var os = require('os') var mkdirp = require('mkdirp').sync exports.copy = function (from, to) { @@ -36,5 +38,22 @@ exports.recurseJson = function (obj, func) { exports.permissions = function (path, chmod) { try { fs.chmodSync(path, chmod) - } catch (e) {} + } catch (e) { + } +} + +var DM_STORAGE_DIR = '/.docker/machine' + +exports.cli = function () { + var cli = {options: {}, params: []} + for (var i = 2; i < process.argv.length; i++) { + if (process.argv[i].substring(0, 2) === '--') { + cli.options[process.argv[i].substring(2)] = process.argv[i + 1] + i++ + } else { + cli.params.push(process.argv[i]) + } + } + cli.storagePath = cli.options['storage-path'] || process.env['MACHINE_STORAGE_PATH'] || path.join(os.homedir(), DM_STORAGE_DIR) + return cli }