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 deployments
Instance Method Summary collapse
-
#as_app(command, pwd = deploy_to) ⇒ Object
Run a command as the application user.
-
#capture_git(command) ⇒ Object
Capture the result of a git command run within the ‘deploy_to` directory.
- #changed_files ⇒ 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.
- #trigger_update?(path) ⇒ Boolean
Instance Method Details
#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 |
#capture_git(command) ⇒ Object
Capture the result of a git command run within the ‘deploy_to` directory
43 44 45 |
# File 'lib/recap/support/capistrano_extensions.rb', line 43 def capture_git(command) capture "cd #{deploy_to} && umask 002 && sg #{application_group} -c 'git #{command}'" end |
#changed_files ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/recap/support/capistrano_extensions.rb', line 74 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 |
#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.
69 70 71 72 |
# File 'lib/recap/support/capistrano_extensions.rb', line 69 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?
63 64 65 |
# File 'lib/recap/support/capistrano_extensions.rb', line 63 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
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/recap/support/capistrano_extensions.rb', line 25 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
20 21 22 |
# File 'lib/recap/support/capistrano_extensions.rb', line 20 def editor ENV['DEPLOY_EDITOR'] || ENV['EDITOR'] end |
#exit_code(command) ⇒ Object
47 48 49 |
# File 'lib/recap/support/capistrano_extensions.rb', line 47 def exit_code(command) capture("#{command} > /dev/null 2>&1; echo $?").strip end |
#exit_code_as_app(command, pwd = deploy_to) ⇒ Object
51 52 53 |
# File 'lib/recap/support/capistrano_extensions.rb', line 51 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
38 39 40 |
# File 'lib/recap/support/capistrano_extensions.rb', line 38 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.
57 58 59 60 |
# File 'lib/recap/support/capistrano_extensions.rb', line 57 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
13 14 15 16 17 18 |
# File 'lib/recap/support/capistrano_extensions.rb', line 13 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 |
#trigger_update?(path) ⇒ Boolean
82 83 84 |
# File 'lib/recap/support/capistrano_extensions.rb', line 82 def trigger_update?(path) force_full_deploy || changed_files.detect {|p| p[0, path.length] == path} end |