2015-01-04 23:06:51 +00:00
|
|
|
require "pathname"
|
|
|
|
|
2014-11-11 23:36:30 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module FTPPush
|
|
|
|
class Adapter
|
|
|
|
attr_reader :host
|
|
|
|
attr_reader :port
|
|
|
|
attr_reader :username
|
|
|
|
attr_reader :password
|
|
|
|
attr_reader :options
|
|
|
|
attr_reader :server
|
|
|
|
|
|
|
|
def initialize(host, username, password, options = {})
|
|
|
|
@host, @port = parse_host(host)
|
|
|
|
@username = username
|
|
|
|
@password = password
|
|
|
|
@options = options
|
|
|
|
@server = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Parse the host into it's url and port parts.
|
|
|
|
# @return [Array]
|
|
|
|
def parse_host(host)
|
|
|
|
if host.include?(":")
|
|
|
|
split = host.split(":", 2)
|
|
|
|
[split[0], split[1].to_i]
|
|
|
|
else
|
|
|
|
[host, default_port]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_port
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def connect(&block)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload(local, remote)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# The FTP Adapter
|
|
|
|
#
|
|
|
|
class FTPAdapter < Adapter
|
|
|
|
def initialize(*)
|
|
|
|
require "net/ftp"
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_port
|
2015-01-04 21:24:26 +00:00
|
|
|
21
|
2014-11-11 23:36:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def connect(&block)
|
|
|
|
@server = Net::FTP.new
|
|
|
|
@server.passive = options.fetch(:passive, true)
|
|
|
|
@server.connect(host, port)
|
|
|
|
@server.login(username, password)
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield self
|
|
|
|
ensure
|
|
|
|
@server.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload(local, remote)
|
2015-01-04 23:06:51 +00:00
|
|
|
parent = File.dirname(remote)
|
|
|
|
fullpath = Pathname.new(File.expand_path(parent, pwd))
|
|
|
|
|
|
|
|
# Create the parent directories if they does not exist (naive mkdir -p)
|
|
|
|
fullpath.descend do |path|
|
2015-04-02 06:44:11 +00:00
|
|
|
unless check_dir_exists? path.to_s
|
2015-01-04 23:06:51 +00:00
|
|
|
@server.mkdir(path.to_s)
|
|
|
|
end
|
2014-11-11 23:36:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Upload the file
|
|
|
|
@server.putbinaryfile(local, remote)
|
|
|
|
end
|
2015-01-04 23:06:51 +00:00
|
|
|
|
2015-04-02 06:44:11 +00:00
|
|
|
def check_dir_exists?(path)
|
|
|
|
begin
|
|
|
|
@server.chdir(path)
|
|
|
|
return true
|
|
|
|
rescue Net::FTPPermError
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-04 23:06:51 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def pwd
|
|
|
|
@pwd ||= @server.pwd
|
|
|
|
end
|
2014-11-11 23:36:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# The SFTP Adapter
|
|
|
|
#
|
|
|
|
class SFTPAdapter < Adapter
|
|
|
|
def initialize(*)
|
|
|
|
require "net/sftp"
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_port
|
|
|
|
22
|
|
|
|
end
|
|
|
|
|
|
|
|
def connect(&block)
|
|
|
|
Net::SFTP.start(@host, @username, password: @password, port: @port) do |server|
|
|
|
|
@server = server
|
|
|
|
yield self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload(local, remote)
|
|
|
|
@server.upload!(local, remote, mkdir: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|