xlogin 0.15.9 → 0.16.4

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: 1ed377f5d1478cfca33b257304579edbbdbe8cb513d5613ab488698a872d3fe7
4
- data.tar.gz: 5c4d51a3f685d0cc9c5f6f27e61344dc9ec295b15a28374121ed77c94a95b69e
3
+ metadata.gz: d805b70c4f761cbca785e8fa6aeed4af49144e7d7ceff3b84f1d0bcea1f03ea3
4
+ data.tar.gz: fd9c8a12f3ed2cb279ec1ddd9121a4ef286ca13a1835f39ff5e0f078df12d8a5
5
5
  SHA512:
6
- metadata.gz: 0f0cdce3afb728a5686b4d6a287cc98723ca0f2eedb4c0101c1fc64eb5301d933a2dbb97b0de6e7d0f4dab8aaca4f36bcaaa3053acebe80b438d09ed4ed2168a
7
- data.tar.gz: cd0e7896daff69587f61ebe08afcd6aae6e4a952359bf70bf4c87446bd840b450a9e2b93763e6ed16c86d5c84c5348122bcd88c959e3674748ea32a58dc0f45a
6
+ metadata.gz: d0ead74a4a9d12aa71be4f87957d5725719e3dfebae3d5d285fcf781852859f2a579e5d67063944d7e1308ec652ab9c6e942fa19ecbe0b304eb6002fe0b39589
7
+ data.tar.gz: 46817a1959cf69e89d80d0162713f9640de2e679431d472ce5e40454e49ea2c1f6e5efa33e4f165249aa10deda9b082159db06847cb1ce0d3aa02ab0b8b81d53
data/lib/xlogin.rb CHANGED
@@ -21,31 +21,25 @@ module Xlogin
21
21
  end
22
22
 
23
23
  class << self
24
- def list(*patterns)
25
- factory.list_hostinfo(*patterns)
26
- end
27
-
28
24
  def find(*patterns)
29
25
  list(*patterns).first
30
26
  end
31
27
 
28
+ def list(*patterns)
29
+ factory.list_hostinfo(*patterns)
30
+ end
31
+
32
32
  def get(args, **opts, &block)
33
- session = case args
34
- when Hash then factory.build(**args.merge(**opts))
35
- when String then factory.build_from_hostname(args, **opts)
36
- else
37
- raise Xlogin::Error.new("Invalid argument: '#{args}'")
38
- end
39
-
40
- return session unless block
41
- begin block.call(session) ensure session.close end
33
+ case args
34
+ when Hash then factory.build(**args.merge(**opts), &block)
35
+ when String then factory.build_from_hostname(args, **opts, &block)
36
+ else
37
+ raise Xlogin::Error.new("Invalid argument: '#{args}'")
38
+ end
42
39
  end
43
40
 
44
41
  def pool(args, **opts, &block)
45
- pool = factory.build_pool(args, **opts)
46
-
47
- return pool unless block
48
- begin block.call(pool) ensure pool.close end
42
+ factory.build_pool(args, **opts, &block)
49
43
  end
50
44
 
51
45
  def configure(&block)
@@ -26,7 +26,7 @@ module Xlogin
26
26
  end
27
27
 
28
28
  def list_hostinfo(*patterns)
29
- patterns = patterns.flat_map{ |e| e.split(/\s+/) }.compact
29
+ patterns = patterns.compact.flat_map{ |e| e.split(/\s+/) }
30
30
  return @inventory.values if patterns.empty?
31
31
 
32
32
  values1 = patterns.map do |pattern|
@@ -93,22 +93,26 @@ module Xlogin
93
93
  end
94
94
  end
95
95
 
96
- def build(type:, **opts)
96
+ def build(type:, **opts, &block)
97
97
  template = get_template(type)
98
98
  raise Xlogin::Error.new("Template not found: '#{type}'") unless template
99
99
 
100
- template.build(uri(opts), **opts)
100
+ session = template.build(uri(opts), **opts)
101
+ return session unless block
102
+ begin block.call(session) ensure session.close end
101
103
  end
102
104
 
