ec2launcher 1.4.1 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 1.4.2
2
+
3
+ * Remove requirement to provide environment name when terminating an instance.
4
+
1
5
  ## 1.4.1
2
6
 
3
7
  * Fixed typo.
data/bin/ec2launcher CHANGED
@@ -27,7 +27,7 @@ if opt_parser.command == "init"
27
27
 
28
28
  puts "Successfully created #{opt_parser.location}"
29
29
  elsif opt_parser.command =~ /^term/
30
- terminator = EC2Launcher::Terminator.new(opt_parser.options.directory, opt_parser.options.environ)
30
+ terminator = EC2Launcher::Terminator.new(opt_parser.options.directory)
31
31
  terminator.terminate(opt_parser.hostname, opt_parser.options.access_key, opt_parser.options.secret_key, opt_parser.options.snapshot_removal)
32
32
  elsif opt_parser.command == "launch"
33
33
  launcher = EC2Launcher::Launcher.new
@@ -14,21 +14,55 @@ module EC2Launcher
14
14
  attr_reader :location
15
15
  attr_reader :hostname
16
16
 
17
- SUB_COMMANDS = %w{init launch terminate term}
17
+ SUB_COMMANDS = %w{help init launch terminate}
18
18
 
19
- def initialize
20
- @opts = OptionParser.new do |opts|
21
- opts.banner = "Usage: ec2launcher [COMMAND]
19
+ def global_options
20
+ OptionParser.new do |opts|
21
+ opts.banner = <<EOH
22
+ SYNOPSIS
23
+ ec2launcher [global options] command [command options] [command arguments]
24
+
25
+ COMMANDS
26
+ init - Initialize a new environment/application repository.
27
+ launch - Launch a new instance.
28
+ terminate - Terminates an instance.
29
+
30
+ EOH
31
+
32
+ opts.separator "Global options:"
22
33
 
23
- where [COMMAND] is one of:
34
+ opts.on("--access-key KEY", String, "Amazon access key. Overrides AWS_ACCESS_KEY environment variable.") do |access_key|
35
+ @options.access_key = access_key
36
+ end
37
+
38
+ opts.on("--secret SECRET", String, "Amazon secret access key. Overrides AWS_SECRET_ACCESS_KEY environment variable.") do |secret|
39
+ @options.secret = secret
40
+ end
41
+
42
+ opts.on("-d", "--directory DIRECTORY", String, "Location of configuration directory. Defaults to current directory.") do |directory|
43
+ @options.directory = directory
44
+ end
24
45
 
25
- init [LOCATION] Initialize a repository in the specified directory.
26
- launch [OPTIONS] Launch a new instance.
27
- terminate [name] [OPTIONS] Terminates an instance.
46
+ opts.on_tail("-q", "--quiet", "Display as little information as possible.") do
47
+ @options.verbosity = :quiet
48
+ end
28
49
 
29
- and [OPTIONS] include:
50
+ opts.on_tail("-v", "--verbose", "Display as much information as possible.") do
51
+ @options.verbosity = :verbose
52
+ end
30
53
 
31
- "
54
+ # No argument, shows at tail. This will print an options summary.
55
+ # Try it and see!
56
+ opts.on_tail("-?", "--help", "Show this message") do
57
+ puts opts
58
+ exit
59
+ end
60
+ end
61
+ end
62
+
63
+ def launch_options
64
+ OptionParser.new do |opts|
65
+ opts.banner = ""
32
66
  opts.separator "Query options:"
33
67
 
34
68
  opts.on("-l", "--list", "Show environments and applications.") do
@@ -44,7 +78,7 @@ module EC2Launcher
44
78
  end
45
79
 
46
80
  opts.separator ""
47
- opts.separator "Required Launch options:"
81
+ opts.separator "Required launch options:"
48
82
 
49
83
  opts.on("-e", "--environment ENV", "The environment for the server.") do |env|
50
84
  @options.environ = env
@@ -74,18 +108,7 @@ module EC2Launcher
74
108
  end
75
109
 
76
110
  opts.separator ""
77
- opts.separator "Termination options:"
78
-
79
- opts.on("--[no-]snapshot-removal", "Remove EBS snapshots. Defaults to TRUE.") do |removal|
80
- @options.snapshot_removal = removal
81
- end
82
-
83
- opts.separator ""
84
- opts.separator "Overrides:"
85
-
86
- opts.on("-d", "--directory DIRECTORY", String, "Location of configuration directory. Defaults to current directory.") do |directory|
87
- @options.directory = directory
88
- end
111
+ opts.separator "Launch overrides:"
89
112
 
