woolen_common 0.0.12 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/woolen_common/ssh_proxy.rb +185 -185
- data/lib/woolen_common/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 026f99f63ac16dd1d2e788aef29cb1ea9e3eb0ab
|
4
|
+
data.tar.gz: 8e1cd294b845a8984ea8cdb5aec9367aa5c36879
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36a1e6cccf1867a8aa69ae06f670e2c35369aae59ac1e59935046f445873ef406805e99cd3bc2dfa408341d6892eb60cdc3087a89c7f40ac506c0765c229fedb
|
7
|
+
data.tar.gz: 777fc85997187c94b6e71c152cec8b64865570edbb8db6d20b10cad2116eac43b415ea04ca4d8c3d99186587923e105b01e48953957a3215bc8f8fd1365041fc
|
@@ -1,209 +1,209 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
begin
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
3
|
+
require 'net/ssh' rescue nil
|
4
|
+
require 'net/sftp' rescue nil
|
5
|
+
require "#{File.join(File.dirname(__FILE__), 'connection_pool')}"
|
6
|
+
require "#{File.join(File.dirname(__FILE__), 'logger')}"
|
7
|
+
module WoolenCommon
|
8
|
+
class SshProxyPool
|
9
|
+
include WoolenCommon::ToolLogger
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_accessor :the_ssh_instances
|
13
|
+
|
14
|
+
def get_ssh_proxy(ip, user, password, port=22, max_ssh=10, time_out=10)
|
15
|
+
@the_ssh_instances ||= {}
|
16
|
+
@the_ssh_instances[ip] ||= {}
|
17
|
+
@the_ssh_instances[ip][port] ||= {}
|
18
|
+
@the_ssh_instances[ip][port][user] ||= {}
|
19
|
+
@the_ssh_instances[ip][port][user][password] ||= self.new(ip, user, password, port, max_ssh, time_out)
|
20
|
+
@the_ssh_instances[ip][port][user][password]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(ip, user, password, port=22, max_ssh=10, time_out=10)
|
25
|
+
debug "ssh setup : [ user::#{user},password::#{password},port:#{port}, time_out #{time_out} ]"
|
26
|
+
@ip = ip
|
27
|
+
@user = user
|
28
|
+
@password = password
|
29
|
+
@port = port
|
30
|
+
@max_ssh = max_ssh
|
31
|
+
@time_out = time_out
|
32
|
+
get_pool
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_pool
|
36
|
+
@ssh_connection_pool ||= ::WoolenCommon::ConnectionPool.new({ :size => @max_ssh, :timeout => @time_out }) do
|
37
|
+
debug "ip:#{@ip},@password:#{@password},@port:#{@port}"
|
38
|
+
::WoolenCommon::SshProxy.new(@ip, @user, :password => @password, :port => @port, :proxy_conn_timeout => @time_out)
|
39
|
+
end
|
40
|
+
end
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
def method_missing(method, *args, &block)
|
43
|
+
debug "need to invoke ssh method ::#{method} #{args}"
|
44
|
+
self.instance_eval <<-THE_END
|
45
45
|
def #{method}(*args,&block)
|
46
46
|
trace "need to invoke ssh method ::#{method} \#{args}"
|
47
47
|
get_pool.with do |ssh_conn|
|
48
48
|
return ssh_conn.send :#{method}, *args, &block
|
49
49
|
end
|
50
50
|
end
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
class SshProxy
|
57
|
-
include WoolenCommon::ToolLogger
|
58
|
-
class << self
|
59
|
-
|
60
|
-
attr_accessor :the_ssh_instances
|
61
|
-
|
62
|
-
def get_ssh_proxy(ip, port, user, passwd)
|
63
|
-
options = { :port => port, :password => passwd }
|
64
|
-
@the_ssh_instances ||= {}
|
65
|
-
@the_ssh_instances[ip] ||= {}
|
66
|
-
@the_ssh_instances[ip][port] ||= {}
|
67
|
-
@the_ssh_instances[ip][port][user] ||= {}
|
68
|
-
@the_ssh_instances[ip][port][user][passwd] ||= SshProxy.new(ip, user, options)
|
69
|
-
@the_ssh_instances[ip][port][user][passwd]
|
70
|
-
end
|
71
|
-
end
|
51
|
+
THE_END
|
52
|
+
self.send method, *args, &block
|
53
|
+
end
|
54
|
+
end
|
72
55
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
@options = options
|
77
|
-
@conn_retry = options[:proxy_conn_retry] || 5
|
78
|
-
options[:paranoid] = false # for disable the public key verify
|
79
|
-
options[:non_interactive] = true # for disable the interactive
|
80
|
-
options.delete :proxy_conn_retry if options[:proxy_conn_retry]
|
81
|
-
# 超时时间设置30秒太长了,不是很合理,实际上5秒没有回复,那就是出问题了
|
82
|
-
@conn_timeout = options[:proxy_conn_timeout] || 5
|
83
|
-
options.delete :proxy_conn_timeout if options[:proxy_conn_timeout]
|
84
|
-
proxy_reset_conn
|
85
|
-
end
|
56
|
+
class SshProxy
|
57
|
+
include WoolenCommon::ToolLogger
|
58
|
+
class << self
|
86
59
|
|
87
|
-
|
88
|
-
@conn_retry.times do
|
89
|
-
begin
|
90
|
-
Timeout.timeout(@conn_timeout) do
|
91
|
-
# if Net::SSH::Version.to_i <= 3_000_000
|
92
|
-
# @ssh_conn = Net::SSH.start(@host, @user, @options)
|
93
|
-
# else
|
94
|
-
# @ssh_conn = Net::SSH.start(@host, @user, @options)
|
95
|
-
# end
|
96
|
-
@ssh_conn = Net::SSH.start(@host, @user, @options)
|
97
|
-
if check_connector_close
|
98
|
-
debug 'reconnect ssh ok'
|
99
|
-
return
|
100
|
-
end
|
101
|
-
end
|
102
|
-
rescue Exception => e
|
103
|
-
error "连接ssh服务器出错~!信息是:#{e.message},用户信息:@host:#{@host},@user:#{@user},@options:#{@options}"
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
60
|
+
attr_accessor :the_ssh_instances
|
107
61
|
|
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
|
-
|
134
|
-
|
62
|
+
def get_ssh_proxy(ip, port, user, passwd, opt={})
|
63
|
+
options = { :port => port, :password => passwd }.merge(opt)
|
64
|
+
@the_ssh_instances ||= {}
|
65
|
+
@the_ssh_instances[ip] ||= {}
|
66
|
+
@the_ssh_instances[ip][port] ||= {}
|
67
|
+
@the_ssh_instances[ip][port][user] ||= {}
|
68
|
+
@the_ssh_instances[ip][port][user][passwd] ||= SshProxy.new(ip, user, options)
|
69
|
+
@the_ssh_instances[ip][port][user][passwd]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def initialize(host, user, options={})
|
74
|
+
@host = host
|
75
|
+
@user = user
|
76
|
+
@options = options
|
77
|
+
@conn_retry = options[:proxy_conn_retry] || 5
|
78
|
+
options[:paranoid] = false # for disable the public key verify
|
79
|
+
options[:non_interactive] = true # for disable the interactive
|
80
|
+
options.delete :proxy_conn_retry if options[:proxy_conn_retry]
|
81
|
+
# 超时时间设置30秒太长了,不是很合理,实际上5秒没有回复,那就是出问题了
|
82
|
+
@conn_timeout = options[:proxy_conn_timeout] || 5
|
83
|
+
options.delete :proxy_conn_timeout if options[:proxy_conn_timeout]
|
84
|
+
proxy_reset_conn
|
85
|
+
end
|
86
|
+
|
87
|
+
def proxy_reset_conn
|
88
|
+
@conn_retry.times do
|
89
|
+
begin
|
90
|
+
Timeout.timeout(@conn_timeout) do
|
91
|
+
# if Net::SSH::Version.to_i <= 3_000_000
|
92
|
+
# @ssh_conn = Net::SSH.start(@host, @user, @options)
|
93
|
+
# else
|
94
|
+
# @ssh_conn = Net::SSH.start(@host, @user, @options)
|
95
|
+
# end
|
96
|
+
@ssh_conn = Net::SSH.start(@host, @user, @options)
|
97
|
+
if check_connector_close
|
98
|
+
debug 'reconnect ssh ok'
|
99
|
+
return
|
100
|
+
end
|
135
101
|
end
|
102
|
+
rescue Exception => e
|
103
|
+
error "连接ssh服务器出错~!信息是:#{e.message},用户信息:@host:#{@host},@user:#{@user},@options:#{@options}"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
136
107
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
108
|
+
def method_missing(name, *args, &block)
|
109
|
+
if check_connector_close
|
110
|
+
@ssh_conn.close rescue nil
|
111
|
+
proxy_reset_conn
|
112
|
+
end
|
113
|
+
#debug "SshProxy need to invoke methdo ::#{name} "
|
114
|
+
#debug "params::#{args}"
|
115
|
+
Timeout.timeout(@conn_timeout) do
|
116
|
+
return_result = ''
|
117
|
+
if @ssh_conn
|
118
|
+
return_result = @ssh_conn.send(name, *args, &block)
|
119
|
+
#debug "SshProxy invoke result ::#{return_result}"
|
120
|
+
else
|
121
|
+
error 'ssh链接建立不起来!'
|
122
|
+
end
|
123
|
+
return return_result
|
124
|
+
end
|
125
|
+
end
|
146
126
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
end
|
157
|
-
rescue Exception => e
|
158
|
-
error "检查连接出错,错误信息是::#{e.message}"
|
159
|
-
return true
|
160
|
-
end
|
161
|
-
true
|
162
|
-
end
|
127
|
+
def exec!(command, &block)
|
128
|
+
if check_connector_close
|
129
|
+
@ssh_conn.close rescue nil
|
130
|
+
proxy_reset_conn
|
131
|
+
end
|
132
|
+
Timeout.timeout(@conn_timeout) do
|
133
|
+
return @ssh_conn.exec!(command.unpack('C*').pack('C*'), &block)
|
134
|
+
end
|
135
|
+
end
|
163
136
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
137
|
+
def exec(command, &block)
|
138
|
+
if check_connector_close
|
139
|
+
@ssh_conn.close rescue nil
|
140
|
+
proxy_reset_conn
|
141
|
+
end
|
142
|
+
Timeout.timeout(@conn_timeout) do
|
143
|
+
return @ssh_conn.exec(command.unpack('C*').pack('C*'), &block)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def check_connector_close
|
148
|
+
begin
|
149
|
+
if @ssh_conn.nil? or @ssh_conn.closed?
|
150
|
+
return true
|
151
|
+
end
|
152
|
+
Timeout.timeout(@conn_timeout) do
|
153
|
+
if @ssh_conn.exec!('echo hello').include? 'hello'
|
154
|
+
return false
|
173
155
|
end
|
156
|
+
end
|
157
|
+
rescue Exception => e
|
158
|
+
error "检查连接出错,错误信息是::#{e.message}"
|
159
|
+
return true
|
160
|
+
end
|
161
|
+
true
|
162
|
+
end
|
163
|
+
|
164
|
+
# 阻塞性下载
|
165
|
+
def sftp_download!(remote_path, local_path)
|
166
|
+
if check_connector_close
|
167
|
+
@ssh_conn.close rescue nil
|
168
|
+
proxy_reset_conn
|
169
|
+
end
|
170
|
+
@ssh_conn.sftp.connect! do |sftp_session|
|
171
|
+
return sftp_session.download!(remote_path, local_path)
|
172
|
+
end
|
173
|
+
end
|
174
174
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
175
|
+
# 非塞性下载
|
176
|
+
def sftp_download(remote_path, local_path)
|
177
|
+
if check_connector_close
|
178
|
+
@ssh_conn.close rescue nil
|
179
|
+
proxy_reset_conn
|
180
|
+
end
|
181
|
+
@ssh_conn.sftp.connect do |sftp_session|
|
182
|
+
return sftp_session.download!(remote_path, local_path)
|
183
|
+
end
|
184
|
+
end
|
185
185
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
186
|
+
def sftp_upload!(remote_path, local_path)
|
187
|
+
if check_connector_close
|
188
|
+
@ssh_conn.close rescue nil
|
189
|
+
proxy_reset_conn
|
190
|
+
end
|
191
|
+
@ssh_conn.sftp.connect! do |sftp_session|
|
192
|
+
return sftp_session.upload!(local_path, remote_path)
|
193
|
+
end
|
194
|
+
end
|
195
195
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
end
|
204
|
-
end
|
196
|
+
def sftp_upload(remote_path, local_path)
|
197
|
+
if check_connector_close
|
198
|
+
@ssh_conn.close rescue nil
|
199
|
+
proxy_reset_conn
|
200
|
+
end
|
201
|
+
@ssh_conn.sftp.connect do |sftp_session|
|
202
|
+
return sftp_session.upload(local_path, remote_path)
|
205
203
|
end
|
204
|
+
end
|
206
205
|
end
|
206
|
+
end
|
207
207
|
rescue Exception => e
|
208
|
-
|
208
|
+
puts "error when load ssh,sftp,no ssh can use #{e.message}"
|
209
209
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: woolen_common
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- just_woolen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -180,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
180
180
|
version: '0'
|
181
181
|
requirements: []
|
182
182
|
rubyforge_project:
|
183
|
-
rubygems_version: 2.4.
|
183
|
+
rubygems_version: 2.4.5.1
|
184
184
|
signing_key:
|
185
185
|
specification_version: 4
|
186
186
|
summary: woolen_common
|