Class: Rudy::CLI::AWS::EC2::Images

Inherits:
CommandBase
  • Object
show all
Defined in:
lib/rudy/cli/aws/ec2/images.rb

Instance Attribute Summary

Attributes inherited from CommandBase

#config

Instance Method Summary collapse

Methods included from Huxtable

change_environment, change_position, change_region, change_role, change_zone, #check_keys, #config_dirname, create_domain, #current_group_name, #current_machine_address, #current_machine_count, #current_machine_group, #current_machine_hostname, #current_machine_image, #current_machine_name, #current_machine_size, #current_user, #current_user_keypairpath, debug?, #debug?, domain, domain_exists?, #group_metadata, #has_keypair?, #has_keys?, #has_pem_keys?, #has_root_keypair?, keypair_path_to_name, #known_machine_group?, #root_keypairname, #root_keypairpath, #switch_user, update_config, update_global, update_logger, #user_keypairname, #user_keypairpath

Instance Method Details

#create_imagesObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rudy/cli/aws/ec2/images.rb', line 102

def create_images
  opts = {}
  opts[:id] = @option.instid if @option.instid
  
  @@global.user = 'root'
  
  puts "You may want to run rudy-ec2 #{@alias} --prepare first".bright
  puts "NOTE 1: This process is currently Linux-only"
  puts "NOTE 2: If you plan to create a public machine image, there are "
  puts "additional steps to take to remove any sensitive information"
  puts "before creating the image. See:"
  puts "http://docs.amazonwebservices.com/AWSEC2/latest/DeveloperGuide/AESDG-chapter-sharingamis.html"
  exit unless Annoy.pose_question("  Continue?\a ", /yes|y|ya|sure|you bet!/i, STDERR)

  # Options to be sent to Net::SSH
  ssh_opts = { :user => @@global.user || Rudy.sysinfo.user, :debug => nil  }
  if @@global.pkey 
    raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
    raise InsecureKeyPermissions, @@global.pkey unless File.stat(@@global.pkey).mode == 33152
    ssh_opts[:keys] = @@global.pkey 
  end
  
  lt = @rinst.list_group(nil, :running, opts[:id]) do |inst|
    
    puts inst.to_s
    
    # Open the connection and run the command
    rbox = Rye::Box.new(inst.dns_public, ssh_opts)
    def rbox.bundle_vol(*args); cmd('ec2-bundle-vol', args); end
    def rbox.upload_vol(*args); cmd('ec2-upload-bundle', args); end

    rbox.upload(@@global.cert, @@global.privatekey, "/mnt")
    rbox.touch("/root/firstrun")
    
    ## TODO: 
    ## We have to delete the host keys just before we run the bundle command. 
    ## The problem is that if we lose the connection we won't be able to connect
    ## to the instance again. A better solution is to add the keys to the ignore
    ## list for the bundle command. 
    ##ret = rbox.rm('/etc/ssh/ssh_host_*_key*')

    puts "Starting bundling process..."
    
    pkeyfile = File.basename(@@global.privatekey)
    certfile = File.basename(@@global.cert)
    
    rbox.bundle_vol(:r, "i386", :p, @option.name, :k, "/mnt/#{pkeyfile}", :c, "/mnt/#{certfile}", :u, @@global.accountnum)
    rbox.upload_vol(:b, @option.bucket, :m, "/tmp/#{@option.name}.manifest.xml", :a, @@global.accesskey, :s, @@global.secretkey)

    @rimages.register("#{@option.bucket}/#{@option.name}.manifest.xml")
    
    break
  end
  
end

#create_images_valid?Boolean

def prepare_images_valid?

true

end def prepare_images

opts = {}
opts[:id] = @option.instid if @option.instid

puts "This will do the following:"
puts "- Clear bash history"
# NOTE: We can't delete the host keys here. Otherwise we can't create the image. 
#puts "- Delete host SSH keys (this is permanent!)"
puts "" 

## TODO:       
## ~/.rudy, /etc/motd, history -c, /etc/hosts, /var/log/rudy*

execute_check(:medium)

# Options to be sent to Net::SSH
ssh_opts = { :user => @global.user || Rudy.sysinfo.user, :debug => STDERR  }
if @@global.pkey 
  raise "Cannot find file #{@@global.pkey}" unless File.exists?(@@global.pkey)
  raise InsecureKeyPermissions, @@global.pkey unless File.stat(@@global.pkey).mode == 33152
  ssh_opts[:keys] = @@global.pkey 
end

rudy = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
lt = rudy.list_group(nil, :running, opts[:id]) do |inst|

  puts "Preparing #{inst.dns_public}..."

  # Open the connection and run the command
  rbox = Rye::Box.new(inst.dns_public, ssh_opts)

  # We need to explicitly add the rm command for rbox so we
  # can delete the SSH host keys. This is will force the instance
  # to re-create it's SSH keys on first boot.
  def rbox.rm(*args); cmd('rm', args); end
  p ret = rbox.history(:c)
  p ret.exit_code
  p ret.stderr
  p ret.stdout

end

puts "done"

end

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rudy/cli/aws/ec2/images.rb', line 86

def create_images_valid?
  raise "No account number" unless @@global.accountnum
  raise "No Amazon cert-***.pem" unless @@global.cert
  raise "No Amazon pk-***.pem" unless @@global.privatekey
  raise "You must supply a root keypair path" unless @@global.pkey
  
  @rinst = Rudy::AWS::EC2::Instances.new(@@global.accesskey, @@global.secretkey, @@global.region)
  @rimages = Rudy::AWS::EC2::Images.new(@@global.accesskey, @@global.secretkey, @@global.region)
  @s3 = Rudy::AWS::S3.new(@@global.accesskey, @@global.secretkey, @@global.region)
  raise "No instances" unless @rinst.any?
  raise "You must supply an S3 bucket name. See: 'rudy-s3 buckets'" unless @option.bucket
  raise "You must supply an image name" unless @option.name
  raise "The bucket '#{@option.bucket}' does not exist" unless @s3.bucket_exists?(@option.bucket)
  true
end

#destroy_imagesObject



164
165
166
# File 'lib/rudy/cli/aws/ec2/images.rb', line 164

def destroy_images
 puts @rimages.deregister(@argv.ami) ? "Done" : "Unknown error"
end

#destroy_images_valid?Boolean

Returns:

  • (Boolean)


158
159
160
161
162
163
# File 'lib/rudy/cli/aws/ec2/images.rb', line 158

def destroy_images_valid?
  unless @argv.ami && Rudy::Utils.is_id?(:image, @argv.ami)  
    raise "Must supply an AMI ID (ami-XXXXXXX)" 
  end
  @rimages = Rudy::AWS::EC2::Images.new(@@global.accesskey, @@global.secretkey, @@global.region)
end

#imagesObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rudy/cli/aws/ec2/images.rb', line 21

def images
  
  rimages = Rudy::AWS::EC2::Images.new(@@global.accesskey, @@global.secretkey, @@global.region)
  unless @option.all
    @option.owner ||= 'amazon' 
    puts "Images owned by #{@option.owner.bright}" unless @argv.awsid
  end
  
  images = rimages.list(@option.owner, @argv) || []
  images.each do |img|
    puts @@global.verbose > 0 ? img.inspect : img.dump(@@global.format)
  end
  puts "No images" if images.empty?
end

#images_valid?Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
# File 'lib/rudy/cli/aws/ec2/images.rb', line 13

def images_valid?
  if @option.owner == 'self'
    raise "AWS_ACCOUNT_NUMBER not set" unless @@global.accountnum 
    @option.owner = @@global.accountnum 
  end
  
  true  
end

#register_imagesObject



174
175
176
# File 'lib/rudy/cli/aws/ec2/images.rb', line 174

def register_images
  puts @rimages.register(@argv.first)
end

#register_images_valid?Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
# File 'lib/rudy/cli/aws/ec2/images.rb', line 168

def register_images_valid?
  unless @argv.first
    raise "Must supply a valid manifest path (bucket/ami-name.manifest.xml)"
  end
  @rimages = Rudy::AWS::EC2::Images.new(@@global.accesskey, @@global.secretkey, @@global.region)
end