103
- def build_pool(args, **opts)
104
- Xlogin::SessionPool.new(args, **opts)
105
+ def build_pool(args, **opts, &block)
106
+ pool = Xlogin::SessionPool.new(args, **opts)
107
+ return pool unless block
108
+ begin block.call(pool) ensure pool.close end
105
109
  end
106
110
 
107
- def build_from_hostname(args, **opts)
111
+ def build_from_hostname(args, **opts, &block)
108
112
  hostinfo = get_hostinfo(args)
109
113
  raise Xlogin::Error.new("Host not found: '#{args}'") unless hostinfo
110
114
 
111
- build(**hostinfo.merge(**opts))
115
+ build(**hostinfo.merge(**opts), &block)
112
116
  end
113
117
 
114
118
  def method_missing(method_name, *args, **opts, &block)
@@ -10,13 +10,20 @@ module Xlogin
10
10
  class << self
11
11
  include Rake::DSL
12
12
 
13
- def generate(*patterns, **opts, &block)
13
+ def all(*patterns, &block)
14
14
  description = Rake.application.last_description
15
- hostnames = Xlogin.list(*patterns).map{ |e| e[:name] }
15
+ task all: Xlogin.list(*patterns).map{ |e| e[:name] }
16
16
 
17
- task 'all' => hostnames unless opts[:all] == false
17
+ desc description
18
+ generate(*patterns, &block)
19
+ end
20
+
21
+ def generate(*patterns, &block)
22
+ description = Rake.application.last_description
23
+
24
+ hostnames = Xlogin.list(*patterns).map{ |e| e[:name] }
18
25
  hostnames.each do |hostname|
19
- desc "#{description} with #{hostname}"
26
+ desc "#{description} - #{hostname}" if description
20
27
  RakeTask.new(hostname, &block)
21
28
  end
22
29
  end
@@ -59,8 +66,10 @@ module Xlogin
59
66
 
60
67
  private
61
68
  def define
62
- mkdir_p(File.dirname(log), verbose: Rake.application.options.trace) if log
63
- mkdir_p(File.dirname(lock), verbose: Rake.application.options.trace) if lock
69
+ self.log = File.join(Dir.pwd, 'log', name + '.log') if self.log == true
70
+ self.lock = File.join(Dir.pwd, 'lock', name_with_scope) if self.lock == true
71
+ mkdir_p(File.dirname(self.log), verbose: Rake.application.options.trace) if self.log
72
+ mkdir_p(File.dirname(self.lock), verbose: Rake.application.options.trace) if self.lock
64
73
 
65
74
  if lock
66
75
  task(name => lock)
@@ -78,41 +87,41 @@ module Xlogin
78
87
 
79
88
  def run_task
80
89
  buffer = StringIO.new
81
- loggers = []
82
- loggers << log if log
83
- loggers << $stdout if !silent && !Rake.application.options.always_multitask
84
- loggers << buffer
90
+ loggers = [buffer]
91
+ loggers << log if log
92
+ loggers << STDOUT if !silent && !Rake.application.options.always_multitask
85
93
 
86
94
  session = Xlogin.get(name, log: loggers, timeout: timeout)
95
+ instance_exec(session, &@runner)
87
96
 
88
- @runner.call(session)
89
- $stdout.print log_text(buffer.string) if !silent && Rake.application.options.always_multitask
90
-
97
+ print(buffer.string) if !silent && Rake.application.options.always_multitask
91
98
  return true
92
99
  rescue => e
93
100
  RakeTask.shutdown! if fail_on_error
94
101
 
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
99
-
102
+ session.log_message(e.to_s.colorize(color: :red)) if session
103
+ print(buffer.string + "\n", color: :red) if Rake.application.options.always_multitask
100
104
  return false
101
105
  ensure
102
106
  session.close rescue nil
103
107
  end
104
108
 
105
- def log_text(text)
106
- text.lines.map{ |line| "#{Time.now.iso8601} #{name}\t|#{line.gsub(/^.*\r/, '')}" }.join
107
- end
109
+ def puts(text, **opts)
110
+ return text.each { |e| puts(e, **opts) } if text.kind_of?(Array)
108
111
 
