digital_ocean_inventory 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 +18 -0
- data/.rspec +3 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +71 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/digital_ocean_inventory.gemspec +29 -0
- data/exe/digital_ocean_inventory +5 -0
- data/lib/digital_ocean_inventory.rb +55 -0
- data/lib/digital_ocean_inventory/collector.rb +121 -0
- data/lib/digital_ocean_inventory/config.rb +86 -0
- data/lib/digital_ocean_inventory/group.rb +25 -0
- data/lib/digital_ocean_inventory/host.rb +61 -0
- data/lib/digital_ocean_inventory/inventory.rb +57 -0
- data/lib/digital_ocean_inventory/serializer.rb +53 -0
- data/lib/digital_ocean_inventory/version.rb +3 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 611b2b70cd34ba50a0ace0402f45605c766665c448d5be96384ea0561974d71f
|
4
|
+
data.tar.gz: e22b96a6616a01b1ab9938cebd2a046482629cf66e356bcfe00fdd5936e2ba38
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a256abc0d6eab4cefea9af3281a17591004f8ba5f7cc9340d314e99a6503bbc0305273052b2ace8740004f42820283d0a1daf7ab2b9bf804142a17ff2da7a0f8
|
7
|
+
data.tar.gz: f3988e39f4f240dfbf42e65172a6fc16e608294a0a8bcfec00c29fef1f7a47559e62d1f60674e01a0956ae711662fcefb0c0db32b9d8e31424ce501f0c800556
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
digital_ocean_inventory (0.1.0)
|
5
|
+
droplet_kit
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
addressable (2.7.0)
|
11
|
+
public_suffix (>= 2.0.2, < 5.0)
|
12
|
+
axiom-types (0.1.1)
|
13
|
+
descendants_tracker (~> 0.0.4)
|
14
|
+
ice_nine (~> 0.11.0)
|
15
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
16
|
+
coderay (1.1.3)
|
17
|
+
coercible (1.0.0)
|
18
|
+
descendants_tracker (~> 0.0.1)
|
19
|
+
descendants_tracker (0.0.4)
|
20
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
21
|
+
diff-lcs (1.4.4)
|
22
|
+
droplet_kit (3.8.0)
|
23
|
+
faraday (>= 0.15)
|
24
|
+
kartograph (~> 0.2.3)
|
25
|
+
resource_kit (~> 0.1.5)
|
26
|
+
virtus (~> 1.0.3)
|
27
|
+
equalizer (0.0.11)
|
28
|
+
faraday (1.0.1)
|
29
|
+
multipart-post (>= 1.2, < 3)
|
30
|
+
ice_nine (0.11.2)
|
31
|
+
kartograph (0.2.8)
|
32
|
+
method_source (1.0.0)
|
33
|
+
multipart-post (2.1.1)
|
34
|
+
pry (0.13.1)
|
35
|
+
coderay (~> 1.1)
|
36
|
+
method_source (~> 1.0)
|
37
|
+
public_suffix (4.0.5)
|
38
|
+
rake (12.3.3)
|
39
|
+
resource_kit (0.1.7)
|
40
|
+
addressable (>= 2.3.6, < 3.0.0)
|
41
|
+
rspec (3.9.0)
|
42
|
+
rspec-core (~> 3.9.0)
|
43
|
+
rspec-expectations (~> 3.9.0)
|
44
|
+
rspec-mocks (~> 3.9.0)
|
45
|
+
rspec-core (3.9.2)
|
46
|
+
rspec-support (~> 3.9.3)
|
47
|
+
rspec-expectations (3.9.2)
|
48
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
49
|
+
rspec-support (~> 3.9.0)
|
50
|
+
rspec-mocks (3.9.1)
|
51
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
52
|
+
rspec-support (~> 3.9.0)
|
53
|
+
rspec-support (3.9.3)
|
54
|
+
thread_safe (0.3.6)
|
55
|
+
virtus (1.0.5)
|
56
|
+
axiom-types (~> 0.1)
|
57
|
+
coercible (~> 1.0)
|
58
|
+
descendants_tracker (~> 0.0, >= 0.0.3)
|
59
|
+
equalizer (~> 0.0, >= 0.0.9)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
digital_ocean_inventory!
|
66
|
+
pry
|
67
|
+
rake (~> 12.0)
|
68
|
+
rspec (~> 3.0)
|
69
|
+
|
70
|
+
BUNDLED WITH
|
71
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 TODO: Write your name
|
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,44 @@
|
|
1
|
+
# DigitalOceanInventory
|
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/digital_ocean_inventory`. 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 'digital_ocean_inventory'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install digital_ocean_inventory
|
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/[USERNAME]/digital_ocean_inventory. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/digital_ocean_inventory/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
41
|
+
|
42
|
+
## Code of Conduct
|
43
|
+
|
44
|
+
Everyone interacting in the DigitalOceanInventory project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/digital_ocean_inventory/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "digital_ocean_inventory"
|
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 "pry"
|
14
|
+
Pry.start
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'lib/digital_ocean_inventory/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "digital_ocean_inventory"
|
5
|
+
spec.version = DigitalOceanInventory::VERSION
|
6
|
+
spec.authors = ["Nick Hurst"]
|
7
|
+
spec.email = ["me@nickhurst.dev"]
|
8
|
+
|
9
|
+
spec.summary = %q{DigitalOcean Ansible Dynamic Inventory}
|
10
|
+
spec.description = %q{DigitalOcean Ansible Dynamic Inventory For Ansible Dynamic Inventory}
|
11
|
+
spec.homepage = "https://gitlab.com/swarm/ansible/digital_ocean_inventory"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://gitlab.com/swarm/ansible/digital_ocean_inventory"
|
17
|
+
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_dependency "droplet_kit"
|
27
|
+
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "digital_ocean_inventory/version"
|
2
|
+
require "digital_ocean_inventory/config"
|
3
|
+
require "digital_ocean_inventory/inventory"
|
4
|
+
require "digital_ocean_inventory/collector"
|
5
|
+
require "digital_ocean_inventory/serializer"
|
6
|
+
|
7
|
+
module DigitalOceanInventory
|
8
|
+
class Error < StandardError; end
|
9
|
+
|
10
|
+
def self.call(argv)
|
11
|
+
config = Config.new
|
12
|
+
|
13
|
+
if cache_valid? config
|
14
|
+
json = load_cached config
|
15
|
+
else
|
16
|
+
inventory = build_inventory config
|
17
|
+
serializer = Serializer.build config
|
18
|
+
|
19
|
+
json = serializer.call inventory
|
20
|
+
|
21
|
+
cache_inventory config, json
|
22
|
+
end
|
23
|
+
|
24
|
+
puts json
|
25
|
+
|
26
|
+
json
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.cache_valid?(config)
|
30
|
+
return false unless File.exists? config.cache_file
|
31
|
+
|
32
|
+
max_age = config.cache_max_age
|
33
|
+
file_stat = File.stat config.cache_file
|
34
|
+
cache_age = Time.now.to_i - file_stat.mtime.to_i
|
35
|
+
|
36
|
+
max_age > cache_age
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.load_cached(config)
|
40
|
+
File.read config.cache_file
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.build_inventory(config)
|
44
|
+
inventory = Inventory.build config
|
45
|
+
collector = Collector.build config
|
46
|
+
|
47
|
+
collector.call inventory
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.cache_inventory(config, json)
|
51
|
+
File.open config.cache_file, "w+" do |f|
|
52
|
+
f.write json
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require "droplet_kit"
|
2
|
+
|
3
|
+
module DigitalOceanInventory
|
4
|
+
class Collector
|
5
|
+
attr_reader :config, :client
|
6
|
+
|
7
|
+
def self.build(config)
|
8
|
+
new config
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(config)
|
12
|
+
@config = config
|
13
|
+
@client = config.client
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(inventory)
|
17
|
+
droplets = get_droplets
|
18
|
+
.select { |d| select_droplet? d }
|
19
|
+
.reject { |d| ignore_droplet? d }
|
20
|
+
|
21
|
+
droplets.each do |droplet|
|
22
|
+
groups = droplet_groups droplet
|
23
|
+
groups.each { |g| inventory.add_group g }
|
24
|
+
|
25
|
+
inventory.add_host droplet, groups
|
26
|
+
end
|
27
|
+
|
28
|
+
inventory
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def project
|
34
|
+
if !config.project || config.project == "default"
|
35
|
+
client.projects.find_default
|
36
|
+
else
|
37
|
+
proj = client.projects.all.find { |p| p.name == config.project }
|
38
|
+
raise "Couldn't find project with name #{config.project}" unless proj
|
39
|
+
|
40
|
+
proj
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_droplets
|
45
|
+
resources = client.projects.list_resources id: project.id
|
46
|
+
resources.select { |r| r.urn.include? "do:droplet" }.map do |r|
|
47
|
+
ns, type, id = r.urn.split ":"
|
48
|
+
client.droplets.find id: id
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def select_droplet?(droplet)
|
53
|
+
return true if config.select_tags.empty?
|
54
|
+
|
55
|
+
tags = droplet.tags || []
|
56
|
+
config.select_tags.any? { |t| tags&.include? t }
|
57
|
+
end
|
58
|
+
|
59
|
+
def ignore_droplet?(droplet)
|
60
|
+
ignore_tags = config.ignore_tags
|
61
|
+
ignore_hosts = config.ignore_hosts
|
62
|
+
|
63
|
+
if ignore_tags.empty? && ignore_hosts.empty?
|
64
|
+
return false
|
65
|
+
end
|
66
|
+
|
67
|
+
return true if ignore_hosts.include? droplet.name
|
68
|
+
|
69
|
+
tags = droplet.tags || []
|
70
|
+
ignore_tags.any? { |t| tags.include? t }
|
71
|
+
end
|
72
|
+
|
73
|
+
def droplet_groups(droplet)
|
74
|
+
groups = []
|
75
|
+
groups << droplet.region.slug if config.group_by_region
|
76
|
+
groups << droplet_vpc(droplet)
|
77
|
+
droplet.tags&.each { |t| groups << t } if config.group_by_tag
|
78
|
+
groups.uniq.map { |g| g.gsub('-', '_') }
|
79
|
+
end
|
80
|
+
|
81
|
+
def droplet_vpc(droplet)
|
82
|
+
id = droplet.vpc_uuid
|
83
|
+
vpc = config.client.vpcs.find id: id
|
84
|
+
vpc.name
|
85
|
+
end
|
86
|
+
# def add_droplets
|
87
|
+
# project_droplets.each do |droplet|
|
88
|
+
# name = droplet.name
|
89
|
+
# groups = ["digitalocean"]
|
90
|
+
|
91
|
+
# if droplet.region.name.include? "nyc"
|
92
|
+
# groups << "usa"
|
93
|
+
# groups << "nyc"
|
94
|
+
# elsif droplet.region.name.include? "sfo"
|
95
|
+
# groups << "usa"
|
96
|
+
# groups << "sfo"
|
97
|
+
# end
|
98
|
+
|
99
|
+
# tags = droplet.tags || []
|
100
|
+
# groups = groups.concat tags
|
101
|
+
|
102
|
+
# groups << droplet.image.distribution.downcase
|
103
|
+
# # groups << droplet.image.name
|
104
|
+
|
105
|
+
# public_net = droplet.networks.v4.find { |n| n.type == "public" }
|
106
|
+
# private_net = droplet.networks.v4.find { |n| n.type == "private" }
|
107
|
+
|
108
|
+
# inventory.host name, groups: groups, vars: dns_results[:vars].merge({
|
109
|
+
# public_ip: public_net.ip_address,
|
110
|
+
# private_ip: private_net.ip_address,
|
111
|
+
# droplet_id: droplet.id,
|
112
|
+
# vpc_uuid: droplet.vpc_uuid,
|
113
|
+
# digitalocean_tags: tags,
|
114
|
+
# internal_dns_records: dns_results[:dns]
|
115
|
+
# })
|
116
|
+
# end
|
117
|
+
# end
|
118
|
+
|
119
|
+
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
module DigitalOceanInventory
|
5
|
+
class Config
|
6
|
+
class MissingAccessTokenError < StandardError; end
|
7
|
+
|
8
|
+
CONFIG_FILE_PATHS = %w[
|
9
|
+
./digitalocean.yml
|
10
|
+
~/.ansible/digitalocean.yml
|
11
|
+
/etc/ansible/digitalocean.yml
|
12
|
+
]
|
13
|
+
|
14
|
+
attr_reader :token,
|
15
|
+
:client,
|
16
|
+
:project,
|
17
|
+
:select_tags,
|
18
|
+
:ignore_tags,
|
19
|
+
:ignore_hosts,
|
20
|
+
:group_by_region,
|
21
|
+
:group_by_tag,
|
22
|
+
:cache_path,
|
23
|
+
:cache_max_age
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
conf = load_from_file.merge load_from_env
|
27
|
+
|
28
|
+
@token = conf[:access_token]
|
29
|
+
raise MissingAccessTokenError unless @token
|
30
|
+
|
31
|
+
@client = DropletKit::Client.new access_token: @token
|
32
|
+
|
33
|
+
@project = conf.fetch :project, "default"
|
34
|
+
@select_tags = conf.fetch :select_tags, []
|
35
|
+
@ignore_tags = conf.fetch :ignore_tags, []
|
36
|
+
@ignore_hosts = conf.fetch :ignore_hosts, []
|
37
|
+
@group_by_region = conf.fetch :group_by_region, true
|
38
|
+
@group_by_tag = conf.fetch :group_by_tag, true
|
39
|
+
@cache_path = conf.fetch :cache_path, "."
|
40
|
+
@cache_max_age = conf.fetch :cache_max_age, 900
|
41
|
+
end
|
42
|
+
|
43
|
+
def cache_file
|
44
|
+
"#{cache_path}/.ansible_digital_ocean_inventory_cache.json"
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def load_from_env
|
50
|
+
token = ENV.fetch "DO_ACCESS_TOKEN", nil
|
51
|
+
project = ENV.fetch "DO_INVENTORY_PROJECT", nil
|
52
|
+
select_tags = ENV.fetch "DO_INVENTORY_SELECT_TAGS", nil
|
53
|
+
select_tags = select_tags.split "," if select_tags
|
54
|
+
ignore_tags = ENV.fetch "DO_INVENTORY_IGNORE_TAGS", nil
|
55
|
+
ignore_tags = ignore_tags.split "," if ignore_tags
|
56
|
+
ignore_hosts = ENV.fetch "DO_INVENTORY_IGNORE_HOSTS", nil
|
57
|
+
ignore_hosts = ignore_hosts.split "," if ignore_hosts
|
58
|
+
group_by_region = ENV.fetch "DO_INVENTORY_GROUP_BY_REGION", nil
|
59
|
+
group_by_tag = ENV.fetch "DO_INVENTORY_GROUP_BY_TAG", nil
|
60
|
+
cache_path = ENV.fetch "DO_INVENTORY_CACHE_PATH", nil
|
61
|
+
cache_max_age = ENV.fetch "DO_INVENTORY_CACHE_MAX_AGE", nil
|
62
|
+
|
63
|
+
|
64
|
+
OpenStruct.new.tap do |c|
|
65
|
+
c.access_token = token if token
|
66
|
+
c.project = project if project
|
67
|
+
c.select_tags = select_tags if select_tags
|
68
|
+
c.ignore_tags = ignore_tags if ignore_tags
|
69
|
+
c.ignore_hosts = ignore_hosts if ignore_hosts
|
70
|
+
c.group_by_region = group_by_region unless group_by_region.nil?
|
71
|
+
c.group_by_tag = group_by_tag unless group_by_tag.nil?
|
72
|
+
c.cache_path = cache_path unless cache_path.nil?
|
73
|
+
c.cache_max_age = cache_max_age unless cache_max_age.nil?
|
74
|
+
end.to_h
|
75
|
+
end
|
76
|
+
|
77
|
+
def load_from_file
|
78
|
+
file = ENV.fetch "DO_INVENTORY_CONFIG_FILE", nil
|
79
|
+
file ||= CONFIG_FILE_PATHS.find { |p| File.exists? p }
|
80
|
+
|
81
|
+
return {} unless file
|
82
|
+
|
83
|
+
YAML.load_file file
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module DigitalOceanInventory
|
2
|
+
class Group
|
3
|
+
attr_reader :name, :hosts, :config
|
4
|
+
|
5
|
+
def self.build(opts = {})
|
6
|
+
name = opts[:name]
|
7
|
+
config = opts[:config]
|
8
|
+
hosts = opts[:hosts] || []
|
9
|
+
|
10
|
+
new name, config, hosts
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(name, config, hosts = [])
|
14
|
+
@name = name
|
15
|
+
@config = config
|
16
|
+
@hosts = hosts
|
17
|
+
end
|
18
|
+
|
19
|
+
def <<(host)
|
20
|
+
return host if hosts.include? host.name
|
21
|
+
|
22
|
+
@hosts << host
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
module DigitalOceanInventory
|
4
|
+
class Host
|
5
|
+
attr_reader :name, :droplet, :config
|
6
|
+
|
7
|
+
def self.build(opts = {})
|
8
|
+
config = opts[:config]
|
9
|
+
droplet = opts[:droplet]
|
10
|
+
|
11
|
+
new droplet.name, droplet, config
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(name, droplet, config)
|
15
|
+
@name = name
|
16
|
+
@droplet = droplet
|
17
|
+
@config = config
|
18
|
+
end
|
19
|
+
|
20
|
+
def vars
|
21
|
+
return @_vars if defined? @_vars
|
22
|
+
|
23
|
+
@_vars = host_vars.to_h
|
24
|
+
@_vars
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def droplet_ip(type = :public)
|
30
|
+
droplet.networks.v4.find { |n| n.type == type.to_s }&.ip_address
|
31
|
+
end
|
32
|
+
|
33
|
+
def droplet_vpc
|
34
|
+
id = droplet.vpc_uuid
|
35
|
+
config.client.vpcs.find id: id
|
36
|
+
end
|
37
|
+
|
38
|
+
def droplet_volume_names
|
39
|
+
ids = droplet.volume_ids || []
|
40
|
+
ids.map { |id| config.client.volumes.find id: id }.map &:name
|
41
|
+
end
|
42
|
+
|
43
|
+
def host_vars
|
44
|
+
vpc = droplet_vpc
|
45
|
+
|
46
|
+
OpenStruct.new.tap do |v|
|
47
|
+
v.public_ip = droplet.public_ip
|
48
|
+
v.private_ip = droplet.private_ip
|
49
|
+
v.droplet_id = droplet.id
|
50
|
+
v.vpc_id = vpc.id
|
51
|
+
v.vpc_name = vpc.name
|
52
|
+
v.vpc_subnet = vpc.ip_range
|
53
|
+
v.droplet_tags = droplet.tags || []
|
54
|
+
v.volumes_ids = droplet.volume_ids || []
|
55
|
+
v.volumes_names = droplet_volume_names
|
56
|
+
v.image_name = droplet.image.name
|
57
|
+
v.image_dist = droplet.image.distribution
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
require_relative "./host"
|
3
|
+
require_relative "./group"
|
4
|
+
|
5
|
+
module DigitalOceanInventory
|
6
|
+
class Inventory
|
7
|
+
attr_reader :config, :groups, :hosts
|
8
|
+
|
9
|
+
def self.build(config)
|
10
|
+
all = Group.build name: "all", config: config
|
11
|
+
|
12
|
+
groups = OpenStruct.new all: all
|
13
|
+
hosts = OpenStruct.new
|
14
|
+
|
15
|
+
new config, groups, hosts
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(config, groups, hosts)
|
19
|
+
@config = config
|
20
|
+
@groups = groups
|
21
|
+
@hosts = hosts
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_group(name)
|
25
|
+
return groups[name] if groups[name]
|
26
|
+
|
27
|
+
group = Group.build name: name, config: config
|
28
|
+
@groups[name] = group
|
29
|
+
|
30
|
+
group
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_host(droplet, host_groups = [])
|
34
|
+
name = droplet.name
|
35
|
+
|
36
|
+
if hosts[name]
|
37
|
+
host = hosts[name]
|
38
|
+
else
|
39
|
+
host = Host.build droplet: droplet, config: config
|
40
|
+
hosts[name] = host
|
41
|
+
end
|
42
|
+
|
43
|
+
groups.all << host
|
44
|
+
|
45
|
+
host_groups.each do |g|
|
46
|
+
if groups[g]
|
47
|
+
groups[g] << host
|
48
|
+
else
|
49
|
+
group = add_group g
|
50
|
+
group << host
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
host
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
module DigitalOceanInventory
|
4
|
+
class Serializer
|
5
|
+
attr_reader :config
|
6
|
+
|
7
|
+
def self.build(config)
|
8
|
+
new config
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(config)
|
12
|
+
@config = config
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(inventory)
|
16
|
+
meta = { _meta: { hostvars: hostvars(inventory) } }
|
17
|
+
data = groups(inventory).merge meta
|
18
|
+
|
19
|
+
JSON.dump data
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def groups(inventory)
|
25
|
+
hash = {}
|
26
|
+
inventory.groups.to_h.each do |name, group|
|
27
|
+
hash[name] = {
|
28
|
+
vars: groupvars(group),
|
29
|
+
hosts: group.hosts.map { |h| h.droplet.name }.uniq,
|
30
|
+
children: []
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
hash
|
35
|
+
end
|
36
|
+
|
37
|
+
def groupvars(group)
|
38
|
+
return {} unless group.name == "all"
|
39
|
+
|
40
|
+
hosts = group.hosts.map { |h| h.vars }
|
41
|
+
|
42
|
+
{
|
43
|
+
public_ips: hosts.map { |v| v[:public_ip] }.uniq,
|
44
|
+
private_ips: hosts.map { |v| v[:private_ip] }.uniq,
|
45
|
+
vpc_subnets: hosts.map { |v| v[:vpc_subnet] }.uniq
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
def hostvars(inventory)
|
50
|
+
inventory.hosts.to_h.values.map { |h| [h.droplet.name, h.vars] }.to_h
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: digital_ocean_inventory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Hurst
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: droplet_kit
|
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: pry
|
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
|
+
description: DigitalOcean Ansible Dynamic Inventory For Ansible Dynamic Inventory
|
42
|
+
email:
|
43
|
+
- me@nickhurst.dev
|
44
|
+
executables:
|
45
|
+
- digital_ocean_inventory
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- bin/console
|
57
|
+
- bin/setup
|
58
|
+
- digital_ocean_inventory.gemspec
|
59
|
+
- exe/digital_ocean_inventory
|
60
|
+
- lib/digital_ocean_inventory.rb
|
61
|
+
- lib/digital_ocean_inventory/collector.rb
|
62
|
+
- lib/digital_ocean_inventory/config.rb
|
63
|
+
- lib/digital_ocean_inventory/group.rb
|
64
|
+
- lib/digital_ocean_inventory/host.rb
|
65
|
+
- lib/digital_ocean_inventory/inventory.rb
|
66
|
+
- lib/digital_ocean_inventory/serializer.rb
|
67
|
+
- lib/digital_ocean_inventory/version.rb
|
68
|
+
homepage: https://gitlab.com/swarm/ansible/digital_ocean_inventory
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata:
|
72
|
+
homepage_uri: https://gitlab.com/swarm/ansible/digital_ocean_inventory
|
73
|
+
source_code_uri: https://gitlab.com/swarm/ansible/digital_ocean_inventory
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 2.3.0
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubygems_version: 3.1.2
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: DigitalOcean Ansible Dynamic Inventory
|
93
|
+
test_files: []
|