Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom storage path #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions export.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#! /usr/bin/env node
console.log('exporting.')

var fs = require('fs')
var path = require('path')
Expand All @@ -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 <machine-name>')
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'))

Expand All @@ -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
}
Expand Down
40 changes: 20 additions & 20 deletions import.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#! /usr/bin/env node
console.log('importing.')

var fs = require('fs')
var path = require('path')
Expand All @@ -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 <config-zip>')
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)
Expand All @@ -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)
Expand All @@ -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))
}
}
})

Expand All @@ -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
Expand Down
21 changes: 20 additions & 1 deletion util.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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
}