`
yangzhihuan
  • 浏览: 165785 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

[ruby]使用ssh,sftp自动上传文件的rake

阅读更多
最近在系统中使用jruby写业务逻辑,写完一个脚本之后,要手工地用ftp工具上传到服务器上面调试.见到大家都是ant自动部署,想起ruby下大名鼎鼎的rake,我想应该也是可以实现这样的功能的吧.参考网上使用ssh,sftp自动上传的文章,不过大都不可用,貌似是sftp的API已经变了,只好自己摸着石头过河,经过一个下午的努力(小弟是新手),终于搞定了,废话少说,上代码,我的第一个rake啊.
请先安装net-ssh,net-sftp这两个gem

gem install net-ssh net-sftp


# @anthor:yanghuan
# To change this template, choose Tools | Templates
# and open the template in the editor.
 

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'rubygems'
require 'net/ssh'
require 'net/sftp'

LOCAL_PATH = 'lib'
REMOTE_PATH = '/home/scripts/test'

SERVER="192.168.0.1"
USER_NAME = "root"
PASSWORD = "password"

module FindUtils
  def find_all_file(file,&block)
    if File.stat(file).directory?
      Dir.foreach(file) do |f|
        find_all_file(file + "/" + f,&block) if( !(f =~ /^\./))
      end
    else
      block.call(file)
    end
  end
  
  def find_all_dir(dir,&block)
    if File.stat(dir).directory?
      block.call(dir)
      Dir.foreach(dir) do |f|
        find_all_dir(dir + "/" + f,&block) if(File.stat(dir + "/" + f).directory? && !(f =~ /^\./))
      end
    end
  end
end

task :default => [:upload]

#文件上传
task :upload do
  puts "task[upload] start"
  include FindUtils
  begin
    Net::SSH.start(SERVER,USER_NAME,:password => PASSWORD) do |ssh|
      ssh.sftp.connect do |sftp|
        #        检查并创建文件夹
        find_all_dir(LOCAL_PATH) do |d|
          if !d.eql?(LOCAL_PATH)
            begin
              local_dir = d.sub(Regexp.new(LOCAL_PATH+"/"),'')
              remote_dir = REMOTE_PATH + "/" + local_dir
              puts "local_dir:#{local_dir} remote_dir:#{remote_dir}"
              sftp.stat!(remote_dir)
            rescue Net::SFTP::StatusException => se
              raise unless se.code == 2
              puts "mkdir on remote : #{remote_dir}"
              sftp.mkdir!(remote_dir, :permissions => 0755)
              puts "mkdir completion"
            end
          end
        end
        
        #        上传文件
        find_all_file(LOCAL_PATH) do |f|
          local_file = f.sub(Regexp.new(LOCAL_PATH+"/"),'')
          remote_file = REMOTE_PATH + "/" + local_file
          sftp.upload!(f,remote_file) do|event,uploader,*args|
            case event
              # args[0] : file metadata
            when :open
              puts "start uploading.#{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!"
            end
          end
          puts "upload success"
        end
      end
    end
  rescue => detail
    puts "error:#{detail.backtrace.join("\n")} \n message:#{detail.message}"
  end
end
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics