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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8cbd7fef6be743435ba6c06a56f1bb78f37afb0e
4
- data.tar.gz: ecf10852212c4266a8ba0c29725897b55114ff19
3
+ metadata.gz: 662b6d271e9f5b47465b7acc6f6d8bfc92df5720
4
+ data.tar.gz: e95a24c60cf9028955027caa738141e5285f572e
5
5
  SHA512:
6
- metadata.gz: cdfb1dafb3fe1467f43d8a80ea50eec0edf94581abc6f2c51eb965f3ed538c05af700280fcf763d5f7326b2bec693fbc904b347078be6c3831b5d432b60663f4
7
- data.tar.gz: 51ce151f19fba2b5fa716849dc54e8e634b8fec5a9448902de9f3f445e38b4dfc0e1d12d733b23a0a9fd9e5fcb60669d26cb3943443abab368231890bc82ef95
6
+ metadata.gz: c5df46c8ff4475b790be9e79a779f58c976a9aea7fb06a0a748d62565105f2dbf3951a66d10f6da5e15e86c6679d9c61efd2a89673aeb6ec9c7249a2b49de4ad
7
+ data.tar.gz: 73b71f744b1a530c4ec6a713b707f74a9df24d03c6a39f99ce0c8b06f0df5c65747c80dec226d772f7a7cafb22dc494632d9b8a748ea49b10b4186f76ee0102f
@@ -1,152 +1,156 @@
1
1
  # -*- encoding : utf-8 -*-
2
- require 'net/ssh' rescue nil
3
- require 'net/sftp' rescue nil
4
- require "#{File.join(File.dirname(__FILE__), 'logger')}"
5
- module WoolenCommon
6
- class SshProxy
7
- include ToolLogger
8
- class << self
9
- attr_accessor :the_ssh_instances
10
- def get_ssh_proxy(ip,port,user,passwd)
11
- options = {:port => port,:password => passwd}
12
- @the_ssh_instances ||= {}
13
- @the_ssh_instances[ip] ||= {}
14
- @the_ssh_instances[ip][port] ||= {}
15
- @the_ssh_instances[ip][port][user] ||= {}
16
- @the_ssh_instances[ip][port][user][passwd] ||= SshProxy.new(ip, user, options)
17
- @the_ssh_instances[ip][port][user][passwd]
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
- def initialize(host, user, options={})
22
- @host = host
23
- @user = user
24
- @options = options
25
- @conn_retry = options[:proxy_conn_retry] || 5
26
- options.delete :proxy_conn_retry if options[:proxy_conn_retry]
27
- # 超时时间设置30秒太长了,不是很合理,实际上5秒没有回复,那就是出问题了
28
- @conn_timeout = options[:proxy_conn_timeout] || 5
29
- options.delete :proxy_conn_timeout if options[:proxy_conn_timeout]
30
- proxy_reset_conn
31
- end
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
- def proxy_reset_conn
34
- @conn_retry.times do
35
- begin
36
- Timeout.timeout(@conn_timeout) do
37
- # if Net::SSH::Version.to_i <= 3_000_000
38
- # @ssh_conn = Net::SSH.start(@host, @user, @options)
39
- # else
40
- # @ssh_conn = Net::SSH.start(@host, @user, @options)
41
- # end
42
- @ssh_conn = Net::SSH.start(@host, @user, @options)
43
- if check_connector_close
44
- debug 'reconnect ssh ok'
45
- return
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
- def method_missing(name, *args, &block)
55
- if check_connector_close
56
- @ssh_conn.close rescue nil
57
- proxy_reset_conn
58
- end
59
- #debug "SshProxy need to invoke methdo ::#{name} "
60
- #debug "params::#{args}"
61
- Timeout.timeout(@conn_timeout) do
62
- return_result = ''
63
- if @ssh_conn
64
- return_result = @ssh_conn.send(name, *args, &block)
65
- #debug "SshProxy invoke result ::#{return_result}"
66
- else
67
- error 'ssh链接建立不起来!'
68
- end
69
- return return_result
70
- end
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
- def exec(command, &block)
84
- if check_connector_close
85
- @ssh_conn.close rescue nil
86
- proxy_reset_conn
87
- end
88
- Timeout.timeout(@conn_timeout) do
89
- return @ssh_conn.exec(command.unpack('C*').pack('C*'), &block)
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
- def check_connector_close
94
- begin
95
- if @ssh_conn.nil? or @ssh_conn.closed?
96
- return true
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
- if @ssh_conn.exec!('echo hello').include? 'hello'
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
- def sftp_download!(remote_path, local_path)
112
- if check_connector_close
113
- @ssh_conn.close rescue nil
114
- proxy_reset_conn
115
- end
116
- @ssh_conn.sftp.connect! do |sftp_session|
117
- return sftp_session.download!(remote_path, local_path)
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
- def sftp_download(remote_path, local_path)
123
- if check_connector_close
124
- @ssh_conn.close rescue nil
125
- proxy_reset_conn
126
- end
127
- @ssh_conn.sftp.connect do |sftp_session|
128
- return sftp_session.download!(remote_path, local_path)
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
- def sftp_upload!(remote_path, local_path)
133
- if check_connector_close
134
- @ssh_conn.close rescue nil
135
- proxy_reset_conn
136
- end
137
- @ssh_conn.sftp.connect! do |sftp_session|
138
- return sftp_session.upload!(local_path,remote_path)
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
- def sftp_upload(remote_path, local_path)
143
- if check_connector_close
144
- @ssh_conn.close rescue nil
145
- proxy_reset_conn
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
- @ssh_conn.sftp.connect do |sftp_session|
148
- return sftp_session.upload(local_path,remote_path)
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
@@ -1,3 +1,3 @@
1
1
  module WoolenCommon
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  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
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-12 00:00:00.000000000 Z
11
+ date: 2016-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler