"Fossies" - the Fresh Open Source Software Archive

Member "vagrant-2.2.14/plugins/pushes/ftp/adapter.rb" (20 Nov 2020, 3072 Bytes) of package /linux/misc/vagrant-2.2.14.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 require "pathname"
    2 
    3 module VagrantPlugins
    4   module FTPPush
    5     class Adapter
    6       attr_reader :host
    7       attr_reader :port
    8       attr_reader :username
    9       attr_reader :password
   10       attr_reader :options
   11       attr_reader :server
   12 
   13       def initialize(host, username, password, options = {})
   14         @host, @port = parse_host(host)
   15         @username = username
   16         @password = password
   17         @options  = options
   18         @server   = nil
   19       end
   20 
   21       # Parse the host into it's url and port parts.
   22       # @return [Array]
   23       def parse_host(host)
   24         if host.include?(":")
   25           split = host.split(":", 2)
   26           [split[0], split[1].to_i]
   27         else
   28           [host, default_port]
   29         end
   30       end
   31 
   32       def default_port
   33         raise NotImplementedError
   34       end
   35 
   36       def connect(&block)
   37         raise NotImplementedError
   38       end
   39 
   40       def upload(local, remote)
   41         raise NotImplementedError
   42       end
   43     end
   44 
   45     #
   46     # The FTP Adapter
   47     #
   48     class FTPAdapter < Adapter
   49       def initialize(*)
   50         require "net/ftp"
   51         super
   52       end
   53 
   54       def default_port
   55         21
   56       end
   57 
   58       def connect(&block)
   59         @server = Net::FTP.new
   60         @server.passive = options.fetch(:passive, true)
   61         @server.connect(host, port)
   62         @server.login(username, password)
   63 
   64         begin
   65           yield self
   66         ensure
   67           @server.close
   68         end
   69       end
   70 
   71       def upload(local, remote)
   72         parent   = File.dirname(remote)
   73         fullpath = Pathname.new(File.expand_path(parent, pwd))
   74 
   75         # Create the parent directories if they does not exist (naive mkdir -p)
   76         fullpath.descend do |path|
   77           if !directory_exists?(path.to_s)
   78             @server.mkdir(path.to_s)
   79           end
   80         end
   81 
   82         # Upload the file
   83         @server.putbinaryfile(local, remote)
   84       end
   85 
   86       def directory_exists?(path)
   87         begin
   88           @server.chdir(path)
   89           return true
   90         rescue Net::FTPPermError
   91           return false
   92         end
   93       end
   94 
   95       private
   96 
   97       def pwd
   98         @pwd ||= @server.pwd
   99       end
  100     end
  101 
  102     #
  103     # The SFTP Adapter
  104     #
  105     class SFTPAdapter < Adapter
  106       def initialize(*)
  107         require "net/sftp"
  108         super
  109         @dirs = {}
  110       end
  111 
  112       def default_port
  113         22
  114       end
  115 
  116       def connect(&block)
  117         Net::SFTP.start(@host, @username, password: @password, port: @port) do |server|
  118           @server = server
  119           yield self
  120         end
  121       end
  122 
  123       def upload(local, remote)
  124         dir = File.dirname(remote)
  125 
  126         fullpath = Pathname.new(dir)
  127         fullpath.descend do |path|
  128           if @dirs[path.to_s].nil?
  129             begin
  130               @server.mkdir!(path.to_s)
  131 
  132               # Cache visited directories in a list to avoid duplicate requests
  133               @dirs[path.to_s] = true
  134             rescue Net::SFTP::StatusException => e
  135               # Directory exists, skip...
  136             end
  137           end
  138         end
  139 
  140         @server.upload!(local, remote)
  141       end
  142     end
  143   end
  144 end