activeaws 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 +10 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +117 -0
- data/Rakefile +2 -0
- data/active_aws.gemspec +37 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_aws/base.rb +54 -0
- data/lib/active_aws/cloud_formation_provisioner.rb +77 -0
- data/lib/active_aws/configure.rb +29 -0
- data/lib/active_aws/ec2.rb +89 -0
- data/lib/active_aws/ec2_provisioner.rb +76 -0
- data/lib/active_aws/elastic_load_balancing_v2.rb +60 -0
- data/lib/active_aws/tag_spec.rb +37 -0
- data/lib/active_aws/target_group.rb +92 -0
- data/lib/active_aws/version.rb +3 -0
- data/lib/active_aws.rb +17 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f3553817e80b5e63cb169014e7fb85b821b2c91e
|
4
|
+
data.tar.gz: 1e7ba39337ee5bbac9ec98c88b68faac0ccda2f2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e3c6938011af3672e7a0f6617d95de3c62369b89b0b551edcc39935ab873a861f28895132204cf60861afe8aca72cd32605d3dd7313d1fb32a20f3aa0a470ad
|
7
|
+
data.tar.gz: d12ca44e3d2325f8ce4ccdb325c608336f8ef3f9993b631e9accc9b753501ac4e93ac8febe2a25c20fc7f1988f10abe28339b3311871ab42a102c34ae490afd9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 metheglin
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# ActiveAws
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'activeaws'
|
9
|
+
```
|
10
|
+
|
11
|
+
If you want to load this library automatically with `Bundler.require`, please specify `require` option at Gemfile.
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'activeaws', require: 'active_aws'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install activeaws
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'active_aws'
|
29
|
+
```
|
30
|
+
|
31
|
+
### Initialize
|
32
|
+
|
33
|
+
```rb
|
34
|
+
ActiveAws::Base.load_config!(
|
35
|
+
region: 'ap-northeast-1',
|
36
|
+
profile: 'rv-metheglin',
|
37
|
+
)
|
38
|
+
```
|
39
|
+
|
40
|
+
```rb
|
41
|
+
ActiveAws::Base.load_config!( File.expand_path("./aws.yml", __dir__) )
|
42
|
+
```
|
43
|
+
|
44
|
+
### Ec2
|
45
|
+
|
46
|
+
```rb
|
47
|
+
provisioner = ActiveAws::Ec2Provisioner.new(:test_instance_name) do |e|
|
48
|
+
e.instance_type = 't2.nano'
|
49
|
+
e.key_name = 'your-credential-key-name-here'
|
50
|
+
e.image_id = 'ami-xxxxxx'
|
51
|
+
e.security_group_ids = ['sg-xxxxxxx']
|
52
|
+
e.subnet_id = 'subnet-xxxxxxx'
|
53
|
+
end
|
54
|
+
ec2 = provisioner.exec!
|
55
|
+
ec2.wait_until :instance_running
|
56
|
+
```
|
57
|
+
|
58
|
+
```rb
|
59
|
+
ec2 = ActiveAws::Ec2.find( 'your-instance-id' )
|
60
|
+
ec2.to_h
|
61
|
+
```
|
62
|
+
|
63
|
+
### CloudFormation
|
64
|
+
|
65
|
+
```rb
|
66
|
+
# Prepare `vpc.json` with like:
|
67
|
+
# Ex): https://aws-quickstart.s3-ap-northeast-1.amazonaws.com/quickstart-linux-bastion/submodules/quickstart-aws-vpc/templates/aws-vpc.template
|
68
|
+
class VpcProduction < ActiveAws::CloudFormationProvisioner
|
69
|
+
|
70
|
+
class << self
|
71
|
+
def generate
|
72
|
+
new(
|
73
|
+
"your-cloudformation-stack-name-here",
|
74
|
+
File.expand_path("vpc.json", __dir__),
|
75
|
+
{
|
76
|
+
"AvailabilityZones" => "ap-northeast-1a,ap-northeast-1c,ap-northeast-1d",
|
77
|
+
"KeyPairName" => "your-credential-key-name-here",
|
78
|
+
"PrivateSubnet1CIDR" => "10.0.0.0/19",
|
79
|
+
"PrivateSubnet2CIDR" => "10.0.32.0/19",
|
80
|
+
"PublicSubnet1CIDR" => "10.0.128.0/20",
|
81
|
+
"PublicSubnet2CIDR" => "10.0.144.0/20",
|
82
|
+
"RemoteAccessCIDR" => "0.0.0.0/0",
|
83
|
+
"VPCCIDR" => "10.0.0.0/16",
|
84
|
+
}
|
85
|
+
)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# # Please carefully use this method!!
|
90
|
+
# # The CloudFormationProvisioner class doesn't implement `destroy!` on purpose.
|
91
|
+
# def destroy!
|
92
|
+
# self.class.client.delete_stack(stack_name: normalized_name)
|
93
|
+
# end
|
94
|
+
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
```rb
|
99
|
+
prov = VpcProduction.generate
|
100
|
+
prov.save!
|
101
|
+
prov.wait_until :stack_create_complete
|
102
|
+
```
|
103
|
+
|
104
|
+
|
105
|
+
## Development
|
106
|
+
|
107
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
108
|
+
|
109
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
110
|
+
|
111
|
+
## Contributing
|
112
|
+
|
113
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/metheglin/activeaws.
|
114
|
+
|
115
|
+
## License
|
116
|
+
|
117
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/active_aws.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "active_aws/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activeaws"
|
8
|
+
spec.version = ActiveAws::VERSION
|
9
|
+
spec.authors = ["metheglin"]
|
10
|
+
spec.email = ["pigmybank@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Managing aws resources with active way."
|
13
|
+
spec.homepage = "https://github.com/metheglin/activeaws"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
# if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
# else
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
# "public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
|
35
|
+
spec.add_dependency "activesupport", "~> 5"
|
36
|
+
spec.add_dependency "aws-sdk", "~> 2"
|
37
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_aws"
|
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
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require_relative './configure'
|
3
|
+
|
4
|
+
module ActiveAws
|
5
|
+
class Base
|
6
|
+
|
7
|
+
@attributes = []
|
8
|
+
|
9
|
+
class << self
|
10
|
+
attr_accessor :attributes
|
11
|
+
attr_reader :root_configure
|
12
|
+
|
13
|
+
def inherited( klass )
|
14
|
+
klass.attributes = @attributes
|
15
|
+
end
|
16
|
+
|
17
|
+
def load_config!( args )
|
18
|
+
@config = if args.is_a? Hash
|
19
|
+
args
|
20
|
+
elsif File.file?( args.to_s )
|
21
|
+
YAML.load_file( args.to_s )
|
22
|
+
else
|
23
|
+
raise ArgumentError
|
24
|
+
end
|
25
|
+
@root_configure = Configure.new( **@config.symbolize_keys )
|
26
|
+
end
|
27
|
+
|
28
|
+
def configure
|
29
|
+
Base.root_configure
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize( **params )
|
34
|
+
self.class.attributes.each do |k|
|
35
|
+
method = "#{k}="
|
36
|
+
self.send(method, params[k]) if params[k].present?
|
37
|
+
end
|
38
|
+
yield( self ) if block_given?
|
39
|
+
end
|
40
|
+
|
41
|
+
def name
|
42
|
+
name_tag = tags.detect{|t| t[:key].to_s == "Name"}
|
43
|
+
return nil unless name_tag
|
44
|
+
name_tag[:value]
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_h
|
48
|
+
self.class.attributes.reduce({}) do |acc,k|
|
49
|
+
acc[k] = self.send( k )
|
50
|
+
acc
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module ActiveAws
|
2
|
+
class CloudFormationProvisioner < Base
|
3
|
+
|
4
|
+
attr_reader :name, :template_path, :parameters
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def client
|
8
|
+
Aws::CloudFormation::Client.new( **configure.default_client_params )
|
9
|
+
end
|
10
|
+
|
11
|
+
def find( stack_name )
|
12
|
+
response = client.describe_stacks( stack_name: stack_name )
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize( name, template_path, parameters={} )
|
17
|
+
@name = name
|
18
|
+
@template_path = template_path
|
19
|
+
@parameters = parameters
|
20
|
+
end
|
21
|
+
|
22
|
+
def template_body
|
23
|
+
@template_body ||= File.read( template_path )
|
24
|
+
end
|
25
|
+
|
26
|
+
def normalized_name
|
27
|
+
# self.class.configure.cfn_stack_name_prefix + name
|
28
|
+
name
|
29
|
+
end
|
30
|
+
|
31
|
+
def normalized_parameters
|
32
|
+
parameters.map do |k,v|
|
33
|
+
{
|
34
|
+
parameter_key: k,
|
35
|
+
parameter_value: v,
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_h
|
41
|
+
{
|
42
|
+
stack_name: normalized_name,
|
43
|
+
template_body: template_body,
|
44
|
+
parameters: normalized_parameters,
|
45
|
+
capabilities: ["CAPABILITY_IAM"],
|
46
|
+
# on_failure: "DELETE",
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def exists?
|
51
|
+
res = self.class.client.describe_stacks( stack_name: normalized_name )
|
52
|
+
return res.stacks.length > 0
|
53
|
+
rescue => e
|
54
|
+
return false
|
55
|
+
end
|
56
|
+
|
57
|
+
def save!
|
58
|
+
response = if exists?
|
59
|
+
self.class.client.update_stack( self.to_h )
|
60
|
+
else
|
61
|
+
self.class.client.create_stack( self.to_h )
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# `waiter_name` can be checked with the command below.
|
66
|
+
# ActiveAws::CloudFormationProvisioner.client.waiter_names
|
67
|
+
#
|
68
|
+
# Usage:
|
69
|
+
# prov.wait_until :instance_running do |i|
|
70
|
+
# i.max_attempts = 5
|
71
|
+
# i.delay = 5
|
72
|
+
# end
|
73
|
+
def wait_until( waiter_name, &block )
|
74
|
+
self.class.client.wait_until(waiter_name, stack_name: normalized_name, &block)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ActiveAws
|
2
|
+
class Configure
|
3
|
+
attr_accessor :profile,
|
4
|
+
:access_key,
|
5
|
+
:secret_key,
|
6
|
+
:region
|
7
|
+
|
8
|
+
def initialize( **params )
|
9
|
+
params.each do |k,v|
|
10
|
+
self.send("#{k}=", v) if self.methods.include?(k)
|
11
|
+
end
|
12
|
+
yield( self ) if block_given?
|
13
|
+
end
|
14
|
+
|
15
|
+
def credentials
|
16
|
+
@credentials ||= profile ?
|
17
|
+
Aws::SharedCredentials.new( profile_name: profile ) :
|
18
|
+
Aws::Credentials.new( access_key, secret_key )
|
19
|
+
@credentials
|
20
|
+
end
|
21
|
+
|
22
|
+
def default_client_params
|
23
|
+
{
|
24
|
+
region: region,
|
25
|
+
credentials: credentials,
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module ActiveAws
|
2
|
+
class Ec2 < Base
|
3
|
+
|
4
|
+
@attributes = [
|
5
|
+
:block_device_mappings,
|
6
|
+
:image_id, :instance_id, :instance_type, :key_name,
|
7
|
+
:launch_time, :network_interfaces,
|
8
|
+
:private_ip_address, :private_dns_name,
|
9
|
+
:public_ip_address, :public_dns_name,
|
10
|
+
:root_device_name, :security_groups, :subnet_id, :tags, :vpc_id,
|
11
|
+
]
|
12
|
+
|
13
|
+
attr_accessor *attributes
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def client
|
17
|
+
Aws::EC2::Client.new( **configure.default_client_params )
|
18
|
+
end
|
19
|
+
|
20
|
+
def find( instance_id )
|
21
|
+
response = client.describe_instances({
|
22
|
+
instance_ids: [instance_id],
|
23
|
+
})
|
24
|
+
return nil unless response.reservations[0]
|
25
|
+
new( **response.reservations[0].instances[0].to_h )
|
26
|
+
end
|
27
|
+
|
28
|
+
def find_by_name( name )
|
29
|
+
response = client.describe_instances({
|
30
|
+
filters: [{ name: "tag:Name", values: [name] }],
|
31
|
+
})
|
32
|
+
return nil unless response.reservations[0]
|
33
|
+
new( **response.reservations[0].instances[0].to_h )
|
34
|
+
end
|
35
|
+
|
36
|
+
# Usage:
|
37
|
+
# Ec2::where( :"tag:Role" => "web" )
|
38
|
+
# Ec2::where( :"instance-type" => "t2.micro" )
|
39
|
+
def where( **args )
|
40
|
+
filter_params = args.map{|k, v| { name: k, values: Array.wrap(v) }}
|
41
|
+
response = client.describe_instances({
|
42
|
+
filters: filter_params,
|
43
|
+
})
|
44
|
+
instance_params = response.reservations.map{|r| r.instances }.flatten
|
45
|
+
instance_params.map{|i| new( **i.to_h )}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def name
|
50
|
+
name_tag = tags.detect{|t| t[:key].to_s == "Name"}
|
51
|
+
return nil unless name_tag
|
52
|
+
name_tag[:value]
|
53
|
+
end
|
54
|
+
|
55
|
+
def reload
|
56
|
+
self.class.find( instance_id )
|
57
|
+
end
|
58
|
+
|
59
|
+
def create_image!( name: nil, description: nil, no_reboot: true, block_device_mappings: nil )
|
60
|
+
name ||= "#{self.name}-#{Time.current.strftime('%Y%m%d%H%M')}"
|
61
|
+
description ||= name
|
62
|
+
block_device_mappings ||= [{
|
63
|
+
device_name: '/dev/xvda',
|
64
|
+
ebs: {
|
65
|
+
volume_size: 8,
|
66
|
+
},
|
67
|
+
}]
|
68
|
+
self.class.client.create_image({
|
69
|
+
block_device_mappings: block_device_mappings,
|
70
|
+
description: description,
|
71
|
+
instance_id: instance_id,
|
72
|
+
name: name,
|
73
|
+
no_reboot: no_reboot,
|
74
|
+
})
|
75
|
+
end
|
76
|
+
|
77
|
+
# `waiter_name` can be checked with the command below.
|
78
|
+
# ActiveAws::Ec2.client.waiter_names
|
79
|
+
#
|
80
|
+
# Usage:
|
81
|
+
# ec2.wait_until :instance_running do |i|
|
82
|
+
# i.max_attempts = 5
|
83
|
+
# i.delay = 5
|
84
|
+
# end
|
85
|
+
def wait_until( waiter_name=:instance_running, &block )
|
86
|
+
self.class.client.wait_until(waiter_name, instance_ids: [instance_id], &block)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module ActiveAws
|
2
|
+
class Ec2Provisioner < Base
|
3
|
+
|
4
|
+
ATTRIBUTES = [
|
5
|
+
:instance_type,
|
6
|
+
:key_name,
|
7
|
+
:image_id,
|
8
|
+
:security_group_ids,
|
9
|
+
:subnet_id,
|
10
|
+
:user_data,
|
11
|
+
:block_device_mappings,
|
12
|
+
:tag_specifications,
|
13
|
+
:network_interfaces,
|
14
|
+
:launch_template,
|
15
|
+
]
|
16
|
+
|
17
|
+
attr_accessor :name
|
18
|
+
attr_accessor *ATTRIBUTES
|
19
|
+
attr_reader :tag_specs
|
20
|
+
|
21
|
+
def initialize( name, **params )
|
22
|
+
@name = name
|
23
|
+
|
24
|
+
ATTRIBUTES.each do |k|
|
25
|
+
method = "#{k}="
|
26
|
+
self.send(method, params[k]) if params[k].present?
|
27
|
+
end
|
28
|
+
|
29
|
+
yield( self ) if block_given?
|
30
|
+
end
|
31
|
+
|
32
|
+
def tag_specs
|
33
|
+
specifications = @tag_specifications || []
|
34
|
+
unless specifications.detect{|a| a[:resource_type] == "instance"}
|
35
|
+
specifications << {
|
36
|
+
resource_type: "instance",
|
37
|
+
tags: []
|
38
|
+
}
|
39
|
+
end
|
40
|
+
TagSpec.parse( specifications )
|
41
|
+
end
|
42
|
+
|
43
|
+
def exec!( extra_tags={} )
|
44
|
+
ec2 = ActiveAws::Ec2.find_by_name( name )
|
45
|
+
return ec2 if ec2
|
46
|
+
forced_exec!( extra_tags )
|
47
|
+
end
|
48
|
+
|
49
|
+
def forced_exec!( extra_tags={} )
|
50
|
+
client = ActiveAws::Ec2.client
|
51
|
+
response = client.run_instances( self.to_h )
|
52
|
+
ActiveAws::Ec2.new( **response.instances[0] )
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_h( extra_tags={} )
|
56
|
+
extra_tags = extra_tags.merge({ "Name" => self.name })
|
57
|
+
attrs = ATTRIBUTES.reduce({}) do |acc,k|
|
58
|
+
acc[k] = self.send( k )
|
59
|
+
acc
|
60
|
+
end
|
61
|
+
attrs = attrs.merge(
|
62
|
+
max_count: 1,
|
63
|
+
min_count: 1,
|
64
|
+
tag_specifications: tag_specs.map {|ts|
|
65
|
+
ts = if ts.resource_type == "instance"
|
66
|
+
ts.merge( extra_tags )
|
67
|
+
else
|
68
|
+
ts
|
69
|
+
end
|
70
|
+
ts.to_param
|
71
|
+
}
|
72
|
+
)
|
73
|
+
attrs
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module ActiveAws
|
2
|
+
class ElasticLoadBalancingV2 < Base
|
3
|
+
@attributes = [
|
4
|
+
:load_balancer_arn,
|
5
|
+
:dns_name, :canonical_hosted_zone_id, :created_time,
|
6
|
+
:load_balancer_name,
|
7
|
+
:scheme, :vpc_id,
|
8
|
+
:state, :type, :availability_zones,
|
9
|
+
:security_groups
|
10
|
+
]
|
11
|
+
|
12
|
+
attr_accessor *attributes
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def client
|
16
|
+
Aws::ElasticLoadBalancingV2::Client.new( **configure.default_client_params )
|
17
|
+
end
|
18
|
+
|
19
|
+
def find( load_balancer_arn )
|
20
|
+
response = client.describe_load_balancers({
|
21
|
+
load_balancer_arns: [load_balancer_arn],
|
22
|
+
})
|
23
|
+
return nil unless response
|
24
|
+
new( **response.load_balancers[0].to_h )
|
25
|
+
end
|
26
|
+
|
27
|
+
def find_by_name( name )
|
28
|
+
response = client.describe_load_balancers({
|
29
|
+
names: [name],
|
30
|
+
})
|
31
|
+
return nil unless response
|
32
|
+
new( **response.load_balancers[0].to_h )
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def name
|
37
|
+
load_balancer_name
|
38
|
+
end
|
39
|
+
|
40
|
+
def reload
|
41
|
+
self.class.find( load_balancer_arn )
|
42
|
+
end
|
43
|
+
|
44
|
+
def target_groups
|
45
|
+
TargetGroup.where( load_balancer_arn: load_balancer_arn )
|
46
|
+
end
|
47
|
+
|
48
|
+
# `waiter_name` can be checked with the command below.
|
49
|
+
# ActiveAws::ElasticLoadBalancingV2.client.waiter_names
|
50
|
+
#
|
51
|
+
# Usage:
|
52
|
+
# alb.wait_until :load_balancer_available do |i|
|
53
|
+
# i.max_attempts = 5
|
54
|
+
# i.delay = 5
|
55
|
+
# end
|
56
|
+
def wait_until( waiter_name=:load_balancer_available, &block )
|
57
|
+
self.class.client.wait_until(waiter_name, load_balancer_arns: [load_balancer_arn], &block)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module ActiveAws
|
2
|
+
class TagSpec
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def parse( tag_specifications )
|
6
|
+
tag_specifications.map do |spec|
|
7
|
+
spec = spec.deep_symbolize_keys
|
8
|
+
self.new(
|
9
|
+
spec[:resource_type],
|
10
|
+
spec[:tags].map{|t| [t[:key], t[:value]]}.to_h
|
11
|
+
)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_reader :resource_type, :tags
|
17
|
+
def initialize( resource_type, tags )
|
18
|
+
@resource_type = resource_type
|
19
|
+
@tags = tags
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_tag!( key, value )
|
23
|
+
@tags[key.to_s] = value
|
24
|
+
end
|
25
|
+
|
26
|
+
def merge( extra_tags )
|
27
|
+
self.class.new( resource_type, tags.merge(extra_tags) )
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_param
|
31
|
+
{
|
32
|
+
resource_type: resource_type,
|
33
|
+
tags: tags.map{|k,v| { key: k, value: v}}
|
34
|
+
}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module ActiveAws
|
2
|
+
class TargetGroup < Base
|
3
|
+
|
4
|
+
@attributes = [
|
5
|
+
:target_group_arn,
|
6
|
+
:target_group_name,
|
7
|
+
:protocol, :port, :vpc_id,
|
8
|
+
:health_check_protocol, :health_check_port,
|
9
|
+
:health_check_interval_seconds, :health_check_timeout_seconds,
|
10
|
+
:healthy_threshold_count, :unhealthy_threshold_count,
|
11
|
+
:health_check_path, :matcher,
|
12
|
+
:load_balancer_arns, :load_balancer_arns,
|
13
|
+
:target_type,
|
14
|
+
]
|
15
|
+
|
16
|
+
attr_accessor *attributes
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def client
|
20
|
+
Aws::ElasticLoadBalancingV2::Client.new( **configure.default_client_params )
|
21
|
+
end
|
22
|
+
|
23
|
+
def find( target_group_arn )
|
24
|
+
response = client.describe_target_groups({
|
25
|
+
target_group_arns: [target_group_arn],
|
26
|
+
})
|
27
|
+
return nil unless response
|
28
|
+
new( **response.target_groups[0].to_h )
|
29
|
+
end
|
30
|
+
|
31
|
+
def find_by_name( name )
|
32
|
+
response = client.describe_target_groups({
|
33
|
+
names: [name],
|
34
|
+
})
|
35
|
+
return nil unless response
|
36
|
+
new( **response.target_groups[0].to_h )
|
37
|
+
end
|
38
|
+
|
39
|
+
# Usage:
|
40
|
+
# where( load_balancer_arn: ["xxxx"] )
|
41
|
+
def where( **args )
|
42
|
+
filter_params = args.map{|k, v| { name: k, values: Array.wrap(v) }}
|
43
|
+
response = client.describe_target_groups( args )
|
44
|
+
response.target_groups.map{|i| new( **i.to_h )}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def name
|
49
|
+
target_group_name
|
50
|
+
end
|
51
|
+
|
52
|
+
def reload
|
53
|
+
self.class.find( target_group_arn )
|
54
|
+
end
|
55
|
+
|
56
|
+
def health_descriptions
|
57
|
+
response = self.class.client.describe_target_health({
|
58
|
+
target_group_arn: target_group_arn
|
59
|
+
})
|
60
|
+
response.target_health_descriptions
|
61
|
+
end
|
62
|
+
|
63
|
+
def target_ids
|
64
|
+
health_descriptions.map{|a| a.target.id }
|
65
|
+
end
|
66
|
+
|
67
|
+
def register!( ids )
|
68
|
+
response = self.class.client.register_targets({
|
69
|
+
target_group_arn: target_group_arn,
|
70
|
+
targets: ids.map{|a| { id: a }},
|
71
|
+
})
|
72
|
+
end
|
73
|
+
|
74
|
+
def deregister!( ids )
|
75
|
+
response = self.class.client.deregister_targets({
|
76
|
+
target_group_arn: target_group_arn,
|
77
|
+
targets: ids.map{|a| { id: a }},
|
78
|
+
})
|
79
|
+
end
|
80
|
+
|
81
|
+
def set!( ids )
|
82
|
+
present_ids = target_ids
|
83
|
+
register_ids = ids.select{|a| ! present_ids.include?( a ) }
|
84
|
+
deregister_ids = present_ids.select{|a| ! ids.include?( a ) }
|
85
|
+
# pp register_ids
|
86
|
+
# pp deregister_ids
|
87
|
+
register!( register_ids ) if register_ids.present?
|
88
|
+
deregister!( deregister_ids ) if deregister_ids.present?
|
89
|
+
target_ids
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/lib/active_aws.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "active_aws/version"
|
2
|
+
|
3
|
+
require 'aws-sdk'
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_support/core_ext'
|
6
|
+
|
7
|
+
require_relative 'active_aws/configure'
|
8
|
+
require_relative 'active_aws/base'
|
9
|
+
require_relative 'active_aws/tag_spec'
|
10
|
+
require_relative 'active_aws/ec2'
|
11
|
+
require_relative 'active_aws/ec2_provisioner'
|
12
|
+
require_relative 'active_aws/cloud_formation_provisioner'
|
13
|
+
require_relative 'active_aws/elastic_load_balancing_v2'
|
14
|
+
require_relative 'active_aws/target_group'
|
15
|
+
|
16
|
+
module ActiveAws
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activeaws
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- metheglin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-11 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.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
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: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: aws-sdk
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- pigmybank@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- active_aws.gemspec
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/active_aws.rb
|
85
|
+
- lib/active_aws/base.rb
|
86
|
+
- lib/active_aws/cloud_formation_provisioner.rb
|
87
|
+
- lib/active_aws/configure.rb
|
88
|
+
- lib/active_aws/ec2.rb
|
89
|
+
- lib/active_aws/ec2_provisioner.rb
|
90
|
+
- lib/active_aws/elastic_load_balancing_v2.rb
|
91
|
+
- lib/active_aws/tag_spec.rb
|
92
|
+
- lib/active_aws/target_group.rb
|
93
|
+
- lib/active_aws/version.rb
|
94
|
+
homepage: https://github.com/metheglin/activeaws
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.6.11
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Managing aws resources with active way.
|
118
|
+
test_files: []
|