asage 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9661fd22c26a956ecf92fc3fe0b305054707df1a
4
+ data.tar.gz: a43f49c19f5d239c6c924615ccc75bc650d5eef0
5
+ SHA512:
6
+ metadata.gz: ea2636464b1024479844012a03bee1d6a7a2cf5ef3d5d702b824e80f885dcf3896879dbaa439f2d32b50603946fd42603436a4350b06a3502bfdda5e152752cf
7
+ data.tar.gz: bde7f04a3259a39eddf69d9197079696a3a918d5cab43d4305597d1c70889c4e4615583fd8e4b7f831d2b56f41b716192ae81ccaa4ef44e7a8312765723edd61
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor
10
+ Gemfile.lock
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at withelmo@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in asage.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 withelmo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,139 @@
1
+ # Asage
2
+
3
+
4
+ This is a simple tool for managing AWS AutoScalingGroup.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'asage'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install asage
21
+
22
+ ## Usage
23
+
24
+ ### All Commands
25
+ ```
26
+ Commands:
27
+ asage attributes [attribute_file] # Read and write parameters.
28
+ asage change_count [attribute_file] # Change count of EC2 instances under auto scaling group
29
+ asage clean_ami [image_id] # Clean AMI. Re-register AMI and delete snapshots.
30
+ asage clean_lc [attribute_file, launch_config_name] # Clean launch config. Delete launch config and re-register AMI and delete snapshots.
31
+ asage create_asg [attribute_file, image_id] # Create auto scaling group
32
+ asage create_lc [attribute_file, image_id] # Create launch config
33
+ asage describe_asg [attribute_file] # Display list of auto-scaling-groups
34
+ asage help [COMMAND] # Describe available commands or one specific command
35
+ asage update_asg [attribute_file] # Update auto scaling group
36
+ asage version # Display version
37
+ ```
38
+
39
+ ### Tour
40
+ 1. Create attribute file for auto scaling group,
41
+ ```
42
+ $ bundle exec asage attributes attribute_file.yaml
43
+ Application Name? new_application
44
+ Instance Type? t3.large
45
+ Key Name? ec2_key_name
46
+ Security Group IDs? sg-xxxxxxxx,sg-yyyyyyy
47
+ User Data File Path? path/to/user_data.txt
48
+ Min Size? 2
49
+ Max Size? 5
50
+ Subnet Group IDs? subnet-xxxxxxx,subnet-yyyyyy
51
+ Load Balancer Name? new_lb_name
52
+ Role Name? new_role_name
53
+ ```
54
+
55
+ Or it is okay to create the file directly with any editor.
56
+
57
+ 2. Create new auto scaling group,
58
+ ```
59
+ $ bundle exec asage create_asg attribute_file.yaml [target AMI ID]
60
+ Created launch configuration(new_application_lc_1519371647)
61
+ Created auto scaling group(new_application_asg)
62
+ ```
63
+
64
+ 3. Display auto scaling group description,
65
+ ```
66
+ $ bundle exec asage describe_asg attribute_file.yaml
67
+ +-------------------+-----------------------+-----------------------------+-----------------------+
68
+ |AutoScalingGroup |AsgCreated |LaunchConfig |LcCreated |
69
+ +-------------------+-----------------------+-----------------------------+-----------------------+
70
+ |new_application_asg|2018-02-23 07:40:49 UTC|new_application_lc_1519371647|2018-02-23 07:40:47 UTC|
71
+ +----------------+-----------------------+--------------------------------+-----------------------+
72
+ +---+-------------------+----------------+--------------+------------+---------------------------------+--------------------+
73
+ |No.|InstanceID |AvailabilityZone|LifecycleState|HealthStatus|LaunchConfigurationName |ProtectedFromScaleIn|
74
+ +---+-------------------+----------------+--------------+------------+---------------------------------+--------------------+
75
+ |1 |i-xxxxxxxxxxxxxxxxx|us-east-1b |InService |Healthy |new_application_asg_lc_1519371647|false |
76
+ +---+-------------------+----------------+--------------+------------+---------------------------------+--------------------+
77
+ |2 |i-yyyyyyyyyyyyyyyyy|us-east-1b |InService |Healthy |new_application_asg_lc_1519371647|false |
78
+ +---+-------------------+----------------+--------------+------------+---------------------------------+--------------------+
79
+ ```
80
+
81
+ 4. When update auto scaling group for creating new AMI from running EC2 instance,
82
+ ```
83
+ $ bundle exec asage update_asg attribute_file.yaml
84
+ Searching EC2 instances of new_application_asg [▸▹▹▹▹]
85
+ Which EC2 instance do you update auto scaling group from based on? i-xxxxxxxxxxxxxxxxx(us-east-1b)
86
+ Creating AMI(ami-xxxxxxxxx) [▹▹▹▹▸]
87
+ Created AMI(new_application_asg_for_asg_1519372804:ami-xxxxxxxxx)
88
+ Created launch configuration(new_application_lc_1519372939)
89
+ Created auto scaling group(new_application_asg)
90
+ +------------------------+-----------------------+-----------------------------+-----------------------+
91
+ |AutoScalingGroup |AsgCreated |LaunchConfig |LcCreated |
92
+ +------------------------+-----------------------+-----------------------------+-----------------------+
93
+ |new_application_asg |2018-02-23 07:40:49 UTC|new_application_lc_1519372939|2018-02-23 08:02:19 UTC|
94
+ +------------------------+-----------------------+---------------------+-------------------------------+
95
+ +---+-------------------+----------------+--------------+------------+-----------------------+------------------------------+
96
+ |No.|InstanceID |AvailabilityZone|LifecycleState|HealthStatus|LaunchConfigurationName|ProtectedFromScaleIn |
97
+ +---+-------------------+----------------+--------------+------------+-----------------------+------------------------------+
98
+ |1 |i-xxxxxxxxxxxxxxxxx|us-east-1b |InService |Healthy |new_application_asg_lc_1519371647|false |
99
+ +---+-------------------+----------------+--------------+------------+---------------------------------+--------------------+
100
+ |2 |i-yyyyyyyyyyyyyyyyy|us-east-1b |InService |Healthy |new_application_asg_lc_1519371647|false |
101
+ +---+-------------------+----------------+--------------+------------+---------------------------------+--------------------+
102
+ ```
103
+
104
+ 5. Change EC2 count under auto scaling group,
105
+ ```
106
+ $ vi attribute_file.yaml
107
+ (modify count of min/max size)
108
+
109
+ $ bundle exec asage change_count fusic_dev.yaml
110
+ Are you sure to change count of EC2 instances under new_application_asg to max:1 and min:1 ? [yes/no] yes
111
+ The count of EC2 instances under new_application_asg was changed to max:1 and min:1.
112
+ ```
113
+
114
+ 6. Clean up OLD AMI,
115
+ ```
116
+ $ bundle exec asage clean_lc fusic_dev.yaml
117
+ Searching launch configs [▹▹▸▹▹]
118
+ Which launch config do you delete?
119
+ ‣ Cancel
120
+ new_application_asg_lc_1519372939|ami-xxxxxxxx|t2.large|2018-02-23 08:02:21 UTC
121
+ new_application_asg_lc_1519371647|ami-yyyyyyyy|t2.large|2018-02-23 07:40:48 UTC
122
+ >> new_application_asg_lc_1519371647(ami-yyyyyyyy)
123
+ Are you sure to delete the above launch config?[yes/no] yes
124
+ Deleted launch config(new_application_asg_lc_1519371647)
125
+ Loading AMI(ami-yyyyyyyy) detail [▹▸▹▹▹]
126
+ +---+----------------------+----------+----+
127
+ |No.|SnapshotID |Volume[GB]|Type|
128
+ +---+----------------------+----------+----+
129
+ |1 |snap-zzzzzzzzzzzzzzzzz|8 |gp2 |
130
+ +---+----------------------+----------+----+
131
+ Are you sure to delete IAM(ami-yyyyyyyy) and above snapshots?[yes/no] yes
132
+ Deregistered IAM(ami-yyyyyyyy)
133
+ Deleted snapshot(snap-zzzzzzzzzzzzzzzzz)
134
+ Completed.
135
+ ```
136
+
137
+ ## License
138
+
139
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "asage/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "asage"
8
+ spec.version = Asage::VERSION
9
+ spec.authors = ["withelmo"]
10
+ spec.email = ["withelmo@gmail.com"]
11
+
12
+ spec.summary = %q{Tool to control AWS auto scaling settings.}
13
+ spec.description = %q{auto-scaling-group, launch-config, AMI}
14
+ spec.homepage = "https://github.com/withelmo/asage"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "pry-byebug"
28
+
29
+ spec.add_dependency "thor"
30
+ spec.add_dependency "tty"
31
+ spec.add_dependency "aws-sdk"
32
+ end
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "asage"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "asage"
4
+
5
+ Asage::CLI.start
@@ -0,0 +1,301 @@
1
+ require "asage/version"
2
+ require "asage/attribute"
3
+ require "thor"
4
+ require "aws-sdk"
5
+ require "tty"
6
+ require "pry"
7
+
8
+ module Asage
9
+
10
+ class CLI < Thor
11
+
12
+ def initialize(*args)
13
+ super
14
+ @attributes = Asage::Attribute.new(args[0][0])
15
+ end
16
+
17
+ desc 'version', 'Display version'
18
+ def version
19
+ puts Asage::VERSION
20
+ end
21
+
22
+ desc 'attributes [attribute_file]', 'Read and write parameters.'
23
+ def attributes(attribute_file)
24
+ if @attributes.exists?
25
+ ans = prompt.ask("Attribute file(#{attribute_file}) already exists. Are you sure to overwrite it?[yes/no]")
26
+
27
+ unless ans == "yes"
28
+ puts "Canceled."
29
+ return
30
+ end
31
+ end
32
+
33
+ @attributes.app_name = prompt.ask("Application Name?")
34
+ @attributes.instance_type = prompt.ask("Instance Type?")
35
+ @attributes.key_name = prompt.ask("Key Name?")
36
+ @attributes.security_group_ids = prompt.ask("Security Group IDs?")
37
+ @attributes.user_data = prompt.ask("User Data File Path?")
38
+ @attributes.min_size = prompt.ask("Min Size?"){|q| q.convert :int}
39
+ @attributes.max_size = prompt.ask("Max Size?"){|q| q.convert :int}
40
+ @attributes.subnet_ids = prompt.ask("Subnet Group IDs?")
41
+
42
+ @attributes.load_balancer_names = prompt.ask("Load Balancer Name?")
43
+ @attributes.role_name = prompt.ask("Role Name?")
44
+
45
+ @attributes.to_yaml
46
+ end
47
+
48
+ desc 'describe_asg [attribute_file]', 'Display list of auto-scaling-groups'
49
+ def describe_asg(attribute_file)
50
+ # s = spinner("Searching auto scaling groups [:spinner]")
51
+ a = asg.describe_auto_scaling_groups
52
+ .auto_scaling_groups
53
+ .select {|asg| asg.auto_scaling_group_name == @attributes.asg_name_generator}
54
+ .first
55
+ # s.stop
56
+
57
+ lc_name = a.launch_configuration_name
58
+ lc_created_at = lc_name
59
+ .match(Regexp.new("#{@attributes.lc_prefix_generator}(.*)"))[1]
60
+
61
+ display_table(%w(AutoScalingGroup AsgCreated LaunchConfig LcCreated),
62
+ [a.auto_scaling_group_name, a.created_time,
63
+ a.launch_configuration_name, Time.at(lc_created_at.to_i).utc])
64
+
65
+ if a.instances.empty?
66
+ puts "No instances."
67
+ else
68
+ instances = a.instances.map.with_index(1) do |i, n|
69
+ [n, i.instance_id, i.availability_zone, i.lifecycle_state,
70
+ i.health_status, i.launch_configuration_name, i.protected_from_scale_in]
71
+ end
72
+ display_table(%w(No. InstanceID AvailabilityZone LifecycleState HealthStatus LaunchConfigurationName ProtectedFromScaleIn),
73
+ instances)
74
+ end
75
+ end
76
+
77
+ desc 'create_lc [attribute_file, image_id]', 'Create launch config'
78
+ def create_lc(attribute_file, image_id)
79
+ lc_name = @attributes.lc_name_generator
80
+ user_data = Base64.encode64(File.open(@attributes.user_data).read)
81
+
82
+ conf = {
83
+ image_id: image_id,
84
+ instance_type: @attributes.instance_type,
85
+ launch_configuration_name: lc_name,
86
+ key_name: @attributes.key_name,
87
+ security_groups: [*@attributes.security_group_ids],
88
+ user_data: user_data
89
+ }
90
+
91
+ if @attributes.present?(:role_name)
92
+ conf.merge!({role_name: @attributes.role_name})
93
+ end
94
+
95
+ asg.create_launch_configuration(conf)
96
+ puts "Created launch configuration(#{lc_name})"
97
+ lc_name
98
+ end
99
+
100
+ desc 'create_asg [attribute_file, image_id]', 'Create auto scaling group'
101
+ def create_asg(attribute_file, image_id)
102
+ conf = {
103
+ auto_scaling_group_name: @attributes.asg_name_generator,
104
+ launch_configuration_name: create_lc(attribute_file, image_id),
105
+ max_size: @attributes.max_size,
106
+ min_size: @attributes.min_size,
107
+ vpc_zone_identifier: @attributes.subnet_ids,
108
+ }
109
+
110
+ if @attributes.present?(:load_balancer_names)
111
+ conf.merge!({load_balancer_names: [*@attributes.load_balancer_names]})
112
+ end
113
+
114
+ asg.create_auto_scaling_group(conf)
115
+ puts "Created auto scaling group(#{@attributes.asg_name_generator})"
116
+ end
117
+
118
+ desc 'update_asg [attribute_file]', 'Update auto scaling group'
119
+ def update_asg(attribute_file)
120
+ asg_name = @attributes.asg_name_generator
121
+ s = spinner("Searching EC2 instances of #{asg_name} [:spinner]")
122
+ resp_asg = asg.describe_auto_scaling_groups({auto_scaling_group_names: ["#{asg_name}"]})
123
+ s.stop
124
+
125
+ selected_instance = prompt.select("Which EC2 instance do you update auto scaling group from based on?") do |menu|
126
+ menu.choice("Cancel", nil)
127
+ resp_asg.auto_scaling_groups[0].instances.each do |instance|
128
+ menu.choice("#{instance.instance_id}(#{instance.availability_zone})", instance)
129
+ end
130
+ end
131
+
132
+ if selected_instance.nil?
133
+ puts "Canceled."
134
+ return
135
+ end
136
+
137
+ created_ami_name = @attributes.ami_name_generator
138
+ conf_ami = {
139
+ instance_id: selected_instance.instance_id,
140
+ name: created_ami_name
141
+ }
142
+ resp_ami = ec2.create_image(conf_ami)
143
+ s = spinner("Creating AMI(#{resp_ami.image_id}) [:spinner]")
144
+ ec2.wait_until(:image_available, image_ids: [resp_ami.image_id]) do |w|
145
+ w.interval = 15
146
+ end
147
+ ec2.describe_images({image_ids: [resp_ami.image_id]})
148
+ s.stop
149
+
150
+ puts "Created AMI(#{created_ami_name}:#{resp_ami.image_id})"
151
+ lc_name = create_lc(attribute_file, resp_ami.image_id)
152
+ conf_asg = {
153
+ auto_scaling_group_name: asg_name,
154
+ launch_configuration_name: lc_name,
155
+ }
156
+ asg.update_auto_scaling_group(conf_asg)
157
+ puts "Created auto scaling group(#{asg_name})"
158
+ describe_asg(attribute_file)
159
+ end
160
+
161
+ desc 'change_count [attribute_file]', 'Change count of EC2 instances under auto scaling group'
162
+ def change_count(attribute_file)
163
+ asg_name = @attributes.asg_name_generator
164
+ min_size = @attributes.min_size
165
+ max_size = @attributes.max_size
166
+ msg = "Are you sure to change count of EC2 instances under #{asg_name} to max:#{max_size} and min:#{min_size} ? [yes/no]"
167
+
168
+ ans = prompt.ask(msg) do |q|
169
+ q.convert :string
170
+ end
171
+
172
+ unless ans == "yes"
173
+ puts "Canceled."
174
+ return
175
+ end
176
+
177
+ asg.update_auto_scaling_group({auto_scaling_group_name: asg_name,
178
+ max_size: max_size,
179
+ min_size: min_size})
180
+
181
+ puts "The count of EC2 instances under #{asg_name} was changed to max:#{max_size} and min:#{min_size}."
182
+ end
183
+
184
+ desc 'clean_lc [attribute_file, launch_config_name]', 'Clean launch config. Delete launch config and re-register AMI and delete snapshots.'
185
+ def clean_lc(attribute_file, lc_name = nil)
186
+ if lc_name.nil?
187
+ s = spinner("Searching launch configs [:spinner]")
188
+ target_lc = asg.describe_launch_configurations.launch_configurations.select {|s|
189
+ s.launch_configuration_name =~ Regexp.new("#{@attributes.app_name}_lc_")
190
+ }.sort_by(&:created_time).reverse
191
+ s.stop
192
+
193
+ selected_lc = prompt.select("Which launch config do you delete?") do |menu|
194
+ menu.choice("Cancel", nil)
195
+ target_lc.each do |lc|
196
+ menu.choice("#{lc.launch_configuration_name}|#{lc.image_id}|#{lc.instance_type}|#{lc.created_time}", lc)
197
+ end
198
+ end
199
+
200
+ if selected_lc.nil?
201
+ puts "Canceled."
202
+ return
203
+ end
204
+ else
205
+ selected_lc = asg.describe_launch_configurations({launch_configuration_names: [lc_name]})
206
+ .launch_configurations[0]
207
+
208
+ if selected_lc.nil?
209
+ puts "No launch config exist.(#{lc_name})"
210
+ return
211
+ end
212
+ end
213
+
214
+ puts ">> #{selected_lc.launch_configuration_name}(#{selected_lc.image_id})"
215
+
216
+ ans = prompt.ask("Are you sure to delete the above launch config?[yes/no]") do |q|
217
+ q.convert :string
218
+ end
219
+
220
+ unless ans == "yes"
221
+ puts "Canceled."
222
+ return
223
+ end
224
+
225
+ asg.delete_launch_configuration({launch_configuration_name: selected_lc.launch_configuration_name})
226
+ puts "Deleted launch config(#{selected_lc.launch_configuration_name})"
227
+ clean_ami(selected_lc.image_id)
228
+
229
+ puts "Completed."
230
+ end
231
+
232
+ desc 'clean_ami [image_id]', 'Clean AMI. Re-register AMI and delete snapshots.'
233
+ def clean_ami(image_id)
234
+ s = spinner("Loading AMI(#{image_id}) detail [:spinner]")
235
+ target_image = ec2.describe_images({image_ids: [image_id]}).images.first
236
+ s.stop
237
+
238
+ if target_image.nil?
239
+ puts "AMI(#{image_id}) does not exist"
240
+ else
241
+ unless target_image.public
242
+ target_blocks = target_image.block_device_mappings
243
+ displayed_blocks = target_blocks.map.with_index(1) do |b, i|
244
+ [i, b.ebs.snapshot_id, b.ebs.volume_size, b.ebs.volume_type]
245
+ end
246
+ display_table(%w(No. SnapshotID Volume[GB] Type), displayed_blocks)
247
+
248
+ ans = prompt.ask("Are you sure to delete IAM(#{target_image.image_id}) and above snapshots?[yes/no]") do |q|
249
+ q.convert :string
250
+ end
251
+
252
+ unless ans == "yes"
253
+ puts "Canceled."
254
+ return
255
+ end
256
+
257
+ ec2.deregister_image({image_id: target_image.image_id})
258
+ puts "Deregistered IAM(#{target_image.image_id})"
259
+
260
+ target_blocks.each do |b|
261
+ ec2.delete_snapshot({snapshot_id: b.ebs.snapshot_id})
262
+ puts "Deleted snapshot(#{b.ebs.snapshot_id})"
263
+ end
264
+ end
265
+ end
266
+
267
+ end
268
+
269
+ private
270
+
271
+ # AWS client generator
272
+ def ec2
273
+ Aws::EC2::Client.new
274
+ end
275
+
276
+ def asg
277
+ Aws::AutoScaling::Client.new
278
+ end
279
+
280
+ # User Interface
281
+ def display_table(header, body)
282
+ unless body[0].kind_of? Array
283
+ body = [body]
284
+ end
285
+
286
+ table = TTY::Table.new header, body
287
+ puts table.render(:ascii)
288
+ end
289
+
290
+ def spinner(meg, format = :arrow_pulse)
291
+ s = TTY::Spinner.new(meg, format: format)
292
+ s.auto_spin
293
+ s
294
+ end
295
+
296
+ def prompt
297
+ TTY::Prompt.new
298
+ end
299
+ end
300
+
301
+ end
@@ -0,0 +1,74 @@
1
+ require "yaml"
2
+
3
+ module Asage
4
+ class Attribute
5
+ attr_accessor :attribute_file, :app_name, :instance_type, :key_name,
6
+ :security_group_ids, :user_data, :role_name, :min_size,
7
+ :max_size, :load_balancer_names, :subnet_ids
8
+
9
+ def initialize(attribute_file)
10
+ self.attribute_file = attribute_file
11
+
12
+ if self.exists?
13
+ attributes = YAML.load_file(attribute_file)
14
+ self.app_name = attributes.keys.first
15
+
16
+ attribute = attributes[self.app_name]
17
+ self.instance_type = attribute["instance_type"]
18
+ self.key_name = attribute["key_name"]
19
+ self.security_group_ids = attribute["security_group_ids"]
20
+ self.user_data = attribute["user_data"]
21
+ self.role_name = attribute["role_name"]
22
+ self.min_size = attribute["min_size"]
23
+ self.max_size = attribute["max_size"]
24
+ self.load_balancer_names = attribute["load_balancer_names"]
25
+ self.subnet_ids = attribute["subnet_ids"]
26
+ end
27
+ end
28
+
29
+ def exists?
30
+ File.exist?(self.attribute_file.to_s)
31
+ end
32
+
33
+ def asg_name_generator
34
+ "#{self.app_name}_asg"
35
+ end
36
+
37
+ def lc_prefix_generator
38
+ "#{self.app_name}_lc_"
39
+ end
40
+
41
+ def lc_name_generator
42
+ "#{lc_prefix_generator}#{Time.now.to_i}"
43
+ end
44
+
45
+ def ami_name_generator
46
+ "#{self.app_name}_for_asg_#{Time.now.to_i}"
47
+ end
48
+
49
+ def to_h
50
+ self.instance_variables.each_with_object({}) do |attr, hash|
51
+ hash[attr.to_s.delete("@")] = self.instance_variable_get(attr)
52
+ end
53
+ end
54
+
55
+ def to_yaml
56
+ h = self.to_h
57
+ h.delete("attribute_file")
58
+ h.delete("app_name")
59
+ attributes_hash = {self.app_name => h}
60
+
61
+ File.open(self.attribute_file, 'w') do |f|
62
+ YAML.dump(attributes_hash, f)
63
+ end
64
+ end
65
+
66
+ def present?(attr)
67
+ !blank?(attr)
68
+ end
69
+
70
+ def blank?(attr)
71
+ self.send(attr).nil? || self.send(attr).empty?
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module Asage
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - withelmo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: thor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: tty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: aws-sdk
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: auto-scaling-group, launch-config, AMI
112
+ email:
113
+ - withelmo@gmail.com
114
+ executables:
115
+ - asage
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - asage.gemspec
128
+ - bin/console
129
+ - bin/setup
130
+ - exe/asage
131
+ - lib/asage.rb
132
+ - lib/asage/attribute.rb
133
+ - lib/asage/version.rb
134
+ homepage: https://github.com/withelmo/asage
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.6.13
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Tool to control AWS auto scaling settings.
158
+ test_files: []