Module: Capistrano::Caplock

Defined in:
lib/gnm-caplock.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_into(configuration) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gnm-caplock.rb', line 35

def self.load_into(configuration)
  configuration.load do
    set :lockfile, "cap.lock"
    
    namespace :lock do
      desc "check lock"
      task :check, :roles => :app do
        if caplock.remote_file_exists?("#{deploy_to}/#{lockfile}")
          abort "\n\n\n\e[0;31m A Deployment is already in progress\n Remove #{deploy_to}/#{lockfile} to unlock  \e[0m\n\n\n"
        end
      end

      desc "create lock"
      task :create, :roles => :app do
        timestamp = Time.now.strftime("%m/%d/%Y %H:%M:%S %Z")
        lock_message = "Deploy started at #{timestamp} in progress"
        put lock_message, "#{deploy_to}/#{lockfile}", :mode => 0644
      end

      desc "release lock"
      task :release, :roles => :app do
         run "rm -f #{deploy_to}/#{lockfile}"
      end
    end

    # Deployment
    before "deploy", "lock:check"
    after "lock:check", "lock:create"
    after "deploy", "lock:release"

    # Rollback
    before "deploy:rollback", "lock:check"
    after "deploy:rollback", "lock:release"
    
  end
end

Instance Method Details

#remote_file_content_same_as?(full_path, content) ⇒ Boolean

Returns Boolean value indicating whether the file at full_path matches content. Checks if file is equivalent to content by checking whether or not the MD5 of the remote content is the same as the MD5 of the String in content.

Returns:

  • (Boolean)


25
26
27
# File 'lib/gnm-caplock.rb', line 25

def remote_file_content_same_as?(full_path, content)
  Digest::MD5.hexdigest(content) == capture("md5sum #{full_path} | awk '{ print $1 }'").strip
end

#remote_file_differs?(full_path, content) ⇒ Boolean

Returns Boolean indicating whether the remote file is present and has the same contents as the String in content.

Returns:

  • (Boolean)


31
32
33
# File 'lib/gnm-caplock.rb', line 31

def remote_file_differs?(full_path, content)
  !remote_file_exists?(full_path) || remote_file_exists?(full_path) && !remote_file_content_same_as?(full_path, content)
end

#remote_file_exists?(full_path) ⇒ Boolean

Returns Boolean value indicating whether file exists on server

Returns:

  • (Boolean)


18
19
20
# File 'lib/gnm-caplock.rb', line 18

def remote_file_exists?(full_path)
  remote_filetest_passes?('-e', full_path)
end

#remote_filetest_passes?(filetest, full_path) ⇒ Boolean

Returns Boolean indicating the result of filetest on full_path on the server, evaluated by shell on the server (usually bash or something roughly compatible).

Returns:

  • (Boolean)


8
9
10
# File 'lib/gnm-caplock.rb', line 8

def remote_filetest_passes?(filetest, full_path)
  'true' ==  capture("if [ #{filetest} #{full_path} ]; then echo 'true'; fi").strip
end

Checks if a symlink exists on the remote machine.

Returns:

  • (Boolean)


13
14
15
# File 'lib/gnm-caplock.rb', line 13

def remote_symlink_exists?(full_path)
  remote_filetest_passes?('-L', full_path)
end