Class: RestFtpDaemon::RemoteSFTP
- Inherits:
-
Remote
- Object
- Remote
- RestFtpDaemon::RemoteSFTP
show all
- Defined in:
- lib/rest-ftp-daemon/remote_sftp.rb
Instance Attribute Summary collapse
Attributes inherited from Remote
#job, #log_prefix, #logger
Instance Method Summary
collapse
Methods inherited from Remote
#initialize
Instance Attribute Details
#sftp ⇒ Object
8
9
10
|
# File 'lib/rest-ftp-daemon/remote_sftp.rb', line 8
def sftp
@sftp
end
|
Instance Method Details
#chdir_or_create(directory, mkdir = false) ⇒ Object
60
61
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
|
# File 'lib/rest-ftp-daemon/remote_sftp.rb', line 60
def chdir_or_create directory, mkdir = false
log_debug "RemoteSFTP.chdir_or_create mkdir[#{mkdir}] dir[#{directory}]"
parent, _current = (directory)
begin
@sftp.opendir! directory
rescue Net::SFTP::StatusException => _e
return false unless mkdir
chdir_or_create parent, mkdir
mkdir directory
retry
else
return true
end
raise JobTargetShouldBeDirectory
end
|
#close ⇒ Object
135
136
137
138
|
# File 'lib/rest-ftp-daemon/remote_sftp.rb', line 135
def close
super
end
|
#connect ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/rest-ftp-daemon/remote_sftp.rb', line 13
def connect
super
log_debug "RemoteSFTP.connect [#{@target.user}]@[#{@target.host}]:[#{@target.port}]"
verbosity = @debug ? Logger::DEBUG : false
@sftp = Net::SFTP.start(@target.host.to_s, @target.user.to_s,
password: @target.password.to_s,
verbose: verbosity,
port: @target.port,
non_interactive: true,
timeout: DEFAULT_SFTP_TIMEOUT
)
end
|
#connected? ⇒ Boolean
140
141
142
|
# File 'lib/rest-ftp-daemon/remote_sftp.rb', line 140
def connected?
@sftp && !@sftp.closed?
end
|
#mkdir(directory) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/rest-ftp-daemon/remote_sftp.rb', line 52
def mkdir directory
log_debug "RemoteSFTP.mkdir [#{directory}]"
@sftp.mkdir! directory
rescue StandardError => ex
raise TargetPermissionError, ex.message
end
|
#prepare ⇒ Object
10
11
|
# File 'lib/rest-ftp-daemon/remote_sftp.rb', line 10
def prepare
end
|
#present?(target) ⇒ Boolean
32
33
34
35
36
37
38
39
40
|
# File 'lib/rest-ftp-daemon/remote_sftp.rb', line 32
def present? target
log_debug "RemoteSFTP.present? [#{target.name}]"
stat = @sftp.stat! target.path
rescue Net::SFTP::StatusException
return false
else
return stat.size
end
|
#remove!(target) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/rest-ftp-daemon/remote_sftp.rb', line 42
def remove! target
log_debug "RemoteSFTP.remove! [#{target.name}]"
@sftp.remove target.path
rescue Net::SFTP::StatusException
log_debug "#{LOG_INDENT}[#{target.name}] file not found"
else
log_debug "#{LOG_INDENT}[#{target.name}] removed"
end
|
#upload(source, target, use_temp_name = false, &callback) ⇒ Object
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/rest-ftp-daemon/remote_sftp.rb', line 90
def upload source, target, use_temp_name = false, &callback
raise RestFtpDaemon::AssertionFailed, "upload/sftp" if @sftp.nil?
dest = target.clone
if use_temp_name
dest.generate_temp_name!
end
log_debug "RemoteSFTP.upload temp[#{use_temp_name}] name[#{dest.name}]"
@sftp.upload! source.path, dest.path do |event, _uploader, *args|
case event
when :open then
when :put then
yield args[2].length, dest.name
when :close then
when :mkdir
when :finish
end
end
if use_temp_name
flags = 0x00000001
log_debug "RemoteSFTP.upload rename [#{dest.name}] > [#{target.name}]"
@sftp.rename! dest.path, target.path, flags
end
end
|