Module: CrowdFavorite::Support::CapistranoExtensions

Defined in:
lib/crowdfavorite/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

Instance Method Details

#as_app(command, pwd = deploy_to) ⇒ Object

Run a command as the application user



32
33
34
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 32

def as_app(command, pwd = deploy_to)
  as_user application_user, command, pwd
end

#as_root(command, pwd = deploy_to) ⇒ Object

Run a command as root



27
28
29
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 27

def as_root(command, pwd = deploy_to)
  as_user 'root', command, pwd
end

#as_user(user, command, pwd = deploy_to) ⇒ Object

Run a command as the given user



22
23
24
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 22

def as_user(user, command, pwd = deploy_to)
  sudo "su - #{user} -c 'cd #{pwd} && #{command}'"
end

#capture_git(command) ⇒ Object

Capture the result of a git command run within the ‘deploy_to` directory



67
68
69
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 67

def capture_git(command)
  capture "cd #{deploy_to} && umask 002 && sg #{application_group} -c 'git #{command}'"
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.

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 93

def deployed_file_changed?(path)
  return true 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?

Returns:

  • (Boolean)


87
88
89
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 87

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



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 49

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)
    CrowdFavorite::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

#editorObject



44
45
46
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 44

def editor
  ENV['DEPLOY_EDITOR'] || ENV['EDITOR']
end

#exit_code(command) ⇒ Object



71
72
73
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 71

def exit_code(command)
  capture("#{command} > /dev/null 2>&1; echo $?").strip
end

#exit_code_as_app(command, pwd = deploy_to) ⇒ Object



75
76
77
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 75

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



62
63
64
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 62

def git(command)
  run "cd #{deploy_to} && umask 002 && sg #{application_group} -c \"git #{command}\""
end

#latest_tag_from_repositoryObject

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.



81
82
83
84
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 81

def latest_tag_from_repository
  result = capture_git("tag | tail -n1").strip
  result.empty? ? nil : result
end

#put_as_app(string, path) ⇒ Object

Put a string into a file as the application user



37
38
39
40
41
42
# File 'lib/crowdfavorite/support/capistrano_extensions.rb', line 37

def put_as_app(string, path)
  put string, "/tmp/cf-put-as-app"
  as_app "cp /tmp/cf-put-as-app #{path} && chmod g+rw #{path}", "/"
ensure
  run "rm /tmp/cf-put-as-app"
end