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,26 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+
3
+ # Override default start/stop/restart tasks for Passenger
4
+ namespace :deploy do
5
+ desc <<-DESC
6
+ Overrides the default Capistrano deploy:start.
7
+ DESC
8
+ task :start, :roles => :web do
9
+ run "touch #{current_release}/tmp/restart.txt"
10
+ end
11
+
12
+ desc <<-DESC
13
+ Overrides the default Capistrano deploy:stop.
14
+ DESC
15
+ task :stop, :roles => :web do
16
+ # Do nothing,
17
+ end
18
+
19
+ desc <<-DESC
20
+ Overrides the default Capistrano deploy:restart.
21
+ DESC
22
+ task :restart, :roles => :web do
23
+ run "touch #{current_release}/tmp/restart.txt"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,517 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ cfg = ec2onrails_config
3
+
4
+ namespace :ec2onrails do
5
+ namespace :server do
6
+ desc <<-DESC
7
+ Tell the servers what roles they are in. This configures them with \
8
+ the appropriate settings for each role, and starts and/or stops the \
9
+ relevant services.
10
+ DESC
11
+ task :set_roles do
12
+ # Create a list of server roles based on the capistrano roles.
13
+ # We treat :db specially because it maps to a different rolename on the server
14
+ # if :primary => true
15
+ server_roles = {}
16
+ roles.keys.delete_if{|r|r == :db}.each do |rolename|
17
+ server_roles[rolename] = hostnames_for_role(rolename)
18
+ end
19
+ server_roles[:db_primary] = hostnames_for_role(:db, :primary => true)
20
+
21
+ roles_yml = YAML::dump(server_roles)
22
+ put roles_yml, "/tmp/roles.yml"
23
+ allow_sudo do
24
+ sudo "cp /tmp/roles.yml /etc/ec2onrails"
25
+ #we want everyone to be able to read to it
26
+ sudo "chmod a+r /etc/ec2onrails/roles.yml"
27
+ sudo "/usr/local/ec2onrails/bin/set_roles"
28
+ end
29
+ end
30
+
31
+ task :init_services do
32
+ allow_sudo do
33
+ #lets pick up the new configuration files
34
+ sudo "/usr/local/ec2onrails/bin/init_services"
35
+ end
36
+ end
37
+
38
+ desc <<-DESC
39
+ Change the default value of RAILS_ENV on the server.
40
+ The value is specified in :rails_env.
41
+ Be sure to do deploy:restart after this.
42
+ DESC
43
+ task :set_rails_env do
44
+ allow_sudo do
45
+ rails_env = fetch(:rails_env, "production")
46
+ sudo "/usr/local/ec2onrails/bin/set_rails_env #{rails_env}"
47
+ end
48
+ end
49
+
50
+ desc <<-DESC
51
+ Upgrade to the newest versions of all Ubuntu packages.
52
+ DESC
53
+ task :upgrade_packages do
54
+ allow_sudo do
55
+ sudo "aptitude -q update"
56
+ sudo "sh -c 'export DEBIAN_FRONTEND=noninteractive; aptitude -q -y safe-upgrade'"
57
+ end
58
+ end
59
+
60
+ desc <<-DESC
61
+ Upgrade to the newest versions of all rubygems.
62
+ DESC
63
+ task :upgrade_gems do
64
+ allow_sudo do
65
+ sudo "gem update --system --no-rdoc --no-ri"
66
+ sudo "gem update --no-rdoc --no-ri"
67
+ end
68
+ end
69
+
70
+ desc <<-DESC
71
+ Install extra Ubuntu packages. Set ec2onrails_config[:packages], it \
72
+ should be an array of strings.
73
+ NOTE: the package installation will be non-interactive, if the packages \
74
+ require configuration either set ec2onrails_config[:interactive_packages] \
75
+ like you would for ec2onrails_config[:packages] (we'll flood the server \
76
+ with 'Y' inputs), or log in as 'root' and run \
77
+ 'dpkg-reconfigure packagename' or replace the package's config files \
78
+ using the 'ec2onrails:server:deploy_files' task.
79
+ DESC
80
+ task :install_packages do
81
+ allow_sudo do
82
+ sudo "aptitude -q update"
83
+ if cfg[:packages] && cfg[:packages].any?
84
+ sudo "sh -c 'export DEBIAN_FRONTEND=noninteractive; aptitude -q -y install #{cfg[:packages].join(' ')}'"
85
+ end
86
+ if cfg[:interactive_packages] && cfg[:interactive_packages].any?
87
+ # sudo "aptitude install #{cfg[:interactive_packages].join(' ')}", {:env => {'DEBIAN_FRONTEND' => 'readline'} }
88
+ #trying to pick WHEN to send a Y is a bit tricky...it totally depends on the
89
+ #interactive package you want to install. FLOODING it with 'Y'... but not sure how
90
+ #'correct' or robust this is
91
+ cmd = "sudo sh -c 'export DEBIAN_FRONTEND=readline; aptitude -y -q install #{cfg[:interactive_packages].join(' ')}'"
92
+ run(cmd) do |channel, stream, data|
93
+ channel.send_data "Y\n"
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ task :configure_firewall do
100
+ # TODO
101
+ end
102
+
103
+
104
+ desc <<-DESC
105
+ Provide extra security measures. Set ec2onrails_config[:harden_server] = true \
106
+ to allow the hardening of the server.
107
+ These security measures are those which can make initial setup and playing around
108
+ with Ec2onRails tricky. For example, you can be logged out of your server forever
109
+ DESC
110
+ task :harden_server do
111
+ #NOTES: for those security features that will get in the way of ease-of-use
112
+ # hook them in here
113
+ # Like encrypting the mnt directory
114
+ # http://groups.google.com/group/ec2ubuntu/web/encrypting-mnt-using-cryptsetup-on-ubuntu-7-10-gutsy-on-amazon-ec2
115
+ if cfg[:harden_server]
116
+ #lets install some extra packages:
117
+ # denyhosts: sshd security tool. config file is already installed...
118
+ #
119
+ security_pkgs = %w{denyhosts}
120
+ allow_sudo do
121
+ sudo "sh -c 'export DEBIAN_FRONTEND=noninteractive; aptitude -q -y install #{security_pkgs.join(' ')}'"
122
+ #lets setup dkim
123
+ setup_email_signing
124
+ end
125
+ end
126
+ end
127
+
128
+ #based on the recipe here (but which is missing a few key steps!)
129
+ #http://www.howtoforge.com/quick-and-easy-setup-for-domainkeys-using-ubuntu-postfix-and-dkim-filter
130
+ desc <<-DESC
131
+ enables dkim signing of outgoing msgs. This helps with fightint spam.
132
+ You'll have to update your dns records to take advantage of this, but we'll
133
+ help you out with that
134
+ NOTE: set ec2onrails_config[:service_domain] = 'yourdomain.com' before running this task
135
+ DESC
136
+ task :setup_email_signing do
137
+ if cfg[:service_domain].nil? || cfg[:service_domain].empty?
138
+ raise "ERROR: missing the :service_domain key. Please set that in your deploy script if you would like to use this task."
139
+ end
140
+
141
+ domain = cfg[:service_domain]
142
+ postmaster_email = "postmaster@#{domain}"
143
+
144
+ #make the selector something that will help us roll over and expire the old key next year
145
+ selector = "mail#{Time.now.year.to_s[-2..-1]}" #ie, mail09
146
+
147
+ allow_sudo do
148
+ sudo "sh -c 'export DEBIAN_FRONTEND=noninteractive; aptitude -q -y install postfix dkim-filter'"
149
+ #do NOT change the size of the key; making it longer can cause problems with some of the dkim implementations
150
+
151
+ keys_exist = File.exist?("config/mail/dkim/dkim_#{selector}.private.key") && File.exist?("config/mail/dkim/dkim_#{selector}.public.key")
152
+
153
+ unless keys_exist
154
+ #lets make them!
155
+ cmds = <<-CMDS
156
+ mkdir -p config/mail/dkim;
157
+ cd config/mail/dkim;
158
+ openssl genrsa -out dkim_#{selector}.private.key 1024;
159
+ openssl rsa -in dkim_#{selector}.private.key -out dkim_#{selector}.public.key -pubout -outform PEM
160
+ CMDS
161
+ system cmds
162
+ end
163
+
164
+ pub_key = File.read("config/mail/dkim/dkim_#{selector}.public.key")
165
+ pub_key = pub_key.split("\n")[1..-2].join('')
166
+
167
+ #lets get the private and public keys up to the server
168
+ put File.read("config/mail/dkim/dkim_#{selector}.private.key"), "/tmp/dkim_#{selector}.private.key"
169
+ put File.read("config/mail/dkim/dkim_#{selector}.public.key"), "/tmp/dkim_#{selector}.public.key"
170
+ sudo "mkdir -p /var/dkim-filter"
171
+ sudo "mv /tmp/dkim_#{selector}.p*.key /var/dkim-filter/."
172
+
173
+ #saw a note that Canonicalization relaxed was helpful for rails applications...
174
+ #haven't tested that yet
175
+ dkim_filter_conf = <<-SCRIPT
176
+ # Log to syslog
177
+ Syslog yes
178
+
179
+ # Sign for example.com with key in /etc/mail/dkim.key using
180
+ Domain #{domain}
181
+ KeyFile /var/dkim-filter/dkim_#{selector}.private.key
182
+ Selector #{selector}
183
+
184
+ # Common settings. See dkim-filter.conf(5) for more information.
185
+ AutoRestart no
186
+ Background yes
187
+ SubDomains no
188
+ Canonicalization relaxed
189
+ SCRIPT
190
+
191
+ put dkim_filter_conf, "/tmp/dkim-filter.conf.tmp"
192
+ sudo "mv /etc/dkim-filter.conf /etc/dkim-filter.conf.orig"
193
+ sudo "mv /tmp/dkim-filter.conf.tmp /etc/dkim-filter.conf"
194
+ cmds = <<-CMDS
195
+ sudo postconf -e 'myhostname = #{domain}';
196
+ sudo postconf -e 'mydomain = #{domain}';
197
+ sudo postconf -e 'myorigin = $mydomain';
198
+ sudo postconf -e 'mynetworks_style=subnet';
199
+ sudo postconf -e 'biff = no';
200
+ sudo postconf -e 'alias_maps = hash:/etc/aliases';
201
+ sudo postconf -e 'alias_database = hash:/etc/aliases';
202
+ sudo postconf -e 'mydestination = localdomain, localhost, localhost.localdomain, localhost';
203
+ sudo postconf -e 'relay_domains=$mydestination';
204
+ sudo postconf -e 'mynetworks = 127.0.0.0/8';
205
+ sudo postconf -e 'smtpd_milters = inet:localhost:8891';
206
+ sudo postconf -e 'non_smtpd_milters = inet:localhost:8891';
207
+ sudo postconf -e 'milter_protocol = 2';
208
+ sudo postconf -e 'milter_default_action = accept'
209
+ CMDS
210
+ sudo cmds
211
+
212
+ #lets lock it down
213
+ sudo "chown -R dkim-filter:dkim-filter /var/dkim-filter"
214
+ sudo "chmod 600 /var/dkim-filter/*"
215
+
216
+ puts "*" * 80
217
+ puts "NOTE: you need to do a few things"
218
+ puts " * created public and private DKIM keys to config/mail/dkim_#{selector}.*.key" unless keys_exist
219
+ puts "\n"
220
+ msg = <<-MSG
221
+ * Enter these *TWO* records into your DNS record:
222
+ #{selector}._domainkey.#{domain} IN TXT 'k=rsa; t=y; p=#{pub_key}'
223
+ _domainkey.#{domain} IN TXT 't=y; o=~; r=#{postmaster_email}'
224
+
225
+ I would recommend signing into your ec2 instance and running some test emails. Gmail is very fast in updating their records, but yahoo (as of this writing) is slow and inconsistent. But you can run a command like this to various email address to see how it works:
226
+
227
+ echo 'something searchable so you can find it in your spam filter! did dkim work?' | mail -s "my dkim email; lets see how it went" adam@someservice.com
228
+
229
+
230
+ NOTE: in the near future, when things are looking good, if you take away the 't=y; ' from the above two records, it tells the email services that you are no longer testing the service and to treat your signings with tough love.
231
+
232
+
233
+ MSG
234
+ puts msg
235
+
236
+ #sometimes the dkim-filter restart fails; it seems to be a race condition with some of the postfix changes going in...
237
+ #but a sleep here seems to do the trick.
238
+ sleep(10)
239
+ output = quiet_capture "sudo /etc/init.d/dkim-filter restart"
240
+ if output =~ /smfi_opensocket\(\) failed/
241
+ #ah, if we didn't sleep enough above, lets try it one more time; but this time it will fail if we still get
242
+ #the smfi_opensocket error
243
+ sleep(5)
244
+ sudo "/etc/init.d/dkim-filter restart 2>&1"
245
+ end
246
+ sleep(2)
247
+ sudo "/etc/init.d/postfix restart 2>&1"
248
+ end
249
+
250
+ end
251
+
252
+
253
+ desc <<-DESC
254
+ Install extra rubygems. Set ec2onrails_config[:rubygems], it should \
255
+ be with an array of strings.
256
+ DESC
257
+ task :install_gems do
258
+ if cfg[:rubygems]
259
+ allow_sudo do
260
+ cfg[:rubygems].each do |g|
261
+ sudo "gem install #{g} --no-rdoc --no-ri"
262
+ end
263
+ end
264
+ end
265
+ end
266
+
267
+ task :run_rails_rake_gems_install do
268
+ #if running under Rails 2.1, lets trigger 'rake gems:install', but in such a way
269
+ #so it fails gracefully if running rails < 2.1
270
+ # ALSO, this might be the first time rake is run, and running it as sudo means that
271
+ # if any plugins are loaded and create directories... like what image_science does for
272
+ # ruby_inline, then the dirs will be created as root. so trigger the rails loading
273
+ # very quickly before the sudo is called
274
+ # run "cd #{release_path} && rake RAILS_ENV=#{rails_env} -T 1>/dev/null && sudo rake RAILS_ENV=#{rails_env} gems:install"
275
+ allow_sudo do
276
+ output = quiet_capture "cd #{release_path} && rake RAILS_ENV=#{rails_env} db:version > /dev/null 2>&1 || sudo rake RAILS_ENV=#{rails_env} gems:install"
277
+ puts output
278
+ end
279
+ end
280
+
281
+ desc <<-DESC
282
+ Add extra gem sources to rubygems (to able to fetch gems from for example gems.github.com).
283
+ Set ec2onrails_config[:rubygems_sources], it should be with an array of strings.
284
+ DESC
285
+ task :add_gem_sources do
286
+ if cfg[:rubygems_sources]
287
+ allow_sudo do
288
+ cfg[:rubygems_sources].each do |gem_source|
289
+ sudo "gem sources -a #{gem_source}"
290
+ end
291
+ end
292
+ end
293
+ end
294
+
295
+ desc <<-DESC
296
+ A convenience task to upgrade existing packages and gems and install \
297
+ specified new ones.
298
+ DESC
299
+ task :upgrade_and_install_all do
300
+ upgrade_packages
301
+ upgrade_gems
302
+ install_packages
303
+ install_gems
304
+ end
305
+
306
+ desc <<-DESC
307
+ Set the timezone using the value of the variable named timezone. \
308
+ Valid options for timezone can be determined by the contents of \
309
+ /usr/share/zoneinfo, which can be seen here: \
310
+ http://packages.ubuntu.com/cgi-bin/search_contents.pl?searchmode=filelist&word=tzdata&version=gutsy&arch=all&page=1&number=all \
311
+ Remove 'usr/share/zoneinfo/' from the filename, and use the last \
312
+ directory and file as the value. For example 'Africa/Abidjan' or \
313
+ 'posix/GMT' or 'Canada/Eastern'.
314
+ DESC
315
+ task :set_timezone do
316
+ if cfg[:timezone]
317
+ allow_sudo do
318
+ sudo "bash -c 'echo #{cfg[:timezone]} > /etc/timezone'"
319
+ sudo "cp /usr/share/zoneinfo/#{cfg[:timezone]} /etc/localtime"
320
+ end
321
+ end
322
+ end
323
+
324
+ desc <<-DESC
325
+ DEPRECATED. See install_system_files.
326
+ DESC
327
+ task :deploy_files do
328
+ if cfg[:server_config_files_root]
329
+ puts "***** DEPRECATION WARNING: you're using the deploy_files task which has been deprecated" # TODO pointer to documentation
330
+ begin
331
+ filename = "config_files.tar"
332
+ local_file = "#{Dir.tmpdir}/#{filename}"
333
+ remote_file = "/tmp/#{filename}"
334
+ FileUtils.cd(cfg[:server_config_files_root]) do
335
+ File.open(local_file, 'wb') { |tar| Minitar.pack(".", tar) }
336
+ end
337
+ put File.read(local_file), remote_file
338
+ allow_sudo do
339
+ sudo "tar xvf #{remote_file} -o -C /"
340
+ end
341
+ ensure
342
+ rm_rf local_file
343
+ run "rm -f #{remote_file}"
344
+ end
345
+ end
346
+ end
347
+
348
+ desc <<-DESC
349
+ Installs files into the system anywhere outside of the Rails app.
350
+ The directory RAILS_ROOT/config/ec2onrails/system_files can contain
351
+ files that will be installed into the server relative to "/", and it
352
+ can contain a manifest file with metadata to change the file owner
353
+ and permissions, and it allows the files to be cleanly uninstalled
354
+ from the system.
355
+ TODO pointer to full documentation
356
+ DESC
357
+ task :install_system_files do
358
+ allow_sudo do
359
+ sudo "/usr/local/ec2onrails/bin/install_system_files #{release_path}"
360
+ end
361
+ end
362
+
363
+ desc <<-DESC
364
+ Restart a set of services. Set ec2onrails_config[:services_to_restart]
365
+ to an array of strings. It's assumed that each service has a script
366
+ in /etc/init.d
367
+ DESC
368
+ task :restart_services do
369
+ if cfg[:services_to_restart] && cfg[:services_to_restart].any?
370
+ allow_sudo do
371
+ cfg[:services_to_restart].each do |service|
372
+ run_init_script(service, "restart")
373
+ end
374
+ end
375
+ end
376
+ end
377
+
378
+ desc <<-DESC
379
+ Set the email address that mail to the app user forwards to.
380
+ DESC
381
+ task :set_mail_forward_address do
382
+ if cfg[:mail_forward_address]
383
+ allow_sudo do
384
+ sudo "sh -c 'echo #{cfg[:mail_forward_address]} > /root/.forward'"
385
+ end
386
+ end
387
+ end
388
+
389
+ desc <<-DESC
390
+ Enable ssl for the web server. You'll want to replace the default SSL
391
+ certificate and key files, the certificate file is at
392
+ /etc/ec2onrails/ssl/cert/ec2onrails-default.crt
393
+ and a the key file is at
394
+ /etc/ec2onrails/ssl/private/ec2onrails-default.key
395
+ (use the deploy_files task).
396
+ The key file should NOT have a passphrase.
397
+ DESC
398
+ task :enable_ssl, :roles => :web do
399
+ # TODO: enable for nginx
400
+ # run_init_script("nginx", "restart")
401
+ end
402
+
403
+ desc <<-DESC
404
+ Upload the app user's SSH deploy keys from
405
+ config/ec2onrails/deploy_keys to /home/app/.ssh
406
+ The deploy_keys dir should contain the SSH config files that
407
+ are needed to deploy your app's source code from your SCM repository
408
+ (if you're deploying from an SCM repo). This means an SSH private key
409
+ (named id_dsa), the public key (named id_dsa.pub) and possibly a
410
+ known_hosts file.
411
+ They can't deployed via install_system_files because the
412
+ app user's SSH credentials might be needed to deploy the
413
+ app itself if it's coming directly from an SCM repository.
414
+ DESC
415
+ task :upload_deploy_keys do
416
+ deploy_keys_dir = "config/ec2onrails/deploy_keys"
417
+ remote_dir = "/home/app/.ssh"
418
+
419
+ if File.exist? deploy_keys_dir
420
+ run "mkdir -p #{remote_dir}"
421
+ Dir.chdir deploy_keys_dir do
422
+ Dir.glob("*").each do |f|
423
+ remote_file = "#{remote_dir}/#{f}"
424
+ put File.read(f), "#{remote_file}"
425
+ end
426
+ end
427
+ run "chmod -R go-rwx /home/app/.ssh"
428
+ end
429
+ end
430
+
431
+ desc <<-DESC
432
+ Clear the varnish proxy cache (if caching is enabled, which it isn't
433
+ by default). Purges all documents from the cache.
434
+ DESC
435
+ task :purge_proxy_cache, :roles => :proxy do
436
+ run "varnishadm -T localhost:6082 'url.purge .*'"
437
+ end
438
+
439
+ desc <<-DESC
440
+ Restrict the app user's sudo access.
441
+ Defaults the user to only be able to \
442
+ sudo to god
443
+ DESC
444
+ task :restrict_sudo_access do
445
+ old_user = fetch(:user)
446
+ is_rootequiv = capture("groups").split.include?("rootequiv") # check groups before changing user
447
+ begin
448
+ set :user, 'root'
449
+ sessions.clear #clear out sessions cache..... this way the ssh connections are reinitialized
450
+
451
+ # Remove the app user from the "rootequiv" group, this removes full sudo ability
452
+ if is_rootequiv
453
+ run "deluser app rootequiv"
454
+ else
455
+ puts "User 'app' is not a member of group 'rootequiv' (old_user = #{old_user})."
456
+ end
457
+ ensure
458
+ set :user, old_user
459
+ sessions.clear
460
+ end
461
+ end
462
+
463
+ desc <<-DESC
464
+ Grant *FULL* sudo access to the app user.
465
+ This is NOT RECOMMENDED, it will make the 'app' user the
466
+ equivalent of 'root' until the 'restrict_sudo_access' task is run.
467
+ Alternatively, a task that requires sudo ability can call the
468
+ allow_sudo method with a block, this will give the app user sudo
469
+ ability only while the block is being run.
470
+ DESC
471
+ task :grant_sudo_access do
472
+ allow_sudo
473
+ end
474
+
475
+ @within_sudo = 0
476
+ def allow_sudo
477
+ begin
478
+ @within_sudo += 1
479
+ old_user = fetch(:user)
480
+ if @within_sudo > 1
481
+ yield if block_given?
482
+ true
483
+ elsif capture("groups").split.include?("rootequiv")
484
+ yield if block_given?
485
+ false
486
+ else
487
+ begin
488
+ # need to cheat and temporarily set the user to ROOT so we
489
+ # can temporarily add the app user to the rootequiv group.
490
+ # we can do this because the root and app user have the same
491
+ # ssh login preferences....
492
+ set :user, 'root'
493
+ sessions.clear #clear out sessions cache..... this way the ssh connections are reinitialized
494
+
495
+ # Temporarily add the app user to the "rootequiv" group, this will give full sudo ability
496
+ run "adduser app rootequiv"
497
+
498
+ set :user, old_user
499
+ sessions.clear
500
+ yield if block_given?
501
+ ensure
502
+ server.restrict_sudo_access if block_given?
503
+ set :user, old_user
504
+ sessions.clear
505
+ true
506
+ end
507
+ end
508
+ ensure
509
+ @within_sudo -= 1
510
+ end
511
+ end
512
+
513
+ end
514
+
515
+ end
516
+
517
+ end