Merge pull request #5104 from mitchellh/sethvargo/ftp_push_fixes

Various fixes for FTP
This commit is contained in:
Seth Vargo 2015-01-04 18:36:38 -05:00
commit c142ff29b2
4 changed files with 38 additions and 46 deletions

View File

@ -1,3 +1,5 @@
require "pathname"
module VagrantPlugins module VagrantPlugins
module FTPPush module FTPPush
class Adapter class Adapter
@ -50,7 +52,7 @@ module VagrantPlugins
end end
def default_port def default_port
20 21
end end
def connect(&block) def connect(&block)
@ -67,16 +69,25 @@ module VagrantPlugins
end end
def upload(local, remote) def upload(local, remote)
parent = File.dirname(remote) parent = File.dirname(remote)
fullpath = Pathname.new(File.expand_path(parent, pwd))
# Create the parent directory if it does not exist # Create the parent directories if they does not exist (naive mkdir -p)
if !@server.list("/").any? { |f| f.start_with?(parent) } fullpath.descend do |path|
@server.mkdir(parent) if @server.list(path.to_s).empty?
@server.mkdir(path.to_s)
end
end end
# Upload the file # Upload the file
@server.putbinaryfile(local, remote) @server.putbinaryfile(local, remote)
end end
private
def pwd
@pwd ||= @server.pwd
end
end end
# #

View File

@ -7,6 +7,12 @@ module VagrantPlugins
module FTPPush module FTPPush
class Push < Vagrant.plugin("2", :push) class Push < Vagrant.plugin("2", :push)
IGNORED_FILES = %w(. ..).freeze IGNORED_FILES = %w(. ..).freeze
DEFAULT_EXCLUDES = %w(.git .hg .svn .vagrant).freeze
def initialize(*)
super
@logger = Log4r::Logger.new("vagrant::pushes::ftp")
end
def push def push
# Grab files early so if there's an exception or issue, we don't have to # Grab files early so if there's an exception or issue, we don't have to
@ -14,11 +20,16 @@ module VagrantPlugins
files = Hash[*all_files.flat_map do |file| files = Hash[*all_files.flat_map do |file|
relative_path = relative_path_for(file, config.dir) relative_path = relative_path_for(file, config.dir)
destination = File.expand_path(File.join(config.destination, relative_path)) destination = File.expand_path(File.join(config.destination, relative_path))
file = File.expand_path(file, env.root_path)
[file, destination] [file, destination]
end] end]
ftp = "#{config.username}@#{config.host}:#{config.destination}"
env.ui.info "Uploading #{env.root_path} to #{ftp}"
connect do |ftp| connect do |ftp|
files.each do |local, remote| files.each do |local, remote|
@logger.info "Uploading #{local} => #{remote}"
ftp.upload(local, remote) ftp.upload(local, remote)
end end
end end
@ -33,16 +44,6 @@ module VagrantPlugins
ftp.connect(&block) ftp.connect(&block)
end end
# Parse the host into it's url and port parts.
# @return [Array]
def parse_host(host)
if host.include?(":")
host.split(":", 2)
else
[host, "22"]
end
end
# The list of all files that should be pushed by this push. This method # The list of all files that should be pushed by this push. This method
# only returns **files**, not folders or symlinks! # only returns **files**, not folders or symlinks!
# @return [Array<String>] # @return [Array<String>]
@ -72,7 +73,10 @@ module VagrantPlugins
# @param [Array<String>] excludes # @param [Array<String>] excludes
# the exclude patterns or files # the exclude patterns or files
def filter_excludes!(list, excludes) def filter_excludes!(list, excludes)
excludes = Array(excludes).flat_map { |e| [e, "#{e}/*"] } excludes = Array(excludes)
excludes = excludes + DEFAULT_EXCLUDES
excludes = excludes.flat_map { |e| [e, "#{e}/*"] }
list.reject! do |file| list.reject! do |file|
basename = relative_path_for(file, config.dir) basename = relative_path_for(file, config.dir)

View File

@ -54,8 +54,8 @@ describe VagrantPlugins::FTPPush::FTPAdapter do
end end
describe "#default_port" do describe "#default_port" do
it "is 20" do it "is 21" do
expect(subject.default_port).to eq(20) expect(subject.default_port).to eq(21)
end end
end end

View File

@ -20,6 +20,11 @@ describe VagrantPlugins::FTPPush::Push do
subject { described_class.new(env, config) } subject { described_class.new(env, config) }
before do
allow(env).to receive(:root_path)
.and_return(File.expand_path("..", __FILE__))
end
describe "#push" do describe "#push" do
before(:all) do before(:all) do
@server = FakeFtp::Server.new(51234, 21213) @server = FakeFtp::Server.new(51234, 21213)
@ -93,34 +98,6 @@ describe VagrantPlugins::FTPPush::Push do
end end
end end
describe "#parse_host" do
let(:result) { subject.parse_host(host) }
context "when no port is given" do
let(:host) { "127.0.0.1" }
it "returns the url and port 22" do
expect(result).to eq(["127.0.0.1", "22"])
end
end
context "when a port is given" do
let(:host) { "127.0.0.1:23456" }
it "returns the url and port 23456" do
expect(result).to eq(["127.0.0.1", "23456"])
end
end
context "when more than more port is given" do
let(:host) { "127.0.0.1:22:33:44" }
it "returns the url and everything after" do
expect(result).to eq(["127.0.0.1", "22:33:44"])
end
end
end
describe "#all_files" do describe "#all_files" do
before(:all) do before(:all) do
@dir = Dir.mktmpdir @dir = Dir.mktmpdir