xlogin 0.14.4 → 0.15.3

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
  SHA256:
3
- metadata.gz: cdbb386674deee491268f35a9d5ca46c721391068d693a68d9a3e289e0168f97
4
- data.tar.gz: b6afba886c65c3744253d58e0d8eccda3c265cf168159d95f3504408962127ce
3
+ metadata.gz: 9f70683b7d62c67930ee2b1d751e8b08de39e1ab9501c7c3941f65c58dcb4230
4
+ data.tar.gz: 5e2aec6727af661463c755f4c730f74d1c35a6381e269a172090759372ede5db
5
5
  SHA512:
6
- metadata.gz: edf8a747bff259718090617554b0d0868a3a3f321c287b328be882ac4dc68f9cbd2943755e55a1772525647bbfc485e849ac56495bed49a92b3a76cfb5bb4d1d
7
- data.tar.gz: 5d699b1d3982c7cf1a7bd4cd19ef174acf27885ac5e38fc4f10f746efd99b8e1e3deea523f52b901f9d4cdb80440aae5dd532abde1d41aa09c7548e07198a838
6
+ metadata.gz: 8b405fd67ab614bfead70cd94be9de78119b53291f8e111809a749cfcc36061bbb85d7ee7bd4e4858d53fc51264d9399ecf145fde13939662deac5e51b4df54e
7
+ data.tar.gz: b1750b6fa4ec8636535866e0de63c479890422e2b6026ed0724276c4fee3531c146286aafec6534fe07f371f95641efa3673f95e24ce87b059e4d3a840849009
@@ -40,15 +40,13 @@ module Xlogin
40
40
  return session unless block
41
41
  begin block.call(session) ensure session.close end
42
42
  end
43
- alias_method :create, :get
44
43
 
45
- def get_pool(args, **opts, &block)
44
+ def pool(args, **opts, &block)
46
45
  pool = factory.build_pool(args, **opts)
47
46
 
48
47
  return pool unless block
49
48
  begin block.call(pool) ensure pool.close end
50
49
  end
51
- alias_method :create_pool, :get_pool
52
50
 
53
51
  def configure(&block)
54
52
  instance_eval(&block)
@@ -13,7 +13,7 @@ module Xlogin
13
13
  def initialize
14
14
  @inventory = Hash.new
15
15
  @templates = Hash.new
16
- @gateways = Hash.new
16
+ @tunnels = Hash.new
17
17
  @mutex = Mutex.new
18
18
  end
19
19
 
@@ -54,32 +54,41 @@ module Xlogin
54
54
  @templates.keys
55
55
  end
56
56
 
57
- def open_tunnel(tunnel, host, port)
57
+ def open_tunnel(name, host, port)
58
58
  @mutex.synchronize do
59
- unless @gateways[tunnel]
60
- gateway_uri = Addressable::URI.parse(tunnel)
61
- case gateway_uri.scheme
59
+ tunnel = @tunnels[name]
60
+ unless tunnel && tunnel.gateway.active?
61
+ uri = Addressable::URI.parse(name)
62
+ case uri.scheme
62
63
  when 'ssh'
