hocho-ec2 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 +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hocho-ec2.gemspec +29 -0
- data/lib/hocho-ec2.rb +1 -0
- data/lib/hocho-ec2/version.rb +3 -0
- data/lib/hocho/inventory_providers/ec2.rb +138 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0f244b10a1aa987e5c40e1c9a718ed7c8d84fe43
|
4
|
+
data.tar.gz: 552b998d5da7fd6780b1033806bda568be5668ff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e0238555988a86c2b1b985db535a9ae435cece9f877bbd4d05f90289df172cefd55125a95b3fd5714a27d6fb8ca3433441e923f4e5e167ee7410045bbace3b0c
|
7
|
+
data.tar.gz: 0daa74f6c680c04f6187d950cfe4c351354222b25fb341d421c72e88687403c08da81991c17ddc4e143c4ed164e1922f18fca8b343e229ced7cbfe4b71a2d0fe
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Sorah Fukumori
|
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,39 @@
|
|
1
|
+
# Hocho::Ec2
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hocho/ec2`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'hocho-ec2'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install hocho-ec2
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sorah/hocho-ec2.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hocho/ec2"
|
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
data/hocho-ec2.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "hocho-ec2/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hocho-ec2"
|
8
|
+
spec.version = HochoEc2::VERSION
|
9
|
+
spec.authors = ["Sorah Fukumori"]
|
10
|
+
spec.email = ["sorah@cookpad.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Hocho gem plugin to discover hosts in EC2}
|
13
|
+
spec.homepage = "https://github.com/sorah/hocho-ec2"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
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_dependency "aws-sdk-ec2"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
end
|
data/lib/hocho-ec2.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "hocho-ec2/version"
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'hocho/inventory_providers/base'
|
2
|
+
require 'hocho/host'
|
3
|
+
require 'yaml'
|
4
|
+
require 'erb'
|
5
|
+
|
6
|
+
require 'aws-sdk-ec2'
|
7
|
+
|
8
|
+
module Hocho
|
9
|
+
module InventoryProviders
|
10
|
+
class Ec2 < Base
|
11
|
+
module TemplateHelper
|
12
|
+
def tag(source, name, default = nil)
|
13
|
+
t = source.tags.find{ |_| _.key == name }
|
14
|
+
t ? t.value : default
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
RunlistTemplate = Struct.new(:instance, :vpc) do
|
19
|
+
include TemplateHelper
|
20
|
+
end
|
21
|
+
class HostnameTemplate
|
22
|
+
def self.erb; @erb; end
|
23
|
+
def self.erb=(x); @erb = x; end
|
24
|
+
|
25
|
+
include TemplateHelper
|
26
|
+
|
27
|
+
def initialize(instance, vpc)
|
28
|
+
@instance = instance
|
29
|
+
@vpc = vpc
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :instance, :vpc
|
33
|
+
|
34
|
+
def result()
|
35
|
+
self.class.erb.result(binding)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(region:, filters: nil, hostname_template: "<%= instance.private_dns_name %>", runlist_template: nil, cache_path: nil, cache_duration: 3600, cache_version: nil)
|
40
|
+
@region = region
|
41
|
+
@filters = filters
|
42
|
+
@cache_path = cache_path
|
43
|
+
@cache_duration = cache_duration&.to_i
|
44
|
+
@cache_version = cache_version
|
45
|
+
@hostname_template = Class.new(HostnameTemplate) do |c|
|
46
|
+
c.erb = ERB.new(hostname_template, trim_mode: '-')
|
47
|
+
end
|
48
|
+
@runlist_template = eval("Class.new(RunlistTemplate){ def result; #{runlist_template}\nend; }")
|
49
|
+
end
|
50
|
+
|
51
|
+
attr_reader :region, :filters, :cache_path, :cache_duration, :cache_version, :hostname_template, :runlist_template
|
52
|
+
|
53
|
+
def ec2
|
54
|
+
@ec2 ||= Aws::EC2::Client.new(region: region)
|
55
|
+
end
|
56
|
+
|
57
|
+
def fetch_hosts
|
58
|
+
vpcs = ec2.describe_vpcs().flat_map do |page|
|
59
|
+
page.vpcs.map do |vpc|
|
60
|
+
[vpc.vpc_id, vpc]
|
61
|
+
end
|
62
|
+
end.to_h
|
63
|
+
ec2.describe_instances(filters: filters).flat_map do |page|
|
64
|
+
page.reservations.flat_map do |reservation|
|
65
|
+
reservation.instances.map do |instance|
|
66
|
+
vpc = vpcs[instance.vpc_id]
|
67
|
+
fetch_instance(instance, vpc)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def cache_enabled?
|
74
|
+
@cache_path && @cache_duration
|
75
|
+
end
|
76
|
+
|
77
|
+
def cache!
|
78
|
+
return yield unless cache_enabled?
|
79
|
+
if ::File.exist?(cache_path)
|
80
|
+
@cache = YAML.load_file(cache_path)
|
81
|
+
end
|
82
|
+
@cache = nil if !@cache&.key?(:ts) || (Time.now - @cache[:ts]) > cache_duration
|
83
|
+
@cache = nil if cache_version && @cache[:user_version] != cache_version
|
84
|
+
|
85
|
+
unless @cache
|
86
|
+
@cache = {ts: Time.now, user_version: cache_version, content: yield}
|
87
|
+
FileUtils.mkdir_p ::File.dirname(cache_path)
|
88
|
+
::File.write cache_path, YAML.dump(@cache)
|
89
|
+
end
|
90
|
+
@cache[:content]
|
91
|
+
end
|
92
|
+
|
93
|
+
def hosts
|
94
|
+
@hosts ||= cache! { fetch_hosts }.map do |host_data|
|
95
|
+
Host.new(
|
96
|
+
host_data.fetch(:name),
|
97
|
+
providers: self.class,
|
98
|
+
properties: host_data[:properties],
|
99
|
+
tags: host_data[:tags],
|
100
|
+
ssh_options: host_data[:ssh_options],
|
101
|
+
)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def fetch_instance(instance, vpc)
|
108
|
+
tags = {
|
109
|
+
'ec2.instance-id' => instance.instance_id,
|
110
|
+
'ec2.iam-instance-profile' => instance.iam_instance_profile,
|
111
|
+
'ec2.vpc-id' => instance.vpc_id,
|
112
|
+
}
|
113
|
+
{'vpc-tags' => vpc.tags, 'tags' => instance.tags}.each do |prefix, aws_tags|
|
114
|
+
aws_tags.each do |tag|
|
115
|
+
tags["ec2.#{prefix}.#{tag.key.downcase}"] = tag.value
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
ec2_attribute = instance.to_h
|
120
|
+
vpc_attribute = vpc.to_h
|
121
|
+
[ec2_attribute, vpc_attribute].each do |attrs|
|
122
|
+
attrs[:tags] = attrs.fetch(:tags, []).map { |_| [_.fetch(:key), _.fetch(:value)] }.to_h
|
123
|
+
end
|
124
|
+
|
125
|
+
properties = {
|
126
|
+
run_list: runlist_template.new(instance, vpc).result(),
|
127
|
+
attributes: {hocho_ec2: ec2_attribute, hocho_vpc: vpc_attribute,},
|
128
|
+
}
|
129
|
+
{
|
130
|
+
name: hostname_template.new(instance, vpc).result(),
|
131
|
+
properties: properties,
|
132
|
+
tags: tags,
|
133
|
+
}
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hocho-ec2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sorah Fukumori
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk-ec2
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
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: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- sorah@cookpad.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- hocho-ec2.gemspec
|
86
|
+
- lib/hocho-ec2.rb
|
87
|
+
- lib/hocho-ec2/version.rb
|
88
|
+
- lib/hocho/inventory_providers/ec2.rb
|
89
|
+
homepage: https://github.com/sorah/hocho-ec2
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.6.13
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Hocho gem plugin to discover hosts in EC2
|
113
|
+
test_files: []
|