Class: EC2Launcher::InitOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2launcher/init_options.rb

Constant Summary collapse

SUB_COMMANDS =
%w{help init launch terminate}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInitOptions

Returns a new instance of InitOptions.



181
182
183
184
185
186
187
# File 'lib/ec2launcher/init_options.rb', line 181

def initialize
  @global_options = global_options()
  @subcommands = {
    'launch' => launch_options(),
    'terminate' => terminate_options()
  }
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



12
13
14
# File 'lib/ec2launcher/init_options.rb', line 12

def command
  @command
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



15
16
17
# File 'lib/ec2launcher/init_options.rb', line 15

def hostname
  @hostname
end

#locationObject (readonly)

Returns the value of attribute location.



14
15
16
# File 'lib/ec2launcher/init_options.rb', line 14

def location
  @location
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/ec2launcher/init_options.rb', line 13

def options
  @options
end

Instance Method Details

#global_optionsObject



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
# File 'lib/ec2launcher/init_options.rb', line 19

def global_options
  OptionParser.new do |opts|
    opts.banner = <<EOH
SYNOPSIS
  ec2launcher [global options] command [command options] [command arguments]

COMMANDS
  help        - Get information about a command.
  init        - Initialize a new environment/application repository.
  launch      - Launch a new instance.
  terminate   - Terminates an instance.

EOH

    opts.separator "Global options:"

    opts.on("--access-key KEY", String, "Amazon access key. Overrides AWS_ACCESS_KEY environment variable.") do |access_key|
      @options.access_key = access_key
    end

    opts.on("--secret SECRET", String, "Amazon secret access key. Overrides AWS_SECRET_ACCESS_KEY environment variable.") do |secret|
      @options.secret = secret
    end

    opts.on("-d", "--directory DIRECTORY", String, "Location of configuration directory. Defaults to current directory.") do |directory|
      @options.directory = directory
    end

    opts.on_tail("-q", "--quiet", "Display as little information as possible.") do
      @options.verbosity = :quiet
    end

    opts.on_tail("-v", "--verbose", "Display as much information as possible.") do
      @options.verbosity = :verbose
    end

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail("-?", "--help", "Show this message") do
      puts opts
      exit
    end    
  end
end

#helpObject



299
300
301
# File 'lib/ec2launcher/init_options.rb', line 299

def help
  puts @opts
end

#launch_optionsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ec2launcher/init_options.rb', line 64

def launch_options
  OptionParser.new do |opts|
    opts.banner = ""
    opts.separator "Query options:"

    opts.on("-l", "--list", "Show environments and applications.") do
      @options.list = true
    end

    opts.on("-s", "--show-defaults", "Show settings, but do not launch any instances. Does not display user-data.") do
      @options.show_defaults = true
    end

    opts.on("--show-user-data", "Show user-data, but do not launch any instances.") do
      @options.show_user_data = true
    end

    opts.separator ""
    opts.separator "Required launch options:"

    opts.on("-e", "--environment ENV", "The environment for the server.") do |env|
      @options.environ = env
    end

    opts.on("-a", "--application NAME", "The name of the application class for the new server.") do |app_name|
      @options.application = app_name
    end

    opts.separator ""
    opts.separator "Additional launch options:"

    opts.on("--command [CMD]", String, "Additional command to run during launch sequence.") do |command|
      @options.commands << command
    end

    opts.on("--clone HOST", String, "Clone the latest snapshots from a specific host.") do |clone_host|
      @options.clone_host = clone_host
    end

    opts.on("-c", "--count COUNT", Integer, "Number of new instances to launch.") do |count|
      @options.count = count
    end

    opts.on("--skip-setup", "Skip running the setup scripts. Still runs pre/post commands.") do
      @options.skip_setup = true
    end

    opts.on("--pretty-print", "Use pretty format for JSON user-data. Defaults to off. Only recommended for testing.") do
      @options.pretty_print = true
    end

    opts.separator ""
    opts.separator "Launch overrides:"

    opts.on("-d", "--dynamic-name", "Dynamically set the host name based on instance id.") do |dynamic_name|
      @options.dynamic_name = dynamic_name
    end

    opts.on("-h", "--hostname NAME", String, "The name for the new server.") do |hostname|
      @options.hostname = hostname
    end

    opts.on("-u", "--chef-validation-url", String, "URL for the Chef validation pem file.") do |chef_validation_url|
      @options.chef_validation_url = chef_validation_url
    end

    opts.on("--ami AMI_ID", "AMI id") do |ami_id|
      @options.ami_id = ami_id
    end

    opts.on("-z", "--availability-zone ZONE", EC2Launcher::AVAILABILITY_ZONES, "AWS availability zone (#{EC2Launcher::AVAILABILITY_ZONES.join(', ')}).") do |zone|
      @options.zone = zone
    end

    opts.on("-i", "--instance-type TYPE", EC2Launcher::INSTANCE_TYPES, "EC2 instance type (#{EC2Launcher::INSTANCE_TYPES.join(', ')}).") do |instance_type|
      @options.instance_type = instance_type
    end

    opts.on("--volume-size SIZE", Integer, "EBS volume size in GB. Defaults to #{EC2Launcher::DEFAULT_VOLUME_SIZE} GB") do |volume_size|
      @options.volume_size = volume_size
    end

    opts.separator ""
    opts.separator "Miscellaneous:"

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail("-?", "--help", "Show this message") do
      puts opts
      exit
    end    
  end
