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



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

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



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

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



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

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

#dispatch_command(command, arguments) ⇒ Object



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

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



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

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



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

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
# 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 " "
    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