xlogin 0.3.0 → 0.3.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
  SHA1:
3
- metadata.gz: 236da0f51f5506ec90714afb37232998575b0fe0
4
- data.tar.gz: 3907742fb721d176716f845f1575f09edae86f73
3
+ metadata.gz: 2adfeee117c2c81c467f4e38db77d70af3f80df7
4
+ data.tar.gz: 006e04c87d2f37ae00341934fe6979d1a75de08c
5
5
  SHA512:
6
- metadata.gz: 143ba3dd1a9f1f9a8dc2d9a8d9fe5121a1d8421d22117b56c6e3a83ed283aa55e80cf91a3f2c02785e8b1ca281bf183e2627a90b4d5ea99f7466ff278b14aa5b
7
- data.tar.gz: 1193fa2db3d0876788e33856d59b4f74cba71400de2d5f479a476ce6453e5226081158a53204f5ff5d4888dcef2ff67113f28c747e63d0f67097263ff1799f20
6
+ metadata.gz: c8ce6423265a437e802910751df6e5c5c26087537ba29dfad5f9601d0ca2dd3b9f2af62ad88e03850e04ed7c8c5c2e19283f952dc2c69dce64ab075025da2e83
7
+ data.tar.gz: bb2ae2504a79215f90c766dbf16349f7141a5fd0b05e7627b25ff594beb58a3a1170022b731c968bbf2207bae847ca54ff800de4cd4e020fee8c9af3a8a9a9a4
@@ -31,5 +31,5 @@ begin
31
31
 
32
32
  end
33
33
  rescue LoadError
34
- $stderr.puts "Option 'gateway' is not supported in your environment."
34
+ $stderr.puts "WARN: 'gateway' option is not supported in your environment."
35
35
  end
@@ -0,0 +1,88 @@
1
+ require 'xlogin'
2
+ require 'xlogin/thread_safe'
3
+
4
+ module Xlogin
5
+
6
+ class Scanner
7
+ class << self
8
+ def login_opts(**opts)
9
+ @login_opts = opts unless opts.empty?
10
+ @login_opts || {}
11
+ end
12
+ end
13
+
14
+ def initialize
15
+ @sessions = Hash.new
16
+ @scan_db = Hash.new
17
+ @queries = Array.new
18
+ end
19
+
20
+ def ids
21
+ @scan_db.keys
22
+ end
23
+
24
+ def sessions(&block)
25
+ hostnames = @queries.map { |_, hostname, _| hostname }.uniq
26
+ sessions = hostnames.map { |hostname| session(hostname) }
27
+ sessions.each { |s| block.call(s) } if block
28
+ sessions
29
+ end
30
+
31
+ def define(id, &block)
32
+ type = ScanType.new
33
+ type.instance_eval(&block)
34
+
35
+ @scan_db[id] = type
36
+ end
37
+
38
+ def add(id, hostname, *args)
39
+ @queries << [id, hostname, *args]
40
+ end
41
+
42
+ def scan
43
+ data = Hash.new
44
+ cache = Hash.new
45
+
46
+ threads = @queries.map do |req_key|
47
+ Thread.new do
48
+ id, hostname, *args = *req_key
49
+ type = @scan_db[id]
50
+
51
+ command = type.command.call(*args)
52
+ content = session(hostname).thread_safe { |s| s.cmd(command) }
53
+ matched = type.scanner.call(content)
54
+
55
+ cache[[hostname, command]] ||= content
56
+ data[req_key] = matched
57
+
58
+ yield(req_key, matched) if block_given?
59
+ end
60
+ end
61
+ threads.each { |th| th.join }
62
+
63
+ data
64
+ end
65
+
66
+ def close
67
+ sessions { |s| s.close }
68
+ end
69
+
70
+ private
71
+ def session(hostname)
72
+ @sessions[hostname] ||= Xlogin.get(hostname, **Xlogin::Scanner.login_opts)
73
+ end
74
+ end
75
+
76
+ class ScanType
77
+ def command(val = nil, &block)
78
+ return @command unless val || block
79
+ @command = (val) ? lambda { val } : block
80
+ end
81
+
82
+ def scanner(&block)
83
+ return @scanner unless block
84
+ @scanner = block
85
+ end
86
+ end
87
+
88
+ end
@@ -8,15 +8,15 @@ module Xlogin
8
8
 
9
9
  def configure_session(**opts)
10
10
  @opts = opts.dup
11
- @host = @name = @opts.delete(:host)
12
- @port = @opts.delete(:port)
13
- @userinfo = @opts.delete(:userinfo).to_s.split(':')
11
+ @host = @name = @opts[:host]
12
+ @port = @opts[:port]
13
+ @userinfo = @opts[:userinfo].to_s.split(':')
14
14
  raise Xlogin::GeneralError.new('Argument error.') unless @host && @port
15
15
 
16
- @prompts = @opts.delete(:prompts) || [[/[$%#>] ?\z/n, nil]]
17
- @timeout = @opts.delete(:timeout) || 60
16
+ @prompts = @opts[:prompts] || [[/[$%#>] ?\z/n, nil]]
17
+ @timeout = @opts[:timeout] || 60
18
18
 
19
- @loglist = [@opts.delete(:log)].flatten.compact
19
+ @loglist = [@opts[:log]].flatten.compact
20
20
  @logger = update_logger
21
21
  end
22
22
 
@@ -0,0 +1,25 @@
1
+ require 'thread'
2
+ require 'timeout'
3
+
4
+ module Xlogin
5
+ module Session
6
+ def thread_safe(timeout: @timeout, maximum_retry: 1)
7
+ @safe_session = self
8
+ @safe_session_mutex ||= Mutex.new
9
+
10
+ Timeout.timeout(timeout) do
11
+ @safe_session_mutex.synchronize do
12
+ retry_count = 0
13
+ begin
14
+ @safe_session ||= Xlogin.get(@host, @opts)
15
+ yield @safe_session
16
+ rescue Errno::ECONNRESET => e
17
+ raise e unless (retry_count += 1) < maximum_retry
18
+ @safe_session = nil
19
+ retry
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Xlogin
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.3"
3
3
  end
data/lib/xlogin.rb CHANGED
@@ -2,7 +2,6 @@ $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  require 'xlogin/firmware'
4
4
  require 'xlogin/firmware_factory'
5
- require 'xlogin/queue'
6
5
  require 'xlogin/version'
7
6
 
8
7
  module Xlogin
@@ -62,4 +61,8 @@ module Xlogin
62
61
  end
63
62
  end
64
63
 
64
+ # do not remove this line!
65
+ # initialize Xlogin systems and load related modules beforehand.
66
+ Xlogin.factory
67
+
65
68
  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.3.0
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-04 00:00:00.000000000 Z
11
+ date: 2017-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,14 +75,15 @@ files:
75
75
  - lib/xlogin/firmware_factory.rb
76
76
  - lib/xlogin/firmwares/vyos.rb
77
77
  - lib/xlogin/gateway.rb
78
- - lib/xlogin/queue.rb
79
78
  - lib/xlogin/rake_task.rb
80
79
  - lib/xlogin/rspec.rb
81
80
  - lib/xlogin/rspec/context.rb
82
81
  - lib/xlogin/rspec/resource.rb
82
+ - lib/xlogin/scanner.rb
83
83
  - lib/xlogin/session.rb
84
84
  - lib/xlogin/ssh.rb
85
85
  - lib/xlogin/telnet.rb
86
+ - lib/xlogin/thread_safe.rb
86
87
  - lib/xlogin/version.rb
87
88
  - xlogin.gemspec
88
89
  homepage: https://github.com/haccht/xlogin
data/lib/xlogin/queue.rb DELETED
@@ -1,55 +0,0 @@
1
- require 'timeout'
2
- require 'thread'
3
-
4
- module Xlogin
5
- class Queue
6
-
7
- class << self
8
- def get(hostname, **args)
9
- @queues ||= {}
10
- @queues[hostname] ||= Xlogin::Queue.new(hostname, **args)
11
- end
12
- end
13
-
14
- def initialize(hostname, **args)
15
- @name = hostname
16
- @args = args
17
- @mutex = Mutex.new
18
-
19
- if hostinfo = Xlogin.factory.get(hostname)
20
- firmware = Xlogin.factory.template_for(hostinfo[:type])
21
- @timeout = firmware.timeout
22
- end
23
-
24
- @timeout = @args['Timeout'] || @args['timeout' || @args[:timeout]] || @timeout || 10
25
- end
26
-
27
- def with(timeout: @timeout, limit: 1)
28
- Timeout.timeout(timeout) do
29
- @mutex.synchronize do
30
- retry_count = 0
31
-
32
- begin
33
- @session ||= Xlogin.get(@name, **@args)
34
- @session.cmd('')
35
- rescue Errno::ECONNRESET => e
36
- raise e unless (retry_count += 1) < limit
37
- @session = nil
38
- retry
39
- end
40
-
41
- yield @session
42
- end
43
- end
44
- end
45
-
46
- def shutdown(**args)
47
- with(args) do |s|
48
- begin
49
- yield s ensure s.close if s
50
- end
51
- end
52
- end
53
-
54
- end
55
- end