capistrano-elobuff 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
12
  gem.name = "capistrano-elobuff"
13
13
  gem.require_paths = ["lib"]
14
- gem.version = "0.1.4"
14
+ gem.version = "0.2.0"
15
15
 
16
16
  gem.add_dependency "capistrano"
17
17
  end
@@ -0,0 +1,15 @@
1
+ require "fog"
2
+ require "capistrano/elobuff/ec2/helpers"
3
+ require "capistrano/elobuff/ec2/compute"
4
+ require "capistrano/elobuff/ec2/server"
5
+
6
+ Capistrano::Configuration.instance.load do
7
+ require_settings :ec2_ami, :ec2_instance_type, :ec2_security_group, :ec2_availability_zone
8
+
9
+ set_default(:ec2_key) { ENV["AWS_EC2_KEY"] || ENV["AWS_S3_KEY"] || raise("Please specify an AWS_EC2_KEY") }
10
+ set_default(:ec2_secret) { ENV["AWS_EC2_SECRET"] || ENV["AWS_S3_SECRET"] || raise("Please specify an AWS_EC2_SECRET") }
11
+
12
+ set_default(:ec2) { Capistrano::Elobuff::Ec2::Compute.new application, ec2_key, ec2_secret }
13
+ end
14
+
15
+ Dir.glob(File.join(File.dirname(__FILE__), "/ec2/recipes/*.rb")).sort.each {|f| load f }
@@ -0,0 +1,63 @@
1
+ require "fog"
2
+
3
+ module Capistrano
4
+ module Elobuff
5
+ module Ec2
6
+ class Compute
7
+ def initialize(application, access_key_id, secret_access_key)
8
+ @application = application
9
+ @access_key_id = access_key_id
10
+ @secret_access_key = secret_access_key
11
+ end
12
+
13
+ def connection
14
+ @connection ||= Fog::Compute.new provider: "AWS", aws_access_key_id: @access_key_id, aws_secret_access_key: @secret_access_key
15
+ end
16
+
17
+ def servers
18
+ @servers ||= connection.servers.select {|s| s.tags.keys.include? @application.to_s }.map {|s| Server.new s, @application }
19
+ end
20
+
21
+ def find_server(id)
22
+ servers.select {|s| (s.id == id) || (s.name == id) || (s.address == id) }.first || die("Could not find server with id, name or address: #{id}")
23
+ end
24
+
25
+ def available_servers
26
+ servers.select {|s| s.available? }
27
+ end
28
+
29
+ def roles
30
+ available_servers.map {|s| s.roles }.flatten.uniq
31
+ end
32
+
33
+ def zones
34
+ available_servers.map {|s| s.zone }.flatten.uniq
35
+ end
36
+
37
+ def available_servers_in_role(role)
38
+ available_servers.select {|s| s.roles.include? role }
39
+ end
40
+
41
+ def available_servers_in_zone(zone)
42
+ available_servers.select {|s| s.zone == zone }
43
+ end
44
+
45
+ def create_server(ami, type, security_groups, availability_zone, name, roles)
46
+ puts "Creating server"
47
+ server = connection.servers.create(
48
+ image_id: ami,
49
+ flavor_id: type,
50
+ groups: security_groups,
51
+ availability_zone: availability_zone,
52
+ tags: { "Name" => name, "#{@application}" => roles })
53
+ server.wait_for { ready? }
54
+ Server.new(server, @application)
55
+ end
56
+
57
+ def update_server_tags(server)
58
+ connection.create_tags server.id, server.tags
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,16 @@
1
+ def die(why)
2
+ $stdout.sync = true
3
+ puts ""
4
+ puts " FATAL ERROR: #{why}"
5
+ exit 1
6
+ end
7
+
8
+ def find_servers
9
+ if ids = ENV["SERVERS"]
10
+ ids.split(",").map {|id| ec2.find_server id }
11
+ elsif id = ENV["SERVER"]
12
+ Array(ec2.find_server id)
13
+ else
14
+ die "Please specify SERVER or SERVERS"
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ Capistrano::Configuration.instance.load do
2
+ require_settings :ec2
3
+
4
+ namespace :a do
5
+ desc "Invoke Servers"
6
+ task :default do
7
+ ec2.available_servers.each do |s|
8
+ server s.address, *s.roles
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,30 @@
1
+ Capistrano::Configuration.instance.load do
2
+ require_settings :ec2
3
+
4
+ namespace :r do
5
+ desc "List roles"
6
+ task :default do
7
+ puts "Listing roles:"
8
+ ec2.roles.each do |r|
9
+ puts "%-16s %s" % ["#{r}:", ec2.available_servers_in_role(r).map(&:id).join(", ")]
10
+ end
11
+ puts ""
12
+ puts "Commands: #{ec2.roles.map {|r| "r:#{r}" }.join(", ")}"
13
+ end
14
+ end
15
+ end
16
+
17
+ Capistrano::Configuration.instance(:must_exist).load do
18
+ def ec2_roles
19
+ namespace :r do
20
+ ec2.roles.each do |r|
21
+ desc "Invoke Role: #{r}"
22
+ task r do
23
+ ec2.available_servers_in_role(r).each do |s|
24
+ server s.address, *s.roles
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,83 @@
1
+ Capistrano::Configuration.instance.load do
2
+ require_settings :ec2
3
+
4
+ namespace :s do
5
+ desc "List servers"
6
+ task :default do
7
+ puts "Listing servers:"
8
+ puts ec2.servers.map(&:to_s)
9
+ puts ""
10
+ puts "SERVERS: #{ec2.available_servers.map(&:id).join(", ")}"
11
+ end
12
+
13
+ desc "Add Server"
14
+ task :add do
15
+ ami = ENV["EC2_AMI"] || ec2_ami || die("Please specify an EC2_AMI")
16
+ type = ENV["EC2_INSTANCE_TYPE"] || ec2_instance_type || die("Please specify an EC2_INSTANCE_TYPE")
17
+ group = ENV["EC2_SECURITY_GROUP"] || ec2_security_group || die("Please specify an EC2_SECURITY_GROUP")
18
+ zone = ENV["EC2_AVAILABILITY_ZONE"] || ec2_availability_zone || die("Please specify an EC2_AVAILABILITY_ZONE")
19
+ name = ENV["NAME"] || die("Please specify a NAME")
20
+ roles = ENV["ROLES"] || die("Please specify ROLES")
21
+
22
+ s = ec2.create_server(ami, type, group, zone, name, roles)
23
+ server s.address, *s.roles
24
+ end
25
+
26
+ desc "Update Server Roles"
27
+ task :rerole do
28
+ find_servers.each do |s|
29
+ if roles = ENV["ROLES"]
30
+ s.roles = roles
31
+ ec2.update_server_tags s
32
+ end
33
+
34
+ puts s.to_s
35
+ end
36
+ end
37
+
38
+ desc "Rename Servers"
39
+ task :rename do
40
+ find_servers.each do |s|
41
+ if name = ENV["NAME"]
42
+ s.name = name
43
+ ec2.update_server_tags s
44
+ end
45
+
46
+ puts s.to_s
47
+ end
48
+ end
49
+
50
+ desc "Console into Server"
51
+ task :console do
52
+ s = find_servers.first
53
+ exec "ssh #{user}@#{s.address}"
54
+ end
55
+
56
+ desc "Terminate Servers"
57
+ task :terminate do
58
+ puts "Preparing to terminate the following servers:"
59
+ find_servers.each do |s|
60
+ puts s.to_s
61
+ end
62
+
63
+ ENV["CONFIRM"] || die("Please set CONFIRM in order to terminate")
64
+
65
+ find_servers.each do |s|
66
+ s.terminate!
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ Capistrano::Configuration.instance(:must_exist).load do
73
+ def ec2_servers
74
+ namespace :s do
75
+ ec2.servers.each do |s|
76
+ desc "Invoke Server: #{s.id}"
77
+ task s.slug do
78
+ define_server s.address, *s.roles
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,30 @@
1
+ Capistrano::Configuration.instance.load do
2
+ require_settings :ec2
3
+
4
+ namespace :z do
5
+ desc "List zones"
6
+ task :default do
7
+ puts "Listing availability zones:"
8
+ ec2.zones.each do |z|
9
+ puts "%-16s %s" % ["#{z}:", ec2.available_servers_in_zone(z).map(&:id).join(", ")]
10
+ end
11
+ puts ""
12
+ puts "Commands: #{ec2.zones.map {|z| "z:#{z}" }.join(", ")}"
13
+ end
14
+ end
15
+ end
16
+
17
+ Capistrano::Configuration.instance(:must_exist).load do
18
+ def ec2_zones
19
+ namespace :z do
20
+ ec2.zones.each do |z|
21
+ desc "Invoke Zone: #{z}"
22
+ task z do
23
+ ec2.available_servers_in_zone(z).each do |s|
24
+ server s.address, *s.roles
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,62 @@
1
+ require "fog"
2
+
3
+ module Capistrano
4
+ module Elobuff
5
+ module Ec2
6
+ class Server
7
+ def initialize(fog_server, application)
8
+ @server = fog_server
9
+ @application = application
10
+ end
11
+
12
+ def to_s
13
+ "%-24s%-16s%-48s%-16s%-16s%-16s%-24s%s" % [name, id, address, @server.image_id, @server.availability_zone, @server.state, @server.groups.join(","), roles.join(",")]
14
+ end
15
+
16
+ def available?
17
+ @server.state == "running"
18
+ end
19
+
20
+ def id
21
+ @server.id
22
+ end
23
+
24
+ def slug
25
+ @server.id.gsub(/i-/, "")
26
+ end
27
+
28
+ def name
29
+ @server.tags["Name"]
30
+ end
31
+
32
+ def name=(string)
33
+ @server.tags["Name"] = string
34
+ end
35
+
36
+ def address
37
+ @server.dns_name
38
+ end
39
+
40
+ def tags
41
+ @server.tags
42
+ end
43
+
44
+ def zone
45
+ @server.availability_zone
46
+ end
47
+
48
+ def roles
49
+ @server.tags[@application].split(",").map(&:to_sym)
50
+ end
51
+
52
+ def roles=(string)
53
+ @server.tags[@application] = string
54
+ end
55
+
56
+ def terminate!
57
+ @server.destroy
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-elobuff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-17 00:00:00.000000000 Z
12
+ date: 2012-08-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capistrano
@@ -43,6 +43,14 @@ files:
43
43
  - lib/capistrano/elobuff.rb
44
44
  - lib/capistrano/elobuff/base.rb
45
45
  - lib/capistrano/elobuff/defaults.rb
46
+ - lib/capistrano/elobuff/ec2.rb
47
+ - lib/capistrano/elobuff/ec2/compute.rb
48
+ - lib/capistrano/elobuff/ec2/helpers.rb
49
+ - lib/capistrano/elobuff/ec2/recipes/a.rb
50
+ - lib/capistrano/elobuff/ec2/recipes/r.rb
51
+ - lib/capistrano/elobuff/ec2/recipes/s.rb
52
+ - lib/capistrano/elobuff/ec2/recipes/z.rb
53
+ - lib/capistrano/elobuff/ec2/server.rb
46
54
  - lib/capistrano/elobuff/helpers.rb
47
55
  - lib/capistrano/elobuff/recipes/bundler.rb
48
56
  - lib/capistrano/elobuff/recipes/code.rb