capify-ec2 1
Sign up to get free protection for your applications and to get access to all the features.
- data/capify-ec2.gemspec +18 -0
- data/lib/capify-ec2.rb +41 -0
- data/lib/capify-ec2/capistrano.rb +51 -0
- metadata +94 -0
data/capify-ec2.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#-*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "capify-ec2"
|
6
|
+
s.version = "1"
|
7
|
+
s.authors = ["Noah Cantor"]
|
8
|
+
s.email = "noah.cantor@forward.co.uk"
|
9
|
+
s.homepage = "http://www.forward.co.uk"
|
10
|
+
s.summary = "Grabs roles from ec2's tags and autogenerates capistrano tasks"
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.files = %w(lib).map {|d| Dir.glob("#{d}/**/*")}.flatten << "capify-ec2.gemspec"
|
13
|
+
s.require_path = "lib"
|
14
|
+
s.has_rdoc = false
|
15
|
+
s.add_dependency('activesupport', '>= 3.0.0')
|
16
|
+
s.add_dependency('fog')
|
17
|
+
end
|
18
|
+
|
data/lib/capify-ec2.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'fog'
|
3
|
+
|
4
|
+
class CapifyEc2
|
5
|
+
|
6
|
+
def self.running_instances
|
7
|
+
ec2_config = YAML.load(File.new("config/ec2.yml"))
|
8
|
+
ec2 = Fog::Compute.new(:provider => 'AWS', :aws_access_key_id => ec2_config[:aws_access_key_id], :aws_secret_access_key => ec2_config[:aws_secret_access_key], :region => ec2_config[:aws_params][:region])
|
9
|
+
running_instances = ec2.servers.select {|instance| instance.state == "running"}
|
10
|
+
running_instances.each do |instance|
|
11
|
+
instance.instance_eval do
|
12
|
+
def case_insensitive_tag(key)
|
13
|
+
tags[key] || tags[key.downcase]
|
14
|
+
end
|
15
|
+
|
16
|
+
def name
|
17
|
+
case_insensitive_tag("Name").split('-').reject {|portion| portion.include?(".")}.join("-")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.get_instances_by_role(role)
|
24
|
+
selected_instances = running_instances.select do |instance|
|
25
|
+
value = instance.case_insensitive_tag("Role")
|
26
|
+
value == role.to_s
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.get_instances_by_name(name)
|
31
|
+
selected_instances = running_instances.select do |instance|
|
32
|
+
value = instance.case_insensitive_tag("Name")
|
33
|
+
value == name.to_s
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.server_names
|
38
|
+
running_instances.map {|instance| instance.name}
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../capify-ec2')
|
2
|
+
|
3
|
+
Capistrano::Configuration.instance(:must_exist).load do
|
4
|
+
def ec2_role(role_to_define)
|
5
|
+
subroles = {}
|
6
|
+
if role_to_define.is_a?(Hash)
|
7
|
+
defined_role = role_to_define[:name]
|
8
|
+
subroles = role_to_define[:options]
|
9
|
+
else
|
10
|
+
defined_role = role_to_define
|
11
|
+
end
|
12
|
+
instances = CapifyEc2.get_instances_by_role(defined_role)
|
13
|
+
|
14
|
+
instances.each do |instance|
|
15
|
+
task instance.name.to_sym do
|
16
|
+
set :server_address, instance.dns_name
|
17
|
+
role :web, *server_address
|
18
|
+
role :app, *server_address
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
task defined_role.to_sym do
|
23
|
+
instances.each do |instance|
|
24
|
+
new_options = {}
|
25
|
+
subroles.each {|key, value| new_options[key] = true if value.to_s == instance.name}
|
26
|
+
if new_options
|
27
|
+
role defined_role.to_sym, instance.dns_name, new_options
|
28
|
+
else
|
29
|
+
role defined_role.to_sym, instance.dns_name
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# task :add_tag do
|
36
|
+
# instance_name = variables[:logger].instance_variable_get("@options")[:actions].first
|
37
|
+
# CapifyEc2.add_tag(instance_name, tag, value)
|
38
|
+
# end
|
39
|
+
|
40
|
+
def ec2_roles(*roles)
|
41
|
+
roles.each {|role| ec2_role(role)}
|
42
|
+
end
|
43
|
+
|
44
|
+
task :date do
|
45
|
+
run "date"
|
46
|
+
end
|
47
|
+
|
48
|
+
task :server_names do
|
49
|
+
puts CapifyEc2.server_names.sort
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: capify-ec2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 1
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
version: "1"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Noah Cantor
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2011-05-11 00:00:00 Z
|
17
|
+
dependencies:
|
18
|
+
- !ruby/object:Gem::Dependency
|
19
|
+
name: activesupport
|
20
|
+
prerelease: false
|
21
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
22
|
+
none: false
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
hash: 7
|
27
|
+
segments:
|
28
|
+
- 3
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
version: 3.0.0
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: fog
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
description:
|
49
|
+
email: noah.cantor@forward.co.uk
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files: []
|
55
|
+
|
56
|
+
files:
|
57
|
+
- lib/capify-ec2/capistrano.rb
|
58
|
+
- lib/capify-ec2.rb
|
59
|
+
- capify-ec2.gemspec
|
60
|
+
homepage: http://www.forward.co.uk
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.7.2
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Grabs roles from ec2's tags and autogenerates capistrano tasks
|
93
|
+
test_files: []
|
94
|
+
|