-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathftp_upload.rb
96 lines (82 loc) · 2.17 KB
/
ftp_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
require 'debugger'
require 'yaml'
require "#{File.expand_path(File.dirname(__FILE__))}/better_ftp.rb"
module FTPExt
def chdirc(dir)
chdir dir
rescue Net::FTPPermError
mkdir_p dir
chdir dir
end
def try_rm(path)
rm path
rescue Net::FTPPermError
puts "#{path} not exists"
end
def upload_file(local_file, ftp_public_html=nil)
remote_path = ftp_public_html + File.dirname(local_file)
if File.directory? local_file
upload_dir(local_file, ftp_public_html)
else
chdirc(remote_path)
putbinaryfile(local_file, File.basename(local_file))
end
end
def upload_dir(dir, ftp_public_html=nil)
Dir["#{dir}/**/*"].each do |inner_file|
upload_file(inner_file, ftp_public_html)
end
end
end
def connect_ftp(ftp_host, ftp_username, ftp_password)
begin
ftp = BetterFTP.new(ftp_host, ftp_username, ftp_password)
rescue Net::FTPPermError => e
puts e.message
puts e.backtrace.join("\n")
abort
end
ftp.extend(FTPExt)
ftp.passive = true
ftp
end
config = YAML.load_file "#{File.expand_path(Dir.pwd)}/ftp_config.yml"
debug = config["debug"]
ftp_host = config["ftp_host"]
ftp_username = config["ftp_username"]
ftp_password = config["ftp_password"]
ftp_public_html = config["ftp_public_html"]
ftp = connect_ftp(ftp_host, ftp_username, ftp_password)
sha_from = ARGV[0]
sha_to = ARGV[1]
if !sha_from || !sha_to
abort "Invalid params"
end
result = `git show --pretty='format:' --name-only #{sha_from}..#{sha_to}`
files = result.strip.split("\n").reject { |file| file.empty? }
num_try = 0
files.each do |file|
begin
remote_path = ftp_public_html + File.dirname(file)
if File.exists? file
puts "- Uploading #{file}" if debug
ftp.upload_file(file, ftp_public_html)
else
puts "- Deleting #{file}" if debug
ftp.try_rm remote_path
end
puts ftp.last_response if debug
rescue Errno::ETIMEDOUT => e
num_try += 1
puts e.message
puts e.backtrace.join("\n")
puts "TRY AGAIN: #{num_try}"
if num_try > 2
`say "Fail Fail Fail"`
else
ftp.try(:quit)
ftp = connect_ftp(ftp_host, ftp_username, ftp_password)
end
end
end
`say "Upload FTP Done"`