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

Constant Summary collapse

NET_SFTP_ISSUE_27_SIZE =
4294049792

Instance Method Summary collapse

Constructor Details

#initializeSFTP

Returns a new instance of SFTP.


19
20
21
# File 'lib/fig/protocol/sftp.rb', line 19

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.


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fig/protocol/sftp.rb', line 77

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  \
        &&  stat.size != ::File.size(path)
      )
        Fig::Logging.debug "#{path} is up to date."
        return false
      end

      if stat.size >= NET_SFTP_ISSUE_27_SIZE
        Fig::Logging.warn(
          "#{uri} is #{stat.size} bytes in size, which is likely to trigger a Net::SFTP bug (https://github.com/net-ssh/net-sftp/issues/27). Fig may hang. If so, you will need to kill the process and switch file transfer protocols or figure out a way to repackage with smaller files."
        )
      end

      log_download uri, path
      connection.download! uri.path, path

      return true
    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


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
52
53
# File 'lib/fig/protocol/sftp.rb', line 23

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)

57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fig/protocol/sftp.rb', line 57

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

    stat_attributes = connection.stat!(uri.path)
    if stat_attributes.size != ::File.size(path)
      return false
    end

    return stat_attributes.mtime.to_f <= ::File.mtime(path).to_f
  end

  return nil
end

#upload(local_file, uri) ⇒ Object


117
118
119
120
121
122
123
124
125
126
# File 'lib/fig/protocol/sftp.rb', line 117

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