Skip to content

Instantly share code, notes, and snippets.

@mapio
Created December 14, 2015 10:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapio/0069e9980aa3656e993a to your computer and use it in GitHub Desktop.
Save mapio/0069e9980aa3656e993a to your computer and use it in GitHub Desktop.
from json import loads, dumps
from base64 import b64decode, b64encode
def relocate( conf_data, old_path, new_path ):
def rdt( dct ):
for k, v in dct.items():
if isinstance( v, dict ):
rdt( v )
else:
if 'Path' in k:
dct[ k ] = v.replace( old_path, new_path )
conf = loads( conf_data )
raw_driver = loads( b64decode( conf[ 'RawDriver' ] ) )
rdt( conf )
rdt( raw_driver )
conf[ 'RawDriver' ] = b64encode( dumps( raw_driver ) )
return dumps( conf, sort_keys = True, indent = 4, separators = ( ',', ': ' ) )
if __name__ == '__main__':
from sys import argv
_, old_conf, new_conf, old_path, new_path = argv
with open( old_conf, 'r' ) as inf:
conf_data = inf.read()
relocated = relocate( conf_data, old_path, new_path )
with open( new_conf, 'w' ) as outf:
outf.write( relocated )
@mapio
Copy link
Author

mapio commented Dec 14, 2015

This hack can come in handy if you have relocated your docker machine configurations. Assuming you want to change /home/foo in /home/bar for the machine named baz you can do something like

cd .docker/machine/machines/baz
mv config.json config.json-backup
python relocate.py config.json-backup config.json /home/foo /home/bar

The scripts performs a (recursive) replacement in every dictionary key that contains Path, and in RawDriver base64 encoded dictionary.

@nathanleclaire
Copy link

FYI, in latest Machine versions we've removed the use of RawDriver, so editing the JSON directly should work. Let us know if you encounter any issues, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment