app-deployer 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ <% mysql_grant_locations.each do |location| %>
2
+ GRANT <%= mysql_grant_priv_type %> ON <%= db_name %>.* TO '<%= db_login %>'@'<%= location %>' IDENTIFIED BY '<%= db_password %>';
3
+ <% end %>
4
+
5
+ CREATE DATABASE IF NOT EXISTS <%= db_name %> CHARACTER SET = <%= db_encoding %>;
6
+
7
+ FLUSH PRIVILEGES;
@@ -0,0 +1,399 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+
3
+ require 'benchmark'
4
+ require 'capistrano/recipes/deploy/scm'
5
+ require 'capistrano/recipes/deploy/strategy'
6
+
7
+ # =========================================================================
8
+ # These variables MUST be set in the client capfiles. If they are not set,
9
+ # the deploy will fail with an error.
10
+ # =========================================================================
11
+
12
+ _cset(:application) { abort "Please specify the name of your application, set :application, 'foo'" }
13
+ _cset(:repository) { abort "Please specify the repository that houses your application's code, set :repository, 'foo'" }
14
+
15
+ # =========================================================================
16
+ # These variables may be set in the client capfile if their default values
17
+ # are not sufficient.
18
+ # =========================================================================
19
+
20
+ _cset :scm, :git
21
+ _cset :deploy_via, :remote_cache
22
+ _cset :branch, 'master'
23
+ _cset(:deploy_to) { "/var/www/#{application}" }
24
+ _cset :keep_releases, 5
25
+ _cset :git_enable_submodules, false
26
+ _cset :use_compass, false
27
+ _cset :use_composer, false
28
+ _cset :composer_bin, false
29
+ _cset :composer_options, "--no-scripts --verbose"
30
+ _cset :curl_options, "-k"
31
+ _cset :copy_vendors, false
32
+ _cset :update_vendors, false
33
+ _cset :clear_cache, false
34
+ _cset :use_sudo, false
35
+ _cset :php_bin, "php"
36
+ default_run_options[:pty] = true
37
+ ssh_options[:forward_agent] = true
38
+
39
+ # =========================================================================
40
+ # Default user settings
41
+ # =========================================================================
42
+
43
+ _cset :user, 'deploy'
44
+ _cset(:group) {'www-data'}
45
+ _cset :runner, :user
46
+
47
+ # =========================================================================
48
+ # These variables should NOT be changed unless you are very confident in
49
+ # what you are doing. Make sure you understand all the implications of your
50
+ # changes if you do decide to muck with these!
51
+ # =========================================================================
52
+
53
+ _cset(:source) { Capistrano::Deploy::SCM.new(scm, self) }
54
+ _cset(:revision) { source.head }
55
+ _cset(:real_revision) { source.local.query_revision(revision) { |cmd| with_env("LC_ALL", "C") { run_locally(cmd) } } }
56
+
57
+ _cset(:strategy) { Capistrano::Deploy::Strategy.new(deploy_via, self) }
58
+
59
+ _cset(:release_name) { set :deploy_timestamped, true; Time.now.utc.strftime("%Y%m%d%H%M%S") }
60
+
61
+ _cset :version_dir, "releases"
62
+ _cset :shared_dir, "shared"
63
+ _cset :shared_children, [] # Empty!
64
+ _cset :current_dir, "current"
65
+
66
+ _cset(:releases_path) { File.join(deploy_to, version_dir) }
67
+ _cset(:shared_path) { File.join(deploy_to, shared_dir) }
68
+ _cset(:current_path) { File.join(deploy_to, current_dir) }
69
+ _cset(:release_path) { File.join(releases_path, release_name) }
70
+
71
+ _cset(:releases) { capture("ls -x #{releases_path}", :except => { :no_release => true }).split.sort }
72
+ _cset(:current_release) { releases.length > 0 ? File.join(releases_path, releases.last) : nil }
73
+ _cset(:previous_release) { releases.length > 1 ? File.join(releases_path, releases[-2]) : nil }
74
+
75
+ _cset(:current_revision) { capture("cat #{current_path}/REVISION", :except => { :no_release => true }).chomp }
76
+ _cset(:latest_revision) { capture("cat #{current_release}/REVISION", :except => { :no_release => true }).chomp }
77
+ _cset(:previous_revision) { capture("cat #{previous_release}/REVISION", :except => { :no_release => true }).chomp if previous_release }
78
+
79
+ _cset(:run_method) { fetch(:use_sudo, true) ? :sudo : :run }
80
+
81
+ # some tasks, like symlink, need to always point at the latest release, but
82
+ # they can also (occassionally) be called standalone. In the standalone case,
83
+ # the timestamped release_path will be inaccurate, since the directory won't
84
+ # actually exist. This variable lets tasks like symlink work either in the
85
+ # standalone case, or during deployment.
86
+ _cset(:latest_release) { exists?(:deploy_timestamped) ? release_path : current_release }
87
+
88
+ STDOUT.sync
89
+ $error = false
90
+ $pretty_errors_defined = false
91
+
92
+ # Be less verbose by default
93
+ # logger.level = Capistrano::Logger::IMPORTANT
94
+
95
+ before "deploy:update_code" do
96
+ msg = "--> Updating code base with #{deploy_via} strategy"
97
+
98
+ if logger.level == Capistrano::Logger::IMPORTANT
99
+ pretty_errors
100
+ puts msg
101
+ else
102
+ puts msg.green
103
+ end
104
+ end
105
+
106
+ after "deploy:update_code" do
107
+ if use_compass
108
+ compass.compile
109
+ end
110
+ end
111
+
112
+ after "deploy:create_symlink" do
113
+ puts "--> Successfully deployed!".green
114
+ end
115
+
116
+
117
+ # =========================================================================
118
+ # These are the tasks that are available to help with deploying web apps,
119
+ # and specifically, Rails applications. You can have cap give you a summary
120
+ # of them with `cap -T'.
121
+ # =========================================================================
122
+
123
+ namespace :deploy do
124
+ desc <<-DESC
125
+ Deploys your project. This calls both `update' and `restart'. Note that \
126
+ this will generally only work for applications that have already been deployed \
127
+ once. For a "cold" deploy, you'll want to take a look at the `deploy:cold' \
128
+ task, which handles the cold start specifically.
129
+ DESC
130
+ task :default do
131
+ update
132
+ end
133
+
134
+ desc <<-DESC
135
+ Prepares one or more servers for deployment. Before you can use any \
136
+ of the Capistrano deployment tasks with your project, you will need to \
137
+ make sure all of your servers have been prepared with `cap deploy:setup'. When \
138
+ you add a new server to your cluster, you can easily run the setup task \
139
+ on just that server by specifying the HOSTS environment variable:
140
+
141
+ $ cap HOSTS=new.server.com deploy:setup
142
+
143
+ It is safe to run this task on servers that have already been set up; it \
144
+ will not destroy any deployed revisions or data.
145
+ DESC
146
+ task :setup, :except => { :no_release => true } do
147
+ dirs = [deploy_to, releases_path, shared_path]
148
+ dirs += shared_children.map { |d| File.join(shared_path, d.split('/').last) }
149
+ run "#{try_sudo} mkdir -p #{dirs.join(' ')}"
150
+ run "#{try_sudo} chmod g+w #{dirs.join(' ')}" if fetch(:group_writable, true)
151
+ end
152
+
153
+ desc <<-DESC
154
+ Copies your project and updates the symlink. It does this in a \
155
+ transaction, so that if either `update_code' or `symlink' fail, all \
156
+ changes made to the remote servers will be rolled back, leaving your \
157
+ system in the same state it was in before `update' was invoked. Usually, \
158
+ you will want to call `deploy' instead of `update', but `update' can be \
159
+ handy if you want to deploy, but not immediately restart your application.
160
+ DESC
161
+ task :update do
162
+ transaction do
163
+ update_code
164
+ create_symlink
165
+ cleanup
166
+ end
167
+ end
168
+
169
+ desc <<-DESC
170
+ Copies your project to the remote servers. This is the first stage \
171
+ of any deployment; moving your updated code and assets to the deployment \
172
+ servers. You will rarely call this task directly, however; instead, you \
173
+ should call the `deploy' task (to do a complete deploy) or the `update' \
174
+ task (if you want to perform the `restart' task separately).
175
+
176
+ You will need to make sure you set the :scm variable to the source \
177
+ control software you are using (it defaults to :subversion), and the \
178
+ :deploy_via variable to the strategy you want to use to deploy (it \
179
+ defaults to :checkout).
180
+ DESC
181
+ task :update_code, :except => { :no_release => true } do
182
+ on_rollback { run "rm -rf #{release_path}; true" }
183
+ strategy.deploy!
184
+ finalize_update
185
+ end
186
+
187
+ desc <<-DESC
188
+ [internal] Touches up the released code. This is called by update_code \
189
+ after the basic deploy finishes. It assumes a Rails project was deployed, \
190
+ so if you are deploying something else, you may want to override this \
191
+ task with your own environment's requirements.
192
+
193
+ This task will make the release group-writable (if the :group_writable \
194
+ variable is set to true, which is the default). It will then set up \
195
+ symlinks to the shared directory for the log, system, and tmp/pids \
196
+ directories, and will lastly touch all assets in public/images, \
197
+ public/stylesheets, and public/javascripts so that the times are \
198
+ consistent (so that asset timestamping works). This touch process \
199
+ is only carried out if the :normalize_asset_timestamps variable is \
200
+ set to true, which is the default The asset directories can be overridden \
201
+ using the :public_children variable.
202
+ DESC
203
+ task :finalize_update, :except => { :no_release => true } do
204
+ run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
205
+ end
206
+ desc <<-DESC
207
+ Deprecated API. This has become deploy:create_symlink, please update your recipes
208
+ DESC
209
+ task :symlink, :except => { :no_release => true } do
210
+ Kernel.warn "[Deprecation Warning] This API has changed, please hook `deploy:create_symlink` instead of `deploy:symlink`."
211
+ create_symlink
212
+ end
213
+
214
+ desc <<-DESC
215
+ Updates the symlink to the most recently deployed version. Capistrano works \
216
+ by putting each new release of your application in its own directory. When \
217
+ you deploy a new version, this task's job is to update the `current' symlink \
218
+ to point at the new version. You will rarely need to call this task \
219
+ directly; instead, use the `deploy' task (which performs a complete \
220
+ deploy, including `restart') or the 'update' task (which does everything \
221
+ except `restart').
222
+ DESC
223
+ task :create_symlink, :except => { :no_release => true } do
224
+ on_rollback do
225
+ if previous_release
226
+ run "rm -f #{current_path}; ln -s #{previous_release} #{current_path}; true"
227
+ else
228
+ logger.important "no previous release to rollback to, rollback of symlink skipped"
229
+ end
230
+ end
231
+
232
+ run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}"
233
+ end
234
+
235
+ desc <<-DESC
236
+ Copy files to the currently deployed version. This is useful for updating \
237
+ files piecemeal, such as when you need to quickly deploy only a single \
238
+ file. Some files, such as updated templates, images, or stylesheets, \
239
+ might not require a full deploy, and especially in emergency situations \
240
+ it can be handy to just push the updates to production, quickly.
241
+
242
+ To use this task, specify the files and directories you want to copy as a \
243
+ comma-delimited list in the FILES environment variable. All directories \
244
+ will be processed recursively, with all files being pushed to the \
245
+ deployment servers.
246
+
247
+ $ cap deploy:upload FILES=templates,controller.rb
248
+
249
+ Dir globs are also supported:
250
+
251
+ $ cap deploy:upload FILES='config/apache/*.conf'
252
+ DESC
253
+ task :upload, :except => { :no_release => true } do
254
+ files = (ENV["FILES"] || "").split(",").map { |f| Dir[f.strip] }.flatten
255
+ abort "Please specify at least one file or directory to update (via the FILES environment variable)" if files.empty?
256
+
257
+ files.each { |file| top.upload(file, File.join(current_path, file)) }
258
+ end
259
+
260
+ namespace :rollback do
261
+ desc <<-DESC
262
+ [internal] Points the current symlink at the previous revision.
263
+ This is called by the rollback sequence, and should rarely (if
264
+ ever) need to be called directly.
265
+ DESC
266
+ task :revision, :except => { :no_release => true } do
267
+ if previous_release
268
+ run "rm #{current_path}; ln -s #{previous_release} #{current_path}"
269
+ else
270
+ abort "could not rollback the code because there is no prior release"
271
+ end
272
+ end
273
+
274
+ desc <<-DESC
275
+ [internal] Removes the most recently deployed release.
276
+ This is called by the rollback sequence, and should rarely
277
+ (if ever) need to be called directly.
278
+ DESC
279
+ task :cleanup, :except => { :no_release => true } do
280
+ run "if [ `readlink #{current_path}` != #{current_release} ]; then rm -rf #{current_release}; fi"
281
+ end
282
+
283
+ desc <<-DESC
284
+ Rolls back to the previously deployed version. The `current' symlink will \
285
+ be updated to point at the previously deployed version, and then the \
286
+ current release will be removed from the servers. You'll generally want \
287
+ to call `rollback' instead, as it performs a `restart' as well.
288
+ DESC
289
+ task :code, :except => { :no_release => true } do
290
+ revision
291
+ cleanup
292
+ end
293
+
294
+ desc <<-DESC
295
+ Rolls back to a previous version and restarts. This is handy if you ever \
296
+ discover that you've deployed a lemon; `cap rollback' and you're right \
297
+ back where you were, on the previously deployed version.
298
+ DESC
299
+ task :default do
300
+ revision
301
+ cleanup
302
+ end
303
+ end
304
+
305
+ desc <<-DESC
306
+ Clean up old releases. By default, the last 5 releases are kept on each \
307
+ server (though you can change this with the keep_releases variable). All \
308
+ other deployed revisions are removed from the servers. By default, this \
309
+ will use sudo to clean up the old releases, but if sudo is not available \
310
+ for your environment, set the :use_sudo variable to false instead.
311
+ DESC
312
+ task :cleanup, :except => { :no_release => true } do
313
+ count = fetch(:keep_releases, 5).to_i
314
+ local_releases = capture("ls -xt #{releases_path}").split.reverse
315
+ if count >= local_releases.length
316
+ logger.important "no old releases to clean up"
317
+ else
318
+ logger.info "keeping #{count} of #{local_releases.length} deployed releases"
319
+ directories = (local_releases - local_releases.last(count)).map { |release|
320
+ File.join(releases_path, release) }.join(" ")
321
+
322
+ try_sudo "rm -rf #{directories}"
323
+ end
324
+ end
325
+
326
+ desc <<-DESC
327
+ Test deployment dependencies. Checks things like directory permissions, \
328
+ necessary utilities, and so forth, reporting on the things that appear to \
329
+ be incorrect or missing. This is good for making sure a deploy has a \
330
+ chance of working before you actually run `cap deploy'.
331
+
332
+ You can define your own dependencies, as well, using the `depend' method:
333
+
334
+ depend :remote, :gem, "tzinfo", ">=0.3.3"
335
+ depend :local, :command, "svn"
336
+ depend :remote, :directory, "/u/depot/files"
337
+ DESC
338
+ task :check, :except => { :no_release => true } do
339
+ dependencies = strategy.check!
340
+
341
+ other = fetch(:dependencies, {})
342
+ other.each do |location, types|
343
+ types.each do |type, calls|
344
+ if type == :gem
345
+ dependencies.send(location).command(fetch(:gem_command, "gem")).or("`gem' command could not be found. Try setting :gem_command")
346
+ end
347
+
348
+ calls.each do |args|
349
+ dependencies.send(location).send(type, *args)
350
+ end
351
+ end
352
+ end
353
+
354
+ if dependencies.pass?
355
+ puts "You appear to have all necessary dependencies installed"
356
+ else
357
+ puts "The following dependencies failed. Please check them and try again:"
358
+ dependencies.reject { |d| d.pass? }.each do |d|
359
+ puts "--> #{d.message}"
360
+ end
361
+ abort
362
+ end
363
+ end
364
+
365
+ desc <<-DESC
366
+ Deploys and starts a `cold' application. This is useful if you have not \
367
+ deployed your application before, or if your application is (for some \
368
+ other reason) not currently running. It will deploy the code, run any \
369
+ pending migrations, and then instead of invoking `deploy:restart', it will \
370
+ invoke `deploy:start' to fire up the application servers.
371
+ DESC
372
+ task :cold do
373
+ update
374
+ end
375
+
376
+ namespace :pending do
377
+ desc <<-DESC
378
+ Displays the `diff' since your last deploy. This is useful if you want \
379
+ to examine what changes are about to be deployed. Note that this might \
380
+ not be supported on all SCM's.
381
+ DESC
382
+ task :diff, :except => { :no_release => true } do
383
+ system(source.local.diff(current_revision))
384
+ end
385
+
386
+ desc <<-DESC
387
+ Displays the commits since your last deploy. This is good for a summary \
388
+ of the changes that have occurred since the last deploy. Note that this \
389
+ might not be supported on all SCM's.
390
+ DESC
391
+ task :default, :except => { :no_release => true } do
392
+ from = source.next_revision(current_revision)
393
+ system(source.local.log(from))
394
+ end
395
+ end
396
+
397
+ end
398
+
399
+ end # Capistrano::Configuration.instance(:must_exist).load do
@@ -0,0 +1,59 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ require 'erb'
3
+
4
+ namespace :apache do
5
+ namespace :maintenance do
6
+ desc <<-DESC
7
+ Present a maintenance page to visitors. Disables your application's web \
8
+ interface by writing a "maintenance.html" file to each web server. The \
9
+ servers must be configured to detect the presence of this file, and if \
10
+ it is present, always display it instead of performing the request.
11
+
12
+ By default, the maintenance page will just say the site is down for \
13
+ "maintenance", and will be back "shortly", but you can customize the \
14
+ page by specifying the REASON and UNTIL environment variables:
15
+
16
+ $ cap deploy:web:disable \\
17
+ REASON="hardware upgrade" \\
18
+ UNTIL="12pm Central Time"
19
+
20
+ Further customization will require that you write your own task.
21
+ DESC
22
+ task :start, :roles => :web, :except => { :no_release => true } do
23
+ on_rollback { run "rm #{shared_path}/maintenance.html" }
24
+
25
+ warn <<-EOHTACCESS
26
+
27
+ # Please add something like this to your site's htaccess to redirect users to the maintenance page.
28
+ # More Info: http://www.shiftcommathree.com/articles/make-your-rails-maintenance-page-respond-with-a-503
29
+
30
+ ErrorDocument 503 /maintenance.html
31
+ RewriteEngine On
32
+ RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png)$
33
+ RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
34
+ RewriteCond %{SCRIPT_FILENAME} !maintenance.html
35
+ RewriteRule ^.*$ - [redirect=503,last]
36
+ EOHTACCESS
37
+
38
+ reason = ENV['REASON']
39
+ deadline = ENV['UNTIL']
40
+
41
+ template = File.read(File.join(File.dirname(__FILE__), "templates", "maintenance.rhtml"))
42
+ result = ERB.new(template).result(binding)
43
+
44
+ put(result, "#{shared_path}/maintenance.html", :mode => 0644, :via => :scp)
45
+ end
46
+
47
+ desc <<-DESC
48
+ Makes the application web-accessible again. Removes the \
49
+ "maintenance.html" page generated by deploy:web:disable, which (if your \
50
+ web servers are configured correctly) will make your application \
51
+ web-accessible again.
52
+ DESC
53
+ task :end, :roles => :web, :except => { :no_release => true } do
54
+ run "rm #{shared_path}/maintenance.html"
55
+ end
56
+ end
57
+
58
+ end
59
+ end