90
113
  opts.on("-h", "--hostname NAME", String, "The name for the new server.") do |hostname|
91
114
  @options.hostname = hostname
@@ -110,47 +133,28 @@ module EC2Launcher
110
133
  opts.on("--volume-size SIZE", Integer, "EBS volume size in GB. Defaults to #{EC2Launcher::DEFAULT_VOLUME_SIZE} GB") do |volume_size|
111
134
  @options.volume_size = volume_size
112
135
  end
136
+ end
137
+ end
113
138
 
114
- opts.separator ""
115
- opts.separator "AWS Security Options:"
116
-
117
- opts.on("--access-key KEY", String, "Amazon access key. Overrides AWS_ACCESS_KEY environment variable.") do |access_key|
118
- @options.access_key = access_key
119
- end
120
-
121
- opts.on("--secret SECRET", String, "Amazon secret access key. Overrides AWS_SECRET_ACCESS_KEY environment variable.") do |secret|
122
- @options.secret = secret
123
- end
124
-
125
- opts.separator ""
126
- opts.separator "Common options:"
127
-
128
- opts.on_tail("-q", "--quiet", "Display as little information as possible.") do
129
- @options.verbosity = :quiet
130
- end
131
-
132
- opts.on_tail("-v", "--verbose", "Display as much information as possible.") do
133
- @options.verbosity = :verbose
139
+ def terminate_options
140
+ OptionParser.new do |opts|
141
+ opts.banner = ""
142
+ opts.separator "Termination options:"
143
+ opts.on("--[no-]snapshot-removal", "Remove EBS snapshots. Defaults to TRUE.") do |removal|
144
+ @options.snapshot_removal = removal
134
145
  end
135
-
136
- # No argument, shows at tail. This will print an options summary.
137
- # Try it and see!
138
- opts.on_tail("-?", "--help", "Show this message") do
139
- puts opts
140
- exit
141
- end
142
146
  end
143
147
  end
144
148
 
145
- def parse(args)
146
- @command = args.shift
147
- unless SUB_COMMANDS.include?(@command)
148
- puts "Missing command! " if @command.nil?
149
- puts "Invalid command: #{@command}" unless @command.nil? || @command == "-?" || @command == "--help"
150
- puts @opts
151
- exit 1
152
- end
149
+ def initialize
150
+ @global_options = global_options()
151
+ @subcommands = {
152
+ 'launch' => launch_options(),
153
+ 'terminate' => terminate_options()
154
+ }
155
+ end
153
156
 
157
+ def parse(args)
154
158
  @options = OpenStruct.new
155
159
  @options.list = false
156
160
  @options.show_defaults = false
@@ -175,6 +179,37 @@ module EC2Launcher
175
179
 
176
180
  @options.directory = "./"
177
181
 
182
+ # Parse global options
183
+ @global_options.order!
184
+
185
+ # Extract the request command
186
+ @command = ARGV.shift.downcase
187
+
188
+ unless SUB_COMMANDS.include?(@command)
189
+ puts "Missing command! " if @command.nil?
190
+ puts "Invalid command: #{@command}" unless @command.nil? || @command == "-?" || @command == "--help" || @command == "help"
191
+ puts @global_options
192
+ exit 1
193
+ end
194
+
195
+ if @command == "-?" || @command == "--help"
196
+ puts @global_options
197
+ exit 1
198
+ end
199
+
200
+ if @command == "help"
201
+ if ARGV.size >= 1 && SUB_COMMANDS.include?(ARGV[0])
202
+ puts @global_options
203
+ puts @subcommands[ARGV[0]]
204
+ else
205
+ puts @global_options
206
+ end
207
+ exit 1
208
+ end
209
+
210
+ # Parse sub command options
211
+ @subcommands[@command].order! if @subcommands.has_key?(@command)
212
+
178
213
  if @command == "init"
179
214
  unless args.length >= 1
180
215
  puts "Missing location!"
@@ -184,15 +219,6 @@ module EC2Launcher
184
219
  end
185
220
  @location = args[0]
186
221
  elsif @command =~ /^term/
