class Chef::Provider::RemoteFile::SFTP

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/sftp.rb, line 36
def initialize(uri, new_resource, current_resource)
  @uri = uri
  @new_resource = new_resource
  @current_resource = current_resource
  validate_path!
  validate_userinfo!
end

Public Instance Methods

fetch() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 56
def fetch
  get
end
hostname() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 44
def hostname
  @uri.host
end
port() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 48
def port
  @uri.port
end
user() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 52
def user
  CGI.unescape(uri.user)
end

Private Instance Methods

get() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 98
def get
  tempfile =
    Chef::FileContentManagement::Tempfile.new(@new_resource).tempfile
  sftp.download!(uri.path, tempfile.path)
  tempfile.close if tempfile
  tempfile
end
pass() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 67
def pass
  CGI.unescape(uri.password)
end
sftp() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 62
def sftp
  host = port ? "#{hostname}:#{port}" : hostname
  @sftp ||= Net::SFTP.start(host, user, password: pass)
end
validate_path!() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 71
def validate_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
end
validate_userinfo!() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 85
def validate_userinfo!
  if uri.userinfo
    unless uri.user
      raise ArgumentError, "no user name provided in the sftp URI"
    end
    unless uri.password
      raise ArgumentError, "no password provided in the sftp URI"
    end
  else
    raise ArgumentError, "no userinfo provided in the sftp URI"
  end
end