Class: Fig::Protocol::FTP

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

Overview

File transfers via FTP

Instance Method Summary collapse

Constructor Details

#initialize(login) ⇒ FTP

Returns a new instance of FTP.



20
21
22
23
# File 'lib/fig/protocol/ftp.rb', line 20

def initialize()
  @login = 
  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.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fig/protocol/ftp.rb', line 62

def download(uri, path, )
  begin
    ftp = Net::FTP.new(uri.host)
    (ftp, uri.host, )

    if (
          ::File.exist?(path)                       \
      &&  ftp.mtime(uri.path) <= ::File.mtime(path) \
      &&  ftp.size(uri.path) == ::File.size(path)
    )
      Fig::Logging.debug "#{path} is up to date."

      return false
    else
      log_download(uri, path)
      ftp.getbinaryfile(uri.path, path, 256*1024)
      return true
    end
  rescue Net::FTPPermError => error
    Fig::Logging.debug error.message
    raise Fig::FileNotFoundError.new error.message, uri
  rescue SocketError => error
    Fig::Logging.debug error.message
    raise Fig::FileNotFoundError.new error.message, uri
  rescue Errno::ETIMEDOUT => error
    Fig::Logging.debug error.message
    raise Fig::FileNotFoundError.new error.message, uri
  end
end

#download_list(uri) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/fig/protocol/ftp.rb', line 25

def download_list(uri)
  ftp = Net::FTP.new(uri.host)
  (ftp, uri.host, :prompt_for_login)
  ftp.chdir(uri.path)
  dirs = ftp.nlst
  ftp.close

  download_ftp_list(uri, dirs)
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)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fig/protocol/ftp.rb', line 37

def path_up_to_date?(uri, path, )
  begin
    ftp = Net::FTP.new(uri.host)
    (ftp, uri.host, )

    if ftp.size(uri.path) != ::File.size(path)
      return false
    end

    if ftp.mtime(uri.path) <= ::File.mtime(path)
      return true
    end

    return false
  rescue Net::FTPPermError => error
    Fig::Logging.debug error.message
    raise Fig::FileNotFoundError.new error.message, uri
  rescue SocketError => error
    Fig::Logging.debug error.message
    raise Fig::FileNotFoundError.new error.message, uri
  end
end

#upload(local_file, uri) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fig/protocol/ftp.rb', line 92

def upload(local_file, uri)
  ftp_uri = Fig::URL.parse(ENV['FIG_REMOTE_URL'])
  ftp_root_path = ftp_uri.path
  ftp_root_dirs = ftp_uri.path.split('/')
  remote_publish_path = uri.path[0, uri.path.rindex('/')]
  remote_publish_dirs = remote_publish_path.split('/')
  # Use array subtraction to deduce which project/version folder to upload
  # to, i.e. [1,2,3] - [2,3,4] = [1]
  remote_project_dirs = remote_publish_dirs - ftp_root_dirs
  Net::FTP.open(uri.host) do |ftp|
    (ftp, uri.host, :prompt_for_login)
    # Assume that the FIG_REMOTE_URL path exists.
    ftp.chdir(ftp_root_path)
    remote_project_dirs.each do |dir|
      # Can't automatically create parent directories, so do it manually.
      if ftp.nlst().index(dir).nil?
        ftp.mkdir(dir)
        ftp.chdir(dir)
      else
        ftp.chdir(dir)
      end
    end
    ftp.putbinaryfile(local_file)
  end
end