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 33
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 53
def fetch
  get
end
hostname() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 41
def hostname
  @uri.host
end
port() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 45
def port
  @uri.port
end
user() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 49
def user
  URI.unescape(uri.user)
end

Private Instance Methods

get() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 95
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 64
def pass
  URI.unescape(uri.password)
end
sftp() click to toggle source
# File lib/chef/provider/remote_file/sftp.rb, line 59
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 68
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 82
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