nirvdrum-rubber 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. data/CHANGELOG +146 -0
  2. data/COPYING +339 -0
  3. data/README +6 -0
  4. data/TODO +11 -0
  5. data/VERSION +1 -0
  6. data/bin/vulcanize +41 -0
  7. data/generators/vulcanize/USAGE +6 -0
  8. data/generators/vulcanize/templates/apache/config/rubber/deploy-apache.rb +51 -0
  9. data/generators/vulcanize/templates/apache/config/rubber/role/apache/deflate.conf +10 -0
  10. data/generators/vulcanize/templates/apache/config/rubber/role/apache/expires.conf +9 -0
  11. data/generators/vulcanize/templates/apache/config/rubber/role/apache/headers.conf +6 -0
  12. data/generators/vulcanize/templates/apache/config/rubber/role/apache/monit-apache.conf +8 -0
  13. data/generators/vulcanize/templates/apache/config/rubber/role/apache/ports.conf +5 -0
  14. data/generators/vulcanize/templates/apache/config/rubber/role/apache/setenvif.conf +52 -0
  15. data/generators/vulcanize/templates/apache/config/rubber/role/web_tools/tools-apache-vhost.conf +66 -0
  16. data/generators/vulcanize/templates/apache/config/rubber/role/web_tools/tools-apache.auth +7 -0
  17. data/generators/vulcanize/templates/apache/config/rubber/role/web_tools/tools-index.html +34 -0
  18. data/generators/vulcanize/templates/apache/config/rubber/rubber-apache.yml +6 -0
  19. data/generators/vulcanize/templates/apache/templates.yml +1 -0
  20. data/generators/vulcanize/templates/base/Capfile +14 -0
  21. data/generators/vulcanize/templates/base/config/deploy.rb +55 -0
  22. data/generators/vulcanize/templates/base/config/rubber/common/crontab +17 -0
  23. data/generators/vulcanize/templates/base/config/rubber/common/monit-postfix.conf +8 -0
  24. data/generators/vulcanize/templates/base/config/rubber/common/rubber.profile +14 -0
  25. data/generators/vulcanize/templates/base/config/rubber/deploy-setup.rb +84 -0
  26. data/generators/vulcanize/templates/base/config/rubber/rubber-dns.yml +79 -0
  27. data/generators/vulcanize/templates/base/config/rubber/rubber.yml +227 -0
  28. data/generators/vulcanize/templates/base/lib/tasks/rubber.rake +15 -0
  29. data/generators/vulcanize/templates/base/script/cron-rake +18 -0
  30. data/generators/vulcanize/templates/base/script/cron-runner +18 -0
  31. data/generators/vulcanize/templates/base/script/cron-sh +67 -0
  32. data/generators/vulcanize/templates/base/templates.yml +1 -0
  33. data/generators/vulcanize/templates/complete_mongrel_mysql/config/rubber/role/haproxy/haproxy-mongrel.conf +23 -0
  34. data/generators/vulcanize/templates/complete_mongrel_mysql/config/rubber/role/nginx/nginx-mongrel.conf +113 -0
  35. data/generators/vulcanize/templates/complete_mongrel_mysql/config/rubber/rubber-complete.yml +41 -0
  36. data/generators/vulcanize/templates/complete_mongrel_mysql/templates.yml +6 -0
  37. data/generators/vulcanize/templates/complete_passenger_mysql/config/rubber/role/haproxy/haproxy-passenger.conf +21 -0
  38. data/generators/vulcanize/templates/complete_passenger_mysql/config/rubber/rubber-complete.yml +40 -0
  39. data/generators/vulcanize/templates/complete_passenger_mysql/templates.yml +10 -0
  40. data/generators/vulcanize/templates/cruise/config/rubber/deploy-cruise.rb +72 -0
  41. data/generators/vulcanize/templates/cruise/config/rubber/role/cruise/cruise +40 -0
  42. data/generators/vulcanize/templates/cruise/config/rubber/role/cruise/my.cnf +165 -0
  43. data/generators/vulcanize/templates/cruise/config/rubber/role/cruise/production.rb +8 -0
  44. data/generators/vulcanize/templates/cruise/config/rubber/role/cruise/site_config.rb +76 -0
  45. data/generators/vulcanize/templates/cruise/config/rubber/role/web_tools/cruise-nginx.conf +11 -0
  46. data/generators/vulcanize/templates/cruise/config/rubber/rubber-cruise.yml +18 -0
  47. data/generators/vulcanize/templates/cruise/templates.yml +1 -0
  48. data/generators/vulcanize/templates/haproxy/config/rubber/deploy-haproxy.rb +45 -0
  49. data/generators/vulcanize/templates/haproxy/config/rubber/role/haproxy/haproxy-base.conf +26 -0
  50. data/generators/vulcanize/templates/haproxy/config/rubber/role/haproxy/haproxy-default.conf +8 -0
  51. data/generators/vulcanize/templates/haproxy/config/rubber/role/haproxy/monit-haproxy.conf +9 -0
  52. data/generators/vulcanize/templates/haproxy/config/rubber/role/haproxy/syslog-haproxy.conf +6 -0
  53. data/generators/vulcanize/templates/haproxy/config/rubber/role/haproxy/syslogd-default.conf +17 -0
  54. data/generators/vulcanize/templates/haproxy/config/rubber/role/web_tools/haproxy-nginx.conf +10 -0
  55. data/generators/vulcanize/templates/haproxy/config/rubber/rubber-haproxy.yml +7 -0
  56. data/generators/vulcanize/templates/haproxy/templates.yml +1 -0
  57. data/generators/vulcanize/templates/jetty/config/rubber/deploy-jetty.rb +59 -0
  58. data/generators/vulcanize/templates/jetty/config/rubber/role/jetty/jetty.sh +589 -0
  59. data/generators/vulcanize/templates/jetty/config/rubber/role/jetty/jetty.xml +199 -0
  60. data/generators/vulcanize/templates/jetty/config/rubber/role/jetty/monit-jetty.conf +9 -0
  61. data/generators/vulcanize/templates/jetty/config/rubber/rubber-jetty.yml +10 -0
  62. data/generators/vulcanize/templates/jetty/templates.yml +1 -0
  63. data/generators/vulcanize/templates/memcached/config/memcached.yml +28 -0
  64. data/generators/vulcanize/templates/memcached/config/rubber/common/memcached.yml +14 -0
  65. data/generators/vulcanize/templates/memcached/config/rubber/role/memcached/memcached.conf +52 -0
  66. data/generators/vulcanize/templates/memcached/config/rubber/role/memcached/memcached_munin_plugin +249 -0
  67. data/generators/vulcanize/templates/memcached/config/rubber/rubber-memcached.yml +7 -0
  68. data/generators/vulcanize/templates/memcached/templates.yml +1 -0
  69. data/generators/vulcanize/templates/minimal_mysql/templates.yml +7 -0
  70. data/generators/vulcanize/templates/minimal_nodb/templates.yml +6 -0
  71. data/generators/vulcanize/templates/mongrel/config/rubber/deploy-mongrel.rb +75 -0
  72. data/generators/vulcanize/templates/mongrel/config/rubber/role/mongrel/mongrel_cluster.yml +12 -0
  73. data/generators/vulcanize/templates/mongrel/config/rubber/role/mongrel/monit-mongrel.conf +20 -0
  74. data/generators/vulcanize/templates/mongrel/config/rubber/rubber-mongrel.yml +9 -0
  75. data/generators/vulcanize/templates/mongrel/templates.yml +1 -0
  76. data/generators/vulcanize/templates/monit/config/rubber/common/monit-default.conf +15 -0
  77. data/generators/vulcanize/templates/monit/config/rubber/common/monit.conf +251 -0
  78. data/generators/vulcanize/templates/monit/config/rubber/deploy-monit.rb +32 -0
  79. data/generators/vulcanize/templates/monit/config/rubber/role/web_tools/monit-admin-nginx.conf +10 -0
  80. data/generators/vulcanize/templates/monit/config/rubber/rubber-monit.yml +6 -0
  81. data/generators/vulcanize/templates/monit/templates.yml +1 -0
  82. data/generators/vulcanize/templates/munin/config/rubber/common/monit-munin.conf +8 -0
  83. data/generators/vulcanize/templates/munin/config/rubber/common/munin-node.conf +48 -0
  84. data/generators/vulcanize/templates/munin/config/rubber/common/munin-plugins.conf +9 -0
  85. data/generators/vulcanize/templates/munin/config/rubber/deploy-munin.rb +46 -0
  86. data/generators/vulcanize/templates/munin/config/rubber/role/web_tools/munin-nginx.conf +8 -0
  87. data/generators/vulcanize/templates/munin/config/rubber/role/web_tools/munin-plugins.conf +31 -0
  88. data/generators/vulcanize/templates/munin/config/rubber/role/web_tools/munin.conf +80 -0
  89. data/generators/vulcanize/templates/munin/config/rubber/rubber-munin.yml +8 -0
  90. data/generators/vulcanize/templates/munin/script/munin/example_mysql_query.rb +57 -0
  91. data/generators/vulcanize/templates/munin/script/munin/example_simple.rb +24 -0
  92. data/generators/vulcanize/templates/munin/templates.yml +1 -0
  93. data/generators/vulcanize/templates/mysql/config/rubber/common/database.yml +11 -0
  94. data/generators/vulcanize/templates/mysql/config/rubber/deploy-mysql.rb +156 -0
  95. data/generators/vulcanize/templates/mysql/config/rubber/role/db/crontab +14 -0
  96. data/generators/vulcanize/templates/mysql/config/rubber/role/db/monit-mysql.cnf +10 -0
  97. data/generators/vulcanize/templates/mysql/config/rubber/role/db/my.cnf +167 -0
  98. data/generators/vulcanize/templates/mysql/config/rubber/role/mysql_slave/mysql_slave_munin_plugin +51 -0
  99. data/generators/vulcanize/templates/mysql/config/rubber/rubber-mysql.yml +46 -0
  100. data/generators/vulcanize/templates/mysql/templates.yml +1 -0
  101. data/generators/vulcanize/templates/mysql_cluster/config/rubber/common/mysql_cluster_migrations.rb +13 -0
  102. data/generators/vulcanize/templates/mysql_cluster/config/rubber/deploy-mysql_cluster.rb +173 -0
  103. data/generators/vulcanize/templates/mysql_cluster/config/rubber/role/mysql_data/my.cnf +15 -0
  104. data/generators/vulcanize/templates/mysql_cluster/config/rubber/role/mysql_mgm/ndb_mgmd.cnf +39 -0
  105. data/generators/vulcanize/templates/mysql_cluster/config/rubber/role/mysql_sql/monit-mysql_cluster_sql.cnf +10 -0
  106. data/generators/vulcanize/templates/mysql_cluster/config/rubber/role/mysql_sql/my.cnf +23 -0
  107. data/generators/vulcanize/templates/mysql_cluster/config/rubber/rubber-mysql_cluster.yml +32 -0
  108. data/generators/vulcanize/templates/mysql_cluster/templates.yml +1 -0
  109. data/generators/vulcanize/templates/mysql_proxy/config/rubber/common/database.yml +16 -0
  110. data/generators/vulcanize/templates/mysql_proxy/config/rubber/common/monit-mysql_proxy.cnf +10 -0
  111. data/generators/vulcanize/templates/mysql_proxy/config/rubber/common/mysql-proxy +153 -0
  112. data/generators/vulcanize/templates/mysql_proxy/config/rubber/common/mysql-proxy.conf +10 -0
  113. data/generators/vulcanize/templates/mysql_proxy/config/rubber/common/mysql-proxy.lua +5 -0
  114. data/generators/vulcanize/templates/mysql_proxy/config/rubber/deploy-mysql_proxy.rb +52 -0
  115. data/generators/vulcanize/templates/mysql_proxy/config/rubber/rubber-mysql_proxy.yml +11 -0
  116. data/generators/vulcanize/templates/mysql_proxy/templates.yml +1 -0
  117. data/generators/vulcanize/templates/nginx/config/rubber/deploy-nginx.rb +45 -0
  118. data/generators/vulcanize/templates/nginx/config/rubber/role/nginx/crontab +9 -0
  119. data/generators/vulcanize/templates/nginx/config/rubber/role/nginx/monit-nginx.conf +8 -0
  120. data/generators/vulcanize/templates/nginx/config/rubber/role/nginx/nginx.conf +42 -0
  121. data/generators/vulcanize/templates/nginx/config/rubber/role/web_tools/nginx-tools.conf +55 -0
  122. data/generators/vulcanize/templates/nginx/config/rubber/role/web_tools/tools-index.html +30 -0
  123. data/generators/vulcanize/templates/nginx/config/rubber/role/web_tools/tools-nginx.auth +7 -0
  124. data/generators/vulcanize/templates/nginx/config/rubber/rubber-nginx.yml +10 -0
  125. data/generators/vulcanize/templates/nginx/templates.yml +1 -0
  126. data/generators/vulcanize/templates/passenger/config/rubber/deploy-passenger.rb +37 -0
  127. data/generators/vulcanize/templates/passenger/config/rubber/role/passenger/munin-passenger-memory.conf +34 -0
  128. data/generators/vulcanize/templates/passenger/config/rubber/role/passenger/munin-passenger-sudoers.conf +7 -0
  129. data/generators/vulcanize/templates/passenger/config/rubber/role/passenger/munin-passenger.conf +47 -0
  130. data/generators/vulcanize/templates/passenger/config/rubber/role/passenger/passenger-apache-vhost.conf +46 -0
  131. data/generators/vulcanize/templates/passenger/config/rubber/role/passenger/passenger.conf +10 -0
  132. data/generators/vulcanize/templates/passenger/config/rubber/rubber-passenger.yml +15 -0
  133. data/generators/vulcanize/templates/passenger/templates.yml +3 -0
  134. data/generators/vulcanize/templates/redis/config/rubber/deploy-redis.rb +36 -0
  135. data/generators/vulcanize/templates/redis/config/rubber/role/redis/crontab +8 -0
  136. data/generators/vulcanize/templates/redis/config/rubber/role/redis/monit-redis.conf +9 -0
  137. data/generators/vulcanize/templates/redis/config/rubber/role/redis/redis.conf +141 -0
  138. data/generators/vulcanize/templates/redis/config/rubber/rubber-redis.yml +4 -0
  139. data/generators/vulcanize/templates/redis/templates.yml +1 -0
  140. data/generators/vulcanize/templates/resque/config/rubber/deploy-resque-worker-default.rb +38 -0
  141. data/generators/vulcanize/templates/resque/config/rubber/deploy-resque.rb +39 -0
  142. data/generators/vulcanize/templates/resque/config/rubber/role/resque_worker_default/monit-resque_worker_default.conf +19 -0
  143. data/generators/vulcanize/templates/resque/config/rubber/rubber-resque.yml +10 -0
  144. data/generators/vulcanize/templates/resque/templates.yml +3 -0
  145. data/generators/vulcanize/templates/sphinx/config/rubber/common/sphinx.yml +46 -0
  146. data/generators/vulcanize/templates/sphinx/config/rubber/deploy-sphinx.rb +112 -0
  147. data/generators/vulcanize/templates/sphinx/config/rubber/role/sphinx/crontab +11 -0
  148. data/generators/vulcanize/templates/sphinx/config/rubber/role/sphinx/monit-sphinx.conf +10 -0
  149. data/generators/vulcanize/templates/sphinx/config/rubber/rubber-sphinx.yml +6 -0
  150. data/generators/vulcanize/templates/sphinx/templates.yml +1 -0
  151. data/generators/vulcanize/vulcanize_generator.rb +67 -0
  152. data/lib/capistrano/hostcmd.rb +12 -0
  153. data/lib/rubber.rb +38 -0
  154. data/lib/rubber/capistrano.rb +1 -0
  155. data/lib/rubber/cloud.rb +13 -0
  156. data/lib/rubber/cloud/aws.rb +334 -0
  157. data/lib/rubber/cloud/base.rb +16 -0
  158. data/lib/rubber/configuration.rb +47 -0
  159. data/lib/rubber/dns.rb +13 -0
  160. data/lib/rubber/dns/base.rb +84 -0
  161. data/lib/rubber/dns/dyndns.rb +78 -0
  162. data/lib/rubber/dns/nettica.rb +117 -0
  163. data/lib/rubber/dns/zerigo.rb +174 -0
  164. data/lib/rubber/environment.rb +169 -0
  165. data/lib/rubber/generator.rb +197 -0
  166. data/lib/rubber/instance.rb +166 -0
  167. data/lib/rubber/recipes/rubber.rb +89 -0
  168. data/lib/rubber/recipes/rubber/bundles.rb +28 -0
  169. data/lib/rubber/recipes/rubber/deploy.rb +90 -0
  170. data/lib/rubber/recipes/rubber/instances.rb +393 -0
  171. data/lib/rubber/recipes/rubber/load_balancers.rb +44 -0
  172. data/lib/rubber/recipes/rubber/security_groups.rb +189 -0
  173. data/lib/rubber/recipes/rubber/setup.rb +457 -0
  174. data/lib/rubber/recipes/rubber/spot_requests.rb +17 -0
  175. data/lib/rubber/recipes/rubber/static_ips.rb +107 -0
  176. data/lib/rubber/recipes/rubber/utils.rb +203 -0
  177. data/lib/rubber/recipes/rubber/volumes.rb +264 -0
  178. data/lib/rubber/tasks/rubber.rb +279 -0
  179. data/lib/rubber/util.rb +37 -0
  180. data/rails/init.rb +9 -0
  181. data/test/environment_test.rb +133 -0
  182. data/test/generator_test.rb +323 -0
  183. data/test/instance_test.rb +93 -0
  184. data/test/test_helper.rb +8 -0
  185. data/test/util_test.rb +16 -0
  186. metadata +298 -0
