Class: Fig::Protocol::SSH

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

Overview

File transfers using external ssh and scp programs

Instance Method Summary collapse

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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fig/protocol/ssh.rb', line 61

def download(uri, path, )
  unescaped_path = CGI.unescape uri.path

  exists = remote_path_exists(uri.host, unescaped_path) {
    |error_message|

    raise Fig::NetworkError.new(
      "Unable to determine whether #{uri} exists: #{error_message}",
    )
  }
  if not exists
    raise Fig::FileNotFoundError.new "#{uri} doesn't exist.", uri
  end

  scp("#{uri.host}:#{unescaped_path}", path) {
    |error_message|

    raise Fig::NetworkError.new(
      "Unable to copy remote file to #{path}: #{error_message}",
    )
  }

  return true
end

#download_list(uri) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fig/protocol/ssh.rb', line 16

def download_list(uri)
  packages = []
  unescaped_path = CGI.unescape uri.path

  ls = ssh(uri.host, 'find', unescaped_path, '-type', 'd') {
    |error_message|

    raise Fig::NetworkError.new error_message
  }

  strip_paths_for_list(ls, packages, unescaped_path)

  return packages
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)


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

def path_up_to_date?(uri, path, )
  unescaped_path = CGI.unescape uri.path

  size_mtime = ssh(uri.host, 'stat', '--format="%s %Z"', unescaped_path) {
    |error_message|

    raise Fig::NetworkError.new(
      "Unable to get size and modification time for remote path #{path}: #{error_message}",
    )
  }

  remote_size, remote_mtime = size_mtime.split
  remote_size  = remote_size.to_i
  remote_mtime = remote_mtime.to_i

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

  if remote_mtime <= ::File.mtime(path).to_i
    return true
  end

  return false
end

#upload(local_file, uri) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fig/protocol/ssh.rb', line 86

def upload(local_file, uri)
  unescaped_path = CGI.unescape uri.path

  ssh(uri.host, 'mkdir', '-p', ::File.dirname(unescaped_path)) {
    |error_message|

    raise Fig::NetworkError.new(
      "Unable to create directory on remote: #{error_message}",
    )
  }

  scp(local_file, "#{uri.host}:#{unescaped_path}") {
    |error_message|

    raise Fig::NetworkError.new(
      "Unable to copy #{local_file} to remote: #{error_message}",
    )
  }

  return
end