Module: TddDeploy::CopyMethods

Included in:
Base, Installer
Defined in:
lib/tdd_deploy/copy_methods.rb

Instance Method Summary collapse

Instance Method Details

#append_dir_to_remote_hosts_as(userid, host_list, src_dir, dest_dir) ⇒ Object

Raises:

  • (::ArgumentError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tdd_deploy/copy_methods.rb', line 59

def append_dir_to_remote_hosts_as(userid, host_list, src_dir, dest_dir)
  raise ::ArgumentError.new("append_dir_to_remote_hosts_as: src_dir does not exist: #{src_dir}") \
      unless File.directory? src_dir
  host_list = [host_list] if host_list.is_a? String
  result = true
  host_list.uniq.each do |host|
    result &= mkdir_on_remote_as userid, host, dest_dir
  end
  Dir.open(src_dir).each do |fname|
    next if fname[0] == '.'
    path = File.join(src_dir, fname)
    result &= append_file_to_remote_hosts_as userid, host_list, path, File.join(dest_dir, fname)
  end
  result
end

#append_file_to_remote_file_as(userid, host, src, dst) ⇒ Object

Raises:

  • (::ArgumentError)


113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/tdd_deploy/copy_methods.rb', line 113

def append_file_to_remote_file_as(userid, host, src, dst)
  raise ::ArgumentError.new("file name cannot be empty") if src.empty?
  raise ::RuntimeError.new("unable to copy #{src} to #{userid}@#{host}: #{src} not found") unless File.exists? src

  f = File.new(src)
  file_mode = f.stat.mode & 0777
  
  if (result = append_string_to_remote_file_as(userid, host, f.read, dst))
    stdout, stderr, cmd = run_on_a_host_as(userid, host, "chmod 0#{sprintf('%o', file_mode)} #{dst}")
    result &= stderr.nil?
  end
  result
end

#append_file_to_remote_hosts_as(userid, host_list, src, dst) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/tdd_deploy/copy_methods.rb', line 50

def append_file_to_remote_hosts_as userid, host_list, src, dst
  result = true
  host_list = [host_list] if host_list.is_a? String
  host_list.uniq.each do |host|
    result &= append_file_to_remote_file_as userid, host, src, dst
  end
  result
end

#append_string_to_remote_file_as(userid, host, str, dst) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tdd_deploy/copy_methods.rb', line 91

def append_string_to_remote_file_as userid, host, str, dst
  result = nil
  Net::SFTP.start(host, userid) do |sftp|
    if handle = sftp.open!(dst, 'a+')
      stat_buf = sftp.fstat! handle
      result = sftp.write handle, stat_buf.size, str
      sftp.close! handle
    end
  end
  ! result.nil?
end

#append_string_to_remote_file_on_hosts_as(userid, host_list, str, dst) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/tdd_deploy/copy_methods.rb', line 41

def append_string_to_remote_file_on_hosts_as userid, host_list, str, dst
  result = true
  host_list = [host_list] if host_list.is_a? String
  host_list.uniq.each do |host|
    result &= append_string_to_remote_file_as userid, host, str, dst
  end
  result
end

#copy_dir_to_remote_hosts_as(userid, host_list, src_dir, dest_dir) ⇒ Object

Raises:

  • (::ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tdd_deploy/copy_methods.rb', line 25

def copy_dir_to_remote_hosts_as(userid, host_list, src_dir, dest_dir)
  raise ::ArgumentError.new("copy_dir_to_remote_hosts_as: src_dir does not exist: #{src_dir}") \
      unless File.directory? src_dir
  host_list = [host_list] if host_list.is_a? String
  result = true
  host_list.uniq.each do |host|
    result &= mkdir_on_remote_as userid, host, dest_dir
  end
  Dir.open(src_dir).each do |fname|
    next if fname[0] == '.'
    path = File.join(src_dir, fname)
    result &= copy_file_to_remote_hosts_as userid, host_list, path, File.join(dest_dir, fname)
  end
  result
end

#copy_file_to_remote_as(userid, host, src, dst) ⇒ Object

Raises:

  • (::ArgumentError)


127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/tdd_deploy/copy_methods.rb', line 127

def copy_file_to_remote_as(userid, host, src, dst)
  raise ::ArgumentError.new("file name cannot be empty") if src.empty?
  raise ::RuntimeError.new("unable to copy #{src} to #{userid}@#{host}: #{src} not found") unless File.exists? src
  
  f = File.new(src)
  file_mode = f.stat.mode & 0777

  if (result = copy_string_to_remote_file_as(userid, host, f.read, dst))
    stdout, stderr, cmd = run_on_a_host_as(userid, host, "chmod 0#{sprintf('%o', file_mode)} #{dst}")
    result &= stderr.nil?
  end
  result
end

#copy_file_to_remote_hosts_as(userid, host_list, src, dst) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/tdd_deploy/copy_methods.rb', line 16

def copy_file_to_remote_hosts_as userid, host_list, src, dst
  result = true
  host_list = [host_list] if host_list.is_a? String
  host_list.uniq.each do |host|
    result &= copy_file_to_remote_as userid, host, src, dst
  end
  result
end

#copy_string_to_remote_file_as(userid, host, str, dst) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/tdd_deploy/copy_methods.rb', line 103

def copy_string_to_remote_file_as userid, host, str, dst
  result = nil
  Net::SFTP.start(host, userid) do |sftp|
    result = sftp.file.open(dst, 'w')  do |f|
      f.write str
    end
  end
  ! result.nil?
end

#copy_string_to_remote_file_on_hosts_as(userid, host_list, str, dst) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/tdd_deploy/copy_methods.rb', line 7

def copy_string_to_remote_file_on_hosts_as userid, host_list, str, dst
  result = true
  host_list = [host_list] if host_list.is_a? String
  host_list.uniq.each do |host|
    result &= copy_string_to_remote_file_as userid, host, str, dst
  end
  result
end

#mkdir_on_remote_as(userid, host, dir, options = {}) ⇒ Object

options are passed to Net::SFTP process. :permissions default to 0755



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tdd_deploy/copy_methods.rb', line 78

def mkdir_on_remote_as userid, host, dir, options = {}
  result = nil
  options[:permissions] = 0755 unless options.include? :permissions
  Net::SFTP.start(host, userid) do |sftp|
    begin
      result = sftp.opendir! dir
    rescue ::Net::SFTP::StatusException
      result = sftp.mkdir dir, options
    end
  end
  result
end