xlogin 0.10.6 → 0.10.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76b76cd8edf7d600a84e2a767d4c8b82b9e10f4a4b23005945e2564b0044d3dd
4
- data.tar.gz: 72a4be4e9695eee0e1d2bfd27ac7d1160c2ebb9b13d6fb0a55b7f6485d92dd3b
3
+ metadata.gz: a3f7a65ed48d5aa25928be4d31714657faa282f21fe317908c6da68f61ce9ab9
4
+ data.tar.gz: 4fc355338732d7df48ef787a152171460c334c3e9494c251e3472c4c892af84a
5
5
  SHA512:
6
- metadata.gz: 9c8016bb18c5eb34c88bdaecc837545051764f3d62cda2b50e32de749024a07b9d478aff4d00e78ce6ed2a20a4f4a17405ac3a415a429234caa48ede5871709c
7
- data.tar.gz: 145902d4170b96bb665c4e6513a6e024f55aaf68b71693ff759c7a5b25f40b5cdee5d77fce255aee20f9ec03c8db81eee07ab594d8a8edbbc49f3bfc7ed45be5
6
+ metadata.gz: 13df83043b60975e0e17a505b0568aec8c779ff321ba512fbecf1bc654a80d044a5221ca9b69db048c3f1b6412706da05cc013b9ec5ff8c82fcadbb76988202f
7
+ data.tar.gz: '063955b5da490638837d0bc04f35c867ba4fe4861d56862854a06939f11d56b94afafec329c0bebc339894e649b34ee509b94b95cb07dfe859c2de9d272f48e6'
@@ -15,7 +15,7 @@ module Xlogin
15
15
 
16
16
  login = @methods.fetch(:login)
17
17
  delegate = @methods.fetch(:delegate)
18
- raise TemplateError.new("'login' and 'delegate' must be defined in the #{target_info[:type]} template.") unless login && delegate
18
+ raise TemplateError.new("'login' and 'delegate' methods must be defined in the #{target_info[:type]} template.") unless login && delegate
19
19
 
20
20
  relay_uri = Addressable::URI.parse(uri.to_s)
21
21
  userinfo_cache = relay_uri.userinfo.dup
@@ -1,5 +1,6 @@
1
1
  require 'addressable/uri'
2
2
  require 'singleton'
3
+ require 'xlogin/session_pool'
3
4
  require 'xlogin/template'
4
5
 
5
6
  module Xlogin
@@ -13,9 +14,8 @@ module Xlogin
13
14
  @session_pool = Hash.new
14
15
  end
15
16
 
16
- def set_inventory(**opts)
17
- return unless name = opts[:name]
18
- @inventory[name] = (get_inventory(name) || {}).merge(opts)
17
+ def set_inventory(name, **opts)
18
+ @inventory[name] = (get_inventory(name) || {name: name}).merge(opts)
19
19
  end
20
20
 
21
21
  def get_inventory(name)
@@ -34,14 +34,14 @@ module Xlogin
34
34
  end
35
35
 
36
36
  def set_template(name, text = nil, &block)
37
- template = get_template(name)
37
+ template = get_template(name) || Xlogin::Template.new(name)
38
38
  template.instance_eval(text) if text
39
39
  template.instance_eval(&block) if block
40
40
  @templates[name.to_s.downcase] = template
41
41
  end
42
42
 
43
43
  def get_template(name)
44
- @templates[name.to_s.downcase] ||= Xlogin::Template.new(name)
44
+ @templates[name.to_s.downcase]
45
45
  end
46
46
 
47
47
  def list_templates
@@ -50,6 +50,8 @@ module Xlogin
50
50
 
51
51
  def build(type:, **opts)
52
52
  template = get_template(type)
53
+ raise TemplateError.new("Template not found: '#{type}'") unless template
54
+
53
55
  template.build(uri(opts), **opts)
54
56
  end
55
57
 
@@ -73,15 +75,16 @@ module Xlogin
73
75
 
74
76
  private
75
77
  def uri(**opts)
76
- return opts[:uri].strip if opts.key?(:uri)
77
- raise SessionError.new("Invalid target: '#{opts}'") unless opts[:scheme] && opts[:host]
78
+ return Addressable::URI.parse(opts[:uri].strip) if opts.key?(:uri)
78
79
 
79
80
  scheme = opts[:scheme].strip
80
81
  address = opts.values_at(:host, :port).compact.map(&:strip).join(':')
81
82
  userinfo = opts[:userinfo].strip
