Skip to content

Commit 2260bcc

Browse files
committed
Initial commit
0 parents  commit 2260bcc

16 files changed

+379
-0
lines changed

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

.rspec

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sudo: false
2+
language: ruby
3+
rvm:
4+
- 2.2.7
5+
before_install: gem install bundler -v 1.16.1

CODE_OF_CONDUCT.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, gender identity and expression, level of experience,
9+
nationality, personal appearance, race, religion, or sexual identity and
10+
orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at matt@amasses.net. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at [http://contributor-covenant.org/version/1/4][version]
72+
73+
[homepage]: http://contributor-covenant.org
74+
[version]: http://contributor-covenant.org/version/1/4/

Gemfile

+6
Original file line numberDiff line numberDiff line change
@@ -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

Gemfile.lock

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
PATH
2+
remote: .
3+
specs:
4+
capistrano-aws-hosts (0.1.0)
5+
aws-sdk-ec2 (~> 1)
6+
capistrano (>= 3.0)
7+
8+
GEM
9+
remote: https://rubygems.org/
10+
specs:
11+
airbrussh (1.3.0)
12+
sshkit (>= 1.6.1, != 1.7.0)
13+
aws-partitions (1.61.0)
14+
aws-sdk-core (3.15.0)
15+
aws-partitions (~> 1.0)
16+
aws-sigv4 (~> 1.0)
17+
jmespath (~> 1.0)
18+
aws-sdk-ec2 (1.27.0)
19+
aws-sdk-core (~> 3)
20+
aws-sigv4 (~> 1.0)
21+
aws-sigv4 (1.0.2)
22+
capistrano (3.10.1)
23+
airbrussh (>= 1.0.0)
24+
i18n
25+
rake (>= 10.0.0)
26+
sshkit (>= 1.9.0)
27+
concurrent-ruby (1.0.5)
28+
diff-lcs (1.3)
29+
i18n (1.0.0)
30+
concurrent-ruby (~> 1.0)
31+
jmespath (1.3.1)
32+
net-scp (1.2.1)
33+
net-ssh (>= 2.6.5)
34+
net-ssh (4.2.0)
35+
rake (10.5.0)
36+
rspec (3.7.0)
37+
rspec-core (~> 3.7.0)
38+
rspec-expectations (~> 3.7.0)
39+
rspec-mocks (~> 3.7.0)
40+
rspec-core (3.7.1)
41+
rspec-support (~> 3.7.0)
42+
rspec-expectations (3.7.0)
43+
diff-lcs (>= 1.2.0, < 2.0)
44+
rspec-support (~> 3.7.0)
45+
rspec-mocks (3.7.0)
46+
diff-lcs (>= 1.2.0, < 2.0)
47+
rspec-support (~> 3.7.0)
48+
rspec-support (3.7.1)
49+
sshkit (1.16.0)
50+
net-scp (>= 1.1.2)
51+
net-ssh (>= 2.8.0)
52+
53+
PLATFORMS
54+
ruby
55+
56+
DEPENDENCIES
57+
bundler (~> 1.16)
58+
capistrano-aws-hosts!
59+
rake (~> 10.0)
60+
rspec (~> 3.0)
61+
62+
BUNDLED WITH
63+
1.16.1

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -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.

README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
## Installation
8+
9+
Add this line to your application's Gemfile:
10+
11+
```ruby
12+
group :development do
13+
gem 'capistrano-aws-hosts'
14+
end
15+
```
16+
17+
And then add the following to your Capfile:
18+
19+
```ruby
20+
require 'capistrano/aws/hosts'
21+
```
22+
23+
Add defaults to your config/deploy.rb file:
24+
25+
```ruby
26+
set :aws_hosts_filter, 'Dashboard'
27+
set :aws_region, 'ap-southeast-2'
28+
```
29+
30+
Finally, in your config/deploy/environment_name.rb files:
31+
32+
```ruby
33+
set :aws_profile, 'sandbox' # Optional
34+
hosts = fetch_aws_hosts.map(&:private_ip)
35+
role :web, hosts
36+
role :app, hosts
37+
role :worker, hosts
38+
role :db, hosts.first
39+
```
40+
41+
## Usage
42+
43+
The call to `fetch_aws_hosts` will return an array of Structs with the following values:
44+
45+
```ruby
46+
private_ip: '10.0.0.1'
47+
instance_id: 'i-abc123de456bb'
48+
name: 'PROD-PRIVATE-DASHBOARD-A'
49+
```
50+
51+
## Testing
52+
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.
53+
54+
## Contributing
55+
56+
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.
57+
58+
## License
59+
60+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
6+
task :default => :spec

bin/console

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "capistrano/aws/hosts"
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__)

bin/setup

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

capistrano-aws-hosts.gemspec

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16+
# to allow pushing to a single host or delete this section to allow pushing to any host.
17+
if spec.respond_to?(:metadata)
18+
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
19+
else
20+
raise "RubyGems 2.0 or newer is required to protect against " \
21+
"public gem pushes."
22+
end
23+
24+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
25+
f.match(%r{^(test|spec|features)/})
26+
end
27+
spec.bindir = "exe"
28+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29+
spec.require_paths = ["lib"]
30+
31+
spec.add_dependency 'capistrano', '>= 3.0'
32+
spec.add_dependency 'aws-sdk-ec2', '~> 1'
33+
34+
spec.add_development_dependency "bundler", "~> 1.16"
35+
spec.add_development_dependency "rake", "~> 10.0"
36+
spec.add_development_dependency "rspec", "~> 3.0"
37+
end

lib/capistrano/aws/hosts.rb

+41
Original file line numberDiff line numberDiff line change
@@ -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

lib/capistrano/aws/hosts/version.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Capistrano
2+
module Aws
3+
module Hosts
4+
VERSION = "0.1.0"
5+
end
6+
end
7+
end

spec/capistrano/aws/hosts_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
RSpec.describe Capistrano::Aws::Hosts do
2+
it "has a version number" do
3+
expect(Capistrano::Aws::Hosts::VERSION).not_to be nil
4+
end
5+
6+
it "does something useful" do
7+
expect(false).to eq(true)
8+
end
9+
end

spec/spec_helper.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require "bundler/setup"
2+
require "capistrano/aws/hosts"
3+
4+
RSpec.configure do |config|
5+
# Enable flags like --only-failures and --next-failure
6+
config.example_status_persistence_file_path = ".rspec_status"
7+
8+
# Disable RSpec exposing methods globally on `Module` and `main`
9+
config.disable_monkey_patching!
10+
11+
config.expect_with :rspec do |c|
12+
c.syntax = :expect
13+
end
14+
end

0 commit comments

Comments
 (0)