Class: Capistrano::SCM::Rsync

Inherits:
Plugin
  • Object
show all
Defined in:
lib/capistrano/rsync.rb

Instance Method Summary collapse

Instance Method Details

#define_tasksObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/capistrano/rsync.rb', line 20

def define_tasks
  namespace :rsync do
    desc <<-DESC
        Copy application source code from (remote) cache to release path.

        If a :rsync_deploy_build_path is set, only that relative path will \
        be copied to the release path.
    DESC
    task create_release: :update_remote_cache do
      on release_roles :all do
        execute :rsync, '--archive', "#{fetch(:deploy_to)}/#{fetch(:rsync_remote_cache)}/#{fetch(:rsync_deploy_build_path)}", "#{release_path}/"
      end
    end

    desc <<-DESC
        Update remote cache of application source code.

        This will be rsynced to :rsync_remote_cache, using rsync options set in \
        :rsync_options
    DESC
    task update_remote_cache: :update_local_cache do
      on release_roles :all do |role|
        host_spec = role.hostname
        host_spec = "#{role.user}@#{host_spec}" if role.user
        run_locally do
          execute :rsync, *fetch(:rsync_options), "#{fetch(:rsync_local_cache)}/", "#{host_spec}:#{fetch(:deploy_to)}/#{fetch(:rsync_remote_cache)}/"
        end
      end
    end

    desc <<-DESC
        Update local cache of application source code.

        This will be checked out to :rsync_local_cache.
    DESC
    task :update_local_cache do
      run_locally do
        unless File.exist?("#{fetch(:rsync_local_cache)}/.git")
          FileUtils.mkdir_p(fetch(:rsync_local_cache))
          recursive_submodules = fetch(:rsync_git_submodules) ? '--recurse-submodules' : ''
          execute :git, :clone, recursive_submodules, '--quiet', repo_url, fetch(:rsync_local_cache)
        end
        within fetch(:rsync_local_cache) do
          execute :git, :fetch, '--quiet', '--all', '--prune'
          execute :git, :checkout, fetch(:branch)
          execute :git, :reset, '--quiet', '--hard', "origin/#{fetch(:branch)}"
          execute :git, :submodule, :update, '--recursive', '--init' if fetch(:rsync_git_submodules)
        end
      end
    end

    desc <<-DESC
        Determine version of code that rsync will deploy.

        By default, this is the latest version of the code on branch :branch.
    DESC
    task :set_current_revision do
      run_locally do
        within fetch(:rsync_local_cache) do
          set :current_revision, capture(:git, "rev-list --max-count=1 #{fetch(:branch)}")
        end
      end
    end
  end
end

#register_hooksObject



86
87
88
89
# File 'lib/capistrano/rsync.rb', line 86

def register_hooks
  after 'deploy:new_release_path', 'rsync:create_release'
  before 'deploy:set_current_revision', 'rsync:set_current_revision'
end

#set_defaultsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/capistrano/rsync.rb', line 5

def set_defaults
   # command-line options for rsync
  set_if_empty :rsync_options, %w[--archive --delete --exclude=.git*]

  # Local cache (Git checkout will be happen here, resulting files then get rsynced to the remote server)
  set_if_empty :rsync_local_cache, 'tmp/.capistrano-rsync-deploy'

  # Remote cache on the server. Will be synced with the local cache before each release, and then used as
  # source for the release. Saves needing to transfer all the source files for each release.
  set_if_empty :rsync_remote_cache, 'rsync-deploy'

  # Git fetch automaticly submodules
  set_if_empty :rsync_git_submodules, true
end