class Chef::Provider::RemoteFile::FTP

Attributes

current_resource[R]
new_resource[R]
uri[R]

Public Class Methods

new(uri, new_resource, current_resource) click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 37
def initialize(uri, new_resource, current_resource)
  @uri = uri
  @new_resource = new_resource
  @current_resource = current_resource
  validate_typecode!
  validate_path!
end

Public Instance Methods

directories() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 77
def directories
  parse_path if @directories.nil?
  @directories
end
fetch() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 87
def fetch
  with_connection do
    get
  end
end
filename() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 82
def filename
  parse_path if @filename.nil?
  @filename
end
ftp() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 93
def ftp
  @ftp ||= Net::FTP.new
end
hostname() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 45
def hostname
  @uri.host
end
pass() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 69
def pass
  if uri.userinfo
    CGI.unescape(uri.password)
  else
    nil
  end
end
port() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 49
def port
  @uri.port
end
typecode() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 57
def typecode
  uri.typecode
end
use_passive_mode?() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 53
def use_passive_mode?
  ! new_resource.ftp_active_mode
end
user() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 61
def user
  if uri.userinfo
    CGI.unescape(uri.user)
  else
    "anonymous"
  end
end

Private Instance Methods

connect() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 127
def connect
  # The access sequence is defined by RFC 1738
  ftp.connect(hostname, port)
  ftp.passive = use_passive_mode?
  ftp.login(user, pass)
  directories.each do |cwd|
    ftp.voidcmd("CWD #{cwd}")
  end
end
disconnect() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 137
def disconnect
  ftp.close
end
get() click to toggle source

Fetches using Net::FTP, returns a Tempfile with the content

# File lib/chef/provider/remote_file/ftp.rb, line 142
def get
  tempfile = Chef::FileContentManagement::Tempfile.new(@new_resource).tempfile
  if typecode
    ftp.voidcmd("TYPE #{typecode.upcase}")
  end
  ftp.getbinaryfile(filename, tempfile.path)
  tempfile.close if tempfile
  tempfile
end
parse_path() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 156
def parse_path
  path = uri.path.sub(%r{\A/}, "%2F") # re-encode the beginning slash because uri library decodes it.
  directories = path.split(%r{/}, -1)
  directories.each do |d|
    d.gsub!(/%([0-9A-Fa-f][0-9A-Fa-f])/) { [$1].pack("H2") }
  end
  unless filename = directories.pop
    raise ArgumentError, "no filename: #{path.inspect}"
  end
  if filename.length == 0 || filename.end_with?( "/" )
    raise ArgumentError, "no filename: #{path.inspect}"
  end

  @directories, @filename = directories, filename
end
proxy_uri(uri) click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 152
def proxy_uri(uri)
  Chef::Config.proxy_uri("ftp", hostname, port)
end
validate_path!() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 123
def validate_path!
  parse_path
end
validate_typecode!() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 116
def validate_typecode!
  # Only support ascii and binary types
  if typecode && /\A[ai]\z/ !~ typecode
    raise ArgumentError, "invalid typecode: #{typecode.inspect}"
  end
end
with_connection() { || ... } click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 107
def with_connection
  with_proxy_env do
    connect
    yield
  end
ensure
  disconnect
end
with_proxy_env() { || ... } click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 99
def with_proxy_env
  saved_socks_env = ENV["SOCKS_SERVER"]
  ENV["SOCKS_SERVER"] = proxy_uri(@uri).to_s
  yield
ensure
  ENV["SOCKS_SERVER"] = saved_socks_env
end