-
-
Save zamnuts/c65100a901464d216bcce046e296a3b7 to your computer and use it in GitHub Desktop.
Rename docker-machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
# usage: docker-machine-rename default my-default | |
function docker-machine-rename { | |
oldName="$1"; | |
newName="$2"; | |
[ -z "$oldName" -o -z "$newName" ] && echo "Must specify both the old and new machine names" && return 2; | |
storePathRoot=$(docker-machine inspect -f '{{ .Driver.StorePath }}' "$oldName"); | |
storePathMachines="$storePathRoot/machines"; | |
oldPath="$storePathMachines/$oldName"; | |
newPath="$storePathMachines/$newName"; | |
[ -d "$newPath" ] && echo "Machine name $newName already exists, abort." && return 1; | |
[ ! -d "$oldPath" ] && echo "Machine name $oldName does not exist, abort." && return 1; | |
timestamp=$(date +%s); | |
machineConfigPath="$newPath/config.json"; | |
vboxConfigPath="$newPath/$newName/$newName.vbox"; | |
# ensure vm is shutdown | |
echo 'shutting down the vm if needed...'; | |
vboxmanage controlvm "$oldName" acpipowerbutton 2>/dev/null; | |
# must do rename via vboxmanage, also verifies that the vm exists in vbox | |
echo 'renaming vm...'; | |
vboxmanage modifyvm "$oldName" --name "$newName" || return $?; | |
# need to move the mediums via vbox before moving files (and unregistering, of course!) | |
echo 'moving vm attached media (disk and b2d)...'; | |
mkdir -p "$newPath"; # prepare the directory that the mediums will be moved to | |
vboxmanage modifymedium disk "$oldPath/disk.vmdk" --move "$newPath/disk.vmdk" || return $?; # move persistent disk | |
vboxmanage modifymedium dvd "$oldPath/boot2docker.iso" --move "$newPath/boot2docker.iso" || return $?; # move b2d | |
# manually move the rest of the configs | |
echo 'moving the docker-machine configurations...'; | |
vboxmanage unregistervm "$newName" || return $?; # will re-register after manual ops | |
mv "$oldPath"/* "$newPath" || return $?; | |
rmdir "$oldPath"; | |
sed -i ".$timestamp.bak" "s/$oldName/$newName/g" "$machineConfigPath" || return $?; | |
vboxmanage registervm "$vboxConfigPath" || return $?; | |
echo "done renaming '$oldName' to '$newName'"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment