Class: VagrantPlugins::AMI::Command

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

Instance Method Summary collapse

Instance Method Details

#executeObject



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
# File 'lib/vagrant-ami/commands/create_ami.rb', line 6

def execute

  options = {
      :tags => {}
  }

  opts = OptionParser.new do |o|
    o.banner = "Usage: vagrant create_ami [vm-name]"
    o.separator ""

    o.on("-n", "--name NAME", "AMI Name") do |n|
      options[:name] = n
    end

    o.on("-d", "--desc DESCRIPTION", "AMI Description.") do |d|
      options[:desc] = d
    end

    o.on("-t", "--tags key1=val1,keyN=valN", Array, "Optional tags to assign to the AMI.") do |t|
      options[:tags] = Hash[t.map {|kv| kv.split("=")}]
    end

    # TODO add option to wait until AMI creation is complete?
    # TODO does this work if the instance is terminated immediately after hte create_ami call
    # is issues?

  end

  # Parse the options
  argv = parse_options(opts)
  return if !argv
  
  if options[:name].nil? or options[:desc].nil?
    raise Vagrant::Errors::CLIInvalidOptions, :help => opts.help.chomp
  end

  with_target_vms(argv, :reverse => true) do |machine|
    @env.action_runner.run(VagrantPlugins::AMI::Action.action_create_ami, {
      :machine    => machine,
      :name       => options[:name],
      :desc       => options[:desc],
      :tags       => options[:tags]
    })
  end
  0
end