82
83
  userinfo ||= opts.values_at(:username, :password).compact.map(&:strip).join(':')
83
84
 
84
- "#{scheme}://" + [userinfo, address].compact.join('@')
85
+ Addressable::URI.parse("#{scheme}://" + [userinfo, address].compact.join('@'))
86
+ rescue
87
+ raise SessionError.new("Invalid target - '#{opts}'")
85
88
  end
86
89
 
87
90
  end
@@ -5,21 +5,18 @@ require 'ostruct'
5
5
  require 'stringio'
6
6
 
7
7
  module Xlogin
8
-
9
8
  class RakeTask < Rake::TaskLib
10
9
 
11
10
  class << self
12
11
  include Rake::DSL
13
12
 
14
- def generate(*target, **opts, &block)
15
- hostnames = target.flat_map { |e| Xlogin.list(e) }.map { |e| e[:name] }
16
-
13
+ def generate(*patterns, **opts, &block)
17
14
  description = Rake.application.last_description
18
- task 'all' => hostnames unless opts[:bundle] == false
15
+ hostnames = Xlogin.list(*patterns).map { |e| e[:name] }
19
16
 
20
- description = opts[:desc]
17
+ task 'all' => hostnames if !!opts[:all]
21
18
  hostnames.each do |hostname|
22
- desc description
19
+ desc "#{description} - #{hostname}" if !!opts[:desc]
23
20
  RakeTask.new(hostname, &block)
24
21
  end
25
22
 
@@ -27,7 +24,6 @@ module Xlogin
27
24
  end
28
25
 
29
26
  attr_reader :name
30
- attr_reader :taskname
31
27
  attr_accessor :lock
32
28
  attr_accessor :log
33
29
  attr_accessor :silent
@@ -37,9 +33,7 @@ module Xlogin
37
33
 
38
34
  def initialize(name)
39
35
  @name = name
40
- @taskname = [*Rake.application.current_scope.to_a.reverse, name].join(':')
41
36
  @runner = nil
42
- @config = OpenStruct.new
43
37
  @silent ||= Rake.application.options.silent
44
38
  @fail_on_error = true
45
39
 
@@ -47,21 +41,17 @@ module Xlogin
47
41
  define
48
42
  end
49
43
 
50
- def run(&block)
51
- @runner = block
44
+ def name_with_scope
45
+ [*Rake.application.current_scope.to_a.reverse, name].join(':')
52
46
  end
53
47
 
54
- def method_missing(name, *args, &block)
55
- super(name, *args, &block) unless name.to_s =~ /^\w+=$/
56
- @config.send(name, *args)
48
+ def run(&block)
49
+ @runner = block
57
50
  end
51
+ alias_method :start, :run
58
52
 
59
53
  private
60
54
  def define
61
- description = Rake.application.last_description
62
- description = "#{description} - #{name}" if description
63
- desc description
64
-
65
55
  mkdir_p(File.dirname(log), verbose: Rake.application.options.trace) if log
66
56
  mkdir_p(File.dirname(lock), verbose: Rake.application.options.trace) if lock
67
57
 
@@ -70,7 +60,6 @@ module Xlogin
70
60
  file(lock) do
71
61
  next if @@graceful_shutdown
72
62
  run_task
73
- mkdir_p(File.dirname(lock), verbose: Rake.application.options.trace)
74
63
  touch(lock, verbose: Rake.application.options.trace)
75
64
  end
76
65
  else
@@ -88,18 +77,16 @@ module Xlogin
88
77
  loggers << buffer if !silent && Rake.application.options.always_multitask
89
78
  loggers << $stdout if !silent && !Rake.application.options.always_multitask
90
79
 
91
- begin
92
- session = Xlogin.get(name, log: loggers, **@config.to_h)
93
- @runner.call(session)
94
- session.close if session
80
+ session = Xlogin.get(name, log: loggers)
81
+ @runner.call(session)
82
+ session.close rescue nil
95
83
 
96
- printf($stdout, buffer.string) if !silent && Rake.application.options.always_multitask
97
- rescue => e
98
- output($stderr, buffer.string) if !silent && Rake.application.options.always_multitask
99
- output($stderr, "[ERROR] Xlogin - #{e}\n")
84
+ printf($stdout, buffer.string) if !silent && Rake.application.options.always_multitask
85
+ rescue => e
86
+ output($stderr, buffer.string) if !silent && Rake.application.options.always_multitask
87
+ output($stderr, "[ERROR] #{e}\n")
100
88
 
