Skip to content

Instantly share code, notes, and snippets.

@wusuopu
Created May 10, 2019 07:36
Show Gist options
  • Save wusuopu/d5349d9670eaa8f186643504df10233f to your computer and use it in GitHub Desktop.
Save wusuopu/d5349d9670eaa8f186643504df10233f to your computer and use it in GitHub Desktop.
docker machine manage
#!/usr/bin/env ruby
#-*- coding:utf-8 -*-
require 'json'
require 'fileutils'
def dump_config config, dest
driver = config['Driver']
ssh_dir = "#{dest}/.sshkey"
if !File.exists?(ssh_dir)
FileUtils.mkdir_p ssh_dir
end
# 复制 ssh key
if File.dirname(driver['SSHKeyPath']) != ssh_dir
FileUtils.cp driver['SSHKeyPath'], ssh_dir
FileUtils.cp "#{driver['SSHKeyPath']}.pub", ssh_dir
end
new_sshkey_path = "#{ssh_dir}/#{File.basename(driver['SSHKeyPath'])}"
driver['SSHKeyPath'] = new_sshkey_path
driver['SSHKey'] = driver['SSHKeyPath']
# 认证配置
options = config['HostOptions']['AuthOptions']
cert_dir = "#{dest}/certs"
if !File.exists?(cert_dir)
FileUtils.mkdir_p cert_dir
end
options['CertDir'] = cert_dir
if File.dirname(options['CaCertPath']) != cert_dir
FileUtils.cp options['CaCertPath'], cert_dir
end
if File.dirname(options['CaPrivateKeyPath']) != cert_dir
FileUtils.cp options['CaPrivateKeyPath'], cert_dir
end
options['CaCertPath'] = "#{cert_dir}/#{File.basename(options['CaCertPath'])}"
options['CaPrivateKeyPath'] = "#{cert_dir}/#{File.basename(options['CaPrivateKeyPath'])}"
if File.dirname(options['ServerCertPath']) != cert_dir
FileUtils.cp options['ServerCertPath'], cert_dir
end
if File.dirname(options['ServerKeyPath']) != cert_dir
FileUtils.cp options['ServerKeyPath'], cert_dir
end
options['ServerCertPath'] = "#{cert_dir}/#{File.basename(options['ServerCertPath'])}"
options['ServerKeyPath'] = "#{cert_dir}/#{File.basename(options['ServerKeyPath'])}"
if File.dirname(options['ClientKeyPath']) != cert_dir
FileUtils.cp options['ClientKeyPath'], cert_dir
end
if File.dirname(options['ClientCertPath']) != cert_dir
FileUtils.cp options['ClientCertPath'], cert_dir
end
options['ClientKeyPath'] = "#{cert_dir}/#{File.basename(options['ClientKeyPath'])}"
options['ClientCertPath'] = "#{cert_dir}/#{File.basename(options['ClientCertPath'])}"
config
end
if caller.length == 0
if ARGV.length < 1
puts "Usage: dm-dump <name>"
exit 1
end
name = "#{ENV['HOME']}/.docker/machine/machines/#{ARGV[0]}"
if !File.exists?(name) || !File.exists?("#{name}/config.json")
puts "directory '#{name}' is not exist!"
exit 1
end
config = JSON.load File.read "#{name}/config.json"
dump_config config, name
File.write "#{name}/config.json", JSON.pretty_generate(config)
Dir.chdir "#{ENV['HOME']}/.docker/machine/machines/"
`tar czf #{ARGV[0]}.tar.gz #{ARGV[0]}`
end
dm-list () {
for i in `ls ~/.docker/machine/machines/`;
do
if [[ -d ~/.docker/machine/machines/$i ]]; then
echo "${i}\t$(cat ~/.docker/machine/machines/${i}/config.json | grep IPAddress)";
fi
done;
}
dm-env () {
eval "$(docker-machine env $(dm-list|awk '{print $1}'|sed -n $(expr $1)p))"
}
dm-ls () {
docker-machine ls
}
dm-ssh () {
docker-machine ssh $(dm-list|awk '{print $1}'|sed -n $(expr $1)p)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment