xlogin 0.14.3 → 0.15.2

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: 2b7da6393c7979c4716280dc7b867aa091ea500b1c94594451681f654429c41d
4
- data.tar.gz: b726972429831ac6169691c974f7bffcf74ff1526d2aa7402537bcfbe423b385
3
+ metadata.gz: 902803f4f718b2af77d3d89721eb0d2f140735b173b1174b58cdfec530502750
4
+ data.tar.gz: 57a6a00bb8912363f8479ce7f2de1a3b86fff6180d242409431320af3021e11f
5
5
  SHA512:
6
- metadata.gz: a65049cdb3360bcb51eceb7c3275ee3db21da38c455bf95fc7d0e20e505efd3b95edac6f5a35fb245b5493df78b736a9c6bc81e5fddfb88a4fc5d349bf496e08
7
- data.tar.gz: d5b37ee384e0c2feb4b330dee8c4ce1e65f1d6b9138a6ecbec7ac5541d225a8d6fc0aa4dd5cd82010b4686dd99398e33aba8e9a16c6673d2cb887740d861f091
6
+ metadata.gz: b2bb034bb516dfe3994dd246d1effdcf6d68dd8cdd5fe49539941b34447b4f8b8eafb69b3467aa92b6fc3e43898355f55a620652855d24fc37ee3b5f754a534e
7
+ data.tar.gz: e1ac9a80f9e8e2473e9def3f6cca08dbe1cbb6daedd664ad63fee765dcd4cb45ab0e5d0c1f82e7e4d5f146928123d9c6a4359331105990c6bafc09f26a7b2c2d
@@ -34,21 +34,19 @@ module Xlogin
34
34
  when Hash then factory.build(**args.merge(**opts))
35
35
  when String then factory.build_from_hostname(args, **opts)
36
36
  else
37
- raise SessionError.new("Invalid argument: '#{args}'")
37
+ raise Xlogin::Error.new("Invalid argument: '#{args}'")
38
38
  end
39
39
 
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)
@@ -91,6 +91,7 @@ module Xlogin
91
91
  template *config[:template].map { |e| File.expand_path(e, ENV['PWD']) }
92
92
  end
93
93
 
94
+ raise Xlogin::Error.new("Invalid host pattern: '#{config[:patterns].join(' ')}'") if Xlogin.list(*config[:patterns]).empty?
94
95
  config[:runner].call(config)
95
96
  rescue => e
96
97
  $stderr.puts e, '', parser
@@ -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
 
@@ -34,9 +34,9 @@ module Xlogin
34
34
  key, val = 'name', key if val.nil?
35
35
  @inventory.values.select{ |e| File.fnmatch(val, e[key.to_sym]) }
36
36
  end
37
- values2.reduce(&:&)
37
+ values2.reduce(&:&) || []
38
38
  end
39
- values1.reduce(&:|)
39
+ values1.reduce(&:|) || []
40
40
  end
41
41
 
42
42
  def set_template(name, text = nil, &block)
@@ -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,11 @@ 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, prefix: "[ERROR]", chomp: true, color: :red) if session
96
+ if Rake.application.options.always_multitasK
97
+ $stderr.print log_text(buffer.string + "\n").colorize(color: :red) unless buffer.string.empty?
98
+ $stderr.print log_text(e.to_s + "\n").colorize(color: :red)
99
+ end
97
100
 
98
101
  return false
99
102
  ensure
@@ -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.3"
2
+ VERSION = "0.15.2"
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.3
4
+ version: 0.15.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-04 00:00:00.000000000 Z
11
+ date: 2020-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-telnet