xlogin 0.2.3 → 0.2.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: 11817df618eb661d0dc3e58f5674467709e0475c
4
- data.tar.gz: 9a729fe08da4e9038b1a6ef62c13e176a4bc05dc
3
+ metadata.gz: ac8f3ae4ad7a17da2c37a40f5c6e0a32545501be
4
+ data.tar.gz: 88dc898abf314811283b50ee0d6b8d62dc31c641
5
5
  SHA512:
6
- metadata.gz: 1dbe170e2ea3046890fd5ade698114ba18c6f6a861b67b19a6031ff610971c56e125c6c7ddd9fb4b518e82f3156b3eeff4d661847ba9d9a62849c3ea1020a30d
7
- data.tar.gz: abaeeb95993ed797b2e61669d7fd9fb9596dea2de2444eebb8eb3902d9a9d4a3069d7b03cba3d21eb95e145adddc914ff174f9b2e77bad6de4a6494b7dad281b
6
+ metadata.gz: 7813c69c44723ef01af12d89f8b2970aca9a4215676fd30adcb7bd304d5e13a7c175c714e821e08d51d7fb88b487a5ffcf95095c94507b0f2360a0a6d56ff671
7
+ data.tar.gz: 00fa87b310643a1de8986586bc5889ad75caeaf8065d226996a41721a58ef81953a2310c7bae37bf3c765ce2a5b8331ed194a453958d8402dc2ce8fbf326b735
data/.gitignore CHANGED
@@ -54,5 +54,7 @@ build-iPhoneSimulator/
54
54
  .rvmrc
55
55
 
56
56
  # End of https://www.gitignore.io/api/ruby