end

#parse(args) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/ec2launcher/init_options.rb', line 189

def parse(args)
  @options = OpenStruct.new
  @options.list = false
  @options.show_defaults = false
  @options.show_user_data = false

  @options.environ = nil
  @options.application = nil
  @options.commands = []
  @options.clone_host = nil
  @options.count = 1
  @options.skip_setup = false

  @options.ami_id = nil
  @options.dynamic_name = false
  @options.hostname = nil
  @options.zone = nil
  @options.instance_type = nil
  @options.volume_size = nil

  @options.snapshot_removal = true
  @options.force = false

  @options.pretty_print = false
  @options.verbosity = :normal

  @options.directory = "./"

  # Parse global options
  begin
    @global_options.order!
  rescue OptionParser::InvalidOption
    puts "Missing command!"
    puts @global_options
    exit 1
  end

  if ARGV.size < 1
    puts @global_options
    exit 1
  end

  # Extract the request command
  @command = ARGV.shift.downcase

  unless SUB_COMMANDS.include?(@command)
    puts "Missing command!" if @command.nil?
    puts "Invalid command: #{@command}" unless @command.nil? || @command == "-?" || @command == "--help" || @command == "help"
    puts @global_options
    exit 1
  end

  if @command == "-?" || @command == "--help"
    puts @global_options
    exit 1
  end

  if @command == "help"
    if ARGV.size >= 1 && SUB_COMMANDS.include?(ARGV[0])
      puts @global_options
      puts @subcommands[ARGV[0]]
    else
      puts @global_options
    end
    exit 1
  end

  # Parse sub command options
  begin
    @subcommands[@command].order! if @subcommands.has_key?(@command)
  rescue OptionParser::InvalidOption
    puts "Invalid option!"
    puts @global_options
    exit 1
  end

  if @command == "init"
    unless args.length >= 1
      puts "Missing location!"
      puts
      help
      exit 1
    end
    @location = args[0]
  elsif @command =~ /^term/
    unless args.length >= 1
      puts "Missing name of server!"
      puts
      help
      exit 1
    end
    @hostname = args[0]
  else
    if (@options.environ.nil? || @options.application.nil?) && ! @options.list
      puts "Missing a required parameter: #{@options.environ.nil? ? '--environment' : '--application'}"
      puts
      help
      exit 1
    end

    if ! @options.hostname.nil? && @options.count > 1
      puts "Cannot specify both a hostname ['#{@options.hostname}'] and multiple instances [#{@options.count}]."
      puts
      exit 1
    end
  end

  @options
end

#terminate_optionsObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ec2launcher/init_options.rb', line 158

def terminate_options
  OptionParser.new do |opts|
    opts.banner = ""
    opts.separator "Termination options:"
    opts.on("--[no-]snapshot-removal", "Remove EBS snapshots. Defaults to TRUE.") do |removal|
      @options.snapshot_removal = removal
    end
    opts.on("-f", "--[no-]force", "Force termination even if missing environment. Defaults to FALSE.") do |force|
      @options.force = true
    end

    opts.separator ""
    opts.separator "Miscellaneous:"

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail("-?", "--help", "Show this message") do
      puts opts
      exit
    end    
  end
end