187
- @opts.parse!(args)
188
-
189
- if @options.environ.nil?
190
- puts "Missing a required parameter: --environment"
191
- puts
192
- help
193
- exit 1
194
- end
195
-
196
222
  unless args.length >= 1
197
223
  puts "Missing name of server!"
198
224
  puts
@@ -201,8 +227,6 @@ module EC2Launcher
201
227
  end
202
228
  @hostname = args[0]
203
229
  else
204
- @opts.parse!(args)
205
-
206
230
  if (@options.environ.nil? || @options.application.nil?) && ! @options.list
207
231
  puts "Missing a required parameter: #{@options.environ.nil? ? '--environment' : '--application'}"
208
232
  puts
@@ -14,7 +14,7 @@ module EC2Launcher
14
14
  include AWSInitializer
15
15
  include BackoffRunner
16
16
 
17
- def initialize(config_directory, environment_name)
17
+ def initialize(config_directory)
18
18
  @log = Logger.new 'ec2launcher'
19
19
  log_output = Outputter.stdout
20
20
  log_output.formatter = PatternFormatter.new :pattern => "%m"
@@ -27,15 +27,6 @@ module EC2Launcher
27
27
 
28
28
  @config = config_wrapper.config
29
29
  @environments = config_wrapper.environments
30
-
31
- ##############################
32
- # ENVIRONMENT
33
- ##############################
34
- unless @environments.has_key? environment_name
35
- @log.fatal "Environment not found: #{environment_name}"
36
- exit 2
37
- end
38
- @environment = @environments[environment_name]
39
30
  end
40
31
 
41
32
  # Terminates a given server instance.
@@ -50,12 +41,6 @@ module EC2Launcher
50
41
  ##############################
51
42
  initialize_aws(access_key, secret)
52
43
  ec2 = AWS::EC2.new
53
-
54
- ##############################
55
- # Create Route53 connection
56
- ##############################
57
- aws_route53 = AWS::Route53.new if @environment.route53_zone_id
58
- route53 = EC2Launcher::Route53.new(aws_route53, @environment.route53_zone_id, @log)
59
44
 
60
45
  ##############################
61
46
  # Find instance
@@ -72,6 +57,27 @@ module EC2Launcher
72
57
  end # memoize
73
58
 
74
59
  if instance
60
+ environment_name = nil
61
+ AWS.memoize do
62
+ environment_name = instance.tags["environment"].strip
63
+ end
64
+
65
+ ##############################
66
+ # ENVIRONMENT
67
+ ##############################
68
+ unless @environments.has_key? environment_name
69
+ @log.fatal "Environment not found: '#{environment_name}'"
70
+ exit 2
71
+ end
72
+ @environment = @environments[environment_name]
73
+
74
+ ##############################
75
+ # Create Route53 connection
76
+ ##############################
77
+ aws_route53 = nil
78
+ aws_route53 = AWS::Route53.new if @environment.route53_zone_id
79
+ route53 = EC2Launcher::Route53.new(aws_route53, @environment.route53_zone_id, @log)
80
+
75
81
  # Remove EBS snapshots
76
82
  AWS.memoize do
77
83
  remove_snapshots(ec2, instance) if snapshot_removal
@@ -103,14 +109,14 @@ module EC2Launcher
103
109
  volumes = instance.block_device_mappings.values
104
110
 
105
111
  # Iterate over over volumes to find snapshots
106
- @log.info(" Searching for snapshots...")
112
+ @log.info("Searching for snapshots...")
107
113
  snapshots = []
108
114
  volumes.each do |vol|
109
115
  volume_snaps = ec2.snapshots.filter("volume-id", vol.volume.id)
110
116
  volume_snaps.each {|volume_snapshot| snaphots << volume_snapshot }
111
117
  end
112
118
 
113
- @log.info(" Deleting #{snapshots.size} snapshots...")
119
+ @log.info("Deleting #{snapshots.size} snapshots...")
114
120
  snapshots.each {|snap| snap.delete }
115
121
  end
116
122
  end
@@ -2,5 +2,5 @@
2
2
  # Copyright (c) 2012 Sean Laurent
3
3
  #
4
4
  module EC2Launcher
5
- VERSION = "1.4.1"
5
+ VERSION = "1.4.2"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ec2launcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.4.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-23 00:00:00.000000000 Z
12
+ date: 2012-11-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk