Agiley-ec2onrails 0.9.9 → 0.9.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. data/{History.txt → CHANGELOG} +0 -0
  2. data/{COPYING.txt → COPYING} +0 -0
  3. data/Manifest +161 -0
  4. data/{website/index.txt → README.textile} +33 -5
  5. data/Rakefile +36 -4
  6. data/TODO +91 -0
  7. data/ec2onrails.gemspec +279 -0
  8. data/examples/Capfile +3 -0
  9. data/examples/deploy.rb +88 -0
  10. data/examples/s3.yml +9 -0
  11. data/lib/ec2onrails/capistrano_utils.rb +0 -11
  12. data/lib/ec2onrails/recipes.rb +165 -59
  13. data/lib/ec2onrails/version.rb +1 -1
  14. data/server/build-ec2onrails.sh +44 -0
  15. data/server/files/etc/aliases +5 -0
  16. data/server/files/etc/aliases.db +0 -0
  17. data/server/files/etc/apache2/apache2.conf +295 -0
  18. data/server/files/etc/apache2/conf.d/app.proxy_cluster.conf +7 -0
  19. data/server/files/etc/apache2/conf.d/app.proxy_frontend.conf +10 -0
  20. data/server/files/etc/apache2/mods-available/proxy.conf +18 -0
  21. data/server/files/etc/apache2/sites-available/app.common +56 -0
  22. data/server/files/etc/apache2/sites-available/app.custom +0 -0
  23. data/server/files/etc/apache2/sites-available/default +14 -0
  24. data/server/files/etc/apache2/sites-available/default-ssl +18 -0
  25. data/server/files/etc/cron.d/backup_app_db_to_s3 +6 -0
  26. data/server/files/etc/cron.daily/app +9 -0
  27. data/server/files/etc/cron.daily/logrotate_post +19 -0
  28. data/server/files/etc/cron.hourly/app +10 -0
  29. data/server/files/etc/cron.monthly/app +10 -0
  30. data/server/files/etc/cron.weekly/app +10 -0
  31. data/server/files/etc/ec2onrails/balancer_members +6 -0
  32. data/server/files/etc/ec2onrails/roles.yml +5 -0
  33. data/server/files/etc/environment +2 -0
  34. data/server/files/etc/event.d/monit +13 -0
  35. data/server/files/etc/init.d/ec2-every-startup +29 -0
  36. data/server/files/etc/init.d/ec2-first-startup +36 -0
  37. data/server/files/etc/init.d/mongrel +91 -0
  38. data/server/files/etc/init.d/nginx +78 -0
  39. data/server/files/etc/init.d/set_roles +3 -0
  40. data/server/files/etc/logrotate.d/apache2 +16 -0
  41. data/server/files/etc/logrotate.d/mongrel +11 -0
  42. data/server/files/etc/logrotate.d/nginx +11 -0
  43. data/server/files/etc/memcached.conf +47 -0
  44. data/server/files/etc/mongrel_cluster/app.yml +9 -0
  45. data/server/files/etc/monit/README +5 -0
  46. data/server/files/etc/monit/app.monitrc.erb +13 -0
  47. data/server/files/etc/monit/db_primary.monitrc.erb +10 -0
  48. data/server/files/etc/monit/memcache.monitrc +8 -0
  49. data/server/files/etc/monit/monitrc +12 -0
  50. data/server/files/etc/monit/system.monitrc +15 -0
  51. data/server/files/etc/monit/web.monitrc.erb +23 -0
  52. data/server/files/etc/motd.tail +13 -0
  53. data/server/files/etc/mysql/my.cnf +149 -0
  54. data/server/files/etc/nginx/nginx.conf +296 -0
  55. data/server/files/etc/postfix/main.cf +4 -0
  56. data/server/files/etc/rc0.d/K10mongrel +1 -0
  57. data/server/files/etc/rc1.d/K10mongrel +1 -0
  58. data/server/files/etc/rc2.d/S90mongrel +1 -0
  59. data/server/files/etc/rc3.d/S90mongrel +1 -0
  60. data/server/files/etc/rc4.d/S90mongrel +1 -0
  61. data/server/files/etc/rc5.d/S90mongrel +1 -0
  62. data/server/files/etc/rc6.d/K10mongrel +1 -0
  63. data/server/files/etc/rcS.d/S91ec2-first-startup +1 -0
  64. data/server/files/etc/rcS.d/S92ec2-every-startup +1 -0
  65. data/server/files/etc/rcS.d/S99set_roles +1 -0
  66. data/server/files/etc/ssh/sshd_config +94 -0
  67. data/server/files/etc/sudoers +1 -0
  68. data/server/files/etc/sudoers.full_access +26 -0
  69. data/server/files/etc/sudoers.restricted_access +28 -0
  70. data/server/files/etc/syslog.conf +69 -0
  71. data/server/files/usr/local/ec2onrails/COPYING +339 -0
  72. data/server/files/usr/local/ec2onrails/bin/archive_file.rb +44 -0
  73. data/server/files/usr/local/ec2onrails/bin/backup_app_db.rb +68 -0
  74. data/server/files/usr/local/ec2onrails/bin/init_services.rb +57 -0
  75. data/server/files/usr/local/ec2onrails/bin/mongrel_start +8 -0
  76. data/server/files/usr/local/ec2onrails/bin/mongrel_stop +8 -0
  77. data/server/files/usr/local/ec2onrails/bin/optimize_mysql.rb +339 -0
  78. data/server/files/usr/local/ec2onrails/bin/rails_env +35 -0
  79. data/server/files/usr/local/ec2onrails/bin/rebundle.sh +70 -0
  80. data/server/files/usr/local/ec2onrails/bin/restore_app_db.rb +58 -0
  81. data/server/files/usr/local/ec2onrails/bin/set_rails_env +40 -0
  82. data/server/files/usr/local/ec2onrails/bin/set_roles.rb +76 -0
  83. data/server/files/usr/local/ec2onrails/bin/setup_web_proxy.rb +106 -0
  84. data/server/files/usr/local/ec2onrails/config +30 -0
  85. data/server/files/usr/local/ec2onrails/lib/mysql_helper.rb +82 -0
  86. data/server/files/usr/local/ec2onrails/lib/roles_helper.rb +137 -0
  87. data/server/files/usr/local/ec2onrails/lib/s3_helper.rb +126 -0
  88. data/server/files/usr/local/ec2onrails/lib/utils.rb +16 -0
  89. data/server/files/usr/local/ec2onrails/lib/vendor/ini.rb +268 -0
  90. data/server/files/usr/local/ec2onrails/startup-scripts/every-startup/get-hostname.sh +27 -0
  91. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/README +5 -0
  92. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/create-dirs.sh +42 -0
  93. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/generate-default-web-cert-and-key.sh +49 -0
  94. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/misc.sh +27 -0
  95. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/prepare-mysql-data-dir.sh +24 -0
  96. data/server/files/usr/local/ec2onrails/startup-scripts/first-startup/setup-credentials.sh +29 -0
  97. data/server/rakefile.rb +222 -0
  98. data/test/autobench.conf +60 -0
  99. data/test/spec/lib/s3_helper_spec.rb +134 -0
  100. data/test/spec/lib/s3_old.yml +3 -0
  101. data/test/spec/test_files/test1 +0 -0
  102. data/test/spec/test_files/test2 +0 -0
  103. data/test/test_app/Capfile +3 -0
  104. data/test/test_app/README +182 -0
  105. data/test/test_app/Rakefile +10 -0
  106. data/test/test_app/app/controllers/application.rb +7 -0
  107. data/test/test_app/app/controllers/db_fast_controller.rb +6 -0
  108. data/test/test_app/app/controllers/fast_controller.rb +5 -0
  109. data/test/test_app/app/controllers/slow_controller.rb +6 -0
  110. data/test/test_app/app/controllers/very_slow_controller.rb +6 -0
  111. data/test/test_app/app/helpers/application_helper.rb +3 -0
  112. data/test/test_app/app/helpers/db_fast_helper.rb +2 -0
  113. data/test/test_app/app/helpers/fast_helper.rb +2 -0
  114. data/test/test_app/app/helpers/slow_helper.rb +2 -0
  115. data/test/test_app/app/helpers/very_slow_helper.rb +2 -0
  116. data/test/test_app/config/boot.rb +109 -0
  117. data/test/test_app/config/database.yml +19 -0
  118. data/test/test_app/config/deploy.rb +21 -0
  119. data/test/test_app/config/environment.rb +60 -0
  120. data/test/test_app/config/environments/development.rb +21 -0
  121. data/test/test_app/config/environments/production.rb +18 -0
  122. data/test/test_app/config/environments/test.rb +19 -0
  123. data/test/test_app/config/routes.rb +27 -0
  124. data/test/test_app/db/schema.rb +7 -0
  125. data/test/test_app/doc/README_FOR_APP +2 -0
  126. data/test/test_app/public/404.html +30 -0
  127. data/test/test_app/public/500.html +30 -0
  128. data/test/test_app/public/dispatch.cgi +10 -0
  129. data/test/test_app/public/dispatch.fcgi +24 -0
  130. data/test/test_app/public/dispatch.rb +10 -0
  131. data/test/test_app/public/favicon.ico +0 -0
  132. data/test/test_app/public/images/rails.png +0 -0
  133. data/test/test_app/public/javascripts/application.js +2 -0
  134. data/test/test_app/public/javascripts/controls.js +963 -0
  135. data/test/test_app/public/javascripts/dragdrop.js +972 -0
  136. data/test/test_app/public/javascripts/effects.js +1120 -0
  137. data/test/test_app/public/javascripts/prototype.js +4225 -0
  138. data/test/test_app/public/robots.txt +1 -0
  139. data/test/test_app/script/about +3 -0
  140. data/test/test_app/script/breakpointer +3 -0
  141. data/test/test_app/script/console +3 -0
  142. data/test/test_app/script/destroy +3 -0
  143. data/test/test_app/script/generate +3 -0
  144. data/test/test_app/script/performance/benchmarker +3 -0
  145. data/test/test_app/script/performance/profiler +3 -0
  146. data/test/test_app/script/performance/request +3 -0
  147. data/test/test_app/script/plugin +3 -0
  148. data/test/test_app/script/process/inspector +3 -0
  149. data/test/test_app/script/process/reaper +3 -0
  150. data/test/test_app/script/process/spawner +3 -0
  151. data/test/test_app/script/runner +3 -0
  152. data/test/test_app/script/server +3 -0
  153. data/test/test_app/test/functional/db_fast_controller_test.rb +18 -0
  154. data/test/test_app/test/functional/fast_controller_test.rb +18 -0
  155. data/test/test_app/test/functional/slow_controller_test.rb +18 -0
  156. data/test/test_app/test/functional/very_slow_controller_test.rb +18 -0
  157. metadata +193 -36
  158. data/Manifest.txt +0 -25
  159. data/README.txt +0 -1
  160. data/config/hoe.rb +0 -70
  161. data/config/requirements.rb +0 -17
  162. data/script/destroy +0 -14
  163. data/script/generate +0 -14
  164. data/script/txt2html +0 -74
  165. data/tasks/deployment.rake +0 -27
  166. data/tasks/environment.rake +0 -7
  167. data/tasks/website.rake +0 -17
  168. data/website/javascripts/rounded_corners_lite.inc.js +0 -285
  169. data/website/stylesheets/screen.css +0 -144
  170. data/website/template.rhtml +0 -53
data/examples/Capfile ADDED
@@ -0,0 +1,3 @@
1
+ load 'deploy' if respond_to?(:namespace) # cap2 differentiator
2
+ load 'config/deploy'
3
+ require 'ec2onrails/recipes'
@@ -0,0 +1,88 @@
1
+ # This is a sample Capistrano config file for EC2 on Rails.
2
+ # It should be edited and customized.
3
+
4
+ set :application, "yourapp"
5
+
6
+ set :repository, "http://svn.foo.com/svn/#{application}/trunk"
7
+
8
+ # NOTE: for some reason Capistrano requires you to have both the public and
9
+ # the private key in the same folder, the public key should have the
10
+ # extension ".pub".
11
+ ssh_options[:keys] = ["#{ENV['HOME']}/.ssh/your-ec2-key"]
12
+
13
+ # Your EC2 instances. Use the ec2-xxx....amazonaws.com hostname, not
14
+ # any other name (in case you have your own DNS alias) or it won't
15
+ # be able to resolve to the internal IP address.
16
+ role :web, "ec2-12-xx-xx-xx.z-1.compute-1.amazonaws.com"
17
+ role :app, "ec2-34-xx-xx-xx.z-1.compute-1.amazonaws.com"
18
+ role :db, "ec2-56-xx-xx-xx.z-1.compute-1.amazonaws.com", :primary => true
19
+ role :memcache, "ec2-12-xx-xx-xx.z-1.compute-1.amazonaws.com"
20
+
21
+ # Whatever you set here will be taken set as the default RAILS_ENV value
22
+ # on the server. Your app and your hourly/daily/weekly/monthly scripts
23
+ # will run with RAILS_ENV set to this value.
24
+ set :rails_env, "production"
25
+
26
+ # EC2 on Rails config.
27
+ # NOTE: Some of these should be omitted if not needed.
28
+ set :ec2onrails_config, {
29
+ # S3 bucket and "subdir" used by the ec2onrails:db:restore task
30
+ :restore_from_bucket => "your-bucket",
31
+ :restore_from_bucket_subdir => "database",
32
+
33
+ # S3 bucket and "subdir" used by the ec2onrails:db:archive task
34
+ # This does not affect the automatic backup of your MySQL db to S3, it's
35
+ # just for manually archiving a db snapshot to a different bucket if
36
+ # desired.
37
+ :archive_to_bucket => "your-other-bucket",
38
+ :archive_to_bucket_subdir => "db-archive/#{Time.new.strftime('%Y-%m-%d--%H-%M-%S')}",
39
+
40
+ # Set a root password for MySQL. Run "cap ec2onrails:db:set_root_password"
41
+ # to enable this. This is optional, and after doing this the
42
+ # ec2onrails:db:drop task won't work, but be aware that MySQL accepts
43
+ # connections on the public network interface (you should block the MySQL
44
+ # port with the firewall anyway).
45
+ # If you don't care about setting the mysql root password then remove this.
46
+ :mysql_root_password => "your-mysql-root-password",
47
+
48
+ # Any extra Ubuntu packages to install if desired
49
+ # If you don't want to install extra packages then remove this.
50
+ :packages => ["logwatch", "imagemagick"],
51
+
52
+ # Any extra RubyGems to install if desired: can be "gemname" or if a
53
+ # particular version is desired "gemname -v 1.0.1"
54
+ # If you don't want to install extra rubygems then remove this
55
+ :rubygems => ["rmagick", "rfacebook -v 0.9.7"],
56
+
57
+ # Defines the web proxy that will be used. Choices are :apache or :nginx
58
+ :web_proxy_server => :apache,
59
+
60
+ # Set the server timezone. run "cap -e ec2onrails:server:set_timezone" for
61
+ # details
62
+ :timezone => "UTC",
63
+
64
+ # Files to deploy to the server (they'll be owned by root). It's intended
65
+ # mainly for customized config files for new packages installed via the
66
+ # ec2onrails:server:install_packages task. Subdirectories and files inside
67
+ # here will be placed in the same structure relative to the root of the
68
+ # server's filesystem.
69
+ # If you don't need to deploy customized config files to the server then
70
+ # remove this.
71
+ :server_config_files_root => "../server_config",
72
+
73
+ # If config files are deployed, some services might need to be restarted.
74
+ # If you don't need to deploy customized config files to the server then
75
+ # remove this.
76
+ :services_to_restart => %w(postfix sysklogd),
77
+
78
+ # Set an email address to forward admin mail messages to. If you don't
79
+ # want to receive mail from the server (e.g. monit alert messages) then
80
+ # remove this.
81
+ :mail_forward_address => "you@yourdomain.com",
82
+
83
+ # Set this if you want SSL to be enabled on the web server. The SSL cert
84
+ # and key files need to exist on the server, The cert file should be in
85
+ # /etc/ssl/certs/default.pem and the key file should be in
86
+ # /etc/ssl/private/default.key (see :server_config_files_root).
87
+ :enable_ssl => true
88
+ }
data/examples/s3.yml ADDED
@@ -0,0 +1,9 @@
1
+ staging:
2
+ aws_access_key: ABC123
3
+ aws_secret_access_key: abc123abc123abc123abc123
4
+ bucket_base_name: yourbucket
5
+
6
+ production:
7
+ aws_access_key: DEF456
8
+ aws_secret_access_key: def456def456def456def456
9
+ bucket_base_name: yourbucket
@@ -10,17 +10,6 @@ module Ec2onrails
10
10
  sudo "sh -c 'if [ -x /etc/init.d/#{script} ] ; then /etc/init.d/#{script} #{arg}; fi'"
11
11
  end
12
12
 
13
- def make_admin_role_for(role)
14
- newrole = "#{role.to_s}_admin".to_sym
15
- roles[role].each do |srv_def|
16
- options = srv_def.options.dup
17
- options[:user] = "admin"
18
- options[:port] = srv_def.port
19
- options[:no_release] = true
20
- role newrole, srv_def.host, options
21
- end
22
- end
23
-
24
13
  # return hostnames for the role named role_sym that has the specified options
25
14
  def hostnames_for_role(role_sym, options = {})
26
15
  role = roles[role_sym]
@@ -35,6 +35,9 @@ Capistrano::Configuration.instance.load do
35
35
  end
36
36
 
37
37
  cfg = ec2onrails_config
38
+
39
+ #:apache or :nginx
40
+ cfg[:web_proxy_server] ||= :apache
38
41
 
39
42
  set :ec2onrails_version, Ec2onrails::VERSION::STRING
40
43
  set :image_id_32_bit, Ec2onrails::VERSION::AMI_ID_32_BIT
@@ -43,47 +46,45 @@ Capistrano::Configuration.instance.load do
43
46
  set :use_sudo, false
44
47
  set :user, "app"
45
48
 
46
- # make an "admin" role for each role, and create arrays containing
47
- # the names of admin roles and non-admin roles for convenience
48
- set :all_admin_role_names, []
49
- set :all_non_admin_role_names, []
50
- roles.keys.clone.each do |name|
51
- make_admin_role_for(name)
52
- all_non_admin_role_names << name
53
- all_admin_role_names << "#{name.to_s}_admin".to_sym
54
- end
55
-
56
- after "deploy:symlink", "ec2onrails:server:set_roles"
57
- after "deploy:cold", "ec2onrails:db:init_backup"
49
+ #in case any changes were made to the configs, like changing the number of mongrels
50
+ after "deploy:symlink", "ec2onrails:server:set_roles", "ec2onrails:server:init_services"
51
+ after "deploy:cold", "ec2onrails:db:init_backup", "ec2onrails:db:optimize", "ec2onrails:server:restrict_sudo_access"
52
+ before "ec2onrails:server:install_gems", "ec2onrails:server:add_gem_sources"
58
53
 
59
54
  # override default start/stop/restart tasks
60
55
  namespace :deploy do
61
56
  desc <<-DESC
62
- Overrides the default Capistrano deploy:restart, uses \
57
+ Overrides the default Capistrano deploy:start, uses \
63
58
  /etc/init.d/mongrel
64
59
  DESC
65
- task :start, :roles => :app_admin do
66
- run_init_script("mongrel", "start")
67
- run "sleep 30" # give the service 30 seconds to start before attempting to monitor it
68
- sudo "monit -g app monitor all"
60
+ task :start, :roles => :app do
61
+ ec2onrails.server.allow_sudo do
62
+ run_init_script("mongrel", "start")
63
+ run "sleep 30" # give the service 30 seconds to start before attempting to monitor it
64
+ sudo "monit -g app monitor all"
65
+ end
69
66
  end
70
67
 
71
68
  desc <<-DESC
72
- Overrides the default Capistrano deploy:restart, uses \
69
+ Overrides the default Capistrano deploy:stop, uses \
73
70
  /etc/init.d/mongrel
74
71
  DESC
75
- task :stop, :roles => :app_admin do
76
- sudo "monit -g app unmonitor all"
77
- run_init_script("mongrel", "stop")
72
+ task :stop, :roles => :app do
73
+ ec2onrails.server.allow_sudo do
74
+ sudo "monit -g app unmonitor all"
75
+ run_init_script("mongrel", "stop")
76
+ end
78
77
  end
