Module: Capistrano::S3Archive::SCM::RsyncStrategy

Defined in:
lib/capistrano/s3_archive.rb

Defined Under Namespace

Classes: MissingSSHKyesError, ResourceBusyError

Instance Method Summary collapse

Instance Method Details

#checkObject



89
90
91
92
93
94
95
# File 'lib/capistrano/s3_archive.rb', line 89

def check
  return if context.class == SSHKit::Backend::Local
  ssh_key  = ssh_key_for(context.host)
  if ssh_key.nil?
    fail MissingSSHKyesError, "#{RsyncStrategy} only supports publickey authentication. Please set #{context.host.hostname}.keys or ssh_options."
  end
end

#cleanupObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/capistrano/s3_archive.rb', line 135

def cleanup
  run_locally do
    archives_dir = File.join(fetch(:s3_archive), fetch(:stage).to_s)
    archives = capture(:ls, '-xtr', archives_dir).split
    if archives.count >= fetch(:keep_releases)
      tobe_removes = (archives - archives.last(fetch(:keep_releases)))
      if tobe_removes.any?
        tobe_removes_str = tobe_removes.map do |file|
          File.join(archives_dir, file)
        end.join(' ')
        execute :rm, tobe_removes_str
      end
    end
  end
end

#current_revisionObject



183
184
185
# File 'lib/capistrano/s3_archive.rb', line 183

def current_revision
  fetch(:version_id) ? "#{archive_object_key}?versionid=#{fetch(:version_id)}" : archive_object_key
end

#local_checkObject



85
86
87
# File 'lib/capistrano/s3_archive.rb', line 85

def local_check
  list_objects(false)
end

#release(server = context.host) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/capistrano/s3_archive.rb', line 151

def release(server = context.host)
  unless context.class == SSHKit::Backend::Local
    user = user_for(server) + '@' unless user_for(server).nil?
    key  = ssh_key_for(server)
    ssh_port_option = server.port.nil? ? '' : "-p #{server.port}"
  end
  rsync = ['rsync']
  rsync.concat fetch(:rsync_options)
  rsync << fetch(:local_cache_dir) + '/'
  unless context.class == SSHKit::Backend::Local
    rsync << "-e 'ssh -i #{key} #{ssh_port_option} #{fetch(:rsync_ssh_options).join(' ')}'"
    rsync << "#{user}#{server.hostname}:#{rsync_cache || release_path}"
  else
    rsync << '--no-compress'
    rsync << "#{rsync_cache || release_path}"
  end
  release_lock do
    run_locally do
      execute *rsync
    end
  end

  unless fetch(:rsync_cache).nil?
    cache = rsync_cache
    link_option = if fetch(:hardlink) && test!("[ `readlink #{current_path}` != #{release_path} ]")
                    "--link-dest `readlink #{current_path}`"
                  end
    copy = %(#{fetch(:rsync_copy)} #{link_option} "#{cache}/" "#{release_path}/")
    context.execute copy
  end
end

#ssh_key_for(host) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/capistrano/s3_archive.rb', line 187

def ssh_key_for(host)
  if not host.keys.empty?
    host.keys.first
  elsif host.ssh_options && host.ssh_options.has_key?(:keys)
    Array(host.ssh_options[:keys]).first
  elsif fetch(:ssh_options, nil) && fetch(:ssh_options).has_key?(:keys)
    fetch(:ssh_options)[:keys].first
  end
end

#stageObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/capistrano/s3_archive.rb', line 97

def stage
  stage_lock do
    archive_dir = File.join(fetch(:s3_archive), fetch(:stage).to_s)
    archive_file = File.join(archive_dir, File.basename(archive_object_key))
    tmp_file = "#{archive_file}.part"
    etag_file = File.join(archive_dir, ".#{File.basename(archive_object_key)}.etag")
    fail "#{tmp_file} is found. Another process is running?" if File.exist?(tmp_file)

    etag = .tap { |it| fail "No such object: #{current_revision}" if it.nil? }.etag
    if [archive_file, etag_file].all? { |f| File.exist?(f) } && File.read(etag_file) == etag
      context.info "#{archive_file} (etag:#{etag}) is found. download skipped."
    else
      context.info "Download #{current_revision} to #{archive_file}"
      mkdir_p(File.dirname(archive_file))
      File.open(tmp_file, 'w') do |f|
        get_object(f)
      end
      move(tmp_file, archive_file)
      File.write(etag_file, etag)
    end

    remove_entry_secure(fetch(:local_cache_dir)) if File.exist? fetch(:local_cache_dir)
    mkdir_p(fetch(:local_cache_dir))
    case archive_file
    when /\.zip\Z/
      cmd = "unzip -q -d #{fetch(:local_cache_dir)} #{archive_file}"
    when /\.tar\.gz\Z|\.tar\.bz2\Z|\.tgz\Z/
      cmd = "tar xf #{archive_file} -C #{fetch(:local_cache_dir)}"
    end

    release_lock(true) do
      run_locally do
        execute cmd
      end
    end
  end
end

#user_for(host) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/capistrano/s3_archive.rb', line 197

def user_for(host)
  if host.user
    host.user
  elsif host.ssh_options && host.ssh_options.has_key?(:user)
    host.ssh_options[:user]
  elsif fetch(:ssh_options, nil) && fetch(:ssh_options).has_key?(:user)
    fetch(:ssh_options)[:user]
  end
end