Module: Stem::CLI

Extended by:
CLI
Included in:
CLI
Defined in:
lib/stem/cli.rb

Instance Method Summary collapse

Instance Method Details

#create(name = nil, instance = nil, tag_list = nil) ⇒ Object



80
81
82
83
84
85
# File 'lib/stem/cli.rb', line 80

def create name = nil, instance = nil, tag_list = nil
  abort "Usage: create ami-name instance-to-capture ami_tag1,ami_tag2" unless name && instance
  tags = tag_list ? tag_list.split(',') : []
  image_id = Stem::Image::create(name, instance, tags)
  puts "New image ID: #{image_id}"
end

#describe(what) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/stem/cli.rb', line 87

def describe what
  require 'pp'
  if (what[0..2] == "ami")
    pp Stem::Image::describe(what)
  elsif
    pp Stem::Instance::describe(what)
  end
end

#destroy(instance = nil) ⇒ Object



96
97
98
99
# File 'lib/stem/cli.rb', line 96

def destroy instance = nil
  abort "Usage: destroy instance-id" unless instance
  Stem::Instance::destroy(instance)
end

#dispatch_command(command, arguments) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/stem/cli.rb', line 44

def dispatch_command command, arguments
  case command
    when "launch"
      launch(*arguments)
    when "create"
      create(*arguments)
    when "list"
      list(*arguments)
    when "describe"
      describe(*arguments)
    when "destroy"
      destroy(*arguments)
    when nil
      puts "Please provide a command."
    else
      puts "Command \"#{command}\" not recognized."
  end
end

#launch(config_file = nil, userdata_file = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/stem/cli.rb', line 63

def launch config_file = nil, userdata_file = nil
  abort "No config file" unless config_file
  userdata = case
               when userdata_file.nil?
                 nil
               when File.directory?(userdata_file)
                 Userdata.compile(userdata_file)
               when File.file?(userdata_file)
                 File.new(userdata_file).read
               else
                 abort 'Unable to interpret userdata object.'
             end
  conf = JSON.parse(File.new(config_file).read)
  instance = Stem::Instance.launch(conf, userdata)
  puts "New instance ID: #{instance}"
end

#list(*arguments) ⇒ Object



101
102
103
# File 'lib/stem/cli.rb', line 101

def list *arguments
  Stem::Instance::list(*arguments)
end

#parse_options(args) ⇒ Object

Return a structure describing the options.



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
# File 'lib/stem/cli.rb', line 12

def parse_options(args)
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: stem COMMAND ..."

    opts.separator " "

    opts.separator "Examples:"
    opts.separator "  $ stem launch prototype.config prototype-userdata.sh"
    opts.separator "  $ stem launch examples/lxc-server/lxc-server.json examples/lxc-server/"
    opts.separator "  $ stem list"
    opts.separator "  $ stem create ami-name instance-id ami_tag1,ami_tag2"
    opts.separator "  $ stem destroy ami-name"

    opts.separator " "
    opts.separator "Options:"

    opts.on("-v", "--version", "Print the version") do |v|
      puts "Stem v#{Stem::Version}"
      exit
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end

  opts.separator ""

  opts.parse!(args)
end