79
78
 
80
79
  desc <<-DESC
81
80
  Overrides the default Capistrano deploy:restart, uses \
82
81
  /etc/init.d/mongrel
83
82
  DESC
84
- task :restart, :roles => :app_admin do
85
- deploy.stop
86
- deploy.start
83
+ task :restart, :roles => :app do
84
+ ec2onrails.server.allow_sudo do
85
+ deploy.stop
86
+ deploy.start
87
+ end
87
88
  end
88
89
  end
89
90
 
@@ -128,12 +129,14 @@ Capistrano::Configuration.instance.load do
128
129
  desc <<-DESC
129
130
  Prepare a newly-started instance for a cold deploy.
130
131
  DESC
131
- task :setup, :roles => all_admin_role_names do
132
- server.set_admin_mail_forward_address
132
+ task :setup do
133
+ server.set_mail_forward_address
133
134
  server.set_timezone
134
135
  server.install_packages
135
136
  server.install_gems
136
137
  server.deploy_files
138
+ server.setup_web_proxy
139
+ server.set_roles
137
140
  server.enable_ssl if cfg[:enable_ssl]
138
141
  server.set_rails_env
139
142
  server.restart_services
@@ -168,12 +171,13 @@ Capistrano::Configuration.instance.load do
168
171
  DESC
169
172
  task :load_config do
170
173
  unless hostnames_for_role(:db, :primary => true).empty?
171
- db_config = YAML::load(ERB.new(File.read("config/database.yml")).result)[rails_env.to_s]
172
- cfg[:db_name] = db_config['database']
173
- cfg[:db_user] = db_config['username'] || db_config['user']
174
- cfg[:db_password] = db_config['password']
175
- cfg[:db_host] = db_config['host']
176
- cfg[:db_socket] = db_config['socket']
174
+ db_config = YAML::load(ERB.new(File.read("config/database.yml")).result)[rails_env.to_s] || {}
175
+ cfg[:db_name] ||= db_config['database']
176
+ cfg[:db_user] ||= db_config['username'] || db_config['user']
177
+ cfg[:db_password] ||= db_config['password']
178
+ cfg[:db_host] ||= db_config['host']
179
+ cfg[:db_port] ||= db_config['port']
180
+ cfg[:db_socket] ||= db_config['socket']
177
181
 