101
- @@graceful_shutdown = true if fail_on_error
102
- end
89
+ @@graceful_shutdown = true if fail_on_error
103
90
  end
104
91
 
105
92
  def printf(fp, text)
@@ -112,5 +99,4 @@ module Xlogin
112
99
  end
113
100
 
114
101
  end
115
-
116
102
  end
@@ -21,8 +21,6 @@ module Xlogin
21
21
  when 'telnet' then 23
22
22
  end
23
23
 
24
- raise SessionError.new("Invalid URI - '#{uri}'") unless @host && @port
25
-
26
24
  @name = opts[:name] || @host
27
25
  @config = OpenStruct.new(opts)
28
26
  @template = template
@@ -54,6 +52,11 @@ module Xlogin
54
52
  @template.name
55
53
  end
56
54
 
55
+ def enable(*args)
56
+ return super(*args) unless args.empty?
57
+ super(@config.enable)
58
+ end
59
+
57
60
  def prompt
58
61
  cmd('').to_s.lines.last&.chomp
59
62
  end
@@ -63,17 +66,18 @@ module Xlogin
63
66
  end
64
67
 
65
68
  def puts(*args, &block)
66
- args = [instance_exec(*args, &@template.interrupt)].flatten if @template.interrupt
67
- super(*args, &block)
69
+ args = [instance_exec(*args, &@template.interrupt!)].flatten.compact if @template.interrupt!
70
+ super(*args, &block) unless args.empty?
68
71
  end
69
72
 
70
73
  def waitfor(*args, &block)
74
+ args = [Regexp.union(*@template.prompt.map(&:first))] if args.empty?
71
75
  @mutex.synchronize { _waitfor(*args, &block) }
72
76
  end
73
77
 
74
78
  def close
75
79
  @mutex.synchronize do
76
- @loggers.values.each do |logger|
80
+ @loggers.each do |_, logger|
77
81
  next if logger.nil? || [$stdout, $stderr].include?(logger)
78
82
  logger.close
79
83
  end
@@ -101,10 +105,10 @@ module Xlogin
101
105
 
102
106
  private
103
107
  def _waitfor(*args, &block)
104
- args << Regexp.union(*@template.prompt.map(&:first)) if args.empty?
105
- line = method(:waitfor).super_method.call(*args) do |recv|
106
- block.call(recv) if block
108
+ __waitfor = method(:waitfor).super_method
109
+ line = __waitfor.call(*args) do |recv|
107
110
  output_log(recv)
111
+ block.call(recv) if block
108
112
  end
109
113
 
110
114
  _, process = @template.prompt.find { |r, p| r =~ line && p }
@@ -116,6 +120,10 @@ module Xlogin
116
120
  return line
117
121
  end
118
122
 
123
+ def output_log(text)
124
+ @loggers.each { |_, logger| logger.syswrite(text) if logger }
125
+ end
126
+
119
127
  def ssh_tunnel(gateway)
120
128
  gateway_uri = Addressable::URI.parse(gateway)
121
129
  case gateway_uri.scheme
@@ -133,10 +141,6 @@ module Xlogin
133
141
  end
134
142
  end
135
143
 
136
- def output_log(text)
137
- @loggers.values.each { |logger| logger.syswrite(text) if logger }
138
- end
139
-
140
144
  def build_logger(log)
141
145
  case log
142
146
  when String
@@ -149,5 +153,6 @@ module Xlogin
149
153
  log
150
154
  end
151
155
  end
156
+
152
157
  end
153
158
  end
@@ -2,7 +2,6 @@ require 'time'
2
2
  require 'thread'
3
3
 
4
4
  module Xlogin
5
-
6
5
  class SessionPool
7
6
 
8
7
  DEFAULT_POOL_SIZE = 1
@@ -14,14 +13,8 @@ module Xlogin
14
13
  @args = args
15
14
  @opts = opts
16
15
 
17
- case @args
18
- when String
19
- @size = @opts.delete(:pool_size) || DEFAULT_POOL_SIZE
20
- @idle = @opts.delete(:pool_idle) || DEFAULT_POOL_IDLE
21
- when Hash
22
- @size = @args.delete(:pool_size) || DEFAULT_POOL_SIZE
23
- @idle = @args.delete(:pool_idle) || DEFAULT_POOL_IDLE
24
- end
16
+ @size = DEFAULT_POOL_SIZE
17
+ @idle = DEFAULT_POOL_IDLE
25
18
 
26
19
  @mutex = Mutex.new
27
20
  @queue = Queue.new
@@ -39,7 +32,7 @@ module Xlogin
39
32
  def with
