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 34
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 74
def directories
  parse_path if @directories.nil?
  @directories
end
fetch() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 85
def fetch
  with_connection do
    get
  end
end
filename() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 79
def filename
  parse_path if @filename.nil?
  @filename
end
ftp() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 91
def ftp
  @ftp ||= Net::FTP.new
end
hostname() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 42
def hostname
  @uri.host
end
pass() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 66
def pass
  if uri.userinfo
    URI.unescape(uri.password)
  else
    nil
  end
end
port() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 46
def port
  @uri.port
end
typecode() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 54
def typecode
  uri.typecode
end
use_passive_mode?() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 50
def use_passive_mode?
  ! new_resource.ftp_active_mode
end
user() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 58
def user
  if uri.userinfo
    URI.unescape(uri.user)
  else
    'anonymous'
  end
end

Private Instance Methods

connect() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 125
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 135
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 140
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 165
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 {|d|
    d.gsub!(/%([0-9A-Fa-f][0-9A-Fa-f])/) { [$1].pack("H2") }
  }
  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

adapted from buildr/lib/buildr/core/transports.rb via chef/rest/rest_client.rb

# File lib/chef/provider/remote_file/ftp.rb, line 151
def proxy_uri(uri)
  proxy = Chef::Config["ftp_proxy"]
  proxy = URI.parse(proxy) if String === proxy
  if Chef::Config["ftp_proxy_user"]
    proxy.user = Chef::Config["ftp_proxy_user"]
  end
  if Chef::Config["ftp_proxy_pass"]
    proxy.password = Chef::Config["ftp_proxy_pass"]
  end
  excludes = Chef::Config[:no_proxy].to_s.split(/\s*,\s*/).compact
  excludes = excludes.map { |exclude| exclude =~ /:\d+$/ ? exclude : "#{exclude}:*" }
  return proxy unless excludes.any? { |exclude| File.fnmatch(exclude, "#{host}:#{port}") }
end
validate_path!() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 121
def validate_path!
  parse_path
end
validate_typecode!() click to toggle source
# File lib/chef/provider/remote_file/ftp.rb, line 114
def validate_typecode!
  # Only support ascii and binary types
  if typecode and /\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 105
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 97
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