Module: AWSCarb::CliArgumentParser

Defined in:
lib/aws-carb/cli_argument_parser.rb

Class Method Summary collapse

Class Method Details

.parseObject



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
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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/aws-carb/cli_argument_parser.rb', line 7

def self.parse
  cli_arguments                               = OpenStruct.new
  cli_arguments.global                        = OpenStruct.new
  cli_arguments.subcommand                    = OpenStruct.new
  cli_arguments.subcommand.user_data_template = OpenStruct.new
  cli_arguments.subcommand.config_overrides   = OpenStruct.new
  cli_arguments.subcommand.ec2                = OpenStruct.new
  cli_arguments.subcommand.route53            = OpenStruct.new

  indent = ' ' * 10 

  banner = <<-HEREDOC.strip_heredoc
  #{File.basename($0)}(1)

  #{'NAME'.colorize({:color => :white, :mode => :bold})}

                          carb

  #{'DESCRIPTION'.colorize({:color => :white, :mode => :bold})}

        amazon web services - cloudinit and route53 bootstrap

  HEREDOC


  # these are the only defaults we need to bother setting since they get used before we load the config file..
  cli_arguments.global.verbose = false
  cli_arguments.global.config_file = File.join(Dir.home, ".carb/config", "config.yaml")

  global_options do |option|

    option.summary_width  = 50
    option.summary_indent = '    '

    synopsis = <<-HEREDOC.strip_heredoc
    #{"SYNOPSIS".colorize({:color => :white, :mode => :bold})}
    
        #{File.basename($0)} [global options] [subcommand [options]]

    HEREDOC

    option.banner = banner + synopsis
    option.separator "#{'GLOBAL OPTIONS'.colorize({:color => :white, :mode => :bold})}"
    option.separator ""

    option.on("-c", "--config=FILE", "") do |file|
      cli_arguments.global.config_file = file
    end

    option.separator ""

    option.on("-v", "--verbose", "") do |boolean|
      cli_arguments.global.verbose = boolean

      # FIXME: use a stupidly named global(!!!!!) variable to avoid clashing with rubys $VERBOSE
      $GLOBAL_VERBOSE = boolean
    end

    option.separator ""
    option.separator "    -h, --help"
  end

  command :create do |option|

    option.summary_width  = 50
    option.summary_indent = '  '

    synopsis = <<-HEREDOC.strip_heredoc
    #{'SYNOPSIS'.colorize({:color => :white, :mode => :bold})}

        #{File.basename($0)} create [options]

    HEREDOC

    option.banner      = banner + synopsis
    option.description = "create an ec2 instance"

    option.separator ""
    option.separator "    user_data tempate options:"
    option.separator ""

    option.on "--user-data-template=FILE", "\n\n#{indent}user data template" do |user_data_template|
      cli_arguments.subcommand.user_data_template.file = user_data_template
    end

    option.separator ""

    option.on "--show-parsed-template=BOOLEAN", "\n\n#{indent}display parsed template file" do |show_parsed_template|
      cli_arguments.subcommand.user_data_template.show_parsed_template = show_parsed_template
    end

    option.separator ""

    option.separator ""
    option.separator "    config file overrides:"
    option.separator ""

    option.on "--common-variables=HASH", String, "\n\n#{indent}common variables" do |common_variables|
      begin
        data = eval(common_variables)
        raise unless data.class == Hash
        cli_arguments.subcommand.config_overrides.common_variables = ActiveSupport::HashWithIndifferentAccess.new(data)
      rescue => e
        puts "# could not parse argument for --common-variables, is it a valid hash?"
        die e
      end
    end

    option.separator ""

    option.on "--general-variables=HASH", String, "\n\n#{indent}general variables" do |general_variables|
      begin
        data = eval(general_variables)
        raise unless data.class == Hash
        cli_arguments.subcommand.config_overrides.general_variables = ActiveSupport::HashWithIndifferentAccess.new(data)
      rescue => e
        puts "# could not parse argument for --general-variables, is it a valid hash?"
        die e
      end
    end

    option.separator ""

    option.on "--ec2-variables=HASH", String, "\n\n#{indent}ec2 variables" do |ec2_variables|
      begin
        data = eval(ec2_variables)
        raise unless data.class == Hash
        cli_arguments.subcommand.config_overrides.ec2_variables = ActiveSupport::HashWithIndifferentAccess.new(data)
      rescue => e
        puts "# could not parse argument for --ec2-variables, is it a valid hash?"
        die e
      end
    end

    option.separator ""

    option.on "--route53-variables=HASH", String, "\n\n#{indent}route53 variables" do |route53_variables|
      begin
        data = eval(route53_variables)
        raise unless data.class == Hash
        cli_arguments.subcommand.config_overrides.route53_variables = ActiveSupport::HashWithIndifferentAccess.new(data)
      rescue => e
        puts "# could not parse argument for --route53-variables, is it a valid hash?"
        die e
      end
    end

    option.separator ""

    option.on "--user-data-template-variables=HASH", String, "\n\n#{indent}user data template variables" do |user_data_template_variables|
      begin
        data = eval(user_data_template_variables)
        raise unless data.class == Hash
        cli_arguments.subcommand.config_overrides.user_data_template_variables = ActiveSupport::HashWithIndifferentAccess.new(data)
      rescue => e
        puts "# could not parse argument for --user-data-template-variables, is it a valid hash?"
        die e
      end
    end

    option.separator ""
    option.separator "          long descriptions for these parameters can be found here:\n            http://<TODO>"
    option.separator ""

    option.separator ""
    option.separator "    ec2 config convenience arguments:"
    option.separator ""

    option.on "--ec2-access-key-id=STRING", "\n\n#{indent}access key id".downcase do |access_key_id|
      cli_arguments.subcommand.ec2.access_key_id = access_key_id
    end

    option.separator ""

    option.on "--ec2-secret-access-key=STRING", "\n\n#{indent}secret access key".downcase do |secret_access_key|
      cli_arguments.subcommand.ec2.secret_access_key = secret_access_key
    end

    option.separator ""

    option.on "--image-id=STRING", "\n\n#{indent}ID of the AMI you want to launch.".downcase do |image_id|
      cli_arguments.subcommand.ec2.image_id = image_id
    end

    option.separator ""

    option.on "--instance-type=STRING", "\n\n#{indent}The type of instance to launch, for example \"m1.small\".".downcase do |instance_type|
      cli_arguments.subcommand.ec2.instance_type = instance_type
    end

    option.separator ""

    option.on "--key-name=STRING", "\n\n#{indent}The name of the key pair to use.".downcase do |key_name|
      cli_arguments.subcommand.ec2.key_name = key_name
    end

    option.separator ""

    block_device_help = <<-HEREDOC.strip_heredoc

       virtual_name - (String) Specifies the virtual device name.
       device_name - (String) Specifies the device name (e.g., /dev/sdh).
       ebs - (Hash) Specifies parameters used to automatically setup Amazon\n#{indent}             EBS volumes when the instance is launched.
         snapshot_id - (String) The ID of the snapshot from which the volume will be created.
         volume_size - (Integer) The size of the volume, in gigabytes.
         delete_on_termination - (Boolean) Specifies whether the Amazon EBS volume is\n#{indent}                          deleted on instance termination.
         volume_type - (String) Valid values include:
           standard
           io1
       iops - (Integer)
       no_device - (String) Specifies the device name to suppress during instance launch.
    HEREDOC

    block_device_help = block_device_help.lines.map { |line| indent + "  #{line}" }

    block_device_help = "\n\n#{indent}Specifies how block devices are exposed to the instance. Each mapping\n#{indent}is made up of a virtualName and a deviceName.\n" + block_device_help.join.downcase

    option.on "--block-device-mappings=ARRAY", block_device_help do |mappings_data|
      begin

        mappings = eval(mappings_data)

        raise "parsed value isn't an Array!" unless mappings.class == Array

        mappings.map! do |mapping|
          raise "mapping data isn't a hash!" unless mapping.class == Hash
          ActiveSupport::HashWithIndifferentAccess.new(mapping)
        end

        cli_arguments.subcommand.ec2.block_device_mappings = mappings
      rescue => e
        puts "# could not parse argument for --block-device-mappings, are your mappings"
        puts "# valid hashes contained in an array and are the hash values properly quoted?"
        die e
      end
    end

    option.separator ""

    option.on "--user-data=STRING", "\n\n#{indent}Arbitrary user data. note: this is merged with user_data_template if also specified.".downcase do |user_data|
      cli_arguments.subcommand.ec2.user_data = user_data
    end

    option.separator ""

    option.on "--iam-instance-profile=STRING", "\n\n#{indent}the name or ARN of an IAM instance profile.".downcase do |profile|
      cli_arguments.subcommand.ec2.iam_instance_profile = profile
    end

    option.separator ""

    option.on "--monitoring-enabled=BOOLEAN", "\n\n#{indent}enable CloudWatch monitoring.".downcase do |boolean|
      cli_arguments.subcommand.ec2.monitoring_enabled = boolean
    end

    option.separator ""

    option.on "--availability-zone=STRING", "\n\n#{indent}availability zone.".downcase do |zone|
      cli_arguments.subcommand.ec2.availability_zone = zone
    end

    option.separator ""

    option.on "--security-groups=ARRAY", Array, "\n\n#{indent}Security groups. can be a single value or an array of values.\n#{indent}Values should be space deliminated group name strings.".downcase do |groups|
      cli_arguments.subcommand.ec2.security_groups = groups
    end

    option.separator ""

    option.on "--security-group-ids=<ID>,<ID>,..", Array, "\n\n#{indent}security_group_ids accepts a single ID or an array of\n#{indent}security group IDs.".downcase do |group_ids|
      cli_arguments.subcommand.ec2.security_group_ids = group_ids
    end

    option.separator ""

    option.on "--disable-api-termination=BOOLEAN", "\n\n#{indent}instance termination via the instance API.".downcase do |api_termination_data|

      api_termination = case api_termination_data
      when "true"
        true
      when "false"
        false
      else raise ArgumentError, "unknown parameter for --disable-api-termination: #{api_termination_data}"
      end

      cli_arguments.subcommand.ec2.disable_api_termination = api_termination
    end

    option.separator ""

    option.on "--instance-initiated-shutdown-behavior=STRING", "\n\n#{indent}instance termination on instance-initiated shutdown".downcase do |shutdown_behavior|
      cli_arguments.subcommand.ec2.instance_initiated_shutdown_behavior = shutdown_behavior
    end

    option.separator ""

    option.on "--subnet=STRING", "\n\n#{indent}The VPC Subnet (or subnet id string) to launch the instance in.".downcase do |subnet|
      cli_arguments.subcommand.ec2.subnet = subnet
    end

    option.separator ""

    option.on "--private-ip-address=STRING", "\n\n#{indent}If you're using VPC, you can optionally use this option to assign the\n#{indent}instance a specific available IP address from the subnet (e.g., '10.0.0.25').\n#{indent}This option is not valid for instances launched outside a VPC (i.e.\n#{indent}those launched without the :subnet option).".downcase do |ip|
      cli_arguments.subcommand.ec2.private_ip_address = ip
    end

    option.separator ""

    option.on "--associate-public-ip-address=STRING", "\n\n#{indent}This option isn't documented but appears to exist in the code. For use when creating instances in a VPC.".downcase do |ip|
      cli_arguments.subcommand.ec2.associate_public_ip_address = ip
    end

    option.separator ""

    option.on "--dedicated-tenancy=BOOLEAN", "\n\n#{indent}Instances with dedicated tenancy will not share physical hardware with\n#{indent}instances outside their VPC. NOTE: Dedicated tenancy incurs an \n#{indent}additional service charge. This option is not valid for\n#{indent}instances launched outside a VPC (e.g.those launched without the :subnet option).".downcase do |tenancy|
      cli_arguments.subcommand.ec2.dedicated_tenancy = tenancy
    end

    option.separator ""

    option.on "--ebs-optimized=BOOLEAN", "\n\n#{indent}EBS-Optimized instances enable Amazon EC2 instances to fully utilize the\n#{indent}IOPS provisioned on an EBS volume. EBS-optimized instances deliver dedicated\n#{indent}throughput between Amazon EC2 and Amazon EBS, with options between\n#{indent}500 Mbps and 1000 Mbps depending on the instance type used. When attached\n#{indent}to EBS-Optimized instances, Provisioned IOPS volumes are designed to\n#{indent}deliver within 10% of their provisioned performance 99.9% of the time.\n#{indent}NOTE: EBS Optimized instances incur an additional service charge.\n#{indent}This option is only valid for certain instance types.".downcase do |ebs_optimized|
      cli_arguments.subcommand.ec2.ebs_optimized = ebs_optimized
    end

    option.separator ""
    option.separator "        long descriptions for these parameters can be found here:\n          http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/InstanceCollection.html"
    option.separator ""

    option.separator ""
    option.separator "    route53 convenience arguments:"
    option.separator ""

    option.on "--route53-access-key-id=STRING", "\n\n#{indent}access key id".downcase do |access_key_id|
      cli_arguments.subcommand.route53.access_key_id = access_key_id
    end

    option.separator ""

    option.on "--route53-secret-access-key=STRING", "\n\n#{indent}secret access key".downcase do |secret_access_key|
      cli_arguments.subcommand.route53.secret_access_key = secret_access_key
    end

    option.separator ""


    option.on "--zone=STRING", "\n\n#{indent}route53 zone".downcase do |zone|
      cli_arguments.subcommand.route53.route = route
    end

    option.separator ""

    option.on "--ttl=STRING", "\n\n#{indent}ttl".downcase do |ttl|
      cli_arguments.subcommand.route53.ttl = ttl
    end

    option.separator ""

  end

  begin
    #cli_arguments.chosen_subcommand = opt_parse
    subcommand = opt_parse

    # show help if no arguments passed in
    if subcommand.nil?
      add_subcommand_help
      puts @global

      exit 1
    end

    if ARGV.length > 0
      raise ArgumentError, "unknown command line argument(s): #{ARGV.inspect.to_s}"
    end
  rescue => e
    puts e
    exit 1
  end

  return cli_arguments
end