minestrone 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/main.yml +32 -0
  3. data/.gitignore +5 -0
  4. data/Gemfile +10 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +35 -0
  7. data/Rakefile +10 -0
  8. data/bin/capify +89 -0
  9. data/bin/min +5 -0
  10. data/docs/lib-codebase-map.md +162 -0
  11. data/docs/lib-dependency-graph.svg +129 -0
  12. data/lib/minestrone/callback.rb +45 -0
  13. data/lib/minestrone/cli/help.rb +131 -0
  14. data/lib/minestrone/cli/help.txt +72 -0
  15. data/lib/minestrone/cli/options.rb +232 -0
  16. data/lib/minestrone/cli.rb +159 -0
  17. data/lib/minestrone/command.rb +177 -0
  18. data/lib/minestrone/configuration/actions/file_transfer.rb +53 -0
  19. data/lib/minestrone/configuration/actions/inspect.rb +46 -0
  20. data/lib/minestrone/configuration/actions/invocation.rb +202 -0
  21. data/lib/minestrone/configuration/alias_task.rb +29 -0
  22. data/lib/minestrone/configuration/callbacks.rb +129 -0
  23. data/lib/minestrone/configuration/connections.rb +66 -0
  24. data/lib/minestrone/configuration/execution.rb +139 -0
  25. data/lib/minestrone/configuration/loading.rb +207 -0
  26. data/lib/minestrone/configuration/log_formatters.rb +75 -0
  27. data/lib/minestrone/configuration/namespaces.rb +225 -0
  28. data/lib/minestrone/configuration/servers.rb +70 -0
  29. data/lib/minestrone/configuration/variables.rb +115 -0
  30. data/lib/minestrone/configuration.rb +69 -0
  31. data/lib/minestrone/errors.rb +17 -0
  32. data/lib/minestrone/ext/string.rb +7 -0
  33. data/lib/minestrone/extensions.rb +56 -0
  34. data/lib/minestrone/logger.rb +171 -0
  35. data/lib/minestrone/processable.rb +50 -0
  36. data/lib/minestrone/recipes/deploy/assets.rb +194 -0
  37. data/lib/minestrone/recipes/deploy/bundler.rb +81 -0
  38. data/lib/minestrone/recipes/deploy/dependencies.rb +44 -0
  39. data/lib/minestrone/recipes/deploy/local_dependency.rb +45 -0
  40. data/lib/minestrone/recipes/deploy/remote_dependency.rb +119 -0
  41. data/lib/minestrone/recipes/deploy/scm/base.rb +204 -0
  42. data/lib/minestrone/recipes/deploy/scm/git.rb +284 -0
  43. data/lib/minestrone/recipes/deploy/scm/none.rb +54 -0
  44. data/lib/minestrone/recipes/deploy/scm.rb +22 -0
  45. data/lib/minestrone/recipes/deploy/strategy/base.rb +87 -0
  46. data/lib/minestrone/recipes/deploy/strategy/copy.rb +353 -0
  47. data/lib/minestrone/recipes/deploy/strategy/remote_cache.rb +80 -0
  48. data/lib/minestrone/recipes/deploy/strategy.rb +22 -0
  49. data/lib/minestrone/recipes/deploy.rb +639 -0
  50. data/lib/minestrone/recipes/standard.rb +23 -0
  51. data/lib/minestrone/recipes/templates/maintenance.rhtml +53 -0
  52. data/lib/minestrone/server_definition.rb +56 -0
  53. data/lib/minestrone/ssh.rb +81 -0
  54. data/lib/minestrone/task_definition.rb +82 -0
  55. data/lib/minestrone/transfer.rb +205 -0
  56. data/lib/minestrone/version.rb +11 -0
  57. data/lib/minestrone.rb +3 -0
  58. data/minestrone.gemspec +32 -0
  59. data/test/cli/execute_test.rb +130 -0
  60. data/test/cli/help_test.rb +178 -0
  61. data/test/cli/options_test.rb +315 -0
  62. data/test/cli/ui_test.rb +26 -0
  63. data/test/cli_test.rb +17 -0
  64. data/test/command_test.rb +305 -0
  65. data/test/configuration/actions/file_transfer_test.rb +61 -0
  66. data/test/configuration/actions/inspect_test.rb +76 -0
  67. data/test/configuration/actions/invocation_test.rb +258 -0
  68. data/test/configuration/alias_task_test.rb +110 -0
  69. data/test/configuration/callbacks_test.rb +201 -0
  70. data/test/configuration/connections_test.rb +192 -0
  71. data/test/configuration/execution_test.rb +176 -0
  72. data/test/configuration/loading_test.rb +149 -0
  73. data/test/configuration/namespace_dsl_test.rb +325 -0
  74. data/test/configuration/servers_test.rb +100 -0
  75. data/test/configuration/variables_test.rb +191 -0
  76. data/test/configuration_test.rb +77 -0
  77. data/test/deploy/local_dependency_test.rb +61 -0
  78. data/test/deploy/remote_dependency_test.rb +146 -0
  79. data/test/deploy/scm/base_test.rb +55 -0
  80. data/test/deploy/scm/git_test.rb +260 -0
  81. data/test/deploy/scm/none_test.rb +26 -0
  82. data/test/deploy/strategy/copy_test.rb +360 -0
  83. data/test/extensions_test.rb +69 -0
  84. data/test/fixtures/cli_integration.rb +5 -0
  85. data/test/fixtures/config.rb +4 -0
  86. data/test/fixtures/custom.rb +3 -0
  87. data/test/logger_formatting_test.rb +149 -0
  88. data/test/logger_test.rb +134 -0
  89. data/test/recipes_test.rb +26 -0
  90. data/test/server_definition_test.rb +121 -0
  91. data/test/ssh_test.rb +99 -0
  92. data/test/task_definition_test.rb +117 -0
  93. data/test/transfer_test.rb +172 -0
  94. data/test/utils.rb +28 -0
  95. data/test/version_test.rb +11 -0
  96. metadata +258 -0
