Module: Recap::Support::CapistranoExtensions
- Defined in:
- lib/recap/support/capistrano_extensions.rb
Overview
These methods are used by recap tasks to run commands and detect when files have changed as part of a deployment.
Instance Method Summary collapse
- #_cset(name, *args, &block) ⇒ Object
-
#as_app(command, pwd = deploy_to) ⇒ Object
Run a command as the application user.
- #as_app_once(command, pwd = deploy_to) ⇒ Object
-
#capture_git(command) ⇒ Object
Capture the result of a git command run within the ‘deploy_to` directory.
- #changed_files ⇒ Object
- #claim_lock(message) ⇒ Object
-
#deployed_file_changed?(path) ⇒ Boolean
Has the given path been created or changed since the previous deployment? During the first successful deployment this will always return true if the file exists.
-
#deployed_file_exists?(path, root_path = deploy_to) ⇒ Boolean
Does the given file exist within the deployment directory?.
-
#edit_file(path) ⇒ Object
Edit a file on the remote server, using a local editor.
- #editor ⇒ Object
- #exit_code(command) ⇒ Object
- #exit_code_as_app(command, pwd = deploy_to) ⇒ Object
-
#git(command) ⇒ Object
Run a git command in the ‘deploy_to` directory.
-
#latest_tag_from_repository ⇒ Object
Find the latest tag from the repository.
-
#put_as_app(string, path) ⇒ Object
Put a string into a file as the application user.
- #release_lock ⇒ Object
- #transaction_with_lock(message) ⇒ Object
- #trigger_update?(path) ⇒ Boolean
Instance Method Details
#_cset(name, *args, &block) ⇒ Object
116 117 118 119 120 |
# File 'lib/recap/support/capistrano_extensions.rb', line 116 def _cset(name, *args, &block) unless exists?(name) set(name, *args, &block) end end |
#as_app(command, pwd = deploy_to) ⇒ Object
Run a command as the application user
8 9 10 |
# File 'lib/recap/support/capistrano_extensions.rb', line 8 def as_app(command, pwd = deploy_to) sudo "su - #{application_user} -c 'cd #{pwd} && #{command}'" end |
#as_app_once(command, pwd = deploy_to) ⇒ Object
12 13 14 |
# File 'lib/recap/support/capistrano_extensions.rb', line 12 def as_app_once(command, pwd = deploy_to) sudo "su - #{application_user} -c 'cd #{pwd} && #{command}'", :once => true end |
#capture_git(command) ⇒ Object
Capture the result of a git command run within the ‘deploy_to` directory
47 48 49 |
# File 'lib/recap/support/capistrano_extensions.rb', line 47 def capture_git(command) capture "cd #{deploy_to} && umask 002 && sg #{application_group} -c 'git #{command}'" end |
#changed_files ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/recap/support/capistrano_extensions.rb', line 78 def changed_files @changed_files ||= if latest_tag capture_git("diff --name-only #{latest_tag} origin/#{branch} | cat").split else capture_git("ls-files | cat").split end end |
#claim_lock(message) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/recap/support/capistrano_extensions.rb', line 90 def claim_lock() begin sudo "[ ! -e #{deploy_lock_file} ] && echo '#{}' > #{deploy_lock_file}" rescue Exception => e abort %{ Failed to claim lock: #{capture("cat #{deploy_lock_file}")} If you think this lock no longer applies, clear it using the `deploy:unlock` task and try again. } end end |
#deployed_file_changed?(path) ⇒ Boolean
Has the given path been created or changed since the previous deployment? During the first successful deployment this will always return true if the file exists.
73 74 75 76 |
# File 'lib/recap/support/capistrano_extensions.rb', line 73 def deployed_file_changed?(path) return deployed_file_exists?(path) unless latest_tag exit_code("cd #{deploy_to} && git diff --exit-code #{latest_tag} origin/#{branch} #{path}") == "1" end |
#deployed_file_exists?(path, root_path = deploy_to) ⇒ Boolean
Does the given file exist within the deployment directory?
67 68 69 |
# File 'lib/recap/support/capistrano_extensions.rb', line 67 def deployed_file_exists?(path, root_path = deploy_to) exit_code("cd #{root_path} && [ -f #{path} ]") == "0" end |
#edit_file(path) ⇒ Object
Edit a file on the remote server, using a local editor
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/recap/support/capistrano_extensions.rb', line 29 def edit_file(path) if editor as_app "touch #{path} && chmod g+rw #{path}" local_path = Tempfile.new('deploy-edit').path get(path, local_path) Recap::Support::ShellCommand.execute_interactive("#{editor} #{local_path}") File.read(local_path) else abort "To edit a remote file, either the EDITOR or DEPLOY_EDITOR environment variables must be set" end end |
#editor ⇒ Object
24 25 26 |
# File 'lib/recap/support/capistrano_extensions.rb', line 24 def editor ENV['DEPLOY_EDITOR'] || ENV['EDITOR'] end |
#exit_code(command) ⇒ Object
51 52 53 |
# File 'lib/recap/support/capistrano_extensions.rb', line 51 def exit_code(command) capture("#{command} > /dev/null 2>&1; echo $?").strip end |
#exit_code_as_app(command, pwd = deploy_to) ⇒ Object
55 56 57 |
# File 'lib/recap/support/capistrano_extensions.rb', line 55 def exit_code_as_app(command, pwd = deploy_to) capture(%|sudo -p 'sudo password: ' su - #{application_user} -c 'cd #{pwd} && #{command} > /dev/null 2>&1'; echo $?|).strip end |
#git(command) ⇒ Object
Run a git command in the ‘deploy_to` directory
42 43 44 |
# File 'lib/recap/support/capistrano_extensions.rb', line 42 def git(command) run "cd #{deploy_to} && umask 002 && sg #{application_group} -c \"git #{command}\"" end |
#latest_tag_from_repository ⇒ Object
Find the latest tag from the repository. As ‘git tag` returns tags in order, and our release tags are timestamps, the latest tag will always be the last in the list.
61 62 63 64 |
# File 'lib/recap/support/capistrano_extensions.rb', line 61 def latest_tag_from_repository = capture_git("tag").strip.split .grep(release_matcher).last end |
#put_as_app(string, path) ⇒ Object
Put a string into a file as the application user
17 18 19 20 21 22 |
# File 'lib/recap/support/capistrano_extensions.rb', line 17 def put_as_app(string, path) put string, "/tmp/recap-put-as-app" as_app "cp /tmp/recap-put-as-app #{path} && chmod g+rw #{path}", "/" ensure run "rm /tmp/recap-put-as-app" end |
#release_lock ⇒ Object
103 104 105 |
# File 'lib/recap/support/capistrano_extensions.rb', line 103 def release_lock sudo "rm -rf #{deploy_lock_file}" end |
#transaction_with_lock(message) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/recap/support/capistrano_extensions.rb', line 107 def transaction_with_lock() on_rollback { release_lock } transaction do claim_lock() yield release_lock end end |
#trigger_update?(path) ⇒ Boolean
86 87 88 |
# File 'lib/recap/support/capistrano_extensions.rb', line 86 def trigger_update?(path) force_full_deploy || changed_files.detect {|p| p[0, path.length] == path} end |