woolen_common 0.0.4 → 0.0.5
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 +127 -123
- data/lib/woolen_common/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 662b6d271e9f5b47465b7acc6f6d8bfc92df5720
|
4
|
+
data.tar.gz: e95a24c60cf9028955027caa738141e5285f572e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5df46c8ff4475b790be9e79a779f58c976a9aea7fb06a0a748d62565105f2dbf3951a66d10f6da5e15e86c6679d9c61efd2a89673aeb6ec9c7249a2b49de4ad
|
7
|
+
data.tar.gz: 73b71f744b1a530c4ec6a713b707f74a9df24d03c6a39f99ce0c8b06f0df5c65747c80dec226d772f7a7cafb22dc494632d9b8a748ea49b10b4186f76ee0102f
|
@@ -1,152 +1,156 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
|
-
|
3
|
-
require 'net/
|
4
|
-
require
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
2
|
+
begin
|
3
|
+
require 'net/ssh' rescue nil
|
4
|
+
require 'net/sftp' rescue nil
|
5
|
+
require "#{File.join(File.dirname(__FILE__), 'logger')}"
|
6
|
+
module WoolenCommon
|
7
|
+
class SshProxy
|
8
|
+
include ToolLogger
|
9
|
+
class << self
|
10
|
+
attr_accessor :the_ssh_instances
|
11
|
+
def get_ssh_proxy(ip,port,user,passwd)
|
12
|
+
options = {:port => port,:password => passwd}
|
13
|
+
@the_ssh_instances ||= {}
|
14
|
+
@the_ssh_instances[ip] ||= {}
|
15
|
+
@the_ssh_instances[ip][port] ||= {}
|
16
|
+
@the_ssh_instances[ip][port][user] ||= {}
|
17
|
+
@the_ssh_instances[ip][port][user][passwd] ||= SshProxy.new(ip, user, options)
|
18
|
+
@the_ssh_instances[ip][port][user][passwd]
|
19
|
+
end
|
18
20
|
end
|
19
|
-
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
22
|
+
def initialize(host, user, options={})
|
23
|
+
@host = host
|
24
|
+
@user = user
|
25
|
+
@options = options
|
26
|
+
@conn_retry = options[:proxy_conn_retry] || 5
|
27
|
+
options.delete :proxy_conn_retry if options[:proxy_conn_retry]
|
28
|
+
# 超时时间设置30秒太长了,不是很合理,实际上5秒没有回复,那就是出问题了
|
29
|
+
@conn_timeout = options[:proxy_conn_timeout] || 5
|
30
|
+
options.delete :proxy_conn_timeout if options[:proxy_conn_timeout]
|
31
|
+
proxy_reset_conn
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
34
|
+
def proxy_reset_conn
|
35
|
+
@conn_retry.times do
|
36
|
+
begin
|
37
|
+
Timeout.timeout(@conn_timeout) do
|
38
|
+
# if Net::SSH::Version.to_i <= 3_000_000
|
39
|
+
# @ssh_conn = Net::SSH.start(@host, @user, @options)
|
40
|
+
# else
|
41
|
+
# @ssh_conn = Net::SSH.start(@host, @user, @options)
|
42
|
+
# end
|
43
|
+
@ssh_conn = Net::SSH.start(@host, @user, @options)
|
44
|
+
if check_connector_close
|
45
|
+
debug 'reconnect ssh ok'
|
46
|
+
return
|
47
|
+
end
|
46
48
|
end
|
49
|
+
rescue Exception => e
|
50
|
+
error "连接ssh服务器出错~!信息是:#{e.message},用户信息:@host:#{@host},@user:#{@user},@options:#{@options}"
|
47
51
|
end
|
48
|
-
rescue Exception => e
|
49
|
-
error "连接ssh服务器出错~!信息是:#{e.message},用户信息:@host:#{@host},@user:#{@user},@options:#{@options}"
|
50
52
|
end
|
51
53
|
end
|
52
|
-
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
def exec!(command, &block)
|
74
|
-
if check_connector_close
|
75
|
-
@ssh_conn.close rescue nil
|
76
|
-
proxy_reset_conn
|
77
|
-
end
|
78
|
-
Timeout.timeout(@conn_timeout) do
|
79
|
-
return @ssh_conn.exec!(command.unpack('C*').pack('C*'), &block)
|
55
|
+
def method_missing(name, *args, &block)
|
56
|
+
if check_connector_close
|
57
|
+
@ssh_conn.close rescue nil
|
58
|
+
proxy_reset_conn
|
59
|
+
end
|
60
|
+
#debug "SshProxy need to invoke methdo ::#{name} "
|
61
|
+
#debug "params::#{args}"
|
62
|
+
Timeout.timeout(@conn_timeout) do
|
63
|
+
return_result = ''
|
64
|
+
if @ssh_conn
|
65
|
+
return_result = @ssh_conn.send(name, *args, &block)
|
66
|
+
#debug "SshProxy invoke result ::#{return_result}"
|
67
|
+
else
|
68
|
+
error 'ssh链接建立不起来!'
|
69
|
+
end
|
70
|
+
return return_result
|
71
|
+
end
|
80
72
|
end
|
81
|
-
end
|
82
73
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
74
|
+
def exec!(command, &block)
|
75
|
+
if check_connector_close
|
76
|
+
@ssh_conn.close rescue nil
|
77
|
+
proxy_reset_conn
|
78
|
+
end
|
79
|
+
Timeout.timeout(@conn_timeout) do
|
80
|
+
return @ssh_conn.exec!(command.unpack('C*').pack('C*'), &block)
|
81
|
+
end
|
90
82
|
end
|
91
|
-
end
|
92
83
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
84
|
+
def exec(command, &block)
|
85
|
+
if check_connector_close
|
86
|
+
@ssh_conn.close rescue nil
|
87
|
+
proxy_reset_conn
|
97
88
|
end
|
98
89
|
Timeout.timeout(@conn_timeout) do
|
99
|
-
|
100
|
-
return false
|
101
|
-
end
|
90
|
+
return @ssh_conn.exec(command.unpack('C*').pack('C*'), &block)
|
102
91
|
end
|
103
|
-
rescue Exception => e
|
104
|
-
error "检查连接出错,错误信息是::#{e.message}"
|
105
|
-
return true
|
106
92
|
end
|
107
|
-
true
|
108
|
-
end
|
109
93
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
94
|
+
def check_connector_close
|
95
|
+
begin
|
96
|
+
if @ssh_conn.nil? or @ssh_conn.closed?
|
97
|
+
return true
|
98
|
+
end
|
99
|
+
Timeout.timeout(@conn_timeout) do
|
100
|
+
if @ssh_conn.exec!('echo hello').include? 'hello'
|
101
|
+
return false
|
102
|
+
end
|
103
|
+
end
|
104
|
+
rescue Exception => e
|
105
|
+
error "检查连接出错,错误信息是::#{e.message}"
|
106
|
+
return true
|
107
|
+
end
|
108
|
+
true
|
118
109
|
end
|
119
|
-
end
|
120
110
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
111
|
+
# 阻塞性下载
|
112
|
+
def sftp_download!(remote_path, local_path)
|
113
|
+
if check_connector_close
|
114
|
+
@ssh_conn.close rescue nil
|
115
|
+
proxy_reset_conn
|
116
|
+
end
|
117
|
+
@ssh_conn.sftp.connect! do |sftp_session|
|
118
|
+
return sftp_session.download!(remote_path, local_path)
|
119
|
+
end
|
129
120
|
end
|
130
|
-
end
|
131
121
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
122
|
+
# 非塞性下载
|
123
|
+
def sftp_download(remote_path, local_path)
|
124
|
+
if check_connector_close
|
125
|
+
@ssh_conn.close rescue nil
|
126
|
+
proxy_reset_conn
|
127
|
+
end
|
128
|
+
@ssh_conn.sftp.connect do |sftp_session|
|
129
|
+
return sftp_session.download!(remote_path, local_path)
|
130
|
+
end
|
139
131
|
end
|
140
|
-
end
|
141
132
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
133
|
+
def sftp_upload!(remote_path, local_path)
|
134
|
+
if check_connector_close
|
135
|
+
@ssh_conn.close rescue nil
|
136
|
+
proxy_reset_conn
|
137
|
+
end
|
138
|
+
@ssh_conn.sftp.connect! do |sftp_session|
|
139
|
+
return sftp_session.upload!(local_path,remote_path)
|
140
|
+
end
|
146
141
|
end
|
147
|
-
|
148
|
-
|
142
|
+
|
143
|
+
def sftp_upload(remote_path, local_path)
|
144
|
+
if check_connector_close
|
145
|
+
@ssh_conn.close rescue nil
|
146
|
+
proxy_reset_conn
|
147
|
+
end
|
148
|
+
@ssh_conn.sftp.connect do |sftp_session|
|
149
|
+
return sftp_session.upload(local_path,remote_path)
|
150
|
+
end
|
149
151
|
end
|
150
152
|
end
|
151
153
|
end
|
154
|
+
rescue Exception => e
|
155
|
+
puts "error when load ssh,sftp,no ssh can use #{e.message}"
|
152
156
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- just_woolen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|