109
- end
112
+ text = text.to_s
113
+ text = text + "\n" unless text[-1] == "\n"
114
+ print(text, **opts)
115
+ end
110
116
 
111
- module SessionModule
117
+ def print(text, **opts)
118
+ text = text.to_s
119
+ return if text.empty?
112
120
 
113
- def comment(line, lf: true, **color)
114
- write_log(line.chomp.colorize({color: :light_white}.merge(**color)))
115
- cmd('') if lf
121
+ text = text.gsub("\r", '')
122
+ text = text.lines.map{ |text| "#{name}\t|#{text}" }.join if Rake.application.options.always_multitask
123
+ text = text.colorize(**opts)
124
+ $stdout.print(text)
116
125
  end
117
126
 
118
127
  end
@@ -10,9 +10,9 @@ module Xlogin
10
10
 
11
11
  def initialize(template, uri, **opts)
12
12
  @uri = uri
13
- @host = uri.host
14
- @port = uri.port
15
- @port ||= case @uri.scheme
13
+ @host = opts[:host] || uri.host
14
+ @port = opts[:port] || uri.port
15
+ @port ||= case opts[:scheme] || uri.scheme
16
16
  when 'ssh' then 22
17
17
  when 'telnet' then 23
18
18
  end
@@ -25,7 +25,7 @@ module Xlogin
25
25
  @host, @port = Xlogin.factory.open_tunnel(@tunnel, @host, @port) if @tunnel
26
26
 
27
27
  num_try = 0
28
- username, password = uri.userinfo.to_s.split(':')
28
+ username, password = (opts[:userinfo] || uri.userinfo).to_s.split(':')
29
29
 
30
30
  begin
31
31
  args = Hash.new
@@ -45,7 +45,7 @@ module Xlogin
45
45
  rescue => e
46
46
  unless (num_try += 1) > (@config.retry || 0)
47
47
  sleep 2.0 ** (num_try)
48
- retry
48
+ retry
49
49
  end
50
50
  raise e
51
51
  end
@@ -64,11 +64,9 @@ module Xlogin
64
64
  Regexp.union(*@template.prompts.map(&:first))
65
65
  end
66
66
 
67
- def duplicate(type: @template.name, **args)
68
- template = Xlogin::Factory.instance.get_template(type)
69
- raise Xlogin::Error.new("Template not found: '#{type}'") unless template
70
-
71
- template.build(@uri, **@config.to_h.merge(args))
67
+ def duplicate(type: @template.name, uri: @uri.dup, **args, &block)
68
+ args = @config.to_h.merge(args)
69
+ Xlogin::Factory.instance.build(type: type, uri: uri, **args, &block)
72
70
  end
73
71
 
74
72
  def puts(string = '', &block)
@@ -84,7 +82,7 @@ module Xlogin
84
82
  return waitfor(prompt_pattern) if args.empty?
85
83
 
86
84
  line = super(*args) do |recv|
87
- write_log(recv)
85
+ log_message(recv)
88
86
  block.call(recv) if block
89
87
  end
90
88
 
@@ -123,11 +121,11 @@ module Xlogin
123
121
  end
124
122
  end
125
123
 
126
- private
127
- def write_log(text)
124
+ def log_message(text)
128
125
  @loggers.each{ |_, logger| logger.syswrite(text) if logger }
129
126
  end
130
127
 
128
+ private
131
129
  def build_log(log)
132
130
  case log
133
131
  when String
data/lib/xlogin/telnet.rb CHANGED
@@ -38,7 +38,7 @@ module Xlogin
38
38
  @sock.syswrite(bs)
39
39
  when @sock
40
40
  begin
41
- write_log(fh.readpartial(1024))
41
+ log_message(fh.readpartial(1024))
42
42
  rescue Errno::EAGAIN
43
43
  retry
44
44
  end
@@ -1,3 +1,3 @@
1
1
  module Xlogin
2
- VERSION = "0.15.9"
2
+ VERSION = "0.16.4"
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.15.9
4
+ version: 0.16.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-28 00:00:00.000000000 Z
11
+ date: 2021-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-telnet