Class: AMI::Commands::CreateAmi

Inherits:
Object
  • Object
show all
Defined in:
lib/ami/commands/create_ami.rb

Instance Method Summary collapse

Instance Method Details

#run(argv) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ami/commands/create_ami.rb', line 5

def run(argv)
  if argv.include?('--help') || argv.include?('-h')
    puts "#{$0} <options> ClassName"
    puts "Options:"
    puts "  --debug - Enable debug mode which stops auto-termination of servers"
    puts ""
    return 0
  end

  name = argv.pop
  debug = argv.include?('--debug')
  AMI::Core.load_amis

  unless AMI::Core.valid_ami?(name)
    warn "Invalid AMI name #{name}. Use ami-list to see available AMIs"
    return 1
  end

  ami = AMI::Core.ami(name).new

  # Ask for the AMIs customised server requirements
  server_template = AMI::Server.new
  ami.server(server_template)
  ami_name = ami.name

  config = AMI::ConfigLoader.load_config

  server = AMI::InstanceProvisioner.provision(
    ami_name,
    server_template,
    config
  )

  at_exit do
    if server.status == :running
      server.terminate
    end
  end

  begin
    ssh = AMI::SSH.create_connection(server, server_template.user_name)
    ami.configure(ssh, config)
    ami.create_ami(ssh, config)
    puts 'done'
    return 0
  rescue Exception => e
    warn "Error Occurred: #{e.message}"
    warn "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
    if debug
      warn "Access server via 'ssh #{server_template.user_name}@#{server.dns_name}'"
      sleep
    end
    return 1
  end
end