63
- username, password = *gateway_uri.userinfo.split(':')
64
- @gateways[tunnel] = Net::SSH::Gateway.new(
65
- gateway_uri.host,
64
+ username, password = *uri.userinfo.split(':')
65
+ gateway = Net::SSH::Gateway.new(
66
+ uri.host,
66
67
  username,
67
68
  password: password,
68
- port: gateway_uri.port || 22
69
+ port: uri.port || 22
69
70
  )
71
+
72
+ @tunnels[name] = Struct.new('Tunnel', :gateway, :ports).new(gateway, [])
70
73
  end
71
74
  end
72
75
 
73
- gateway = @gateways[tunnel]
74
- return host, port unless gateway
75
- return '127.0.0.1', gateway.open(host, port)
76
+ if tunnel = @tunnels[name]
77
+ port = tunnel.gateway.open(host, port)
78
+ host = '127.0.0.1'
79
+ tunnel.ports << port
80
+ end
81
+ return host, port
76
82
  end
77
83
  end
78
84
 
79
- def close_tunnel(tunnel, port)
85
+ def close_tunnel(name, port)
80
86
  @mutex.synchronize do
81
- gateway = @gateways[tunnel]
82
- gateway.close(port) if gateway
87
+ if tunnel = @tunnels[name]
88
+ tunnel.ports.delete(port)
89
+ tunnel.gateway.close(port)
90
+ tunnel.gateway.shutdown! if tunnel.ports.empty?
91
+ end
83
92
  end
84
93
  end
85
94
 
@@ -92,8 +92,10 @@ module Xlogin
92
92
  rescue => e
93
93
  RakeTask.shutdown! if fail_on_error
94
94
 
95
- session.comment(e.to_s, prefix: "[ERROR]", chomp: true, color: :white, background: :red)
96
- $stderr.print log_text(buffer.string + "\n").colorize(color: :light_red) if Rake.application.options.always_multitask
95
+ session.comment(e.to_s, lf: false, color: :red) if session
96
+ if Rake.application.options.always_multitask && !buffer.string.empty?
97
+ $stderr.print log_text(buffer.string + "\n").colorize(color: :red)
98
+ end
97
99
 
98
100
  return false
99
101
  ensure
@@ -101,16 +103,16 @@ module Xlogin
101
103
  end
102
104
 
103
105
  def log_text(text)
104
- text.lines.map{ |line| "#{Time.now.iso8601} - #{name}\t|#{line.gsub(/^.*\r/, '')}" }.join
106
+ text.lines.map{ |line| "#{Time.now.iso8601} #{name}\t|#{line.gsub(/^.*\r/, '')}" }.join
105
107
  end
106
108
 
107
109
  end
108
110
 
109
111
  module SessionModule
110
112
 
111
- def comment(line, prefix: "[INFO]", chomp: false, **color)
112
- write_log("#{prefix} #{line}".colorize({color: :light_white}.merge(**color)))
113
- cmd('') unless chomp
113
+ def comment(line, lf: true, **color)
114
+ write_log(line.chomp.colorize({color: :light_white}.merge(**color)))
115
+ cmd('') if lf
114
116
  end
115
117
 
116
118
  end
@@ -7,7 +7,7 @@ module Xlogin
7
7
  DEFAULT_POOL_SIZE = 1
8
8
  DEFAULT_POOL_IDLE = 60
9
9
 
10
- attr_reader :size, :idle
10
+ attr_accessor :size, :idle
11
11
 
12
12
  def initialize(args, **opts)
13
13
  @args = args
@@ -18,15 +18,7 @@ module Xlogin
18
18
 
19
19
  @mutex = Mutex.new
20
20
  @queue = Queue.new
21
- @created = 0
22
- end
23
-
24
- def size=(val)
25
- @mutex.synchronize{ @size = val }
26
- end
27
-
28
- def idle=(val)
29
- @mutex.synchronize{ @idle = val }
21
+ @count = 0
30
22
  end
31
23
 
32
24
  def with
@@ -38,20 +30,22 @@ module Xlogin
38
30
 
39
31
  def close
40
32
  until @queue.empty?
41
- session, _ = @queue.deq
33
+ session, _, _ = @queue.deq
42
34
  destroy(session)
43
35
  end
44
36
  end
45
37
 
46
38
  def deq
47
39
  @mutex.synchronize do
48
- if @queue.empty? && @created < @size
49
- @created += 1
40
+ if @queue.empty? && @count < @size
41
+ @count += 1
50
42
  return Xlogin.get(@args, **@opts)
51
43
  end
52
44
  end
53
45
 
54
- session, last_used = @queue.deq
46
+ session, last_used, watch_dog = @queue.deq
47
+
48
+ watch_dog.kill
55
49
  if Time.now - last_used > @idle
56
50
  destroy(session)
57
51
  return deq
@@ -59,23 +53,24 @@ module Xlogin
59
53
 
60
54
  begin
61
55
  raise IOError if session&.sock&.closed?
56
+ return session
62
57
  rescue IOError, EOFError, Errno::ECONNABORTED, Errno::ECONNREFUSED, Errno::ECONNRESET
63
58
  destroy(session)
64
59
  return deq
65
60
  end
66
-
67
- session
68
61
  end
69
62
 
70
63
  def enq(session)
71
64
  last_used = Time.now
72
- @queue.enq [session, last_used]
65
+ watch_dog = Thread.new(session){ |s| sleep(@idle * 1.5) && s.close rescue nil }
66
+ @queue.enq [session, last_used, watch_dog]
73
67
  end
74
68
 
69
+ private
75
70
  def destroy(session)
76
71
  @mutex.synchronize do
77
72
  session.close rescue nil
78
- @created -= 1
73
+ @count -= 1
79
74
  end
80
75
  end
81
76
 
@@ -1,3 +1,3 @@
1
1
  module Xlogin
2
- VERSION = "0.14.4"
2
+ VERSION = "0.15.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xlogin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.4
4
+ version: 0.15.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-16 00:00:00.000000000 Z
11
+ date: 2020-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-telnet