gists/2013.ruby.ftp-upload.rb

100 lines
1.9 KiB
Ruby

require "rubygems"
require 'net/ftp'
require 'stringio'
ftp = ""
username = ""
passwd = ""
root_dir = "../../../../"
current_rev_file = root_dir + "__CURRENT_VERSION__"
root_dir = ""
# new rev is either HEAD or a commit, set by ARGV
new_rev = ""
new_rev = ARGV.first unless ARGV.first != nil
cmd_newrev = "git show -s --format=\"%%h %%s (%%ar)\" %s" % new_rev
new_rev = `#{cmd_newrev}`
# old rev (current in production) are stored in a file.
old_rev = File.open(current_rev_file, "r").read
cmd = "git diff --name-status %s %s" % [old_rev.split(" ").first, new_rev.split(" ").first]
files = `#{cmd}`
# change to root
Dir.chdir root_dir
class Net::FTP
def puttextcontent(content, remotefile, &block)
f = StringIO.new(content)
begin
storlines("STOR " + remotefile, f, &block)
ensure
f.close
end
end
end
def chkdir(ftp, parents)
path = []
parents.each do |d|
parent_path = path.join("/")
path << d
unless ftp.list(parent_path).any? { |dir| dir.match(d) }
ftp.mkdir(path.join("/"))
puts "[chkdir] created dir: %s" % path.join("/")
end
end
end
def put_file(ftp, path)
parents = path.split("/")
parents.pop
chkdir ftp, parents
if path.match(".png")
ftp.putbinaryfile(path, path)
else
ftp.puttextfile(path, path)
end
puts "[put_file] %s" % path
end
puts "\nCurrent rev in Production: %s" % old_rev
puts " The new rev: %s\n" % new_rev
if old_rev == new_rev
puts "Everything is up to date, exiting"
exit
end
# list files to be changed in ftp prod
puts "files changed:", files, ""
# connect to ftp server
ftp = Net::FTP.new(ftp)
ftp.login username, passwd
remote = ftp.chdir root_dir
# make changes
files.each_line do |file|
flag, file = file.split("\t")
file.strip!
if flag == "A" || flag == "M"
put_file ftp, file
end
if flag == "D"
ftp.delete file
end
end
File.open(current_rev_file, "w").write(new_rev)
ftp.close