Class: Spectre::FTP::SFTPConnection

Inherits:
DslClass
  • Object
show all
Defined in:
lib/spectre/ftp.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, username, opts, logger) ⇒ SFTPConnection

Returns a new instance of SFTPConnection.



74
75
76
77
78
79
80
81
82
# File 'lib/spectre/ftp.rb', line 74

def initialize host, username, opts, logger
  opts[:non_interactive] = true

  @__logger = logger
  @__session = nil
  @__host = host
  @__username = username
  @__opts = opts
end

Instance Method Details

#can_connect?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
# File 'lib/spectre/ftp.rb', line 116

def can_connect?
  begin
    connect!
    return true
  rescue
    return false
  end
end

#closeObject



110
111
112
113
114
# File 'lib/spectre/ftp.rb', line 110

def close
  return unless @__session and not @__session.closed?

  # @__session.close!
end

#connect!Object



102
103
104
105
106
107
108
# File 'lib/spectre/ftp.rb', line 102

def connect!
  return unless @__session == nil or @__session.closed?

  @__logger.info "Connecting to '#{@__host}' with user '#{@__username}'"
  @__session = Net::SFTP.start(@__host, @__username, @__opts)
  @__session.connect!
end

#download(remotefile, to: File.basename(remotefile)) ⇒ Object



125
126
127
128
129
# File 'lib/spectre/ftp.rb', line 125

def download remotefile, to: File.basename(remotefile)
  connect!
  @__logger.info "Downloading '#{@__username}@#{@__host}:#{remotefile}' to '#{File.expand_path to}'"
  @__session.download!(remotefile, to)
end

#exists(path) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/spectre/ftp.rb', line 144

def exists path
  begin
    @__session.stat! path
  rescue Net::SFTP::StatusException => e
    return false if e.description == 'no such file'

    raise e
  end

  return true
end

#passphrase(phrase) ⇒ Object



98
99
100
# File 'lib/spectre/ftp.rb', line 98

def passphrase phrase
  @__opts[:passphrase] = phrase
end

#password(pass) ⇒ Object



88
89
90
91
# File 'lib/spectre/ftp.rb', line 88

def password pass
  @__opts[:password] = pass
  @__opts[:auth_methods].push 'password' unless @__opts[:auth_methods].include? 'password'
end

#private_key(file_path) ⇒ Object



93
94
95
96
# File 'lib/spectre/ftp.rb', line 93

def private_key file_path
  @__opts[:keys] = [file_path]
  @__opts[:auth_methods].push 'publickey' unless @__opts[:auth_methods].include? 'publickey'
end

#stat(path) ⇒ Object



137
138
139
140
141
142
# File 'lib/spectre/ftp.rb', line 137

def stat path
  connect!
  file_info = @__session.stat! path
  @__logger.info "Stat '#{path}'\n#{JSON.pretty_generate file_info.attributes}"
  file_info.attributes
end

#upload(localfile, to: File.basename(localfile)) ⇒ Object



131
132
133
134
135
# File 'lib/spectre/ftp.rb', line 131

def upload localfile, to: File.basename(localfile)
  connect!
  @__logger.info "Uploading '#{File.expand_path localfile}' to '#{@__username}@#{@__host}:#{to}'"
  @__session.upload!(localfile, to)
end

#username(user) ⇒ Object



84
85
86
# File 'lib/spectre/ftp.rb', line 84

def username user
  @__username = user
end