-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsftp_upload.rb
191 lines (147 loc) · 5.2 KB
/
sftp_upload.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
85
86
87
88
89
90
91
92
93
94
95
96
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
182
183
184
185
186
187
188
189
190
191
require 'base64'
require 'fileutils'
require 'uri'
require 'optparse'
require 'net/sftp'
options = {
user_home: ENV['HOME'],
private_key_file_path: nil,
formatted_output_file_path: nil,
}
opt_parser = OptionParser.new do |opt|
opt.banner = "Usage: sftp_upload.rb [OPTIONS]"
opt.separator ""
opt.separator "Options (options without [] are required)"
opt.on("--source-dir URL", "path to sourcefiles") do |value|
options[:source_dir] = value[0] == "/" ? value :File.expand_path( File.join( options[:work_dir], value))
end
opt.on("--dest-dir [DESTINATIONDIR]", "local clone destination directory path") do |value|
options[:destination_dir] = value
end
opt.on("-h","--help","Shows this help message") do
puts opt_parser
end
end
ssh_key_from_env = ENV['auth_ssh_private_key']
options[:username] = ENV['username']
options[:hostname] = ENV['hostname']
options[:work_dir] = ENV['BITRISE_SOURCE_DIR']
if ssh_key_from_env
options[:auth_ssh_key_raw] = ssh_key_from_env
end
opt_parser.parse!
if options[:formatted_output_file_path] and options[:formatted_output_file_path].length < 1
options[:formatted_output_file_path] = nil
end
#
# Print configs
puts '========== Configs =========='
puts " * source: #{options[:source_dir]} #{File.directory?(options[:source_dir])}"
puts " * destination_dir: #{options[:destination_dir]}"
puts " * auth_ssh_key_raw: #{options[:auth_ssh_key_raw].to_s.empty? ? 'no SSH key provided' : '*****'}"
puts " * username: #{options[:username].to_s.empty? ? 'username is not set' : '*****'}"
puts " * hostname: #{options[:hostname].to_s.empty? ? 'no SSH hostname provided' : options[:hostname]}"
puts
unless options[:source_dir] and options[:source_dir].length > 0
puts opt_parser
exit 1
end
# -----------------------
# --- functions
# -----------------------
def write_private_key_to_file(user_home, auth_ssh_private_key)
private_key_file_path = File.join(user_home, '.ssh/bitrise')
# create the folder if not yet created
FileUtils::mkdir_p(File.dirname(private_key_file_path))
# private key - save to file
File.open(private_key_file_path, 'wt') { |f| f.write(auth_ssh_private_key) }
system "chmod 600 #{private_key_file_path}"
return private_key_file_path
end
# -----------------------
# --- main
# -----------------------
# normalize input pathes
options[:destination_dir] = File.expand_path(options[:destination_dir])
if !options[:auth_ssh_key_raw].to_s.empty?
options[:private_key_file_path] = write_private_key_to_file(options[:user_home], options[:auth_ssh_key_raw])
else
# Auth: No Authentication information found - trying without authentication
end
# do
$options = options
$this_script_path = File.expand_path(File.dirname(__FILE__))
class String
def prepend_lines_with(prepend_with_string)
return self.gsub(/^.*$/, prepend_with_string.to_s+'\&')
end
end
def write_string_to_formatted_output(str_to_write)
formatted_output_file_path = $options[:formatted_output_file_path]
if formatted_output_file_path
File.open(formatted_output_file_path, "w+") { |f|
f.puts(str_to_write)
}
end
end
def export_step_output(key, value)
IO.popen("envman add --key #{key}", 'r+') {|f|
f.write(value)
f.close_write
f.read
}
end
def do_upload()
upload_path = File.join($options[:destination_dir])
is_upload_success = false
if $options[:private_key_file_path]
puts system(%Q{ls -l #{$this_script_path}})
ssh_no_prompt_file = 'ssh_no_prompt.sh'
system(%Q{#{$this_script_path}/#{ssh_no_prompt_file} #{$options[:username]}@#{$options[:hostname]} "ls -l #{$options[:destination_dir]}"})
end
sftp = Net::SFTP.start($options[:hostname], $options[:username], keys: $options[:private_key_file_path] ) #do |sftp|
remote_path = ""
$options[:destination_dir].split('/').map do |dir|
remote_path = File.join remote_path, dir
begin
sftp.dir.entries(remote_path)
rescue
sftp.mkdir! remote_path
end
end
target = $options[:destination_dir]
if File.file? $options[:source_dir] then
source_file = $options[:source_dir].split('/').pop
target = File.join target, source_file
end
system(%Q{pwd && ls -l #{$options[:source_dir]}})
is_upload_success = sftp.upload!( $options[:source_dir], target ) do |event, uploader, *args|
case event
when :open then
# args[0] : file metadata
puts "starting upload: #{args[0].local} -> #{args[0].remote} (#{args[0].size} bytes}"
when :put then
# args[0] : file metadata
# args[1] : byte offset in remote file
# args[2] : data being written (as string)
puts "writing #{args[2].length} bytes to #{args[0].remote} starting at #{args[1]}"
when :close then
# args[0] : file metadata
puts "finished with #{args[0].remote}"
when :mkdir then
# args[0] : remote path name
puts "creating directory #{args[0]}"
when :finish then
puts "all done!"
return true
end
end
return is_upload_success
end
is_upload_success = do_upload()
puts "upload Is Success?: #{is_upload_success}"
if options[:private_key_file_path]
puts " (i) Removing private key file: #{options[:private_key_file_path]}"
system(%Q{rm -P #{options[:private_key_file_path]}})
end
exit (is_upload_success ? 0 : 1)