spectre-ftp 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/spectre/ftp.rb +195 -0
  3. metadata +76 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 13456bf294907a5b79f995804c119d803a1a565e6b93520d924ee7429e0defca
4
+ data.tar.gz: edaa3c30ad479bfe490086f9d3ec6268befa9b6cd26b445790f403cc4884aafe
5
+ SHA512:
6
+ metadata.gz: 5105ff5be1c42b1d163ebea6b5844b770df74be2cb1f97ab1f239e89c9837c939ad6f115688bd105da273ffda6b41e8ece9cbd83b999b6f4a69c4977258380e1
7
+ data.tar.gz: 3310db259c6786404503daff0a05286c60c9aa1af01eae59c85e9aab2e27c76ec22bb7d4ff2018328a9139b254086e11ab6586f682c92f4f3942aa7fb41b4172
@@ -0,0 +1,195 @@
1
+ require 'net/ftp'
2
+ require 'net/sftp'
3
+ require 'logger'
4
+ require 'json'
5
+
6
+
7
+ module Spectre
8
+ module FTP
9
+ @@cfg = {}
10
+
11
+ class FTPConnection < DslClass
12
+ def initialize host, username, password, opts, logger
13
+ @__logger = logger
14
+ @__session = nil
15
+
16
+ @__host = host
17
+ @__username = username
18
+ @__password = password
19
+ @__opts = opts
20
+ end
21
+
22
+ def connect!
23
+ return unless @__session == nil or @__session.closed?
24
+ @__logger.info "Connecting to '#{@__host}' with user '#{@__username}'"
25
+ @__session = Net::FTP.new(@__host, @__opts)
26
+ @__session.login @__username, @__password
27
+ end
28
+
29
+ def close
30
+ return unless @__session and not @__session.closed?
31
+ @__session.close
32
+ end
33
+
34
+ def can_connect?
35
+ begin
36
+ connect!
37
+ return true
38
+ rescue
39
+ return false
40
+ end
41
+ end
42
+
43
+ def download remotefile, to: File.basename(remotefile)
44
+ connect!
45
+ @__logger.info "Downloading '#{@__username}@#{@__host}:#{File.join @__session.pwd, remotefile}' to '#{File.expand_path to}'"
46
+ @__session.getbinaryfile(remotefile, to)
47
+ end
48
+
49
+ def upload localfile, to: File.basename(localfile)
50
+ connect!
51
+ @__logger.info "Uploading '#{File.expand_path localfile}' to '#{@__username}@#{@__host}:#{File.join @__session.pwd, to}'"
52
+ @__session.putbinaryfile(localfile, to)
53
+ end
54
+
55
+ def list
56
+ connect!
57
+ file_list = @__session.list
58
+ @__logger.info "Listing file in #{@__session.pwd}\n#{file_list}"
59
+ file_list
60
+ end
61
+ end
62
+
63
+
64
+ class SFTPConnection < DslClass
65
+ def initialize host, username, opts, logger
66
+ opts[:non_interactive] = true
67
+
68
+ @__logger = logger
69
+ @__session = nil
70
+ @__host = host
71
+ @__username = username
72
+ @__opts = opts
73
+ end
74
+
75
+ def connect!
76
+ return unless @__session == nil or @__session.closed?
77
+ @__logger.info "Connecting to '#{@__host}' with user '#{@__username}'"
78
+ @__session = Net::SFTP.start(@__host, @__username, @__opts)
79
+ @__session.connect!
80
+ end
81
+
82
+ def close
83
+ return unless @__session and not @__session.closed?
84
+ # @__session.close!
85
+ end
86
+
87
+ def can_connect?
88
+ begin
89
+ connect!
90
+ return true
91
+ rescue
92
+ return false
93
+ end
94
+ end
95
+
96
+ def download remotefile, to: File.basename(remotefile)
97
+ @__logger.info "Downloading '#{@__username}@#{@__host}:#{remotefile}' to '#{File.expand_path to}'"
98
+ connect!
99
+ @__session.download!(remotefile, to)
100
+ end
101
+
102
+ def upload localfile, to: File.basename(localfile)
103
+ @__logger.info "Uploading '#{File.expand_path localfile}' to '#{@__username}@#{@__host}:#{to}'"
104
+ connect!
105
+ @__session.upload!(localfile, to)
106
+ end
107
+
108
+ def stat path
109
+ connect!
110
+ file_info = @__session.stat! path
111
+ @__logger.info "Stat '#{path}'\n#{JSON.pretty_generate file_info.attributes}"
112
+ file_info.attributes
113
+ end
114
+
115
+ def exists path
116
+ begin
117
+ file_info = @__session.stat! path
118
+
119
+ rescue Net::SFTP::StatusException => e
120
+ return false if e.description == 'no such file'
121
+ raise e
122
+ end
123
+
124
+ return true
125
+ end
126
+ end
127
+
128
+
129
+ class << self
130
+ def ftp name, config={}, &block
131
+ raise "FTP connection '#{name}' not configured" unless @@cfg.key?(name) or config.count > 0
132
+ cfg = @@cfg[name] || {}
133
+
134
+ host = config[:host] || cfg['host'] || name
135
+ username = config[:username] || cfg['username']
136
+ password = config[:password] || cfg['password']
137
+
138
+ opts = {}
139
+ opts[:ssl] = config[:ssl]
140
+ opts[:port] = config[:port] || cfg['port'] || 21
141
+
142
+ @@logger.info "Connecting to #{host} with user #{username}"
143
+
144
+ ftp_conn = FTPConnection.new(host, username, password, opts, @@logger)
145
+
146
+ begin
147
+ ftp_conn.instance_eval &block
148
+ ensure
149
+ ftp_conn.close
150
+ end
151
+ end
152
+
153
+ def sftp name, config={}, &block
154
+ raise "FTP connection '#{name}' not configured" unless @@cfg.key?(name) or config.count > 0
155
+
156
+ cfg = @@cfg[name] || {}
157
+
158
+ host = config[:host] || cfg['host'] || name
159
+ username = config[:username] || cfg['username']
160
+ password = config[:password] || cfg['password']
161
+
162
+ opts = {}
163
+ opts[:password] = password
164
+ opts[:port] = config[:port] || cfg['port'] || 22
165
+ opts[:keys] = [cfg['key']] if cfg.key? 'key'
166
+ opts[:passphrase] = cfg['passphrase'] if cfg.key? 'passphrase'
167
+
168
+ opts[:auth_methods] = []
169
+ opts[:auth_methods].push 'publickey' if opts[:keys]
170
+ opts[:auth_methods].push 'password' if opts[:password]
171
+
172
+ sftp_con = SFTPConnection.new(host, username, opts, @@logger)
173
+
174
+ begin
175
+ sftp_con.instance_eval &block
176
+ ensure
177
+ sftp_con.close
178
+ end
179
+ end
180
+ end
181
+
182
+ Spectre.register do |config|
183
+ @@logger = ::Logger.new config['log_file'], progname: 'spectre/ftp'
184
+
185
+ if config.key? 'ftp'
186
+
187
+ config['ftp'].each do |name, cfg|
188
+ @@cfg[name] = cfg
189
+ end
190
+ end
191
+ end
192
+
193
+ Spectre.delegate :ftp, :sftp, to: self
194
+ end
195
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spectre-ftp
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Christian Neubauer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-sftp
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: spectre-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.8.4
41
+ description: Adds FTP access functionality to the spectre framework
42
+ email:
43
+ - me@christianneubauer.de
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/spectre/ftp.rb
49
+ homepage: https://bitbucket.org/cneubaur/spectre-ftp
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ allowed_push_host: https://rubygems.org/
54
+ homepage_uri: https://bitbucket.org/cneubaur/spectre-ftp
55
+ source_code_uri: https://bitbucket.org/cneubaur/spectre-ftp
56
+ changelog_uri: https://bitbucket.org/cneubaur/spectre-ftp/src/master/CHANGELOG.md
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.5.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.0.8
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: FTP module for spectre
76
+ test_files: []