@@ -0,0 +1,6 @@
1
+ gems: []
2
+
3
+ roles:
4
+ sphinx:
5
+ packages: [bzr-builddeb, autotools-dev, dpatch, libpq-dev, autoconf,
6
+ libtool, automake, gawk, libmysqlclient15-dev]
@@ -0,0 +1 @@
1
+ description: The sphinx searchd / thinking sphinx module
@@ -0,0 +1,67 @@
1
+ require 'fileutils'
2
+ require 'find'
3
+
4
+ class VulcanizeGenerator < Rails::Generator::NamedBase
5
+
6
+ TEMPLATE_ROOT = File.dirname(__FILE__) + "/templates"
7
+ TEMPLATE_FILE = "templates.yml"
8
+
9
+ def manifest
10
+ record do |m|
11
+ templates = [file_name] + actions
12
+ templates.each do |t|
13
+ apply_template(m, t)
14
+ end
15
+ end
16
+ end
17
+
18
+ def apply_template(m, name)
19
+ sp = source_path("#{name}/")
20
+ unless File.directory?(sp)
21
+ raise Rails::Generator::UsageError.new("Invalid template #{name}, use one of #{valid_templates.join(', ')}")
22
+ end
23
+
24
+ templ_conf = load_template_config(sp)
25
+ deps = templ_conf['dependent_templates'] || []
26
+ deps.each do |dep|
27
+ apply_template(m, dep)
28
+ end
29
+
30
+ Find.find(sp) do |f|
31
+ Find.prune if File.basename(f) =~ /^(CVS|\.svn)$/
32
+ Find.prune if f == "#{sp}#{TEMPLATE_FILE}"
33
+
34
+ rel = f.gsub(/#{source_root}\//, '')
35
+ dest_rel = rel.gsub(/^#{name}\//, '')
36
+ m.directory(dest_rel) if File.directory?(f)
37
+ if File.file?(f)
38
+ # force scripts to be executable
39
+ opts = (File.read(f) =~ /^#!/) ? {:chmod => 0755} : {}
40
+ m.file(rel, dest_rel, opts)
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ protected
47
+ def valid_templates
48
+ valid = Dir.entries(TEMPLATE_ROOT).delete_if {|e| e =~ /(^\.)|svn|CVS/ }
49
+ end
50
+
51
+ def load_template_config(template_dir)
52
+ templ_file = "#{template_dir}/templates.yml"
53
+ templ_conf = YAML.load(File.read(templ_file)) rescue {}
54
+ return templ_conf
55
+ end
56
+
57
+ def banner
58
+ usage = "Usage: #{$0} vulcanize template_name ...\n"
59
+ usage << "where template_name is one of:\n\n"
60
+ valid_templates.each do |t|
61
+ templ_conf = load_template_config("#{TEMPLATE_ROOT}/#{t}")
62
+ desc = templ_conf['description']
63
+ usage << " #{t}: #{desc}\n"
64
+ end
65
+ return usage
66
+ end
67
+ end
@@ -0,0 +1,12 @@
1
+ require 'capistrano/command'
2
+
3
+ # capistrano hack to allow us to run slightly different commands on multiple
4
+ # hosts in parallel
5
+ module Capistrano
6
+ class Command
7
+ def replace_placeholders(command, channel)
8
+ command = command.gsub(/\$CAPISTRANO:HOST\$/, channel[:host])
9
+ command.gsub(/\$CAPISTRANO:VAR\$/, @options["hostvar_#{channel[:host]}"].to_s)
10
+ end
11
+ end
12
+ end
data/lib/rubber.rb ADDED
@@ -0,0 +1,38 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ module Rubber
4
+
5
+ VERSION = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).chomp
6
+
7
+ def self.initialize(project_root, project_env)
8
+ return if defined?(RUBBER_ROOT) && defined?(RUBBER_ENV)
9
+
10
+ Object.const_set('RUBBER_ENV', project_env)
11
+ Object.const_set('RUBBER_ROOT', File.expand_path(project_root))
12
+
13
+ # pull in basic rails env. rubber only needs RAILS_ROOT and RAILS_ENV.
14
+ # We actually do NOT want the entire rails environment because it
15
+ # complicates bootstrap (i.e. can't run config to create db because full
16
+ # rails env needs db to exist as some plugin accesses model or something)
17
+ rails_boot_file = File.join(RUBBER_ROOT, 'config', 'boot.rb')
18
+ require(rails_boot_file) if File.exists? rails_boot_file
19
+
20
+ if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER
21
+ Object.const_set('LOGGER', RAILS_DEFAULT_LOGGER)
22
+ else
23
+ Object.const_set('LOGGER', Logger.new($stdout))
24
+ LOGGER.level = Logger::INFO
25
+ LOGGER.formatter = lambda {|severity, time, progname, msg| "Rubber[%s]: %s\n" % [severity, msg.to_s.lstrip]}
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+
32
+ require 'rubber/configuration'
33
+ require 'rubber/environment'
34
+ require 'rubber/generator'
35
+ require 'rubber/instance'
36
+ require 'rubber/util'
37
+ require 'rubber/cloud'
38
+ require 'rubber/dns'
@@ -0,0 +1 @@
1
+ Capistrano::Configuration.instance.load(File.join(File.dirname(__FILE__), 'recipes', 'rubber.rb'))
@@ -0,0 +1,13 @@
1
+ require "rubber/cloud/base"
2
+
3
+ module Rubber
4
+ module Cloud
5
+
6
+ def self.get_provider(provider, env, capistrano)
7
+ require "rubber/cloud/#{provider}"
8
+ clazz = Rubber::Cloud.const_get(provider.capitalize)
9
+ return clazz.new(env, capistrano)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,334 @@
1
+ require 'rubygems'
2
+ require 'AWS'
3
+ require 'aws/s3'
4
+
5
+ module Rubber
6
+ module Cloud
7
+
8
+ class Aws < Base
9
+
10
+ def initialize(env, capistrano)
11
+ super(env, capistrano)
12
+ @aws_env = env.cloud_providers.aws
13
+ @ec2 = AWS::EC2::Base.new(:access_key_id => @aws_env.access_key, :secret_access_key => @aws_env.secret_access_key)
14
+ @ec2elb = AWS::ELB::Base.new(:access_key_id => @aws_env.access_key, :secret_access_key => @aws_env.secret_access_key)
15
+ AWS::S3::Base.establish_connection!(:access_key_id => @aws_env.access_key, :secret_access_key => @aws_env.secret_access_key)
16
+ end
17
+
18
+ def create_instance(ami, ami_type, security_groups, availability_zone)
19
+ response = @ec2.run_instances(:image_id => ami, :key_name => @aws_env.key_name, :instance_type => ami_type, :security_group => security_groups, :availability_zone => availability_zone)
20
+ instance_id = response.instancesSet.item[0].instanceId
21
+ return instance_id
22
+ end
23
+
24
+ def create_spot_instance_request(spot_price, ami, ami_type, security_groups, availability_zone)
25
+ response = @ec2.create_spot_instances_request(:spot_price => spot_price, :image_id => ami, :key_name => @aws_env.key_name, :instance_type => ami_type, :security_group => security_groups, :availability_zone => availability_zone)
26
+ request_id = response.spotInstanceRequestSet.item[0].spotInstanceRequestId
27
+ return request_id
28
+ end
29
+
30
+ def describe_instances(instance_id=nil)
31
+ instances = []
32
+ opts = {}
33
+ opts[:instance_id] = instance_id if instance_id
34
+
35
+ response = @ec2.describe_instances(opts)
36
+ response.reservationSet.item.each do |ritem|
37
+ ritem.instancesSet.item.each do |item|
38
+ instance = {}
39
+ instance[:id] = item.instanceId
40
+ instance[:external_host] = item.dnsName
41
+ instance[:external_ip] = IPSocket.getaddress(instance[:external_host]) rescue nil
42
+ instance[:internal_host] = item.privateDnsName
43
+ instance[:state] = item.instanceState.name
44
+ instance[:zone] = item.placement.availabilityZone
45
+ instances << instance
46
+ end
47
+ end if response.reservationSet
48
+
49
+ return instances
50
+ end
51
+
52
+ def destroy_instance(instance_id)
53
+ response = @ec2.terminate_instances(:instance_id => instance_id)
54
+ end
55
+
56
+ def describe_availability_zones
57
+ zones = []
58
+ response = @ec2.describe_availability_zones()
59
+ response.availabilityZoneInfo.item.each do |item|
60
+ zone = {}
61
+ zone[:name] = item.zoneName
62
+ zone[:state] =item.zoneState
63
+ zones << zone
64
+ end if response.availabilityZoneInfo
65
+ return zones
66
+ end
67
+
68
+ def create_security_group(group_name, group_description)
69
+ @ec2.create_security_group(:group_name => group_name, :group_description => group_description)
70
+ end
71
+
72
+ def describe_security_groups(group_name=nil)
73
+ groups = []
74
+
75
+ opts = {}
76
+ opts[:group_name] = group_name if group_name
77
+ response = @ec2.describe_security_groups(opts)
78
+
79
+ response.securityGroupInfo.item.each do |item|
80
+ group = {}
81
+ group[:name] = item.groupName
82
+ group[:description] = item.groupDescription
83
+
84
+ item.ipPermissions.item.each do |ip_item|
85
+ group[:permissions] ||= []
86
+ rule = {}
87
+
88
+ rule[:protocol] = ip_item.ipProtocol
89
+ rule[:from_port] = ip_item.fromPort
90
+ rule[:to_port] = ip_item.toPort
91
+
92
+ ip_item.groups.item.each do |rule_group|
93
+ rule[:source_groups] ||= []
94
+ source_group = {}
95
+ source_group[:account] = rule_group.userId
96
+ source_group[:name] = rule_group.groupName
97
+ rule[:source_groups] << source_group
98
+ end if ip_item.groups
99
+
100
+ ip_item.ipRanges.item.each do |ip_range|
101
+ rule[:source_ips] ||= []
102
+ rule[:source_ips] << ip_range.cidrIp
103
+ end if ip_item.ipRanges
104
+
105
+ group[:permissions] << rule
106
+ end if item.ipPermissions
107
+
108
+ groups << group
109
+
110
+ end if response.securityGroupInfo
111
+
112
+ return groups
113
+ end
114
+
115
+ def add_security_group_rule(group_name, protocol, from_port, to_port, source)
116
+ opts = {:group_name => group_name}
117
+ if source.instance_of? Hash
118
+ opts = opts.merge(:source_security_group_name => source[:name], :source_security_group_owner_id => source[:account])
119
+ else
120
+ opts = opts.merge(:ip_protocol => protocol, :from_port => from_port, :to_port => to_port, :cidr_ip => source)
121
+ end
122
+ @ec2.authorize_security_group_ingress(opts)
123
+ end
124
+
125
+ def remove_security_group_rule(group_name, protocol, from_port, to_port, source)
126
+ opts = {:group_name => group_name}
127
+ if source.instance_of? Hash
128
+ opts = opts.merge(:source_security_group_name => source[:name], :source_security_group_owner_id => source[:account])
129
+ else
130
+ opts = opts.merge(:ip_protocol => protocol, :from_port => from_port, :to_port => to_port, :cidr_ip => source)
131
+ end
132
+ @ec2.revoke_security_group_ingress(opts)
133
+ end
134
+
135
+ def destroy_security_group(group_name)
136
+ @ec2.delete_security_group(:group_name => group_name)
137
+ end
138
+
139
+ def create_static_ip
140
+ response = @ec2.allocate_address()
141
+ return response.publicIp
142
+ end
143
+
144
+ def attach_static_ip(ip, instance_id)
145
+ response = @ec2.associate_address(:instance_id => instance_id, :public_ip => ip)
146
+ return response.return == "true"
147
+ end
148
+
149
+ def detach_static_ip(ip)
150
+ response = @ec2.disassociate_address(:public_ip => ip)
151
+ return response.return == "true"
152
+ end
153
+
154
+ def describe_static_ips(ip=nil)
155
+ ips = []
156
+ opts = {}
157
+ opts[:public_ip] = ip if ip
158
+ response = @ec2.describe_addresses(opts)
159
+ response.addressesSet.item.each do |item|
160
+ ip = {}
161
+ ip[:instance_id] = item.instanceId
162
+ ip[:ip] = item.publicIp
163
+ ips << ip
164
+ end if response.addressesSet
165
+ return ips
166
+ end
167
+
168
+ def destroy_static_ip(ip)
169
+ response = @ec2.release_address(:public_ip => ip)
170
+ return response.return == "true"
171
+ end
172
+
173
+ def create_volume(size, zone)
174
+ response = @ec2.create_volume(:size => size.to_s, :availability_zone => zone)
175
+ return response.volumeId
176
+ end
177
+
178
+ def attach_volume(volume_id, instance_id, device)
179
+ response = @ec2.attach_volume(:volume_id => volume_id, :instance_id => instance_id, :device => device)
180
+ return response.status
181
+ end
182
+
183
+ def detach_volume(volume_id)
184
+ @ec2.detach_volume(:volume_id => volume_id, :force => 'true')
185
+ end
186
+
187
+ def describe_volumes(volume_id=nil)
188
+ volumes = []
189
+ opts = {}
190
+ opts[:volume_id] = volume_id if volume_id
191
+ response = @ec2.describe_volumes(opts)
192
+ response.volumeSet.item.each do |item|
193
+ volume = {}
194
+ volume[:id] = item.volumeId
195
+ volume[:status] = item.status
196
+ if item.attachmentSet
197
+ attach = item.attachmentSet.item[0]
198
+ volume[:attachment_instance_id] = attach.instanceId
199
+ volume[:attachment_status] = attach.status
200
+ end
201
+ volumes << volume
202
+ end if response.volumeSet
203
+ return volumes
204
+ end
205
+
206
+ def destroy_volume(volume_id)
207
+ @ec2.delete_volume(:volume_id => volume_id)
208
+ end
209
+
210
+ def create_image(image_name)
211
+ ec2_key = @aws_env.key_file
212
+ ec2_pk = @aws_env.pk_file
213
+ ec2_cert = @aws_env.cert_file
214
+ ec2_key_dest = "/mnt/#{File.basename(ec2_key)}"
215
+ ec2_pk_dest = "/mnt/#{File.basename(ec2_pk)}"
216
+ ec2_cert_dest = "/mnt/#{File.basename(ec2_cert)}"
217
+
218
+ capistrano.put(File.read(ec2_key), ec2_key_dest)
219
+ capistrano.put(File.read(ec2_pk), ec2_pk_dest)
220
+ capistrano.put(File.read(ec2_cert), ec2_cert_dest)
221
+
222
+ arch = capistrano.capture "uname -m"
223
+ arch = case arch when /i\d86/ then "i386" else arch end
224
+
225
+ capistrano.sudo_script "create_bundle", <<-CMD
226
+ export RUBYLIB=/usr/lib/site_ruby/
227
+ nohup ec2-bundle-vol --batch -d /mnt -k #{ec2_pk_dest} -c #{ec2_cert_dest} -u #{@aws_env.account} -p #{image_name} -r #{arch} &> /tmp/ec2-bundle-vol.log &
228
+ echo "Creating image from instance volume..."
229
+ while true; do
230
+ if ! ps ax | grep -q "[e]c2-bundle-vol"; then exit; fi
231
+ echo -n .
232
+ sleep 1
233
+ done
234
+ CMD
235
+
236
+ capistrano.sudo_script "register_bundle", <<-CMD
237
+ export RUBYLIB=/usr/lib/site_ruby/
238
+ echo "Uploading image to S3..."
239
+ ec2-upload-bundle --batch -b #{@aws_env.image_bucket} -m /mnt/#{image_name}.manifest.xml -a #{@aws_env.access_key} -s #{@aws_env.secret_access_key}
240
+ CMD
241
+
242
+ image_location = "#{@aws_env.image_bucket}/#{image_name}.manifest.xml"
243
+ response = @ec2.register_image(:image_location => image_location)
244
+ return response.imageId
245
+ end
246
+
247
+ def describe_images(image_id=nil)
248
+ images = []
249
+ opts = {:owner_id => 'self'}
250
+ opts[:image_id] = image_id if image_id
251
+ response = @ec2.describe_images(opts)
252
+ response.imagesSet.item.each do |item|
253
+ image = {}
254
+ image[:id] = item.imageId
255
+ image[:location] = item.imageLocation
256
+ images << image
257
+ end if response.imagesSet
258
+ return images
259
+ end
260
+
261
+ def destroy_image(image_id)
262
+ image = describe_images(image_id).first
263
+ raise "Could not find image: #{image_id}, aborting destroy_image" if image.nil?
264
+ image_location = image[:location]
265
+ bucket = image_location.split('/').first
266
+ image_name = image_location.split('/').last.gsub(/\.manifest\.xml$/, '')
267
+
268
+ @ec2.deregister_image(:image_id => image_id)
269
+
270
+ s3_bucket = AWS::S3::Bucket.find(bucket)
271
+ s3_bucket.objects(:prefix => image_name).clone.each do |obj|
272
+ obj.delete
273
+ end
274
+ if s3_bucket.empty?
275
+ s3_bucket.delete
276
+ end
277
+ end
278
+
279
+ def destroy_spot_instance_request(request_id)
280
+ @ec2.destroy_spot_instance_requests :spot_instance_request_id => request_id
281
+ end
282
+
283
+ def describe_load_balancers(name=nil)
284
+ lbs = []
285
+ opts = {}
286
+ opts[:load_balancer_names] = name if name
287
+ response = @ec2elb.describe_load_balancers(opts)
288
+ response.describeLoadBalancersResult.member.each do |member|
289
+ lb = {}
290
+ lb[:name] = member.loadBalancerName
291
+ lb[:dns_name] = member.dNSName
292
+
293
+ member.availabilityZones.member.each do |zone|
294
+ lb[:zones] ||= []
295
+ lb[:zones] << zone
296
+ end
297
+
298
+ member.listeners.member.each do |member|
299
+ listener = {}
300
+ listener[:protocol] = member.protocol
301
+ listener[:port] = member.loadBalancerPort
302
+ listener[:instance_port] = member.instancePort
303
+ lb[:listeners] ||= []
304
+ lb[:listeners] << listener
305
+ end
306
+
307
+ lbs << lb
308
+ end if response.describeLoadBalancersResult
309
+ return lbs
310
+ end
311
+
312
+ def describe_spot_instance_requests(request_id=nil)
313
+ requests = []
314
+ opts = {}
315
+ opts[:spot_instance_request_id] = request_id if request_id
316
+ response = @ec2.describe_spot_instance_requests(opts)
317
+ response.spotInstanceRequestSet.item.each do |item|
318
+ request = {}
319
+ request[:id] = item.spotInstanceRequestId
320
+ request[:spot_price] = item.spotPrice
321
+ request[:state] = item.state
322
+ request[:created_at] = item.createTime
323
+ request[:type] = item.launchSpecification.instanceType
324
+ request[:image_id] = item.launchSpecification.imageId
325
+ request[:instance_id] = item.instanceId
326
+ requests << request
327
+ end if response.spotInstanceRequestSet
328
+ return requests
329
+ end
330
+
331
+ end
332
+
333
+ end
334
+ end