capistrano-aws-hosts 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e77ffd50a4ac9dc978149cf4e1d1273473497970
4
+ data.tar.gz: 2d3a4844ea7b9b098231bc5c4e035f813a7c2a6c
5
+ SHA512:
6
+ metadata.gz: 2bdefb9729bd4308f1cda3250d500bcb0670b127e951ea1521665f9f54d6bffa176aab06190b976e4e0c03a5a75548be99dd49f85bf23e49f27b15d2e0566c2b
7
+ data.tar.gz: 7e387afc6a27d2a8df491d409b0aa5e7e1c35ec59185ba346ee52fdfef9129660b930c8c73d6e0dd00d61f59dad83d29aca3b105065314ca40a7fa2b8d00873f
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in capistrano-aws-hosts.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Matthew Savage
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,62 @@
1
+ # Capistrano::Aws::Hosts
2
+
3
+ Provides an interface to retrieve host inventory from AWS for deployment to a dynamic range of targets.
4
+
5
+ This depends on EC2 hosts being tagged with the Role tag (e.g. Role=Dashboard)
6
+
7
+ This plugin is for Capistrano 3 - sorry, no love for version 2...
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ group :development do
15
+ gem 'capistrano-aws-hosts'
16
+ end
17
+ ```
18
+
19
+ And then add the following to your Capfile:
20
+
21
+ ```ruby
22
+ require 'capistrano/aws/hosts'
23
+ ```
24
+
25
+ Add defaults to your config/deploy.rb file:
26
+
27
+ ```ruby
28
+ set :aws_hosts_filter, 'Dashboard'
29
+ set :aws_region, 'ap-southeast-2'
30
+ ```
31
+
32
+ Finally, in your config/deploy/environment_name.rb files:
33
+
34
+ ```ruby
35
+ set :aws_profile, 'sandbox' # Optional
36
+ hosts = fetch_aws_hosts.map(&:private_ip)
37
+ role :web, hosts
38
+ role :app, hosts
39
+ role :worker, hosts
40
+ role :db, hosts.first
41
+ ```
42
+
43
+ ## Usage
44
+
45
+ The call to `fetch_aws_hosts` will return an array of Structs with the following values:
46
+
47
+ ```ruby
48
+ private_ip: '10.0.0.1'
49
+ instance_id: 'i-abc123de456bb'
50
+ name: 'PROD-PRIVATE-DASHBOARD-A'
51
+ ```
52
+
53
+ ## Testing
54
+ This has been manually tested specifically to our AWS environment, and while this should work across other AWS environments I cannot guarantee this will be the case.
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/capistrano-aws-hosts. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "capistrano/aws/hosts/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "capistrano-aws-hosts"
8
+ spec.version = Capistrano::Aws::Hosts::VERSION
9
+ spec.authors = ["Matthew Savage"]
10
+ spec.email = ["matt@amasses.net"]
11
+
12
+ spec.summary = %q{Provide access to AWS EC2 inventory for deploy host targets}
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'capistrano', '>= 3.0'
23
+ spec.add_dependency 'aws-sdk-ec2', '~> 1'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,7 @@
1
+ module Capistrano
2
+ module Aws
3
+ module Hosts
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ require "capistrano/aws/hosts/version"
2
+ require 'aws-sdk-ec2'
3
+ module Capistrano
4
+ module Aws
5
+ module Hosts
6
+ # Fetches the hosts from AWS, filtering based on the tag 'Role' values (wildcarded)
7
+ #
8
+ # @return [Array<Struct(private_ip, instance_id, name>] array of instance records
9
+ def fetch_aws_hosts
10
+ role_name = fetch(:aws_hosts_filter)
11
+ profile = fetch(:aws_hosts_profile) || ENV['AWS_PROFILE']
12
+ region = fetch(:aws_region) || ENV['AWS_DEFAULT_REGION']
13
+
14
+ raise 'Region is not specified - please set :aws_region or export AWS_DEFAULT_REGION as an environment variable' unless region.to_s != ''
15
+ # raise "Profile is not specified - please set :aws_region or export AWS_DEFAULT_REGION as an environment variable" unless region.present?
16
+
17
+ client = ::Aws::EC2::Client.new(region: region, profile: profile)
18
+
19
+ instances = client.describe_instances(filters: [
20
+ {name: 'instance-state-name', values: ['running']},
21
+ {name: 'tag-key', values: ['Role']},
22
+ {name: 'tag-value', values: ["*#{role_name}*"]}
23
+ ])
24
+
25
+ instances = instances.first.reservations.first.instances
26
+ result = instances.each_with_object([]) do |item, arr|
27
+ arr << OpenStruct.new(
28
+ private_ip: item.private_ip_address,
29
+ instance_id: item.instance_id,
30
+ name: item.tags.select {|t| t.key == 'Name'}.first.value
31
+ )
32
+ end
33
+
34
+ print "Target hosts: #{result.map(&:name).join(', ')}\n"
35
+ result
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ include Capistrano::Aws::Hosts
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-aws-hosts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Savage
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capistrano
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-ec2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - matt@amasses.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - capistrano-aws-hosts.gemspec
83
+ - lib/capistrano/aws/hosts.rb
84
+ - lib/capistrano/aws/hosts/version.rb
85
+ homepage:
86
+ licenses:
87
+ - MIT
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.6.8
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Provide access to AWS EC2 inventory for deploy host targets
109
+ test_files: []