hashicorptools 0.5.1 → 1.0.0
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.
- checksums.yaml +4 -4
- data/Gemfile +0 -4
- data/VERSION +1 -1
- data/hashicorptools.gemspec +18 -33
- data/lib/hashicorptools.rb +0 -1
- data/lib/hashicorptools/ec2_utilities.rb +5 -6
- data/lib/hashicorptools/packer.rb +34 -23
- data/spec/ec2_utilities_spec.rb +14 -0
- metadata +4 -18
- data/spec/hashicorptools_spec.rb +0 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1dfcc2c084abef69cad0b5b5860982b1389e399e23e539a979eb7fd32dd245e9
|
|
4
|
+
data.tar.gz: b5d51aa37497b9d61f6d7c0b7e1bcfc0e76fb6bee9857eb51e02ba2ddf8df350
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bad4f2253923af3acadf6c59846834a07bf8905dddbd8ccf1ca41920c98544dde39592c936b22f07d77763c86382e82a3983502a15c515a4b29b4b76bc90b4b6
|
|
7
|
+
data.tar.gz: 467388e24b4ea0f583dd1536834f21ed5e9c820feaf19f3347d0002b3106f91481886d6bd44549978c5bf652ceebf1bf116f5193304b4881a5c95abb6fb39252
|
data/Gemfile
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
source "http://rubygems.org"
|
|
2
2
|
|
|
3
|
-
# try to slowly migrate to v2 of the aws api
|
|
4
3
|
gem 'aws-sdk', '~> 2'
|
|
5
|
-
|
|
6
|
-
# this is somewhat undesirable but lets us update json gem version. We should really replace the old v1 code.
|
|
7
|
-
gem 'aws-sdk-v1-ruby24', github: 'seielit/aws-sdk-v1-ruby24', ref: 'aws-sdk-v1-ruby24'
|
|
8
4
|
gem 'dotenv', '~> 2.2', '>= 2.2.1'
|
|
9
5
|
gem 'thor', '>= 0.20.0'
|
|
10
6
|
gem 'activesupport', '>= 5.1.4'
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
1.0.0
|
data/hashicorptools.gemspec
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
3
|
# Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
|
5
|
-
# stub: hashicorptools 0.
|
|
5
|
+
# stub: hashicorptools 1.0.0 ruby lib
|
|
6
6
|
|
|
7
7
|
Gem::Specification.new do |s|
|
|
8
8
|
s.name = "hashicorptools".freeze
|
|
9
|
-
s.version = "0.
|
|
9
|
+
s.version = "1.0.0"
|
|
10
10
|
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
|
12
12
|
s.require_paths = ["lib".freeze]
|
|
13
13
|
s.authors = ["Nathan Woodhull".freeze]
|
|
14
|
-
s.date = "2020-07-
|
|
14
|
+
s.date = "2020-07-30"
|
|
15
15
|
s.description = "Wrappers for terraform and packer".freeze
|
|
16
16
|
s.email = "systems@controlshiftlabs.com".freeze
|
|
17
17
|
s.executables = ["ec2_host".freeze]
|
|
@@ -39,47 +39,32 @@ Gem::Specification.new do |s|
|
|
|
39
39
|
"lib/hashicorptools/packer.rb",
|
|
40
40
|
"lib/hashicorptools/update_launch_configuration.rb",
|
|
41
41
|
"lib/hashicorptools/variables.rb",
|
|
42
|
-
"spec/
|
|
42
|
+
"spec/ec2_utilities_spec.rb",
|
|
43
43
|
"spec/spec_helper.rb"
|
|
44
44
|
]
|
|
45
45
|
s.homepage = "http://github.com/woodhull/hashicorptools".freeze
|
|
46
46
|
s.licenses = ["MIT".freeze]
|
|
47
|
-
s.rubygems_version = "3.
|
|
47
|
+
s.rubygems_version = "3.1.2".freeze
|
|
48
48
|
s.summary = "Wrappers for terraform and packer".freeze
|
|
49
49
|
|
|
50
50
|
if s.respond_to? :specification_version then
|
|
51
51
|
s.specification_version = 4
|
|
52
|
+
end
|
|
52
53
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
s.add_development_dependency(%q<simplecov>.freeze, [">= 0"])
|
|
66
|
-
else
|
|
67
|
-
s.add_dependency(%q<aws-sdk>.freeze, ["~> 2"])
|
|
68
|
-
s.add_dependency(%q<aws-sdk-v1-ruby24>.freeze, [">= 0"])
|
|
69
|
-
s.add_dependency(%q<dotenv>.freeze, ["~> 2.2", ">= 2.2.1"])
|
|
70
|
-
s.add_dependency(%q<thor>.freeze, [">= 0.20.0"])
|
|
71
|
-
s.add_dependency(%q<activesupport>.freeze, [">= 5.1.4"])
|
|
72
|
-
s.add_dependency(%q<byebug>.freeze, [">= 10.0.2"])
|
|
73
|
-
s.add_dependency(%q<git>.freeze, ["> 1.3"])
|
|
74
|
-
s.add_dependency(%q<rspec>.freeze, ["> 3.7"])
|
|
75
|
-
s.add_dependency(%q<rdoc>.freeze, ["> 3.12"])
|
|
76
|
-
s.add_dependency(%q<bundler>.freeze, ["> 2.0"])
|
|
77
|
-
s.add_dependency(%q<juwelier>.freeze, [">= 0"])
|
|
78
|
-
s.add_dependency(%q<simplecov>.freeze, [">= 0"])
|
|
79
|
-
end
|
|
54
|
+
if s.respond_to? :add_runtime_dependency then
|
|
55
|
+
s.add_runtime_dependency(%q<aws-sdk>.freeze, ["~> 2"])
|
|
56
|
+
s.add_runtime_dependency(%q<dotenv>.freeze, ["~> 2.2", ">= 2.2.1"])
|
|
57
|
+
s.add_runtime_dependency(%q<thor>.freeze, [">= 0.20.0"])
|
|
58
|
+
s.add_runtime_dependency(%q<activesupport>.freeze, [">= 5.1.4"])
|
|
59
|
+
s.add_runtime_dependency(%q<byebug>.freeze, [">= 10.0.2"])
|
|
60
|
+
s.add_runtime_dependency(%q<git>.freeze, ["> 1.3"])
|
|
61
|
+
s.add_development_dependency(%q<rspec>.freeze, ["> 3.7"])
|
|
62
|
+
s.add_development_dependency(%q<rdoc>.freeze, ["> 3.12"])
|
|
63
|
+
s.add_development_dependency(%q<bundler>.freeze, ["> 2.0"])
|
|
64
|
+
s.add_development_dependency(%q<juwelier>.freeze, [">= 0"])
|
|
65
|
+
s.add_development_dependency(%q<simplecov>.freeze, [">= 0"])
|
|
80
66
|
else
|
|
81
67
|
s.add_dependency(%q<aws-sdk>.freeze, ["~> 2"])
|
|
82
|
-
s.add_dependency(%q<aws-sdk-v1-ruby24>.freeze, [">= 0"])
|
|
83
68
|
s.add_dependency(%q<dotenv>.freeze, ["~> 2.2", ">= 2.2.1"])
|
|
84
69
|
s.add_dependency(%q<thor>.freeze, [">= 0.20.0"])
|
|
85
70
|
s.add_dependency(%q<activesupport>.freeze, [">= 5.1.4"])
|
data/lib/hashicorptools.rb
CHANGED
|
@@ -5,7 +5,8 @@ module Hashicorptools
|
|
|
5
5
|
end
|
|
6
6
|
|
|
7
7
|
def amis(tag = tag_name)
|
|
8
|
-
|
|
8
|
+
images = ec2.describe_images({owners: ['self'], filters: [{name: 'tag:Name', values: [tag]}]}).images
|
|
9
|
+
sort_by_created_at(images)
|
|
9
10
|
end
|
|
10
11
|
|
|
11
12
|
def ec2
|
|
@@ -17,17 +18,15 @@ module Hashicorptools
|
|
|
17
18
|
'us-east-1'
|
|
18
19
|
end
|
|
19
20
|
|
|
20
|
-
@_ec2 =
|
|
21
|
+
@_ec2 = Aws::EC2::Client.new(region: reg)
|
|
21
22
|
end
|
|
22
23
|
|
|
23
24
|
def vpc_with_name(name)
|
|
24
|
-
|
|
25
|
-
vpcs.first
|
|
25
|
+
ec2.describe_vpcs({filters: [{name: 'tag:Name', values: [name]}]}).vpcs.first
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
def internet_gateway_for_vpc(vpc_id)
|
|
29
|
-
|
|
30
|
-
igs.first
|
|
29
|
+
ec2.describe_internet_gateways({filters: [{name: 'attachment.vpc-id', values: [vpc_id]}]}).internet_gateways.first
|
|
31
30
|
end
|
|
32
31
|
|
|
33
32
|
def sort_by_created_at(collection)
|
|
@@ -45,7 +45,7 @@ module Hashicorptools
|
|
|
45
45
|
|
|
46
46
|
desc "clean_snapshots", "clean obsolete EBS snapshots not associated with any AMI"
|
|
47
47
|
def clean_snapshots
|
|
48
|
-
snapshots = ec2.
|
|
48
|
+
snapshots = ec2.describe_snapshots({owner_ids: ['self']}).snapshots
|
|
49
49
|
snapshots.each do |snapshot|
|
|
50
50
|
match = snapshot.description.match(/Created by CreateImage\(.+\) for (ami-[0-9a-f]+) from vol-.+/)
|
|
51
51
|
if match.nil?
|
|
@@ -54,25 +54,28 @@ module Hashicorptools
|
|
|
54
54
|
end
|
|
55
55
|
|
|
56
56
|
ami_id = match[1]
|
|
57
|
-
unless
|
|
58
|
-
puts "Removing obsolete snapshot #{snapshot.
|
|
59
|
-
|
|
60
|
-
snapshot.delete
|
|
57
|
+
unless Aws::EC2::Image.new(ami_id, region: region).exists?
|
|
58
|
+
puts "Removing obsolete snapshot #{snapshot.snapshot_id} - #{snapshot.description}"
|
|
59
|
+
ec2.delete_snapshot({snapshot_id: snapshot.snapshot_id})
|
|
61
60
|
end
|
|
62
61
|
end
|
|
63
62
|
end
|
|
64
63
|
|
|
65
64
|
desc "boot", "start up an instance of the latest version of AMI"
|
|
66
65
|
def boot
|
|
67
|
-
run_instances_resp = ec2.run_instances(
|
|
66
|
+
run_instances_resp = ec2.run_instances({
|
|
67
|
+
image_id: current_ami('base-image').image_id,
|
|
68
68
|
min_count: 1,
|
|
69
69
|
max_count: 1,
|
|
70
|
-
instance_type: "t2.micro"
|
|
70
|
+
instance_type: "t2.micro"
|
|
71
|
+
})
|
|
71
72
|
|
|
72
|
-
ec2.create_tags(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
ec2.create_tags({
|
|
74
|
+
resources: run_instances_resp.instances.collect(&:instance_id),
|
|
75
|
+
tags: [ {key: 'Name', value: "packer test boot #{tag_name}"},
|
|
76
|
+
{key: 'environment', value: 'packer-development'},
|
|
77
|
+
{key: 'temporary', value: 'kill me'}]
|
|
78
|
+
})
|
|
76
79
|
|
|
77
80
|
require 'byebug'
|
|
78
81
|
byebug
|
|
@@ -106,7 +109,7 @@ module Hashicorptools
|
|
|
106
109
|
end
|
|
107
110
|
|
|
108
111
|
def ami_building_subnet_id
|
|
109
|
-
ec2.
|
|
112
|
+
ec2.describe_subnets({filters: [{name: "vpc-id", values: [ami_building_vpc_id]}]}).subnets.first.subnet_id
|
|
110
113
|
end
|
|
111
114
|
|
|
112
115
|
def format_variable(key, value)
|
|
@@ -133,15 +136,16 @@ module Hashicorptools
|
|
|
133
136
|
@auto_scaling ||= Aws::AutoScaling::Client.new(region: client_region)
|
|
134
137
|
end
|
|
135
138
|
|
|
136
|
-
def
|
|
137
|
-
@
|
|
139
|
+
def regional_ec2_client(client_region=region)
|
|
140
|
+
@_regional_ec2_clients = {} if @_regional_ec2_clients.nil?
|
|
141
|
+
@_regional_ec2_clients[client_region] ||= Aws::EC2::Client.new(region: client_region)
|
|
138
142
|
end
|
|
139
143
|
|
|
140
144
|
def amis_in_use(client_region)
|
|
141
145
|
launch_configs = auto_scaling(client_region).describe_launch_configurations
|
|
142
146
|
image_ids = launch_configs.data['launch_configurations'].collect{|lc| lc.image_id}.flatten
|
|
143
147
|
|
|
144
|
-
ec2_reservations =
|
|
148
|
+
ec2_reservations = regional_ec2_client(client_region).describe_instances
|
|
145
149
|
image_ids << ec2_reservations.reservations.collect{|res| res.instances.collect{|r| r.image_id}}.flatten
|
|
146
150
|
image_ids.flatten
|
|
147
151
|
end
|
|
@@ -170,7 +174,9 @@ module Hashicorptools
|
|
|
170
174
|
amis_to_remove.each do |ami|
|
|
171
175
|
ebs_mappings = ami.block_device_mappings
|
|
172
176
|
puts "Deregistering #{ami.image_id}"
|
|
173
|
-
|
|
177
|
+
regional_ec2_client(region_to_clean).deregister_image({
|
|
178
|
+
image_id: ami.image_id
|
|
179
|
+
})
|
|
174
180
|
delete_ami_snapshots(ebs_mappings, snapshot_region: region_to_clean)
|
|
175
181
|
end
|
|
176
182
|
|
|
@@ -184,16 +190,21 @@ module Hashicorptools
|
|
|
184
190
|
end
|
|
185
191
|
|
|
186
192
|
def amis_in_region(ami_region)
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
193
|
+
images = regional_ec2_client(ami_region).describe_images({
|
|
194
|
+
owners: ['self'],
|
|
195
|
+
filters: [{name: 'tag:Name', values: [tag_name]}]
|
|
196
|
+
}).images
|
|
197
|
+
sort_by_created_at(images)
|
|
190
198
|
end
|
|
191
199
|
|
|
192
200
|
def delete_ami_snapshots(ebs_mappings, snapshot_region:)
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
201
|
+
ec2_client = regional_ec2_client(snapshot_region)
|
|
202
|
+
|
|
203
|
+
ebs_mappings.each do |ebs_mapping|
|
|
204
|
+
unless ebs_mapping.ebs.nil?
|
|
205
|
+
puts "Deleting snapshot #{ebs_mapping.ebs.snapshot_id}"
|
|
206
|
+
ec2_client.delete_snapshot({snapshot_id: ebs_mapping.ebs.snapshot_id})
|
|
207
|
+
end
|
|
197
208
|
end
|
|
198
209
|
end
|
|
199
210
|
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
|
+
|
|
3
|
+
describe Hashicorptools::Ec2Utilities, type: :helper do
|
|
4
|
+
let(:including_class) { Class.new { include Hashicorptools::Ec2Utilities } }
|
|
5
|
+
|
|
6
|
+
subject { including_class.new }
|
|
7
|
+
|
|
8
|
+
describe '#ec2' do
|
|
9
|
+
it 'should return a client' do
|
|
10
|
+
client = subject.ec2
|
|
11
|
+
expect(client).to be_a Aws::EC2::Client
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hashicorptools
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nathan Woodhull
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-07-
|
|
11
|
+
date: 2020-07-30 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: aws-sdk
|
|
@@ -24,20 +24,6 @@ dependencies:
|
|
|
24
24
|
- - "~>"
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: '2'
|
|
27
|
-
- !ruby/object:Gem::Dependency
|
|
28
|
-
name: aws-sdk-v1-ruby24
|
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
|
30
|
-
requirements:
|
|
31
|
-
- - ">="
|
|
32
|
-
- !ruby/object:Gem::Version
|
|
33
|
-
version: '0'
|
|
34
|
-
type: :runtime
|
|
35
|
-
prerelease: false
|
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
-
requirements:
|
|
38
|
-
- - ">="
|
|
39
|
-
- !ruby/object:Gem::Version
|
|
40
|
-
version: '0'
|
|
41
27
|
- !ruby/object:Gem::Dependency
|
|
42
28
|
name: dotenv
|
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -212,7 +198,7 @@ files:
|
|
|
212
198
|
- lib/hashicorptools/packer.rb
|
|
213
199
|
- lib/hashicorptools/update_launch_configuration.rb
|
|
214
200
|
- lib/hashicorptools/variables.rb
|
|
215
|
-
- spec/
|
|
201
|
+
- spec/ec2_utilities_spec.rb
|
|
216
202
|
- spec/spec_helper.rb
|
|
217
203
|
homepage: http://github.com/woodhull/hashicorptools
|
|
218
204
|
licenses:
|
|
@@ -233,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
233
219
|
- !ruby/object:Gem::Version
|
|
234
220
|
version: '0'
|
|
235
221
|
requirements: []
|
|
236
|
-
rubygems_version: 3.
|
|
222
|
+
rubygems_version: 3.1.2
|
|
237
223
|
signing_key:
|
|
238
224
|
specification_version: 4
|
|
239
225
|
summary: Wrappers for terraform and packer
|