@@ -0,0 +1,639 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "benchmark"
4
+ require "set"
5
+ require "shellwords"
6
+ require "yaml"
7
+ require "minestrone/recipes/deploy/scm"
8
+ require "minestrone/recipes/deploy/strategy"
9
+
10
+ def _cset(name, *args, &block)
11
+ unless exists?(name)
12
+ set(name, *args, &block)
13
+ end
14
+ end
15
+
16
+ # =========================================================================
17
+ # These variables MUST be set in the client capfiles. If they are not set,
18
+ # the deploy will fail with an error.
19
+ # =========================================================================
20
+
21
+ _cset(:application) { abort "Please specify the name of your application, set :application, 'foo'" }
22
+ _cset(:repository) { abort "Please specify the repository that houses your application's code, set :repository, 'foo'" }
23
+
24
+ # =========================================================================
25
+ # These variables may be set in the client capfile if their default values
26
+ # are not sufficient.
27
+ # =========================================================================
28
+
29
+ _cset(:scm) { scm_default }
30
+ _cset :deploy_via, :remote_cache
31
+
32
+ _cset(:deploy_to) { "/var/www/#{application}" }
33
+ _cset(:revision) { source.head }
34
+
35
+ _cset :rails_env, "production"
36
+ _cset :rake, "rake"
37
+
38
+ _cset :keep_releases, 5
39
+
40
+ # =========================================================================
41
+ # These variables should NOT be changed unless you are very confident in
42
+ # what you are doing. Make sure you understand all the implications of your
43
+ # changes if you do decide to muck with these!
44
+ # =========================================================================
45
+
46
+ _cset(:source) { Minestrone::Deploy::SCM.new(scm, self) }
47
+ _cset(:real_revision) { source.local.query_revision(revision) { |cmd| with_env("LC_ALL", "C") { run_locally(cmd) } } }
48
+
49
+ _cset(:strategy) { Minestrone::Deploy::Strategy.new(deploy_via, self) }
50
+
51
+ # If overriding release name, please also select an appropriate setting for :releases below.
52
+ _cset(:release_name) { set :deploy_timestamped, true; Time.now.utc.strftime("%Y%m%d%H%M%S") }
53
+
54
+ _cset :version_dir, "releases"
55
+ _cset :shared_dir, "shared"
56
+ _cset :shared_children, %w(public/system log tmp/pids)
57
+ _cset :current_dir, "current"
58
+
59
+ _cset(:releases_path) { File.join(deploy_to, version_dir) }
60
+ _cset(:shared_path) { File.join(deploy_to, shared_dir) }
61
+ _cset(:current_path) { File.join(deploy_to, current_dir) }
62
+ _cset(:release_path) { File.join(releases_path, release_name) }
63
+
64
+ _cset(:releases) { capture("#{try_sudo} ls -x #{releases_path}").split.sort }
65
+ _cset(:current_release) { releases.length > 0 ? File.join(releases_path, releases.last) : nil }
66
+ _cset(:previous_release) { releases.length > 1 ? File.join(releases_path, releases[-2]) : nil }
67
+
68
+ _cset(:current_revision) { capture("#{try_sudo} cat #{current_path}/REVISION").chomp }
69
+ _cset(:latest_revision) { capture("#{try_sudo} cat #{current_release}/REVISION").chomp }
70
+ _cset(:previous_revision) { capture("#{try_sudo} cat #{previous_release}/REVISION").chomp if previous_release }
71
+
72
+ _cset(:run_method) { fetch(:use_sudo, true) ? :sudo : :run }
73
+
74
+ # some tasks, like symlink, need to always point at the latest release, but
75
+ # they can also (occassionally) be called standalone. In the standalone case,
76
+ # the timestamped release_path will be inaccurate, since the directory won't
77
+ # actually exist. This variable lets tasks like symlink work either in the
78
+ # standalone case, or during deployment.
79
+ _cset(:latest_release) { exists?(:deploy_timestamped) ? release_path : current_release }
80
+
81
+ _cset :maintenance_basename, "maintenance"
82
+ _cset(:maintenance_template_path) { File.join(__dir__, "templates", "maintenance.rhtml") }
83
+
84
+ # =========================================================================
85
+ # These are helper methods that will be available to your recipes.
86
+ # =========================================================================
87
+
88
+ # Checks known version control directories to set the version control in-use.
89
+ # If a .git directory exists in the project, it will set the :scm variable to
90
+ # :git. If no supported directory is found, it will default to :none.
91
+ def scm_default
92
+ File.exist?('.git') ? :git : :none
93
+ end
94
+
95
+ # Auxiliary helper method for the `deploy:check' task. Lets you set up your own dependencies.
96
+ def depend(location, type, *args)
97
+ deps = fetch(:dependencies, {})
98
+ deps[location] ||= {}
99
+ deps[location][type] ||= []
100
+ deps[location][type] << args
101
+ set :dependencies, deps
102
+ end
103
+
104
+ # Temporarily sets an environment variable, yields to a block, and restores the value when it is done.
105
+ def with_env(name, value)
106
+ saved, ENV[name] = ENV[name], value
107
+ yield
108
+ ensure
109
+ ENV[name] = saved
110
+ end
111
+
112
+ # Logs the command then executes it locally. Returns the command output as a string.
113
+ def run_locally(cmd)
114
+ if dry_run
115
+ return logger.debug "executing locally: #{cmd.inspect}"
116
+ end
117
+
118
+ logger.trace "executing locally: #{cmd.inspect}" if logger
119
+ output_on_stdout = nil
120
+
121
+ elapsed = Benchmark.realtime do
122
+ output_on_stdout = `#{cmd}`
123
+ end
124
+
125
+ if $?.to_i > 0 # $? is command exit code (posix style)
126
+ raise Minestrone::LocalArgumentError, "Command #{cmd} returned status code #{$?}"
127
+ end
128
+
129
+ logger.trace "command finished in #{(elapsed * 1000).round}ms" if logger
130
+ output_on_stdout
131
+ end
132
+
133
+
134
+ # If a command is given, this will try to execute the given command, as
135
+ # described below. Otherwise, it will return a string for use in embedding in
136
+ # another command, for executing that command as described below.
137
+ #
138
+ # If :run_method is :sudo (or :use_sudo is true), this executes the given command
139
+ # via +sudo+. Otherwise is uses +run+. If :as is given as a key, it will be
140
+ # passed as the user to sudo as, if using sudo. If the :as key is not given,
141
+ # it will default to whatever the value of the :admin_runner variable is,
142
+ # which (by default) is unset.
143
+ #
144
+ # THUS, if you want to try to run something via sudo, and what to use the
145
+ # root user, you'd just to try_sudo('something'). If you wanted to try_sudo as
146
+ # someone else, you'd just do try_sudo('something', :as => "bob"). If you
147
+ # always wanted sudo to run as a particular user, you could do
148
+ # set(:admin_runner, "bob").
149
+
150
+ def try_sudo(*args)
151
+ options = args.last.is_a?(Hash) ? args.pop : {}
152
+ command = args.shift
153
+ raise ArgumentError, "too many arguments" if args.any?
154
+
155
+ as = options.fetch(:as, fetch(:admin_runner, nil))
156
+ via = fetch(:run_method, :sudo)
157
+
158
+ if command
159
+ invoke_command(command, :via => via, :as => as)
160
+ elsif via == :sudo
161
+ sudo(:as => as)
162
+ else
163
+ ""
164
+ end
165
+ end
166
+
167
+ # Same as sudo, but tries sudo with :as set to the value of the :runner variable (which defaults to "app").
168
+ def try_runner(*args)
169
+ options = args.last.is_a?(Hash) ? args.pop : {}
170
+ args << options.merge(:as => fetch(:runner, "app"))
171
+ try_sudo(*args)
172
+ end
173
+
174
+ # =========================================================================
175
+ # These are the tasks that are available to help with deploying web apps,
176
+ # and specifically, Rails applications. You can have min give you a summary
177
+ # of them with `min -T'.
178
+ # =========================================================================
179
+
180
+ namespace :deploy do
181
+ desc <<-DESC
182
+ Deploys your project. This calls both `update' and `restart'. Note that \
183
+ this will generally only work for applications that have already been deployed \
184
+ once. For a "cold" deploy, you'll want to take a look at the `deploy:cold' \
185
+ task, which handles the cold start specifically.
186
+ DESC
187
+
188
+ task :default do
189
+ update
190
+ restart
191
+ cleanup
192
+ end
193
+
194
+ desc <<-DESC
195
+ Prepares the server for deployment. Before you can use any \
196
+ of the Minestrone deployment tasks with your project, you will need to \
197
+ make sure your server has been prepared with `min deploy:setup'. To \
198
+ temporarily run setup against a different server, specify the SERVER \
199
+ environment variable:
200
+
201
+ $ min SERVER=new.server.com deploy:setup
202
+
203
+ It is safe to run this task on a server that has already been set up; it \
204
+ will not destroy any deployed revisions or data.
205
+ DESC
206
+
207
+ task :setup do
208
+ dirs = [deploy_to, releases_path, shared_path]
209
+ dirs += shared_children.map { |d| File.join(shared_path, d.split('/').last) }
210
+ run "#{try_sudo} mkdir -p #{dirs.join(' ')}"
211
+ run "#{try_sudo} chmod g+w #{dirs.join(' ')}" if fetch(:group_writable, true)
212
+ end
213
+
214
+ desc <<-DESC
215
+ Copies your project and updates the symlink. It does this in a \
216
+ transaction, so that if either `update_code' or `symlink' fail, all \
217
+ changes made to the remote server will be rolled back, leaving your \
218
+ system in the same state it was in before `update' was invoked. Usually, \
219
+ you will want to call `deploy' instead of `update', but `update' can be \
220
+ handy if you want to deploy, but not immediately restart your application.
221
+ DESC
222
+
223
+ task :update do
224
+ transaction do
225
+ update_code
226
+ create_symlink
227
+ end
228
+ end
229
+
230
+ desc <<-DESC
231
+ Copies your project to the remote server. This is the first stage \
232
+ of any deployment; moving your updated code and assets to the deployment \
233
+ server. You will rarely call this task directly, however; instead, you \
234
+ should call the `deploy' task (to do a complete deploy) or the `update' \
235
+ task (if you want to perform the `restart' task separately).
236
+
237
+ You will need to make sure you set the :scm variable to the source \
238
+ control software you are using (it defaults to :git when a .git directory \
239
+ is present, and :none otherwise), and the :deploy_via variable to the \
240
+ strategy you want to use to deploy (it defaults to :remote_cache).
241
+ DESC
242
+
243
+ task :update_code do
244
+ on_rollback { run "rm -rf #{release_path}; true" }
245
+ strategy.deploy!
246
+ finalize_update
247
+ end
248
+
249
+ desc <<-DESC
250
+ [internal] Touches up the released code. This is called by update_code \
251
+ after the basic deploy finishes. It assumes a Rails project was deployed, \
252
+ so if you are deploying something else, you may want to override this \
253
+ task with your own environment's requirements.
254
+
255
+ This task will make the release group-writable (if the :group_writable \
256
+ variable is set to true, which is the default). It will then set up \
257
+ symlinks to the shared directory for the log, system, and tmp/pids \
258
+ directories, and will lastly touch all assets in public/images, \
259
+ public/stylesheets, and public/javascripts so that the times are \
260
+ consistent (so that asset timestamping works). This touch process \
261
+ is only carried out if the :normalize_asset_timestamps variable is \
262
+ set to true, which is the default. The asset directories can be overridden \
263
+ using the :public_children variable.
264
+ DESC
265
+
266
+ task :finalize_update do
267
+ escaped_release = latest_release.to_s.shellescape
268
+ commands = []
269
+ commands << "chmod -R -- g+w #{escaped_release}" if fetch(:group_writable, true)
270
+
271
+ # mkdir -p is making sure that the directories are there for some SCM's that don't
272
+ # save empty folders
273
+ shared_children.map do |dir|
274
+ d = dir.shellescape
275
+
276
+ if (dir.rindex('/')) then
277
+ commands += [
278
+ "rm -rf -- #{escaped_release}/#{d}",
279
+ "mkdir -p -- #{escaped_release}/#{dir.slice(0..(dir.rindex('/'))).shellescape}"
280
+ ]
281
+ else
282
+ commands << "rm -rf -- #{escaped_release}/#{d}"
283
+ end
284
+
285
+ commands << "ln -s -- #{shared_path}/#{dir.split('/').last.shellescape} #{escaped_release}/#{d}"
286
+ end
287
+
288
+ run commands.join(' && ') if commands.any?
289
+
290
+ if fetch(:normalize_asset_timestamps, true)
291
+ stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
292
+
293
+ asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).
294
+ map { |p| "#{latest_release}/public/#{p}" }.
295
+ map { |p| p.shellescape }
296
+
297
+ if asset_paths.any?
298
+ run("find #{asset_paths.join(" ")} -exec touch -t #{stamp} -- {} ';'; true", :env => { "TZ" => "UTC" })
299
+ end
300
+ end
301
+ end
302
+
303
+ desc <<-DESC
304
+ Updates the symlink to the most recently deployed version. Minestrone works \
305
+ by putting each new release of your application in its own directory. When \
306
+ you deploy a new version, this task's job is to update the `current' symlink \
307
+ to point at the new version. You will rarely need to call this task \
308
+ directly; instead, use the `deploy' task (which performs a complete \
309
+ deploy, including `restart') or the 'update' task (which does everything \
310
+ except `restart').
311
+ DESC
312
+
313
+ task :create_symlink do
314
+ on_rollback do
315
+ if previous_release
316
+ run "#{try_sudo} rm -f #{current_path}; #{try_sudo} ln -s #{previous_release} #{current_path}; true"
317
+ else
318
+ logger.important "no previous release to rollback to, rollback of symlink skipped"
319
+ end
320
+ end
321
+
322
+ run "#{try_sudo} rm -f #{current_path} && #{try_sudo} ln -s #{latest_release} #{current_path}"
323
+ end
324
+
325
+ desc <<-DESC
326
+ Copy files to the currently deployed version. This is useful for updating \
327
+ files piecemeal, such as when you need to quickly deploy only a single \
328
+ file. Some files, such as updated templates, images, or stylesheets, \
329
+ might not require a full deploy, and especially in emergency situations \
330
+ it can be handy to just push the updates to production, quickly.
331
+
332
+ To use this task, specify the files and directories you want to copy as a \
333
+ comma-delimited list in the FILES environment variable. All directories \
334
+ will be processed recursively, with all files being pushed to the \
335
+ deployment server.
336
+
337
+ $ min deploy:upload FILES=templates,controller.rb
338
+
339
+ Dir globs are also supported:
340
+
341
+ $ min deploy:upload FILES='config/apache/*.conf'
342
+ DESC
343
+
344
+ task :upload do
345
+ files = (ENV["FILES"] || "").split(",").map { |f| Dir[f.strip] }.flatten
346
+ abort "Please specify at least one file or directory to update (via the FILES environment variable)" if files.empty?
347
+
348
+ files.each { |file| top.upload(file, File.join(current_path, file)) }
349
+ end
350
+
351
+ desc <<-DESC
352
+ Blank task exists as a hook into which to install your own environment \
353
+ specific behaviour.
354
+ DESC
355
+
356
+ task :restart do
357
+ # Empty Task to overload with your platform specifics
358
+ end
359
+
360
+ namespace :rollback do
361
+ desc <<-DESC
362
+ [internal] Points the current symlink at the previous revision.
363
+ This is called by the rollback sequence, and should rarely (if
364
+ ever) need to be called directly.
365
+ DESC
366
+
367
+ task :revision do
368
+ if previous_release
369
+ run "#{try_sudo} rm #{current_path}; #{try_sudo} ln -s #{previous_release} #{current_path}"
370
+ else
371
+ abort "could not rollback the code because there is no prior release"
372
+ end
373
+ end
374
+
375
+ desc <<-DESC
376
+ [internal] Removes the most recently deployed release.
377
+ This is called by the rollback sequence, and should rarely
378
+ (if ever) need to be called directly.
379
+ DESC
380
+
381
+ task :cleanup do
382
+ run "if [ `readlink #{current_path}` != #{current_release} ]; then #{try_sudo} rm -rf #{current_release}; fi"
383
+ end
384
+
385
+ desc <<-DESC
386
+ Rolls back to the previously deployed version. The `current' symlink will \
387
+ be updated to point at the previously deployed version, and then the \
388
+ current release will be removed from the server. You'll generally want \
389
+ to call `rollback' instead, as it performs a `restart' as well.
390
+ DESC
391
+
392
+ task :code do
393
+ revision
394
+ cleanup
395
+ end
396
+
397
+ desc <<-DESC
398
+ Rolls back to a previous version and restarts. This is handy if you ever \
399
+ discover that you've deployed a lemon; `min rollback' and you're right \
400
+ back where you were, on the previously deployed version.
401
+ DESC
402
+
403
+ task :default do
404
+ revision
405
+ restart
406
+ cleanup
407
+ end
408
+ end
409
+
410
+ desc <<-DESC
411
+ Run the migrate rake task. By default, it runs this in most recently \
412
+ deployed version of the app. However, you can specify a different release \
413
+ via the migrate_target variable, which must be one of :latest (for the \
414
+ default behavior), or :current (for the release indicated by the \
415
+ `current' symlink). Strings will work for those values instead of symbols, \
416
+ too. You can also specify additional environment variables to pass to rake \
417
+ via the migrate_env variable. Finally, you can specify the full path to the \
418
+ rake executable by setting the rake variable. The defaults are:
419
+
420
+ set :rake, "rake"
421
+ set :rails_env, "production"
422
+ set :migrate_env, ""
423
+ set :migrate_target, :latest
424
+ DESC
425
+
426
+ task :migrate do
427
+ rake = fetch(:rake, "rake")
428
+ rails_env = fetch(:rails_env, "production")
429
+ migrate_env = fetch(:migrate_env, "")
430
+ migrate_target = fetch(:migrate_target, :latest)
431
+
432
+ directory = case migrate_target.to_sym
433
+ when :current then current_path
434
+ when :latest then latest_release
435
+ else raise ArgumentError, "unknown migration target #{migrate_target.inspect}"
436
+ end
437
+
438
+ run "cd #{directory} && #{rake} RAILS_ENV=#{rails_env} #{migrate_env} db:migrate"
439
+ end
440
+
441
+ desc <<-DESC
442
+ Deploy and run pending migrations. This will work similarly to the \
443
+ `deploy' task, but will also run any pending migrations (via the \
444
+ `deploy:migrate' task) prior to updating the symlink. Note that the \
445
+ update in this case it is not atomic, and transactions are not used, \
446
+ because migrations are not guaranteed to be reversible.
447
+ DESC
448
+
449
+ task :migrations do
450
+ set :migrate_target, :latest
451
+ update_code
452
+ migrate
453
+ create_symlink
454
+ restart
455
+ cleanup
456
+ end
457
+
458
+ desc <<-DESC
459
+ Clean up old releases. By default, the last 5 releases are kept on each \
460
+ server (though you can change this with the keep_releases variable). All \
461
+ other deployed revisions are removed from the server. By default, this \
462
+ will use sudo to clean up the old releases, but if sudo is not available \
463
+ for your environment, set the :use_sudo variable to false instead.
464
+ DESC
465
+
466
+ task :cleanup do
467
+ if (count = fetch(:keep_releases)) && count != :all
468
+ try_sudo "ls -1dt #{releases_path}/* | tail -n +#{count.to_i + 1} | #{try_sudo} xargs rm -rf"
469
+ end
470
+ end
471
+
472
+ desc <<-DESC
473
+ Test deployment dependencies. Checks things like directory permissions, \
474
+ necessary utilities, and so forth, reporting on the things that appear to \
475
+ be incorrect or missing. This is good for making sure a deploy has a \
476
+ chance of working before you actually run `min deploy'.
477
+
478
+ You can define your own dependencies, as well, using the `depend' method:
479
+
480
+ depend :remote, :gem, "tzinfo", ">=0.3.3"
481
+ depend :local, :command, "svn"
482
+ depend :remote, :directory, "/u/depot/files"
483
+ DESC
484
+
485
+ task :check do
486
+ dependencies = strategy.check!
487
+
488
+ other = fetch(:dependencies, {})
489
+ other.each do |location, types|
490
+ types.each do |type, calls|
491
+ if type == :gem
492
+ dependencies.send(location).command(fetch(:gem_command, "gem")).or("`gem' command could not be found. Try setting :gem_command")
493
+ end
494
+
495
+ calls.each do |args|
496
+ dependencies.send(location).send(type, *args)
497
+ end
498
+ end
499
+ end
500
+
501
+ if dependencies.pass?
502
+ puts "You appear to have all necessary dependencies installed"
503
+ else
504
+ puts "The following dependencies failed. Please check them and try again:"
505
+
506
+ dependencies.reject { |d| d.pass? }.each do |d|
507
+ puts "--> #{d.message}"
508
+ end
509
+
510
+ abort
511
+ end
512
+ end
513
+
514
+ desc <<-DESC
515
+ Deploys and starts a `cold' application. This is useful if you have not \
516
+ deployed your application before, or if your application is (for some \
517
+ other reason) not currently running. It will deploy the code, run any \
518
+ pending migrations, and then instead of invoking `deploy:restart', it will \
519
+ invoke `deploy:start' to fire up the application servers.
520
+ DESC
521
+
522
+ task :cold do
523
+ update
524
+ migrate
525
+ start
526
+ end
527
+
528
+ desc <<-DESC
529
+ Blank task exists as a hook into which to install your own environment \
530
+ specific behaviour.
531
+ DESC
532
+
533
+ task :start do
534
+ # Empty Task to overload with your platform specifics
535
+ end
536
+
537
+ desc <<-DESC
538
+ Blank task exists as a hook into which to install your own environment \
539
+ specific behaviour.
540
+ DESC
541
+
542
+ task :stop do
543
+ # Empty Task to overload with your platform specifics
544
+ end
545
+
546
+ namespace :pending do
547
+ desc <<-DESC
548
+ Displays the `diff' since your last deploy. This is useful if you want \
549
+ to examine what changes are about to be deployed. Note that this might \
550
+ not be supported on all SCM's.
551
+ DESC
552
+
553
+ task :diff do
554
+ system(source.local.diff(current_revision))
555
+ end
556
+
557
+ desc <<-DESC
558
+ Displays the commits since your last deploy. This is good for a summary \
559
+ of the changes that have occurred since the last deploy. Note that this \
560
+ might not be supported on all SCM's.
561
+ DESC
562
+
563
+ task :default do
564
+ from = source.next_revision(current_revision)
565
+ system(source.local.log(from))
566
+ end
567
+ end
568
+
569
+ namespace :web do
570
+ desc <<-DESC
571
+ Present a maintenance page to visitors. Disables your application's web \
572
+ interface by writing a "#{maintenance_basename}.html" file to the web server. The \
573
+ server must be configured to detect the presence of this file, and if \
574
+ it is present, always display it instead of performing the request.
575
+
576
+ By default, the maintenance page will just say the site is down for \
577
+ "maintenance", and will be back "shortly", but you can customize the \
578
+ page by specifying the REASON and UNTIL environment variables:
579
+
580
+ $ min deploy:web:disable \\
581
+ REASON="hardware upgrade" \\
582
+ UNTIL="12pm Central Time"
583
+
584
+ You can use a different template for the maintenance page by setting the \
585
+ :maintenance_template_path variable in your deploy.rb file. The template file \
586
+ should either be a plaintext or an erb file.
587
+
588
+ Further customization will require that you write your own task.
589
+ DESC
590
+
591
+ task :disable do
592
+ require 'erb'
593
+ on_rollback { run "rm -f #{shared_path}/system/#{maintenance_basename}.html" }
594
+
595
+ warn <<-EOHTACCESS
596
+
597
+ # Please add something like this to your site's Apache htaccess to redirect users to the maintenance page.
598
+ # More Info: http://www.shiftcommathree.com/articles/make-your-rails-maintenance-page-respond-with-a-503
599
+
600
+ ErrorDocument 503 /system/#{maintenance_basename}.html
601
+ RewriteEngine On
602
+ RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png)$
603
+ RewriteCond %{DOCUMENT_ROOT}/system/#{maintenance_basename}.html -f
604
+ RewriteCond %{SCRIPT_FILENAME} !#{maintenance_basename}.html
605
+ RewriteRule ^.*$ - [redirect=503,last]
606
+
607
+ # Or if you are using Nginx add this to your server config:
608
+
609
+ if (-f $document_root/system/maintenance.html) {
610
+ return 503;
611
+ }
612
+ error_page 503 @maintenance;
613
+ location @maintenance {
614
+ rewrite ^(.*)$ /system/maintenance.html break;
615
+ break;
616
+ }
617
+ EOHTACCESS
618
+
619
+ reason = ENV['REASON']
620
+ deadline = ENV['UNTIL']
621
+
622
+ template = File.read(maintenance_template_path)
623
+ result = ERB.new(template).result(binding)
624
+
625
+ put result, "#{shared_path}/system/#{maintenance_basename}.html", :mode => 0644
626
+ end
627
+
628
+ desc <<-DESC
629
+ Makes the application web-accessible again. Removes the \
630
+ "#{maintenance_basename}.html" page generated by deploy:web:disable, which (if your \
631
+ web server is configured correctly) will make your application \
632
+ web-accessible again.
633
+ DESC
634
+
635
+ task :enable do
636
+ run "rm -f #{shared_path}/system/#{maintenance_basename}.html"
637
+ end
638
+ end
639
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ desc <<-DESC
4
+ Invoke a single command on the remote server. This is useful for performing \
5
+ one-off commands that may not require a full task to be written for them. \
6
+ Simply specify the command to execute via the COMMAND environment variable. \
7
+ To temporarily replace the configured server, specify the SERVER environment \
8
+ variable. Lastly, if you want to \
9
+ execute the command via sudo, specify a non-empty value for the SUDO \
10
+ environment variable.
11
+
12
+ Sample usage:
13
+
14
+ $ min COMMAND=uptime SERVER=foo.capistano.test invoke
15
+ $ min SERVER=app1.example.com SUDO=1 COMMAND="tail -f /var/log/messages" invoke
16
+ DESC
17
+
18
+ task :invoke do
19
+ command = ENV["COMMAND"] || ""
20
+ abort "Please specify a command to execute on the remote server (via the COMMAND environment variable)" if command.empty?
21
+ method = ENV["SUDO"] ? :sudo : :run
22
+ invoke_command(command, :via => method)
23
+ end