~nchohan/+junk/mytools

« back to all changes in this revision

Viewing changes to bin/appscale-add-keypair

  • Committer: root
  • Date: 2010-11-03 07:43:57 UTC
  • Revision ID: root@appscale-image0-20101103074357-xea7ja3sor3x93oc
init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/ruby -w
 
2
# Programmer: Chris Bunch
 
3
 
 
4
$VERBOSE = nil # to supress excessive SSL cert warnings
 
5
 
 
6
require 'fileutils'
 
7
require 'yaml'
 
8
 
 
9
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
 
10
require 'common_functions'
 
11
require 'node_layout'
 
12
 
 
13
USAGE = <<END_OF_USAGE
 
14
#{AS_VERSION}
 
15
 
 
16
Usage: appscale-add-keypair [OPTIONS]
 
17
 
 
18
Examples:
 
19
  appscale-add-keypair --ips ips.yaml
 
20
  
 
21
Flags:
 
22
  --ips: The YAML file containing the IPs of the machines to use if not using a cloud infrastructure (for example, Xen).
 
23
END_OF_USAGE
 
24
 
 
25
ALL_FLAGS = ["help", "usage", "h", "ips", "keyname", "version"]
 
26
 
 
27
require 'parse_args'
 
28
 
 
29
keyname = KEYNAME
 
30
ips_yaml = IPS
 
31
 
 
32
node_layout = NodeLayout.new(IPS, { :database => "cassandra" } )
 
33
 
 
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)
 
36
}
 
37
 
 
38
appscale_dir = File.expand_path("~/.appscale")
 
39
FileUtils.mkdir(appscale_dir) unless File.exists?(appscale_dir)
 
40
 
 
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")
 
44
 
 
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}")
 
49
end
 
50
FileUtils.chmod(0600, [path, pub_key])
 
51
 
 
52
ips = node_layout.nodes.collect { |node| node.id }
 
53
ips.each { |ip|
 
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")
 
58
}
 
59
 
 
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")
 
65
 
 
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}"