40
33
  session = deq
41
34
  begin
42
- session.prompt
35
+ session.prompt # confirm that the session is still alive
43
36
  rescue IOError, EOFError, Errno::ECONNABORTED, Errno::ECONNREFUSED, Errno::ECONNRESET
44
37
  destroy session
45
38
  session = deq
@@ -94,5 +87,4 @@ module Xlogin
94
87
  end
95
88
 
96
89
  end
97
-
98
90
  end
data/lib/xlogin/spec.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  module Xlogin
2
-
3
2
  class ExpectationError < StandardError
4
3
 
5
4
  def initialize(expect, actual)
@@ -48,5 +47,4 @@ module Xlogin
48
47
  end
49
48
 
50
49
  end
51
-
52
50
  end
@@ -1,4 +1,3 @@
1
- require 'addressable/uri'
2
1
  require 'xlogin/ssh'
3
2
  require 'xlogin/telnet'
4
3
 
@@ -10,7 +9,6 @@ module Xlogin
10
9
  RESERVED_METHODS = %i( login logout enable disable delegate )
11
10
 
12
11
  attr_reader :name
13
- attr_reader :methods
14
12
 
15
13
  def initialize(name)
16
14
  @name = name
@@ -35,20 +33,15 @@ module Xlogin
35
33
  @methods[name] = block
36
34
  end
37
35
 
38
- def interrupt(&block)
36
+ def interrupt!(&block)
39
37
  return @interrupt unless block
40
38
  @interrupt = block
41
39
  end
42
40
 
43
41
  def build(uri, **opts)
44
- uri = Addressable::URI.parse(uri.to_s)
45
42
  klass = Class.new(Xlogin.const_get(uri.scheme.capitalize))
46
43
  klass.class_exec(@methods) do |methods|
47
44
  methods.each do |name, block|
48
- if name == :enable
49
- define_method(name) { |*args| instance_exec([*args, opts[name]].first, &block) }
50
- next
51
- end
52
45
  define_method(name, &block)
53
46
  end
54
47
  end
@@ -57,8 +50,9 @@ module Xlogin
57
50
  end
58
51
 
59
52
  def method_missing(name, *, &block)
60
- super unless RESERVED_METHODS.include?(name) and block_given?
53
+ super unless RESERVED_METHODS.include?(name)
61
54
  bind(name) { |*args| instance_exec(*args, &block) }
62
55
  end
56
+
63
57
  end
64
58
  end
@@ -1,3 +1,3 @@
1
1
  module Xlogin
2
- VERSION = "0.10.6"
2
+ VERSION = "0.10.9"
3
3
  end
data/lib/xlogin.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  require 'xlogin/factory'
4
- require 'xlogin/session_pool'
5
4
  require 'xlogin/version'
6
5
 
7
6
  module Xlogin
@@ -16,20 +15,21 @@ module Xlogin
16
15
  @factory ||= Xlogin::Factory.instance
17
16
  end
18
17
 
18
+ def list(*patterns)
19
+ factory.list_inventory(*patterns)
20
+ end
21
+
19
22
  def get(args, **opts, &block)
20
23
  session = case args
21
24
  when Hash then factory.build(**args.merge(**opts))
22
25
  when String then factory.build_from_hostname(args, **opts)
26
+ else return
23
27
  end
24
28
 
25
29
  return session unless block
26
30
  begin block.call(session) ensure session.close end
27
31
  end
28
32
 
29
- def list(*patterns)
30
- factory.list_inventory(*patterns)
31
- end
32
-
33
33
  def get_pool(args, **opts, &block)
34
34
  pool = factory.build_pool(args, **opts)
35
35
 
@@ -56,10 +56,6 @@ module Xlogin
56
56
  @authorized = boolean == true || (block && block.call == true)
57
57
  end
58
58
 
59
- def register(**args)
60
- factory.set_inventory(**args)
61
- end
62
-
63
59
  def source(*source_files, &block)
64
60
  return source_file(*source_files) unless block
65
61
  instance_eval(&block) unless source_files.empty?
@@ -99,7 +95,7 @@ module Xlogin
99
95
  opts = args[2] || {}
100
96
 
101
97
  super unless args.size == 2 || args.size == 3
102
- register(type: type, name: name, uri: uri, **opts)
98
+ factory.set_inventory(name, type: type, uri: uri, **opts)
103
99
  end
104
100
 
105
101
  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.10.6
4
+ version: 0.10.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-22 00:00:00.000000000 Z
11
+ date: 2019-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-telnet