57
+ /lib/xlogin/.xloginrc
58
+ /lib/xlogin/_xloginrc
57
59
  /lib/xlogin/firmwares/*
58
60
  !/lib/xlogin/firmwares/vyos.rb
data/lib/xlogin.rb CHANGED
@@ -2,6 +2,7 @@ $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  require 'xlogin/firmware'
4
4
  require 'xlogin/firmware_factory'
5
+ require 'xlogin/queue'
5
6
  require 'xlogin/version'
6
7
 
7
8
  module Xlogin
@@ -18,6 +19,7 @@ module Xlogin
18
19
 
19
20
  class << self
20
21
  def configure(name)
22
+ name = name.to_s
21
23
  firmware = Xlogin::FirmwareFactory[name] || Xlogin::Firmware.new
22
24
  yield firmware if block_given?
23
25
 
@@ -12,8 +12,8 @@ module Xlogin
12
12
  target_os = FirmwareFactory[target[:type]]
13
13
  target_uri = URI(target[:uri])
14
14
 
15
- login = @methods[:login]
16
- delegate = @methods[:delegate]
15
+ login = @methods.delete(:login)
16
+ delegate = @methods.delete(:delegate)
17
17
 
18
18
  userinfo = uri.userinfo.dup
19
19
  uri.userinfo = ''
@@ -50,7 +50,7 @@ module Xlogin
50
50
  # begin
51
51
  # waittime = 3
52
52
  # Timeout.timeout(waittime) do
53
- # login(*uri.userinfo.split(':'), opts)
53
+ # login(*uri.userinfo.split(':'))
54
54
  # end
55
55
  # rescue Timeout::Error
56
56
  # end
@@ -50,8 +50,9 @@ module Xlogin
50
50
  @database[name]
51
51
  end
52
52
 
53
- def set(type, name, uri, opts = {})
54
- @database[name] = opts.merge(uri: uri, type: type, name: name)
53
+ def set(**opts)
54
+ opts[:type] = opts[:type].to_s
55
+ @database[opts[:name]] = opts
55
56
  end
56
57
 
57
58
  def list
@@ -60,7 +61,7 @@ module Xlogin
60
61
 
61
62
  def build(args)
62
63
  uri = args.delete(:uri)
63
- type = args.delete(:type)
64
+ type = args.delete(:type).to_s
64
65
  name = args.delete(:name)
65
66
  opts = args.reduce({}) { |a, (k, v)| a.merge(k.to_s.downcase.to_sym => v) }
66
67
  raise Xlogin::GeneralError.new("Host not found: #{args}") unless uri && type
@@ -72,6 +73,9 @@ module Xlogin
72
73
  end
73
74
 
74
75
  def build_from_hostname(hostname, **args)
76
+ host = get(hostname)
77
+ raise Xlogin::GeneralError.new("Host not found: #{hostname}") unless host
78
+
75
79
  build(get(hostname).merge(args))
76
80
  end
77
81
 
@@ -79,7 +83,11 @@ module Xlogin
79
83
  firmware = Xlogin::FirmwareFactory[name]
80
84
  super unless firmware && args.size >= 2
81
85
 
82
- set(name, *args)
86
+ type = name
87
+ name = args.shift
88
+ uri = args.shift
89
+ opts = args.shift || {}
90
+ set(type: type, name: name, uri: uri, **opts)
83
91
  end
84
92
 
85
93
  end
@@ -1,31 +1,35 @@
1
- require 'uri'
2
- require 'net/ssh/gateway'
1
+ begin
2
+ require 'uri'
3
+ require 'net/ssh/gateway'
3
4
 
4
- module Xlogin
5
- module Session
5
+ module Xlogin
6
+ module Session
6
7
 
7
- alias_method :original_configure_session, :configure_session
8
- def configure_session(**opts)
9
- original_configure_session(**opts)
8
+ alias_method :original_configure_session, :configure_session
9
+ def configure_session(**opts)
10
+ original_configure_session(**opts)
10
11
 
11
- if uri = opts[:via]
12
- gateway = URI(uri)
13
- username, password = *gateway.userinfo.split(':')
12
+ if uri = opts[:via]
13
+ gateway = URI(uri)
14
+ username, password = *gateway.userinfo.split(':')
14
15
 
15
- case gateway.scheme
16
- when 'ssh'
17
- @gateway = Net::SSH::Gateway.new(
18
- gateway.host,
19
- username,
20
- password: password,
21
- port: gateway.port || 22
22
- )
16
+ case gateway.scheme
17
+ when 'ssh'
18
+ @gateway = Net::SSH::Gateway.new(
19
+ gateway.host,
20
+ username,
21
+ password: password,
22
+ port: gateway.port || 22
23
+ )
23
24
 
24
- @port = @gateway.open(@host, @port)
25
- @host = '127.0.0.1'
25
+ @port = @gateway.open(@host, @port)
26
+ @host = '127.0.0.1'
27
+ end
26
28
  end
27
29
  end
28
30
  end
29
- end
30
31
 
32
+ end
33
+ rescue LoadError
34
+ $stderr.puts "Option 'gateway' is not supported in your environment."
31
35
  end
@@ -0,0 +1,56 @@
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
+ hostinfo = Xlogin::FirmwareFactory.new.get(hostname)
20
+ if hostinfo
21
+ firmware = Xlogin::FirmwareFactory[hostinfo[:type]]
22
+ @timeout = firmware.timeout
23
+ end
24
+
25
+ @timeout = @args['Timeout'] || @args['timeout' || @args[:timeout]] || @timeout || 10
26
+ end
27
+
28
+ def with(timeout: @timeout, limit: 1)
29
+ Timeout.timeout(timeout) do
30
+ @mutex.synchronize do
31
+ retry_count = 0
32
+
33
+ begin
34
+ @session ||= Xlogin.get(@name, **@args)
35
+ @session.cmd('')
36
+ rescue Errno::ECONNRESET => e
37
+ raise e unless (retry_count += 1) < limit
38
+ @session = nil
39
+ retry
40
+ end
41
+
42
+ yield @session
43
+ end
44
+ end
45
+ end
46
+
47
+ def shutdown(**args)
48
+ with(args) do |s|
49
+ begin
50
+ yield s ensure s.close if s
51
+ end
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -9,7 +9,7 @@ module Xlogin
9
9
  class << self
10
10
  include Rake::DSL
11
11
 
12
- def bulk(names, &block)
12
+ def bulk(names, **xlogin_opts, &block)
13
13
  names = names.map(&:strip).grep(/^\s*[^#]/)
14
14
 
15
15
  namecount = names.uniq.inject({}) { |a, e| a.merge(e => names.count(e)) }
@@ -21,7 +21,7 @@ module Xlogin
21
21
 
22
22
  names.each do |name|
23
23
  desc "#{description} (#{name})"
24
- RakeTask.new(name, &block)
24
+ RakeTask.new(name, **xlogin_opts, &block)
25
25
  end
26
26
  end
27
27
  end
@@ -50,17 +50,16 @@ module Xlogin
50
50
  attr_accessor :silent
51
51
  attr_accessor :lockfile
52
52
  attr_accessor :logfile
53
- attr_accessor :timeout
54
53
  attr_accessor :uncomment
55
54
 
56
- def initialize(name, *args)
55
+ def initialize(name, **xlogin_opts)
57
56
  @name = name
58
57
  @fail_on_error = true
59
58
  @silent = Rake.application.options.silent
60
59
  @lockfile = nil
61
60
  @logfile = nil
62
- @timeout = nil
63
61
  @uncomment = false
62
+ @xlogin_opts = xlogin_opts
64
63
 
65
64
  @session = nil
66
65
  @taskrunner = nil
@@ -117,12 +116,10 @@ module Xlogin
117
116
  loggers << logfile if logfile
118
117
  end
119
118
 
120
- xlogin_opts = Hash.new
121
- xlogin_opts[:log] = loggers unless loggers.empty?
122
- xlogin_opts[:timeout] = timeout if timeout
119
+ @xlogin_opts[:log] = loggers unless loggers.empty?
123
120
 
124
121
  begin
125
- @session = Xlogin.get(name, xlogin_opts)
122
+ @session = Xlogin.get(name, @xlogin_opts.dup)
126
123
  @session.extend(SessionExt)
127
124
 
128
125
  method_proc = method(:safe_puts)
data/lib/xlogin/telnet.rb CHANGED
@@ -66,7 +66,7 @@ module Xlogin
66
66
  end
67
67
  end
68
68
  end
69
- rescue EOFError
69
+ rescue EOFError, Errno::ECONNRESET
70
70
  $stdout.puts "\r\n", "Conneciton closed."
71
71
  self.close
72
72
  ensure
@@ -1,3 +1,3 @@
1
1
  module Xlogin
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.5"
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.2.3
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-22 00:00:00.000000000 Z
11
+ date: 2017-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,6 +75,7 @@ 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
78
79
  - lib/xlogin/rake_task.rb
79
80
  - lib/xlogin/rspec.rb
80
81
  - lib/xlogin/rspec/context.rb