2
# Programmer: Chris Bunch
4
$VERBOSE = nil # to supress excessive SSL cert warnings
9
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
10
require 'common_functions'
13
USAGE = <<END_OF_USAGE
16
Usage: appscale-add-keypair [OPTIONS]
19
appscale-add-keypair --ips ips.yaml
22
--ips: The YAML file containing the IPs of the machines to use if not using a cloud infrastructure (for example, Xen).
25
ALL_FLAGS = ["help", "usage", "h", "ips", "keyname", "version"]
32
node_layout = NodeLayout.new(IPS, { :database => "cassandra" } )
34
["ssh-keygen", "ssh-copy-id"].each { |cmd|
35
abort("You do not have the '#{cmd}' command in your PATH. Please ensure that it is in your path and try again.") unless CommonFunctions.user_has_cmd?(cmd)
38
appscale_dir = File.expand_path("~/.appscale")
39
FileUtils.mkdir(appscale_dir) unless File.exists?(appscale_dir)
41
path = File.expand_path("~/.appscale/#{keyname}")
42
backup_key = File.expand_path("~/.appscale/#{keyname}.key")
43
pub_key = File.expand_path("~/.appscale/#{keyname}.pub")
45
#FileUtils.rm_f([path, backup_key, pub_key])
46
unless File.exists?(path) and File.exists?(pub_key)
47
FileUtils.rm_f([path, backup_key, pub_key])
48
puts CommonFunctions.shell("ssh-keygen -t rsa -N '' -f #{path}")
50
FileUtils.chmod(0600, [path, pub_key])
52
ips = node_layout.nodes.collect { |node| node.id }
54
puts CommonFunctions.shell("ssh-copy-id -i #{path} root@#{ip}")
55
# puts CommonFunctions.shell("scp -i #{path} #{path} root@#{ip}:.ssh/id_rsa")
56
# puts CommonFunctions.shell("scp -i #{path} #{path} root@#{ip}:.ssh/id_dsa")
57
# puts CommonFunctions.shell("scp -i #{path} #{pub_key} root@#{ip}:.ssh/id_rsa.pub")
60
head_ip = node_layout.head_node.id
61
puts CommonFunctions.shell("scp -i #{path} #{path} root@#{head_ip}:.ssh/id_rsa")
62
# this is needed for EC2 integration.
63
puts CommonFunctions.shell("scp -i #{path} #{path} root@#{head_ip}:.ssh/id_dsa")
64
puts CommonFunctions.shell("scp -i #{path} #{pub_key} root@#{head_ip}:.ssh/id_rsa.pub")
66
FileUtils.cp(path, backup_key)
67
puts "A new ssh key has been generated for you and placed at #{path}. You can now use this key to log into any of the machines you specified without providing a password via the following command:\n\tssh root@XXX.XXX.XXX.XXX -i #{path}"