Class: Fig::Protocol::SFTP

Inherits:
Object
  • Object
show all
Includes:
Fig::Protocol, NetRCEnabled
Defined in:
lib/fig/protocol/sftp.rb

Overview

File transfers via SFTP

Instance Method Summary collapse

Constructor Details

#initializeSFTP

Returns a new instance of SFTP.



17
18
19
# File 'lib/fig/protocol/sftp.rb', line 17

def initialize()
  initialize_netrc
end

Instance Method Details

#download(uri, path, prompt_for_login) ⇒ Object

Returns whether the file was not downloaded because the file already exists and is already up-to-date.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fig/protocol/sftp.rb', line 67

def download(uri, path, )
  sftp_run(uri, ) do
    |connection|

    begin
      # *sigh* Always call #stat!(), even if the local file does not exist
      # because #download!() throws Strings and not proper exception objects
      # when the remote path does not exist.
      stat = connection.stat!(uri.path)

      if ::File.exist?(path) && stat.mtime.to_f <= ::File.mtime(path).to_f
        Fig::Logging.debug "#{path} is up to date."
        return false
      else
        log_download uri, path
        connection.download! uri.path, path

        return true
      end
    rescue Net::SFTP::StatusException => error
      if error.code == Net::SFTP::Constants::StatusCodes::FX_NO_SUCH_FILE
        raise Fig::FileNotFoundError.new(error.message, uri)
      end
      raise error
    end
  end

  return
end

#download_list(uri) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fig/protocol/sftp.rb', line 21

def download_list(uri)
  package_versions = []

  sftp_run(uri, :prompt_for_login) do
    |connection|

    connection.dir.foreach uri.path do
      |package_directory|

      if package_directory.directory?
        package_name = package_directory.name

        if package_name =~ Fig::PackageDescriptor::COMPONENT_PATTERN
          connection.dir.foreach "#{uri.path}/#{package_name}" do
            |version_directory|

            if version_directory.directory?
              version_name = version_directory.name

              if version_name =~ Fig::PackageDescriptor::COMPONENT_PATTERN
                package_versions << "#{package_name}/#{version_name}"
              end
            end
          end
        end
      end
    end
  end

  return package_versions
end

#path_up_to_date?(uri, path, prompt_for_login) ⇒ Boolean

Determine whether we need to update something. Returns nil to indicate “don’t know”.

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
# File 'lib/fig/protocol/sftp.rb', line 55

def path_up_to_date?(uri, path, )
  sftp_run(uri, ) do
    |connection|

    return connection.stat!(uri.path).mtime.to_f <= ::File.mtime(path).to_f
  end

  return nil
end

#upload(local_file, uri) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/fig/protocol/sftp.rb', line 97

def upload(local_file, uri)
  sftp_run(uri, :prompt_for_login) do
    |connection|

    ensure_directory_exists connection, ::File.dirname(uri.path)
    connection.upload! local_file, uri.path
  end

  return
end