infopark-aws_utils 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.rspec +1 -0
- data/Gemfile +3 -0
- data/Rakefile +1 -0
- data/aws_utils.gemspec +18 -0
- data/lib/infopark/aws_utils/env.rb +95 -0
- data/lib/infopark/aws_utils/version.rb +5 -0
- data/lib/infopark/aws_utils.rb +57 -0
- data/spec/env_spec.rb +38 -0
- data/spec/spec_helper.rb +31 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 78f958024570261c6c789338b7e9d35ffef622e6
|
4
|
+
data.tar.gz: 0494bb77b8f40b3db3e2ca8e9dd4a1941ff6fc4b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2a277c0579471d4b08f0cfac7894365eea0ece83baeefd6d80a324138d16a38964a3f6a9e3d69bb5864d301784b92d8cfc68551c14685edc485b2f3ee36c158
|
7
|
+
data.tar.gz: 21dfbc376fb2a28b6980a34c80614d37395576f8889a6cc36f34b8735545a08d2f90350812052bbae61e15ff64dd272b131e4143e1859a5120496d3ce6255228
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/aws_utils.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative 'lib/infopark/aws_utils/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'infopark-aws_utils'
|
5
|
+
s.version = Infopark::AwsUtils::VERSION
|
6
|
+
s.summary = 'A utility lib to ease the use of the AWS SDK'
|
7
|
+
s.description = s.summary
|
8
|
+
s.authors = ['Tilo Prütz']
|
9
|
+
s.email = 'tilo@infopark.de'
|
10
|
+
s.files = `git ls-files -z`.split("\0")
|
11
|
+
s.license = 'UNLICENSED'
|
12
|
+
|
13
|
+
s.add_dependency "aws-sdk", ">=2.10"
|
14
|
+
|
15
|
+
s.add_development_dependency "bundler"
|
16
|
+
s.add_development_dependency "rake"
|
17
|
+
s.add_development_dependency "rspec"
|
18
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'aws-sdk'
|
2
|
+
|
3
|
+
require 'infopark/aws_utils'
|
4
|
+
|
5
|
+
module Infopark
|
6
|
+
module AwsUtils
|
7
|
+
|
8
|
+
class Env
|
9
|
+
def initialize(profile_name = nil)
|
10
|
+
@credentials = Aws::SharedCredentials.new(profile_name: profile_name)
|
11
|
+
@clients = Hash.new do |clients, mod|
|
12
|
+
mod.const_get(:Client).new(credentials: @credentials, region: 'eu-west-1')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def profile_name
|
17
|
+
@credentials.profile_name
|
18
|
+
end
|
19
|
+
|
20
|
+
def ecs
|
21
|
+
@clients[Aws::ECS]
|
22
|
+
end
|
23
|
+
|
24
|
+
def ecr
|
25
|
+
@clients[Aws::ECR]
|
26
|
+
end
|
27
|
+
|
28
|
+
def ec2
|
29
|
+
@clients[Aws::EC2]
|
30
|
+
end
|
31
|
+
|
32
|
+
def as
|
33
|
+
@clients[Aws::AutoScaling]
|
34
|
+
end
|
35
|
+
|
36
|
+
def alb
|
37
|
+
@clients[Aws::ElasticLoadBalancingV2]
|
38
|
+
end
|
39
|
+
|
40
|
+
def aas
|
41
|
+
@clients[Aws::ApplicationAutoScaling]
|
42
|
+
end
|
43
|
+
|
44
|
+
def cw
|
45
|
+
@clients[Aws::CloudWatch]
|
46
|
+
end
|
47
|
+
|
48
|
+
def cwl
|
49
|
+
@clients[Aws::CloudWatchLogs]
|
50
|
+
end
|
51
|
+
|
52
|
+
def sts
|
53
|
+
@clients[Aws::STS]
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
def account_type
|
58
|
+
return "dev" if dev_account?
|
59
|
+
return "prod" if account?(PROD_ACCOUNT_ID)
|
60
|
+
raise "Could not determine account type."
|
61
|
+
end
|
62
|
+
|
63
|
+
def dev_account?
|
64
|
+
account?(DEV_ACCOUNT_ID)
|
65
|
+
end
|
66
|
+
|
67
|
+
def latest_base_image
|
68
|
+
available_images = AwsUtils.gather_all(ec2, :describe_images,
|
69
|
+
owners: [AWS_AMI_OWNER],
|
70
|
+
filters: [
|
71
|
+
{name: "root-device-type", values: ["instance-store"]},
|
72
|
+
{name: "image-type", values: ["machine"]},
|
73
|
+
{name: "virtualization-type", values: ["hvm"]},
|
74
|
+
]).reject {|image| image.name.include?(".rc-") || image.name.include?("-minimal-") }
|
75
|
+
available_images.sort_by(&:creation_date).last
|
76
|
+
end
|
77
|
+
|
78
|
+
def find_image_by_id(id)
|
79
|
+
AwsUtils.gather_all(ec2, :describe_images, image_ids: [id]).first
|
80
|
+
end
|
81
|
+
|
82
|
+
def find_image_by_name(name)
|
83
|
+
AwsUtils.gather_all(ec2, :describe_images, owners: [DEV_ACCOUNT_ID],
|
84
|
+
filters: [{name: "name", values: [name]}]).first
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def account?(account_id)
|
90
|
+
sts.get_caller_identity.account == account_id
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'infopark/aws_utils/env'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Infopark
|
5
|
+
module AwsUtils
|
6
|
+
AWS_AMI_OWNER = "137112412989"
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def gather_all(client, method, **options)
|
10
|
+
@gather_cache ||= {}
|
11
|
+
cache_key = [client, method, options]
|
12
|
+
return @gather_cache[cache_key] if @gather_cache[cache_key]
|
13
|
+
|
14
|
+
result = []
|
15
|
+
loop do
|
16
|
+
response = client.send(method, **options)
|
17
|
+
key = (response.members - [:next_token, :failures]).first
|
18
|
+
if response.members.include?(:failures) && !response.failures.empty?
|
19
|
+
raise "Failed gathering all #{method}: #{response.failures}"
|
20
|
+
end
|
21
|
+
result += response[key]
|
22
|
+
unless options[:next_token] = response.members.include?(:next_token) && response.next_token
|
23
|
+
break
|
24
|
+
end
|
25
|
+
end
|
26
|
+
@gather_cache[cache_key] = result
|
27
|
+
end
|
28
|
+
|
29
|
+
def wait_for(progress, client, waiter, delay: 2, max_attempts: 60, **waiter_params)
|
30
|
+
client.wait_until(waiter, waiter_params) do |w|
|
31
|
+
w.delay = delay
|
32
|
+
w.max_attempts = max_attempts
|
33
|
+
w.before_wait { progress.increment }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
|
39
|
+
def local_config
|
40
|
+
@local_config ||= (
|
41
|
+
path = "#{Dir.home}/.config/infopark/aws_utils.json"
|
42
|
+
if File.exists?(path)
|
43
|
+
JSON.parse(File.read(path))
|
44
|
+
else
|
45
|
+
{}
|
46
|
+
end
|
47
|
+
)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
DEV_ACCOUNT_ID = ENV['INFOPARK_AWS_DEV_ACCOUNT_ID'] || self.local_config['dev_account_id']
|
52
|
+
PROD_ACCOUNT_ID = ENV['INFOPARK_AWS_PROD_ACCOUNT_ID'] || self.local_config['prod_account_id']
|
53
|
+
|
54
|
+
puts "WARN: The Infopark AWS development account ID is not configured." unless DEV_ACCOUNT_ID
|
55
|
+
puts "WARN: The Infopark AWS production account ID is not configured." unless PROD_ACCOUNT_ID
|
56
|
+
end
|
57
|
+
end
|
data/spec/env_spec.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
RSpec.describe Infopark::AwsUtils::Env do
|
2
|
+
subject(:env) { Infopark::AwsUtils::Env.new }
|
3
|
+
|
4
|
+
describe "#account_type" do
|
5
|
+
let(:account_id) { nil }
|
6
|
+
let(:sts) do
|
7
|
+
instance_double(Aws::STS::Client,
|
8
|
+
get_caller_identity: double(:caller_identity, account: account_id)
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
before { allow(Aws::STS::Client).to receive(:new).and_return(sts) }
|
13
|
+
|
14
|
+
subject(:account_type) { env.account_type }
|
15
|
+
|
16
|
+
context "for profile in development account" do
|
17
|
+
let(:account_id) { "012615398682" }
|
18
|
+
|
19
|
+
it { is_expected.to eq("dev") }
|
20
|
+
end
|
21
|
+
|
22
|
+
context "for profile in production account" do
|
23
|
+
let(:account_id) { "115379056088" }
|
24
|
+
|
25
|
+
it { is_expected.to eq("prod") }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "for profile in some other account" do
|
29
|
+
let(:account_id) { "137112412989" }
|
30
|
+
|
31
|
+
it "fails" do
|
32
|
+
expect {
|
33
|
+
account_type
|
34
|
+
}.to raise_error("Could not determine account type.")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'infopark/aws_utils'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
config.expect_with :rspec do |expectations|
|
5
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
6
|
+
end
|
7
|
+
|
8
|
+
config.mock_with :rspec do |mocks|
|
9
|
+
mocks.verify_partial_doubles = true
|
10
|
+
end
|
11
|
+
|
12
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
13
|
+
|
14
|
+
config.filter_run_when_matching :focus
|
15
|
+
|
16
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
17
|
+
|
18
|
+
config.disable_monkey_patching!
|
19
|
+
|
20
|
+
config.warnings = true
|
21
|
+
|
22
|
+
if config.files_to_run.one?
|
23
|
+
config.default_formatter = "doc"
|
24
|
+
end
|
25
|
+
|
26
|
+
config.profile_examples = 10
|
27
|
+
|
28
|
+
config.order = :random
|
29
|
+
|
30
|
+
Kernel.srand config.seed
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: infopark-aws_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tilo Prütz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.10'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
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
|
+
description: A utility lib to ease the use of the AWS SDK
|
70
|
+
email: tilo@infopark.de
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- ".gitignore"
|
76
|
+
- ".rspec"
|
77
|
+
- Gemfile
|
78
|
+
- Rakefile
|
79
|
+
- aws_utils.gemspec
|
80
|
+
- lib/infopark/aws_utils.rb
|
81
|
+
- lib/infopark/aws_utils/env.rb
|
82
|
+
- lib/infopark/aws_utils/version.rb
|
83
|
+
- spec/env_spec.rb
|
84
|
+
- spec/spec_helper.rb
|
85
|
+
homepage:
|
86
|
+
licenses:
|
87
|
+
- UNLICENSED
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.4.5.1
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: A utility lib to ease the use of the AWS SDK
|
109
|
+
test_files: []
|