Guest User

Untitled

a guest
Apr 19th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #Simple FTP Deploy Script
  2. #Pushes all file changes by a specific git commit
  3. #Defaults to last commit
  4.  
  5. require 'rubygems'
  6. require 'net/ssh'
  7. require 'net/sftp'
  8.  
  9. path = "/"
  10. ftp_host = 'ftp.example.com'
  11. ftp_user = 'username'
  12. ftp_password = 'secret'
  13.  
  14. def upload(options={})
  15.  
  16. Net::SSH.start(options[:ftp_host],options[:ftp_user],:password =>options[:ftp_password] ) do |ssh|
  17. ssh.sftp.connect do |sftp|
  18. total = files.size
  19.  
  20. options[:files].each_with_index do |file,index|
  21. from = file
  22. to = "#{options[:path]}/#{file}"
  23.  
  24. begin
  25. remote_dir = File.dirname(to)
  26. sftp.upload!(from,to)
  27. rescue Net::SFTP::StatusException => e
  28. raise unless e.code == 2
  29. mkdir(sftp,remote_dir)
  30. sftp.upload!(from,to)
  31. else
  32. puts "#{index+1} of #{total} Uploading #{to}"
  33. end
  34.  
  35. end
  36.  
  37. end
  38. end
  39. end
  40.  
  41. def mkdir(sftp,remote_dir)
  42. begin
  43. sftp.mkdir!(remote_dir, :permissions => 0755)
  44. rescue Net::SFTP::StatusException => e
  45. raise unless e.code == 2
  46. mkdir(sftp,File.dirname("#{remote_dir}.dir"))
  47. sftp.mkdir!(remote_dir, :permissions => 0755)
  48. end
  49. end
  50.  
  51. commit = ''
  52.  
  53. ARGV.each do |var|
  54. if var =~ /^([\w]{5,40})/
  55. commit = $1.strip
  56. break
  57. end
  58. end
  59.  
  60. updated_file_names = `git show #{commit} --name-only`
  61. files = updated_file_names.split(/\n/)
  62. files = lines[5..-1].reject {|v| v.strip.empty? }
  63.  
  64. puts "Ready to upload the following..."
  65. puts
  66. files.each {|file| puts file }
  67. puts
  68. puts "#{lines.size} files waiting to be pushed..."
  69. print "Proceed? "
  70. y_or_no = STDIN.gets
  71.  
  72. if y_or_no.match(/^y/i)
  73. puts "Uploading..."
  74. upload(:path => path,:files => files,:ftp_host => ftp_host,:ftp_user=>ftp_user,:ftp_password=>ftp_password)
  75. else
  76. puts "Quiting"
  77. end
Add Comment
Please, Sign In to add comment