admiral-opsworks 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +1 -0
- data/admiral.gemspec +23 -0
- data/lib/admiral-opsworks/tasks.rb +74 -0
- data/lib/admiral-opsworks/util.rb +112 -0
- data/lib/admiral-opsworks.rb +1 -0
- metadata +104 -0
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3-p547
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Peter T. Brown
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Admiral for AWS OpsWorks
|
2
|
+
|
3
|
+
Admiral tasks for wielding AWS OpsWorks resources.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile (recommended):
|
8
|
+
|
9
|
+
gem 'admiral-opsworks'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install admiral-opsworks
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
On your command line type:
|
22
|
+
|
23
|
+
$ admiral ow help
|
24
|
+
|
25
|
+
To see a list of available commands. Make sure your bundle bin is in your PATH.
|
26
|
+
|
27
|
+
The following commands are available:
|
28
|
+
|
29
|
+
```
|
30
|
+
Commands:
|
31
|
+
admiral ow deploy APP_NAME # Deploy the APP_NAME application. APP_NAME need only partially match one existing app.
|
32
|
+
admiral ow help [COMMAND] # Describe subcommands or one specific subcommand
|
33
|
+
admiral ow provision # Replace and update existing instances
|
34
|
+
admiral ow ssh # ssh to first instance in environment.
|
35
|
+
|
36
|
+
Options:
|
37
|
+
--env, [--environment=ENVIRONMENT] # The environment (e.g. staging or production). Can also be specified with ADMIRAL_ENV.
|
38
|
+
# Default: production
|
39
|
+
```
|
40
|
+
|
41
|
+
Some commands have additional options you can discover with:
|
42
|
+
|
43
|
+
# admiral ow help [COMMAND]
|
44
|
+
|
45
|
+
# Setup and Configuration
|
46
|
+
|
47
|
+
Admiral for OpsWorks requires and builds on the setup implemented by Admiral for CloudFormation. Commands will look for parameters in the specific environment and query the CloudFormation stack created for the current environment.
|
48
|
+
|
49
|
+
For example, to SSH to an instance on your production database server:
|
50
|
+
|
51
|
+
$ admiral ow ssh --environment production --template MongoDB.template
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/admiral.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "admiral-opsworks"
|
7
|
+
spec.version = '0.0.1'
|
8
|
+
spec.authors = ["Peter T. Brown"]
|
9
|
+
spec.email = ["p@ptb.io"]
|
10
|
+
spec.description = %q{Admiral tasks for wielding AWS OpsWorks resources.}
|
11
|
+
spec.summary = %q{Admiral tasks for wielding AWS OpsWorks resources.}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
20
|
+
spec.add_development_dependency "rake"
|
21
|
+
|
22
|
+
spec.add_dependency 'admiral-cloudformation', '~> 0.0.2'
|
23
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'admiral/base'
|
3
|
+
require_relative 'util'
|
4
|
+
|
5
|
+
module Admiral
|
6
|
+
module Tasks
|
7
|
+
class OpsWorks < Thor
|
8
|
+
extend Admiral::Base
|
9
|
+
include Util::OpsWorks
|
10
|
+
include Util::CloudFormation
|
11
|
+
|
12
|
+
NAME = 'ow'
|
13
|
+
USAGE = 'ow <command> <options>'
|
14
|
+
DESCRIPTION = 'Commands for wielding AWS OpsWorks stacks.'
|
15
|
+
|
16
|
+
namespace :ow
|
17
|
+
|
18
|
+
default_command :create
|
19
|
+
|
20
|
+
desc "provision", "Replace and update existing instances"
|
21
|
+
|
22
|
+
option :count,
|
23
|
+
desc: 'Number of instances to create.',
|
24
|
+
type: :numeric,
|
25
|
+
default: 0
|
26
|
+
|
27
|
+
def provision
|
28
|
+
stack = client.stacks[stack_name(options[:environment])]
|
29
|
+
stack_id = cf_query_output(stack, "StackId")
|
30
|
+
layer_id = cf_query_output(stack, "LayerId")
|
31
|
+
|
32
|
+
update_instances stack_id, layer_id, options[:count]
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
desc 'ssh', 'ssh to first instance in environment. Connects to first instance in current environment stack.'
|
37
|
+
|
38
|
+
option :username,
|
39
|
+
desc: 'Override the default ssh username.',
|
40
|
+
type: :string,
|
41
|
+
default: 'ec2-user'
|
42
|
+
|
43
|
+
def ssh
|
44
|
+
stack = client.stacks[stack_name(options[:environment])]
|
45
|
+
stack_id = cf_query_output(stack, "StackId")
|
46
|
+
layer_id = cf_query_output(stack, "LayerId")
|
47
|
+
|
48
|
+
instances = get_all_instances(layer_id)
|
49
|
+
ssh_to_instance instances[0], params(options[:environment])['SshKeyName'], options[:username]
|
50
|
+
end
|
51
|
+
|
52
|
+
desc 'deploy APP_NAME', 'Deploy the APP_NAME application. APP_NAME need only partially match one existing app.'
|
53
|
+
|
54
|
+
def deploy(app_name)
|
55
|
+
puts "[admiral] Deploying to opsworks"
|
56
|
+
stack = client.stacks[stack_name(options[:environment])]
|
57
|
+
stack_id = cf_query_output stack, 'StackId'
|
58
|
+
app_id = cf_query_output stack, app_name
|
59
|
+
|
60
|
+
raise "#{app_name} did not match any existing applications." unless app_id
|
61
|
+
|
62
|
+
puts "[admiral] Deploying app #{app_id}."
|
63
|
+
|
64
|
+
opsworks.create_deployment(
|
65
|
+
stack_id: stack_id,
|
66
|
+
app_id: app_id,
|
67
|
+
command: {
|
68
|
+
name: "deploy",
|
69
|
+
}
|
70
|
+
)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'aws-sdk-v1'
|
2
|
+
|
3
|
+
module Admiral
|
4
|
+
module Util
|
5
|
+
module OpsWorks
|
6
|
+
|
7
|
+
def opsworks
|
8
|
+
AWS::OpsWorks::Client.new(region: 'us-east-1') # opsworks command-and-control is us-east-1 ONLY ;)
|
9
|
+
end
|
10
|
+
|
11
|
+
def instance_online?(instance_id)
|
12
|
+
response = opsworks.describe_instances(:instance_ids => [instance_id])
|
13
|
+
response[:instances].first[:status] == "online"
|
14
|
+
end
|
15
|
+
|
16
|
+
def instance_status(instance_id)
|
17
|
+
begin
|
18
|
+
response = opsworks.describe_instances(:instance_ids => [instance_id])
|
19
|
+
rescue AWS::OpsWorks::Errors::ResourceNotFoundException
|
20
|
+
return "nonexistent"
|
21
|
+
end
|
22
|
+
response[:instances].first[:status].tap do |status|
|
23
|
+
raise "Instance #{instance_id} has a failed status #{status}" if status =~ /fail|error/i
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def wait_for_instance(instance_id, status)
|
28
|
+
while (ins_status = instance_status(instance_id)) != status
|
29
|
+
puts "[admiral] Waiting for #{instance_id} instance to become #{status}. Current status: #{ins_status}"
|
30
|
+
sleep 10
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def all_availability_zones
|
35
|
+
ec2 = AWS::EC2.new
|
36
|
+
ec2.availability_zones.map(&:name)
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_all_instances(layer_id)
|
40
|
+
response = opsworks.describe_instances({:layer_id => layer_id})
|
41
|
+
response[:instances]
|
42
|
+
end
|
43
|
+
|
44
|
+
def ssh_to_instance(instance, ssh_key_name, username = 'ec2-user')
|
45
|
+
Kernel.exec "ssh -i ~/.ssh/#{ssh_key_name}.pem #{username}@#{instance[:public_ip]}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def attach_ebs_volumes(instance_id, volume_ids)
|
49
|
+
volume_ids.each do |volume_id|
|
50
|
+
puts "[admiral] Attaching EBS volume #{volume_id} to instance #{instance_id}"
|
51
|
+
opsworks.assign_volume({:volume_id => volume_id, :instance_id => instance_id})
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def detach_ebs_volumes(instance_id)
|
56
|
+
response = opsworks.describe_volumes(:instance_id => instance_id)
|
57
|
+
volume_ids = response[:volumes].map { |v| v[:volume_id] }
|
58
|
+
volume_ids.each do |volume_id|
|
59
|
+
puts "[admiral] Detaching EBS volume #{volume_id} from instance #{instance_id}"
|
60
|
+
opsworks.unassign_volume(:volume_id => volume_id)
|
61
|
+
end
|
62
|
+
|
63
|
+
volume_ids
|
64
|
+
end
|
65
|
+
|
66
|
+
def create_instance(stack_id, layer_id, az)
|
67
|
+
opsworks.create_instance({:stack_id => stack_id,
|
68
|
+
:layer_ids => [layer_id],
|
69
|
+
:instance_type => ENV['INSTANCE_TYPE'] || 't2.small',
|
70
|
+
:install_updates_on_boot => !ENV['SKIP_INSTANCE_PACKAGE_UPDATES'],
|
71
|
+
:availability_zone => az})
|
72
|
+
end
|
73
|
+
|
74
|
+
def update_instances(stack_id, layer_id, count)
|
75
|
+
azs = all_availability_zones
|
76
|
+
existing_instances = get_all_instances(layer_id)
|
77
|
+
count_to_create = count.to_i - existing_instances.size
|
78
|
+
new_instances = (1..count_to_create).map do |i|
|
79
|
+
instance = create_instance(stack_id, layer_id, azs[(existing_instances.size + i) % azs.size])
|
80
|
+
puts "[admiral] Created instance #{instance[:instance_id]}; now starting up."
|
81
|
+
opsworks.start_instance(:instance_id => instance[:instance_id])
|
82
|
+
instance
|
83
|
+
end
|
84
|
+
|
85
|
+
new_instances.each do |instance|
|
86
|
+
wait_for_instance(instance[:instance_id], "online")
|
87
|
+
end
|
88
|
+
|
89
|
+
puts "Replacing existing instances.." if existing_instances.size > 0
|
90
|
+
|
91
|
+
existing_instances.each do |instance|
|
92
|
+
puts "[admiral] Stopping instance #{instance[:hostname]}, id: #{instance[:instance_id]}"
|
93
|
+
opsworks.stop_instance({:instance_id => instance[:instance_id]})
|
94
|
+
wait_for_instance(instance[:instance_id], "stopped")
|
95
|
+
ebs_volume_ids = detach_ebs_volumes(instance[:instance_id])
|
96
|
+
|
97
|
+
puts "[admiral] Creating replacement instance"
|
98
|
+
replacement = create_instance(stack_id, layer_id, instance[:availability_zone])
|
99
|
+
attach_ebs_volumes(replacement[:instance_id], ebs_volume_ids)
|
100
|
+
|
101
|
+
puts "[admiral] Starting new instance, id: #{replacement[:instance_id]}"
|
102
|
+
opsworks.start_instance(:instance_id => replacement[:instance_id])
|
103
|
+
wait_for_instance(replacement[:instance_id], "online")
|
104
|
+
|
105
|
+
puts "[admiral] Deleting old instance #{instance[:hostname]}, #{instance[:instance_id]}"
|
106
|
+
opsworks.delete_instance(:instance_id => instance[:instance_id])
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "admiral-opsworks/tasks"
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: admiral-opsworks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Peter T. Brown
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-05-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: admiral-cloudformation
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.0.2
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.0.2
|
62
|
+
description: Admiral tasks for wielding AWS OpsWorks resources.
|
63
|
+
email:
|
64
|
+
- p@ptb.io
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .ruby-version
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- admiral.gemspec
|
76
|
+
- lib/admiral-opsworks.rb
|
77
|
+
- lib/admiral-opsworks/tasks.rb
|
78
|
+
- lib/admiral-opsworks/util.rb
|
79
|
+
homepage: ''
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.8.23.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Admiral tasks for wielding AWS OpsWorks resources.
|
104
|
+
test_files: []
|