178
182
  if (cfg[:db_host].nil? || cfg[:db_host].empty?) && (cfg[:db_socket].nil? || cfg[:db_socket].empty?)
179
183
  raise "ERROR: missing database config. Make sure database.yml contains a '#{rails_env}' section with either 'host: hostname' or 'socket: /var/run/mysqld/mysqld.sock'."
@@ -199,8 +203,11 @@ Capistrano::Configuration.instance.load do
199
203
  load_config
200
204
  start
201
205
 
202
- # For some reason the default db on Hardy contains users with '' as the name.
203
- # This causes authentication problems when connecting from localhost
206
+ # remove the default test database
207
+ #run %{mysql -u root -e "drop database test; flush privileges;"}
208
+
209
+ # removing anonymous mysql accounts
210
+ run %{mysql -u root -D mysql -e "delete from db where User = ''; flush privileges;"}
204
211
  run %{mysql -u root -D mysql -e "delete from user where User = ''; flush privileges;"}
205
212
 
206
213
  run %{mysql -u root -e "create database if not exists #{cfg[:db_name]};"}
@@ -214,7 +221,7 @@ Capistrano::Configuration.instance.load do
214
221
  hasn't been set, e.g. when called from ec2onrails:setup.
215
222
  (But don't enable monitoring on it.)
216
223
  DESC
217
- task :start, :roles => :db_admin do
224
+ task :start, :roles => :db do
218
225
  sudo "chmod a+x /etc/init.d/mysql"
219
226
  # The mysql init script can fail on the first startup if mysql takes too long
220
227
  # to create the logfiles, so try again
@@ -275,6 +282,25 @@ Capistrano::Configuration.instance.load do
275
282
  task :init_backup, :roles => :db do
276
283
  run "/usr/local/ec2onrails/bin/backup_app_db.rb --reset"
277
284
  end
285
+
286
+ # do NOT run if the flag does not exist. This is placed by a startup script
287
+ # and it is only run on the first-startup. This means after the db has been
288
+ # optimized, this task will not work again.
289
+ #
290
+ # Of course you can overload it or call the file directly
291
+ task :optimize, :roles => :db do
292
+ found = capture("test -e /tmp/optimize_db_flag && echo 'file exists'") rescue false
293
+ if found
294
+ begin
295
+ sudo "/usr/local/ec2onrails/bin/optimize_mysql.rb"
296
+ ensure
297
+ sudo "rm -rf /tmp/optimize_db_flag" #remove so we cannot run again
298
+ end
299
+ else
300
+ puts "skipping as it looks like this task has already been run"
301
+ end
302
+ end
303
+
278
304
  end
279
305
 
280
306
  namespace :server do
@@ -283,18 +309,34 @@ Capistrano::Configuration.instance.load do
283
309
  the appropriate settings for each role, and starts and/or stops the \
284
310
  relevant services.
285
311
  DESC
286
- task :set_roles, :roles => all_admin_role_names do
312
+ task :set_roles do
287
313
  # TODO generate this based on the roles that actually exist so arbitrary new ones can be added
288
314
  roles = {
289
- :web => hostnames_for_role(:web),
290
- :app => hostnames_for_role(:app),
315
+ :web => hostnames_for_role(:web),
316
+ :app => hostnames_for_role(:app),
291
317
  :db_primary => hostnames_for_role(:db, :primary => true),
292
- :memcache => hostnames_for_role(:memcache)
318
+ # doing th ebelow can cause errors elsewhere unless :db is populated.
319
+ # :db => hostnames_for_role(:db),
320
+ :memcache => hostnames_for_role(:memcache)
293
321
  }
294
322
  roles_yml = YAML::dump(roles)
295
323
  put roles_yml, "/tmp/roles.yml"
296
- sudo "cp /tmp/roles.yml /etc/ec2onrails"
297
- sudo "/usr/local/ec2onrails/bin/set_roles.rb"
324
+ server.allow_sudo do
325
+ sudo "cp /tmp/roles.yml /etc/ec2onrails"
326
+ #we want everyone to be able to read to it
327
+ sudo "chmod a+r /etc/ec2onrails/roles.yml"
328
+ sudo "/usr/local/ec2onrails/bin/set_roles.rb"
329
+ end
330
+ end
331
+
332
+ task :init_services do
333
+ server.allow_sudo do
334
+ sudo "/usr/local/ec2onrails/bin/init_services.rb"
335
+ end
336
+ end
337
+
338
+ task :setup_web_proxy, :roles => :web do
339
+ sudo "/usr/local/ec2onrails/bin/setup_web_proxy.rb --mode #{cfg[:web_proxy_server].to_s}"
298
340
  end
299
341
 
300
342
  desc <<-DESC
@@ -303,7 +345,7 @@ Capistrano::Configuration.instance.load do
303
345
  for "environment". The value is specified in :rails_env.
304
346
  Be sure to do deploy:restart after this.
305
347
  DESC
306
- task :set_rails_env, :roles => all_admin_role_names do
348
+ task :set_rails_env do
307
349
  rails_env = fetch(:rails_env, "production")
308
350
  sudo "/usr/local/ec2onrails/bin/set_rails_env #{rails_env}"
309
351
  end
@@ -311,15 +353,15 @@ Capistrano::Configuration.instance.load do
311
353
  desc <<-DESC
312
354
  Upgrade to the newest versions of all Ubuntu packages.
313
355
  DESC
314
- task :upgrade_packages, :roles => all_admin_role_names do
356
+ task :upgrade_packages do
315
357
  sudo "aptitude -q update"
316
- run "export DEBIAN_FRONTEND=noninteractive; sudo aptitude -q -y safe-upgrade"
358
+ sudo "sh -c 'export DEBIAN_FRONTEND=noninteractive; aptitude -q -y safe-upgrade'"
317
359
  end
318
360
 
319
361
  desc <<-DESC
320
362
  Upgrade to the newest versions of all rubygems.
321
363
  DESC
322
- task :upgrade_gems, :roles => all_admin_role_names do
364
+ task :upgrade_gems do
323
365
  sudo "gem update --system --no-rdoc --no-ri"
324
366
  sudo "gem update --no-rdoc --no-ri" do |ch, str, data|
325
367
  ch[:data] ||= ""
@@ -339,13 +381,14 @@ Capistrano::Configuration.instance.load do
339
381
  Install extra Ubuntu packages. Set ec2onrails_config[:packages], it \
340
382
  should be an array of strings.
341
383
  NOTE: the package installation will be non-interactive, if the packages \
342
- require configuration either log in as 'admin' and run \
384
+ require configuration either log in as 'root' and run \
343
385
  'dpkg-reconfigure packagename' or replace the package's config files \
344
386
  using the 'ec2onrails:server:deploy_files' task.
345
387
  DESC
346
- task :install_packages, :roles => all_admin_role_names do
388
+ task :install_packages do
389
+ sudo "aptitude -q update"
347
390
  if cfg[:packages] && cfg[:packages].any?
348
- run "export DEBIAN_FRONTEND=noninteractive; sudo aptitude -q -y install #{cfg[:packages].join(' ')}"
391
+ sudo "sh -c 'export DEBIAN_FRONTEND=noninteractive; aptitude -q -y install #{cfg[:packages].join(' ')}'"
349
392
  end
350
393
  end
351
394
 
@@ -353,7 +396,7 @@ Capistrano::Configuration.instance.load do
353
396
  Install extra rubygems. Set ec2onrails_config[:rubygems], it should \
354
397
  be with an array of strings.
355
398
  DESC
356
- task :install_gems, :roles => all_admin_role_names do
399
+ task :install_gems do
357
400
  if cfg[:rubygems]
358
401
  cfg[:rubygems].each do |gem|
359
402
  sudo "gem install #{gem} --no-rdoc --no-ri" do |ch, str, data|
@@ -371,11 +414,23 @@ Capistrano::Configuration.instance.load do
371
414
  end
372
415
  end
373
416
 
417
+ desc <<-DESC
418
+ Add extra gem sources to rubygems (to be able to fetch gems from for example gems.github.com).
419
+ Set ec2onrails_config[:rubygems_sources], it should be with an array of strings.
420
+ DESC
421
+ task :add_gem_sources do
422
+ if cfg[:rubygems_sources]
423
+ cfg[:rubygems_sources].each do |gem_source|
424
+ sudo "gem sources -a #{gem_source}"
425
+ end
426
+ end
427
+ end
428
+
374
429
  desc <<-DESC
375
430
  A convenience task to upgrade existing packages and gems and install \
376
431
  specified new ones.
377
432
  DESC
378
- task :upgrade_and_install_all, :roles => all_admin_role_names do
433
+ task :upgrade_and_install_all do
379
434
  upgrade_packages
380
435
  upgrade_gems
381
436
  install_packages
@@ -391,7 +446,7 @@ Capistrano::Configuration.instance.load do
391
446
  directory and file as the value. For example 'Africa/Abidjan' or \
392
447
  'posix/GMT' or 'Canada/Eastern'.
393
448
  DESC
394
- task :set_timezone, :roles => all_admin_role_names do
449
+ task :set_timezone do
395
450
  if cfg[:timezone]
396
451
  sudo "bash -c 'echo #{cfg[:timezone]} > /etc/timezone'"
397
452
  sudo "cp /usr/share/zoneinfo/#{cfg[:timezone]} /etc/localtime"
@@ -406,7 +461,7 @@ Capistrano::Configuration.instance.load do
406
461
  inside here will be placed within the same directory structure \
407
462
  relative to the root of the server's filesystem.
408
463
  DESC
409
- task :deploy_files, :roles => all_admin_role_names do
464
+ task :deploy_files do
410
465
  if cfg[:server_config_files_root]
411
466
  begin
412
467
  filename = "config_files.tar"
@@ -419,7 +474,7 @@ Capistrano::Configuration.instance.load do
419
474
  sudo "tar xvf #{remote_file} -o -C /"
420
475
  ensure
421
476
  rm_rf local_file
422
- run "rm -f #{remote_file}"
477
+ sudo "rm -f #{remote_file}"
423
478
  end
424
479
  end
425
480
  end
@@ -429,7 +484,7 @@ Capistrano::Configuration.instance.load do
429
484
  to an array of strings. It's assumed that each service has a script \
430
485
  in /etc/init.d
431
486
  DESC
432
- task :restart_services, :roles => all_admin_role_names do
487
+ task :restart_services do
433
488
  if cfg[:services_to_restart] && cfg[:services_to_restart].any?
434
489
  cfg[:services_to_restart].each do |service|
435
490
  run_init_script(service, "restart")
@@ -438,10 +493,11 @@ Capistrano::Configuration.instance.load do
438
493
  end
439
494
 
440
495
  desc <<-DESC
441
- Set the email address that mail to the admin user forwards to.
496
+ Set the email address that mail to the app user forwards to.
442
497
  DESC
443
- task :set_admin_mail_forward_address, :roles => all_admin_role_names do
444
- put cfg[:admin_mail_forward_address], "/home/admin/.forward" if cfg[:admin_mail_forward_address]
498
+ task :set_mail_forward_address do
499
+ run "echo '#{cfg[:mail_forward_address]}' >> /home/app/.forward" if cfg[:mail_forward_address]
500
+ # put cfg[:admin_mail_forward_address], "/home/admin/.forward" if cfg[:admin_mail_forward_address]
445
501
  end
446
502
 
447
503
  desc <<-DESC
@@ -449,12 +505,62 @@ Capistrano::Configuration.instance.load do
449
505
  /etc/ssl/certs/default.pem and the SSL key file should be in
450
506
  /etc/ssl/private/default.key (use the deploy_files task).
451
507
  DESC
452
- task :enable_ssl, :roles => :web_admin do
508
+ task :enable_ssl, :roles => :web do
509
+ #TODO: enable for nginx
453
510
  sudo "a2enmod ssl"
454
511
  sudo "a2ensite default-ssl"
455
- run_init_script("apache2", "restart")
512
+ run_init_script("web_proxy", "restart")
513
+ end
514
+
515
+ desc <<-DESC
516
+ Restrict the main user's sudo access.
517
+ Defaults the user to only be able to \
518
+ sudo to monit
519
+ DESC
520
+ task :restrict_sudo_access do
521
+ sudo "cp -f /etc/sudoers.restricted_access /etc/sudoers"
522
+ # run "ln -sf /etc/sudoers.restricted_access /etc/sudoers"
523
+ end
524
+
525
+ desc <<-DESC
526
+ Grant *FULL* sudo access to the main user.
527
+ DESC
528
+ task :grant_sudo_access do
529
+ allow_sudo
456
530
  end
531
+
532
+ @within_sudo = 0
533
+ def allow_sudo
534
+ @within_sudo += 1
535
+ old_user = fetch(:user)
536
+ if @within_sudo > 1
537
+ yield if block_given?
538
+ true
539
+ elsif capture("ls -l /etc/sudoers /etc/sudoers.full_access | awk '{print $5}'").split.uniq.size == 1
540
+ yield if block_given?
541
+ false
542
+ else
543
+ begin
544
+ # need to cheet and temporarily set the user to ROOT so we
545
+ # can (re)grant full sudo access.
546
+ # we can do this because the root and app user have the same
547
+ # ssh login preferences....
548
+ set :user, 'root'
549
+ sessions.clear #clear out sessions cache..... this way the ssh connections are reinitialized
550
+ run "cp -f /etc/sudoers.full_access /etc/sudoers"
551
+ yield if block_given?
552
+ ensure
553
+ @within_sudo -= 1
554
+ server.restrict_sudo_access if block_given?
555
+ set :user, old_user
556
+ sessions.clear
557
+ true
558
+ end
559
+ end
560
+ end
561
+
457
562
  end
458
563
 
459
564
  end
460
565
  end
566
+