Class: Blender::Cli::Start

Inherits:
Blender::Cli show all
Defined in:
lib/blender/cli/start.rb

Constant Summary collapse

AMI_64 =
"ami-55739e3c"
AMI_32 =
"ami-bb709dd2"

Instance Method Summary collapse

Methods inherited from Blender::Cli

#initialize, #run

Constructor Details

This class inherits a constructor from Blender::Cli

Instance Method Details

#default_ami(options = {}) ⇒ Object



82
83
84
85
86
87
# File 'lib/blender/cli/start.rb', line 82

def default_ami(options = {})
  name = options[64] ? "~/.blender/ami64" : "~/.blender/ami"
  ami = File.read(File.expand_path(name)).strip rescue nil
  ami = options[64] ? AMI_64 : AMI_32 if ami.nil? || ami.empty?
  ami
end

#default_keyObject



89
90
91
92
93
94
# File 'lib/blender/cli/start.rb', line 89

def default_key
  keys = `ec2dkey`.strip.split("\n")
  raise("too many keys") if keys.length > 1
  raise("can't find any keys") if keys.length != 1
  keys.first.split("\t")[1].strip || raise("invalid key")
end

#executeObject



105
106
107
108
109
110
111
112
# File 'lib/blender/cli/start.rb', line 105

def execute
  options, extra = parse_options(@args)
  start_ami(options, extra)

rescue => e
  puts e.backtrace
  abort(e.to_s)
end

#parse_options(args) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/blender/cli/start.rb', line 8

def parse_options(args)
  options = {}

  args, extra_args = args.inject([[]]) do |result, element|
    if element == '--'
      result << []
    else
      result.last << element
    end
    result
  end

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: blender start [OPTIONS] [-- [ec2run options]]"
    opts.separator "Options:"

    opts.on("-a", "--ami AMI",
      "use specified AMI instead of the default one.",
      "If you don't specify your own AMI blender will choose a defaule one:",
      "* #{AMI_32} for 32 bits",
      "* #{AMI_64} for 64 bits",
      "You can change the defaults by writing your own AMIs",
      "into ~/.blender/ami and ~/.blender/ami64 files"
    ) do |val|
      options[:ami] = val
    end

    opts.separator ""

    opts.on("-k", "--key KEY",
      "use KEY when starting instance. KEY should already be generated.",
      "If you don't specify a KEY blender will try to use the key from your EC2 account",
      "Note: There must be only ONE key on the account for it to work."
    ) do |val|
      options[:key] = val
    end
    opts.separator ""

    opts.on("--64", "use 64 bit default AMI. This does nothing if you specify your own AMI") do
      options[64] = true
    end

    opts.on("-n", "--dry-run", "Don't do anything, just print the command line to be executed") do |val|
      @dry = options[:dry] = true
    end

    opts.separator "\nCommon options:"

    opts.on("-h", "--help", "Show this message") do
      raise(opts.to_s)
    end

    opts.on_tail <<-EXAMPLE

Example:

# start a 64bit instance with default options
blender start -64

# start with a custom ami
blender start --ami ami-2d4aa444

# start with passing arguments to ec2run: use security group default+test
blender start -- -g default -g test
     EXAMPLE

  end
  opts.parse!(args)

  raise("unexpected: #{args*" "}\n#{opts}") unless args.empty?

  return options, extra_args
end

#start_ami(options = {}, args = []) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/blender/cli/start.rb', line 96

def start_ami(options = {}, args = [])
  ami = options[:ami] || default_ami(options)
  key = options[:key] || default_key

  cmd = ["ec2run", ami, "-k", key, *args]

  run(*cmd) or raise "failed to start ami"
end