Class: Capistrano::Former03::Rsync

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Rsync

Returns a new instance of Rsync.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/capistrano/former03/rsync.rb', line 19

def initialize(opts={})

  # test for required flags
  required_flags = [:src_path, :dest_path]
  required_flags.each do |flag|
    if not opts.has_key?(flag)
      raise "Required arg '#{flag}' not given"
    end
  end

  if not opts[:src_type].nil? and not opts[:dest_type].nil?
    raise "Only one path can be remote"
  end

  # copy opts as instance vars
  opts.each do |k,v|
    instance_variable_set("@#{k}", v)
  end
end

Class Method Details

.build_path(path, type) ⇒ Object



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

def self.build_path(path,type)
  return_path = ""

  if not type.nil?
    user = "#{type.user}@" unless type.user.nil?
    return_path += "#{user}#{type.hostname}:"
  end

  # Ensure path ends with slash
  return_path += path.to_s
  return_path << '/' unless return_path.end_with?('/')

  return_path
end

Instance Method Details

#commandObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/capistrano/former03/rsync.rb', line 99

def command
  if @rsync_path.nil?
    options = [:rsync]
  else 
    options = [@rsync_path]
  end

  # Add options
  options += fetch(:rsync_options)

  # Add remote options if needed
  options += remote_options

  # Add supplied options
  if not @rsync_options.nil?
    options += @rsync_options
  end

  # Add source and destination
  options += [src_path, dest_path]

  options
end

#dest_pathObject



95
96
97
# File 'lib/capistrano/former03/rsync.rb', line 95

def dest_path
  return Rsync.build_path(@dest_path,@dest_type)
end

#passwordObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/capistrano/former03/rsync.rb', line 44

def password
  [role.ssh_options, fetch(:ssh_options)].each do |s|
    begin
      return s[:password]
    rescue
      next
    end
  end
  return nil
end

#remote_optionsObject



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
85
86
87
88
89
# File 'lib/capistrano/former03/rsync.rb', line 56

def remote_options
  options = []
  ssh_options = []
  # No remote site
  return [] if role.nil?

  # Handle passsword
  if not password.nil?
    ssh_options += [
      :sshpass,
      '-p',
      "'#{password}'",
      :ssh,
      '-o',
      'PubkeyAuthentication=no',
    ]
  end

  if not role.port.nil?
    ssh_options += ['ssh'] if ssh_options.length == 0
    ssh_options += ['-p', role.port]
  end

  # Add options
  options += ['-e', "\"#{ssh_options.join(' ')}\""] if ssh_options.length > 0

  # Add rsync path if needed
  rsync_path = role.properties.fetch(:deploy_rsync_path)
  if not rsync_path.nil?
    options += ['--rsync-path', rsync_path]
  end

  return options
end

#roleObject



39
40
41
# File 'lib/capistrano/former03/rsync.rb', line 39

def role
  @src_type || @dest_type
end

#src_pathObject



91
92
93
# File 'lib/capistrano/former03/rsync.rb', line 91

def src_path
  return Rsync.build_path(@src_path,@src_type)
end