gigpark-ec2onrails 0.9.10.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. data/CHANGELOG +190 -0
  2. data/COPYING +339 -0
  3. data/Manifest +160 -0
  4. data/README.textile +233 -0
  5. data/Rakefile +31 -0
  6. data/TODO +104 -0
  7. data/ec2onrails.gemspec +46 -0
  8. data/echoe_config.rb +27 -0
  9. data/examples/Capfile +3 -0
  10. data/examples/deploy.rb +103 -0
  11. data/examples/s3.yml +9 -0
  12. data/lib/ec2onrails.rb +20 -0
  13. data/lib/ec2onrails/capistrano_utils.rb +47 -0
  14. data/lib/ec2onrails/recipes.rb +146 -0
  15. data/lib/ec2onrails/recipes/db.rb +366 -0
  16. data/lib/ec2onrails/recipes/deploy.rb +26 -0
  17. data/lib/ec2onrails/recipes/server.rb +517 -0
  18. data/lib/ec2onrails/version.rb +29 -0
  19. data/server/build +74 -0
  20. data/server/files/etc/README +7 -0
  21. data/server/files/etc/aliases +5 -0
  22. data/server/files/etc/cron.d/ec2onrails +16 -0
  23. data/server/files/etc/cron.daily/app +31 -0
  24. data/server/files/etc/cron.daily/logrotate_post +25 -0
  25. data/server/files/etc/cron.hourly/app +31 -0
  26. data/server/files/etc/cron.monthly/app +31 -0
  27. data/server/files/etc/cron.weekly/app +31 -0
  28. data/server/files/etc/default/varnish +33 -0
  29. data/server/files/etc/default/varnishncsa +11 -0
  30. data/server/files/etc/denyhosts.conf +628 -0
  31. data/server/files/etc/dpkg/dpkg.cfg +13 -0
  32. data/server/files/etc/ec2onrails/rails_env +1 -0
  33. data/server/files/etc/ec2onrails/roles.yml +5 -0
  34. data/server/files/etc/environment +2 -0
  35. data/server/files/etc/event.d/god +8 -0
  36. data/server/files/etc/god/db_primary.god +16 -0
  37. data/server/files/etc/god/dkim_filter.god +17 -0
  38. data/server/files/etc/god/master.conf +40 -0
  39. data/server/files/etc/god/memcache.god +15 -0
  40. data/server/files/etc/god/notifications.god +11 -0
  41. data/server/files/etc/god/proxy.god +40 -0
  42. data/server/files/etc/god/system.god +37 -0
  43. data/server/files/etc/god/web.god +23 -0
  44. data/server/files/etc/init.d/ec2-every-startup +29 -0
  45. data/server/files/etc/init.d/ec2-first-startup +37 -0
  46. data/server/files/etc/init.d/nginx +78 -0
  47. data/server/files/etc/logrotate.d/nginx +10 -0
  48. data/server/files/etc/logrotate.d/rails +8 -0
  49. data/server/files/etc/logrotate.d/varnish +14 -0
  50. data/server/files/etc/memcached.conf +47 -0
  51. data/server/files/etc/motd.tail +13 -0
  52. data/server/files/etc/mysql/my.cnf +152 -0
  53. data/server/files/etc/nginx/conf.d/custom.conf +1 -0
  54. data/server/files/etc/nginx/nginx.conf.erb +152 -0
  55. data/server/files/etc/postfix/main.cf +4 -0
  56. data/server/files/etc/ssh/sshd_config +96 -0
  57. data/server/files/etc/sudoers +31 -0
  58. data/server/files/etc/syslog.conf +63 -0
  59. data/server/files/etc/varnish/default.vcl.erb +140 -0
  60. data/server/files/usr/local/ec2onrails/COPYING +339 -0
  61. data/server/files/usr/local/ec2onrails/bin/archive_file +44 -0
  62. data/server/files/usr/local/ec2onrails/bin/backup_app_db +160 -0
  63. data/server/files/usr/local/ec2onrails/bin/backup_dir +89 -0
  64. data/server/files/usr/local/ec2onrails/bin/ec2_meta_data +80 -0
  65. data/server/files/usr/local/ec2onrails/bin/exec_runner +76 -0
  66. data/server/files/usr/local/ec2onrails/bin/in_role +33 -0
  67. data/server/files/usr/local/ec2onrails/bin/init_services +43 -0
  68. data/server/files/usr/local/ec2onrails/bin/install_system_files +56 -0
  69. data/server/files/usr/local/ec2onrails/bin/optimize_mysql +346 -0
  70. data/server/files/usr/local/ec2onrails/bin/public-hostname +24 -0
  71. data/server/files/usr/local/ec2onrails/bin/rails_env +33 -0
  72. data/server/files/usr/local/ec2onrails/bin/rebundle +62 -0
  73. data/server/files/usr/local/ec2onrails/bin/restore_app_db +58 -0
  74. data/server/files/usr/local/ec2onrails/bin/set_rails_env +36 -0
  75. data/server/files/usr/local/ec2onrails/bin/set_roles +36 -0
  76. data/server/files/usr/local/ec2onrails/bin/uninstall_system_files +34 -0
  77. data/server/files/usr/local/ec2onrails/config +30 -0
  78. data/server/files/usr/local/ec2onrails/lib/aws_helper.rb +75 -0
  79. data/server/files/usr/local/ec2onrails/lib/god_helper.rb +84 -0
  80. data/server/files/usr/local/ec2onrails/lib/mysql_helper.rb +100 -0
  81. data/server/files/usr/local/ec2onrails/lib/roles_helper.rb +156 -0
  82. data/server/files/usr/local/ec2onrails/lib/s3_helper.rb +116 -0
  83. data/server/files/usr/local/ec2onrails/lib/system_files_helper.rb +110 -0
  84. data/server/files/usr/local/ec2onrails/lib/system_files_manifest.rb +107 -0
  85. data/server/files/usr/local/ec2onrails/lib/utils.rb +26 -0
  86. data/server/files/usr/local/ec2onrails/lib/vendor/ini.rb +268 -0
  87. data/server/files/usr/local/ec2onrails/startup-scripts/every-startup/README +5 -0
  88. data/server/files/usr/local/ec2onrails/startup-scripts/every-startup/create-mysqld-pid-dir +26 -0
  89. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/README +5 -0
  90. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/create-dirs +45 -0
  91. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/generate-default-web-cert-and-key +49 -0
  92. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/get-hostname +27 -0
  93. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/misc +27 -0
  94. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/prepare-mysql-data-dir +24 -0
  95. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/setup-credentials +32 -0
  96. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/setup-file-permissions +30 -0
  97. data/server/rakefile-wrapper +26 -0
  98. data/server/rakefile.rb +267 -0
  99. data/test/autobench.conf +60 -0
  100. data/test/spec/lib/s3_helper_spec.rb +154 -0
  101. data/test/spec/lib/s3_old.yml +3 -0
  102. data/test/spec/lib/system_files_manifest_spec.rb +122 -0
  103. data/test/spec/test_files/system_files1/_manifest +13 -0
  104. data/test/spec/test_files/system_files1/test1 +0 -0
  105. data/test/spec/test_files/system_files1/test2 +0 -0
  106. data/test/spec/test_files/system_files1/testfolder/test3 +0 -0
  107. data/test/spec/test_files/system_files2/_manifest +2 -0
  108. data/test/spec/test_files/system_files2/test1 +0 -0
  109. data/test/spec/test_files/system_files2/test2 +0 -0
  110. data/test/spec/test_files/system_files2/testfolder/test3 +0 -0
  111. data/test/spec/test_files/test2 +0 -0
  112. data/test/test_app/Capfile +3 -0
  113. data/test/test_app/README +243 -0
  114. data/test/test_app/Rakefile +10 -0
  115. data/test/test_app/app/controllers/application_controller.rb +10 -0
  116. data/test/test_app/app/controllers/db_fast_controller.rb +6 -0
  117. data/test/test_app/app/controllers/fast_controller.rb +5 -0
  118. data/test/test_app/app/controllers/slow_controller.rb +6 -0
  119. data/test/test_app/app/controllers/very_slow_controller.rb +6 -0
  120. data/test/test_app/app/helpers/application_helper.rb +3 -0
  121. data/test/test_app/config/boot.rb +110 -0
  122. data/test/test_app/config/database.yml +24 -0
  123. data/test/test_app/config/deploy.rb +22 -0
  124. data/test/test_app/config/ec2onrails/config.rb +5 -0
  125. data/test/test_app/config/environment.rb +41 -0
  126. data/test/test_app/config/environments/development.rb +17 -0
  127. data/test/test_app/config/environments/production.rb +28 -0
  128. data/test/test_app/config/environments/test.rb +28 -0
  129. data/test/test_app/config/initializers/backtrace_silencers.rb +7 -0
  130. data/test/test_app/config/initializers/inflections.rb +10 -0
  131. data/test/test_app/config/initializers/mime_types.rb +5 -0
  132. data/test/test_app/config/initializers/new_rails_defaults.rb +19 -0
  133. data/test/test_app/config/initializers/session_store.rb +15 -0
  134. data/test/test_app/config/locales/en.yml +5 -0
  135. data/test/test_app/config/routes.rb +43 -0
  136. data/test/test_app/doc/README_FOR_APP +2 -0
  137. data/test/test_app/public/404.html +30 -0
  138. data/test/test_app/public/422.html +30 -0
  139. data/test/test_app/public/500.html +30 -0
  140. data/test/test_app/public/favicon.ico +0 -0
  141. data/test/test_app/public/images/rails.png +0 -0
  142. data/test/test_app/public/index.html +275 -0
  143. data/test/test_app/public/javascripts/application.js +2 -0
  144. data/test/test_app/public/javascripts/controls.js +963 -0
  145. data/test/test_app/public/javascripts/dragdrop.js +973 -0
  146. data/test/test_app/public/javascripts/effects.js +1128 -0
  147. data/test/test_app/public/javascripts/prototype.js +4320 -0
  148. data/test/test_app/public/robots.txt +5 -0
  149. data/test/test_app/script/about +4 -0
  150. data/test/test_app/script/console +3 -0
  151. data/test/test_app/script/dbconsole +3 -0
  152. data/test/test_app/script/destroy +3 -0
  153. data/test/test_app/script/generate +3 -0
  154. data/test/test_app/script/performance/benchmarker +3 -0
  155. data/test/test_app/script/performance/profiler +3 -0
  156. data/test/test_app/script/plugin +3 -0
  157. data/test/test_app/script/runner +3 -0
  158. data/test/test_app/script/server +3 -0
  159. data/test/test_app/test/performance/browsing_test.rb +9 -0
  160. data/test/test_app/test/test_helper.rb +38 -0
  161. metadata +266 -0
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # This file is part of EC2 on Rails.
4
+ # http://rubyforge.org/projects/ec2onrails/
5
+ #
6
+ # Copyright 2007 Paul Dowman, http://pauldowman.com/
7
+ #
8
+ # EC2 on Rails is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # EC2 on Rails is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+
21
+ # This script archives a file to S3
22
+
23
+ require "rubygems"
24
+ require "optiflag"
25
+ require "#{File.dirname(__FILE__)}/../lib/s3_helper"
26
+ require "#{File.dirname(__FILE__)}/../lib/utils"
27
+
28
+ include FileUtils
29
+
30
+ module CommandLineArgs extend OptiFlagSet
31
+ optional_flag "bucket"
32
+ optional_flag "dir"
33
+ optional_flag "file"
34
+ and_process!
35
+ end
36
+
37
+ # include the hostname in the bucket name so test instances don't accidentally clobber real backups
38
+ bucket = ARGV.flags.bucket
39
+ dir = ARGV.flags.dir
40
+ file = ARGV.flags.file
41
+ exit unless File.exists?(file)
42
+
43
+ @s3 = Ec2onrails::S3Helper.new(bucket, dir)
44
+ @s3.store_file(file)
@@ -0,0 +1,160 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # This file is part of EC2 on Rails.
4
+ # http://rubyforge.org/projects/ec2onrails/
5
+ #
6
+ # Copyright 2007 Paul Dowman, http://pauldowman.com/
7
+ #
8
+ # EC2 on Rails is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # EC2 on Rails is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+
21
+ #exit if the current application is not deployed, meaning there is nothing to back up!
22
+ exit unless File.exists?("/mnt/app/current")
23
+
24
+ require "rubygems"
25
+ require "optiflag"
26
+ require "fileutils"
27
+ require 'EC2'
28
+ require "#{File.dirname(__FILE__)}/../lib/mysql_helper"
29
+ require "#{File.dirname(__FILE__)}/../lib/s3_helper"
30
+ require "#{File.dirname(__FILE__)}/../lib/aws_helper"
31
+ require "#{File.dirname(__FILE__)}/../lib/roles_helper"
32
+
33
+ require "#{File.dirname(__FILE__)}/../lib/utils"
34
+
35
+ # Only run if this instance is the db_pimrary
36
+ # The original code would run on any instance that had /etc/init.d/mysql
37
+ # Which was pretty much all instances no matter what role
38
+ include Ec2onrails::RolesHelper
39
+ exit unless in_role?(:db_primary)
40
+
41
+
42
+ module CommandLineArgs extend OptiFlagSet
43
+ optional_flag "bucket"
44
+ optional_flag "dir"
45
+ optional_switch_flag "incremental"
46
+ optional_switch_flag "reset"
47
+ optional_switch_flag "no_ebs"
48
+ and_process!
49
+ end
50
+ @mysql = Ec2onrails::MysqlHelper.new
51
+
52
+
53
+ if File.exists?("/etc/mysql/conf.d/mysql-ec2-ebs.cnf") && !ARGV.flags.no_ebs
54
+ # we have ebs enabled....
55
+
56
+ @aws = Ec2onrails::AwsHelper.new
57
+ vols = YAML::load(File.read("/etc/ec2onrails/ebs_info.yml"))
58
+ ec2 = EC2::Base.new( :access_key_id => @aws.aws_access_key, :secret_access_key => @aws.aws_secret_access_key )
59
+
60
+ #lets make sure we have space: AMAZON puts a 500 limit on the number of snapshots
61
+ snaps = ec2.describe_snapshots['snapshotSet']['item'] rescue nil
62
+ if snaps && snaps.size > 450
63
+ # TODO:
64
+ # can we make this a bit smarter? With a limit of 500, that is difficult.
65
+ # possible ideas (and some commented out code below)
66
+ # * only apply cleanups to the volume_ids attached to this instance
67
+ # * keep the last week worth (at hrly snapshots), then daily for a month, then monthly
68
+ # * a sweeper task
69
+ #
70
+ # vol_ids = []
71
+ # vols.each_pair{|k,v| vol_ids << v['volume_id']}
72
+ # #lets only work on those that apply for these volumnes attached
73
+ # snaps = snaps.collect{|sn| vol_ids.index(sn['volumeId']) ? sn : nil}.compact
74
+ # # get them sorted
75
+ snaps = snaps.sort_by{|snapshot| snapshot['startTime']}.reverse
76
+ curr_batch = {}
77
+ remaining = []
78
+ snaps[200..-1].each do |sn|
79
+ next if sn.blank? || sn['status'] != 'completed'
80
+ today = Date.parse(sn['startTime']).to_s
81
+ if curr_batch[sn['volumeId']] != today
82
+ curr_batch[sn['volumeId']] = today
83
+ remaining << sn
84
+ else
85
+ ec2.delete_snapshot(:snapshot_id => sn['snapshotId'])
86
+ end
87
+ # next unless vol_ids.index(sn['volumeId'])
88
+ end
89
+ if remaining.size > 400
90
+ puts " WARNING: still contains #{remaining.size} snapshots; removing the oldest 100 to clean up space"
91
+ remaining[350..-1].each do |sn|
92
+ ec2.delete_snapshot(:snapshot_id => sn['snapshotId'])
93
+ end
94
+ end
95
+ else
96
+ puts "Could not retrieve snapshots: auto archiving cleanup will not occur" unless snaps
97
+ end
98
+
99
+ @mysql.execute do |conn|
100
+ begin
101
+ conn.query "FLUSH TABLES WITH READ LOCK;"
102
+
103
+ res = conn.query "SHOW MASTER STATUS"
104
+ logfile, position = res.fetch_row[0..1]
105
+ # puts "Snapshot occuring at: log: #{logfile}, #{position}"
106
+ vols.each_pair do |mount, ebs_info|
107
+ begin
108
+ `sudo xfs_freeze -f #{mount}`
109
+ output = ec2.create_snapshot(:volume_id => ebs_info['volume_id'])
110
+ snap_id = output['CreateSnapshotResponse']['snapshotId'] rescue nil
111
+ snap_id ||= output['snapshotId'] rescue nil #this is for the old version of the amazon-ec2
112
+ if snap_id.nil? || snap_id.empty?
113
+ puts "Snapshot for #{ebs_info['volume_id']} FAILED"
114
+ exit
115
+ end
116
+ vol_id = ebs_info['volume_id']
117
+ ensure
118
+ `sudo xfs_freeze -u #{mount}`
119
+ end
120
+ end
121
+ ensure
122
+ conn.query <<-SQL
123
+ UNLOCK TABLES;
124
+ SQL
125
+ end
126
+ end
127
+ else
128
+ #not persisted, so lets push the binary log files to s3
129
+ # include the hostname in the bucket name so test instances don't accidentally clobber real backups
130
+ bucket = ARGV.flags.bucket
131
+ dir = ARGV.flags.dir || "database"
132
+ @s3 = Ec2onrails::S3Helper.new(bucket, dir)
133
+ @temp_dir = "/mnt/tmp/ec2onrails-backup-#{@s3.bucket_name}-#{dir.gsub(/\//, "-")}"
134
+ if File.exists?(@temp_dir)
135
+ puts "Temp dir exists (#{@temp_dir}), aborting. Is another backup process running?"
136
+ exit
137
+ end
138
+
139
+ begin
140
+ FileUtils.mkdir_p @temp_dir
141
+ if ARGV.flags.incremental
142
+ # Incremental backup
143
+ @mysql.execute_sql "flush logs"
144
+ logs = Dir.glob("/mnt/log/mysql/mysql-bin.[0-9]*").sort
145
+ logs_to_archive = logs[0..-2] # all logs except the last
146
+ logs_to_archive.each {|log| @s3.store_file log}
147
+ @mysql.execute_sql "purge master logs to '#{File.basename(logs[-1])}'"
148
+ else
149
+ # Full backup
150
+ file = "#{@temp_dir}/dump.sql.gz"
151
+ @mysql.dump(file, ARGV.flags.reset)
152
+ @s3.store_file file
153
+ @s3.delete_files("mysql-bin")
154
+ end
155
+ ensure
156
+ FileUtils.rm_rf(@temp_dir)
157
+ end
158
+
159
+ end
160
+
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # This file is part of EC2 on Rails.
4
+ # http://rubyforge.org/projects/ec2onrails/
5
+ #
6
+ # Copyright 2007 Paul Dowman, http://pauldowman.com/
7
+ #
8
+ # EC2 on Rails is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # EC2 on Rails is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+
21
+ require "rubygems"
22
+ require "optiflag"
23
+ require "fileutils"
24
+ require 'EC2'
25
+ require "#{File.dirname(__FILE__)}/../lib/mysql_helper"
26
+ require "#{File.dirname(__FILE__)}/../lib/s3_helper"
27
+ require "#{File.dirname(__FILE__)}/../lib/aws_helper"
28
+ require "#{File.dirname(__FILE__)}/../lib/roles_helper"
29
+
30
+ require "#{File.dirname(__FILE__)}/../lib/utils"
31
+
32
+ # Only run if this instance is the db_pimrary
33
+ # The original code would run on any instance that had /etc/init.d/mysql
34
+ # Which was pretty much all instances no matter what role
35
+ include Ec2onrails::RolesHelper
36
+
37
+
38
+ module CommandLineArgs extend OptiFlagSet
39
+ curr_env = Ec2onrails::Utils.rails_env
40
+ default_bucket = "#{curr_env}_backup"
41
+
42
+ flag "dir" do
43
+ description "the directory that will be tarred and compressed and put on S3 with the name DIR_#{Ec2onrails::Utils.hostname}_TIMESTAMP.tgz"
44
+ end
45
+
46
+ optional_flag "role" do
47
+ description "The role of this server, as defined by capistrano. ex. 'db', or 'app' If not used, will be applied to all roles"
48
+ end
49
+
50
+ optional_flag "only_env" do
51
+ description "Only apply the script if it is running within this environment"
52
+ end
53
+
54
+ optional_flag "bucket" do
55
+ description "The s3 bucket you would like to save this backup to. Will default to #{default_bucket}"
56
+ end
57
+
58
+ optional_switch_flag "v" do
59
+ description "let you know if the script stopped because it was running in either a different role or environment than the one specified"
60
+ end
61
+
62
+ and_process!
63
+ end
64
+ curr_env = Ec2onrails::Utils.rails_env
65
+ default_bucket = "#{curr_env}_backup"
66
+
67
+ verbose = ARGV.flags.v
68
+ dir = ARGV.flags.dir
69
+ bucket = ARGV.flags.bucket || default_bucket
70
+ curr_env = Ec2onrails::Utils.rails_env
71
+ default_bucket = "#{curr_env}_backup"
72
+
73
+ if ARGV.flags.role && !in_role?(ARGV.flags.role.sub(/^:/, '').to_sym)
74
+ puts "This script is not being run because the server is not running under the #{role} role" if verbose
75
+ exit
76
+ end
77
+
78
+ if ARGV.flags.only_env && ARGV.flags.only_env.strip.downcase != curr_env.strip.downcase
79
+ puts "This script is not being run because the server is not running under the #{curr_env} environment" if verbose
80
+ exit
81
+ end
82
+
83
+ if !dir || File.exists?(dir)
84
+ puts "The directory '#{dir}' does not exist. Please enter a valid, full path to a directory you would like backed up" if verbose
85
+ end
86
+
87
+
88
+ @s3 = Ec2onrails::S3Helper.new(bucket, dir)
89
+ @s3.store_dir(dir, :compress => true)
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # This file is part of EC2 on Rails.
4
+ # http://rubyforge.org/projects/ec2onrails/
5
+ #
6
+ # Copyright 2007 Paul Dowman, http://pauldowman.com/
7
+ #
8
+ # EC2 on Rails is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # EC2 on Rails is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ #
21
+ #
22
+ # This file helps in setting up and retrieving ec2 meta-data
23
+ # If a key is passed in, it will only retrieve that key
24
+ # if not, then it will return all of the meta-data formatted in yaml
25
+ #
26
+
27
+ require "rubygems"
28
+ require "optiflag"
29
+ require 'yaml'
30
+ require "#{File.dirname(__FILE__)}/../lib/roles_helper"
31
+ include Ec2onrails::RolesHelper
32
+
33
+ CURL_OPTS = "-s -S -f -L --retry 7"
34
+ META_URL = "http://169.254.169.254/latest/meta-data"
35
+
36
+
37
+ module CommandLineArgs extend OptiFlagSet
38
+ optional_flag "key" do
39
+ description "The ec2 meta-data value you would like to look up"
40
+ end
41
+ and_process!
42
+ end
43
+
44
+
45
+ def process_files(files, root_url="")
46
+ output = {}
47
+ files.split.each do |file|
48
+ if file =~ /\/$/
49
+ output.merge! process_files(`curl #{CURL_OPTS} #{META_URL}/#{file}`, "#{file}")
50
+ else
51
+ if file =~ /=/
52
+ key, data = file.split('=')
53
+ else
54
+ url = "#{META_URL}/#{root_url}/#{file}"
55
+ data = `curl #{CURL_OPTS} #{url}`
56
+ raise "Failed to fetch entry #{file}: code #{$?.exitstatus} -- #{data}" unless $?.success?
57
+ end
58
+ if root_url.nil? || root_url.strip.length == 0
59
+ output[file] = data
60
+ else
61
+ output[root_url] ||= {}
62
+ output[root_url][file] = data
63
+ end
64
+ end
65
+ end
66
+ output
67
+ end
68
+
69
+
70
+
71
+ if ARGV.flags.key
72
+ puts get_metadata(ARGV.flags.key)
73
+ else
74
+
75
+ files = `curl #{CURL_OPTS} #{META_URL}/`
76
+ raise "Failed to fetch directory: code #{$?.exitstatus} -- #{files}" unless $?.success?
77
+ val = process_files(files)
78
+ puts val.to_yaml
79
+ end
80
+
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # This file is part of EC2 on Rails.
4
+ # http://rubyforge.org/projects/ec2onrails/
5
+ #
6
+ # Copyright 2007 Paul Dowman, http://pauldowman.com/
7
+ #
8
+ # EC2 on Rails is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # EC2 on Rails is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+ #
21
+ # This script allows you to ONLY execute within a shell IF the role of
22
+ # the server (as defined by capistrano) matches the role specified.
23
+ # ex.
24
+ # * exec_runner -role :db -exec 'echo "hello world"'
25
+ # # will only run if the server is in the db role
26
+ # * exec_runner -role :web -exec /some/app/specific/script/to/call
27
+ #
28
+
29
+ require "rubygems"
30
+ require "optiflag"
31
+
32
+ require "fileutils"
33
+ require "#{File.dirname(__FILE__)}/../lib/roles_helper"
34
+ require "#{File.dirname(__FILE__)}/../lib/utils"
35
+
36
+ include Ec2onrails::RolesHelper
37
+
38
+ module CommandLineArgs extend OptiFlagSet
39
+ optional_flag "role" do
40
+ description "The role of this server, as defined by capistrano. ex. 'db', or 'app' If not used, will be applied to all roles"
41
+ end
42
+
43
+ optional_flag "only_env" do
44
+ description "Only apply the script if it is running within this environment"
45
+ end
46
+
47
+ flag "exec" do
48
+ description "what to run if the role of the server matches the -role passed in"
49
+ end
50
+
51
+ optional_switch_flag "v" do
52
+ description "let you know if the script stopped because it was running in either a different role or environment than the one specified"
53
+ end
54
+
55
+ and_process!
56
+ end
57
+
58
+ #strip out the ':', in case the user enters ':db', or ':web'
59
+ if ARGV.flags.role && !in_role?(ARGV.flags.role.sub(/^:/, '').to_sym)
60
+ puts "This script is not being run because the server is not running under the #{role} role" if ARGV.flags.v
61
+ exit
62
+ end
63
+
64
+ curr_env = Ec2onrails::Utils.rails_env
65
+ if ARGV.flags.only_env && ARGV.flags.only_env.strip.downcase != curr_env.strip.downcase
66
+ puts "This script is not being run because the server is not running under the #{curr_env} environment" if ARGV.flags.v
67
+ exit
68
+ end
69
+
70
+
71
+ # set the default to the current directory; makes it easier to
72
+ Dir.chdir('/mnt/app/current')
73
+
74
+ ENV['RAILS_ENV'] = curr_env
75
+ run ARGV.flags.exec
76
+