xlogin 0.16.2 → 0.16.7

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: 342f00cdb5f923c597cf7f40c1d4cf35c8cb7ecb596caa8457c05b09c9b7bd2d
4
- data.tar.gz: 4a7f6f3041a53b942ea434a8417b6b150397fed622e1a2f7a3b7dc0148f87865
3
+ metadata.gz: a8d97fd12ca6f889f47992398783c38f3ebf172823c941696f3c012804440bb2
4
+ data.tar.gz: 80111e038a1302cbcae48b528d74c759021f9820cbb34d5e307ef5cdb19065f2
5
5
  SHA512:
6
- metadata.gz: 7bb500f47f1b95b5f29c3f10083df805a1dd09cb23ce3696719a8bcffe00812cd947b3001743085663e698628c1d7ed78aa1c2db9c77e434791ad2bf32f0acb9
7
- data.tar.gz: abeccd10d1efca609233477172d1926bd3003f05ac4faec3d81b8750c096f7745f0c886fe3801143a52d09afabfcafecbd5c253480ec4c899e0b60bf1d5f7236
6
+ metadata.gz: 7cf57105505b9611989006139c592a6a36577eefdd619b582929d88568f3f2d9baf86d0896a6b2a407c7f411bb1d0e2c517b06f243d49140dafdf8d388c242f6
7
+ data.tar.gz: 6fca3d40be5b12a7cfcebd8e1d19a78f980a5991302b536f0b90de0e3b07cbfaebca8782e2335c817e117a1aeb8b445eee2a95b2c68985e9877c0dbba62ce4a9
data/lib/xlogin.rb CHANGED
@@ -30,22 +30,16 @@ module Xlogin
30
30
  end
31
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)
data/lib/xlogin/cli.rb CHANGED
@@ -42,7 +42,7 @@ module Xlogin
42
42
  loggers << ((jobs > 1)? buffer : $stdout)
43
43
  loggers << File.expand_path(File.join(config[:logdir], "#{info[:name]}.log"), ENV['PWD']) if config[:logdir]
44
44
 
45
- session = Xlogin.get(info.merge(log: loggers))
45
+ session = Xlogin.get(info.merge(log: loggers, **config[:env]))
46
46
  session.enable(session.config.enable) if session.config.enable && Xlogin.settings.enable?
47
47
 
48
48
  command_lines = config[:command].flat_map { |e| e.to_s.split(';').map(&:strip) }
@@ -60,7 +60,7 @@ module Xlogin
60
60
 
61
61
  def run(args)
62
62
  config = Hash.new
63
- config[:env] = []
63
+ config[:env] = {}
64
64
  config[:inventory] = []
65
65
  config[:template] = []
66
66
  config[:command] = []
@@ -70,23 +70,21 @@ module Xlogin
70
70
  parser.banner = "#{File.basename($0)} HOST_PATTERN [Options]"
71
71
  parser.version = Xlogin::VERSION
72
72
 
73
- parser.on('-i PATH', '--inventory', String, 'The PATH to the inventory file.') { |v| config[:inventory] << v }
74
- parser.on('-t PATH', '--template', String, 'The PATH to the template file or directory.'){ |v| config[:template] << v }
75
- parser.on('-L PATH', '--log-dir', String, 'The PATH to the log directory.') { |v| config[:logdir] = v }
76
-
77
- parser.on('-l', '--list', TrueClass, 'List the inventory.') { |v| config[:runner] = self.method(:list) }
73
+ parser.on('-i PATH', '--inventory', String, 'The PATH to the inventory file.') { |v| config[:inventory] << v }
74
+ parser.on('-t PATH', '--template', String, 'The PATH to the template file or directory.'){ |v| config[:template] << v }
75
+ parser.on('-L PATH', '--log-dir', String, 'The PATH to the log directory.') { |v| config[:logdir] = v }
76
+ parser.on('-j NUM', '--jobs', Integer, 'The NUM of jobs to execute in parallel.') { |v| config[:jobs] = v }
77
+ parser.on( '--enable', TrueClass, 'Automatically enable privilege mode.') { |v| config[:enable] = v }
78
+ parser.on('-l', '--list', TrueClass, 'List the inventory.') { |v| config[:runner] = self.method(:list) }
78
79
  parser.on('-e COMMAND', '--exec', String, 'Execute commands and quit.'){ |v| config[:runner] = self.method(:exec); config[:command] << v }
79
-
80
- parser.on('-E KEY=VAL', '--env', /(\w+=\w+)+/, 'Environment variables.') { |v| config[:env] << v }
81
- parser.on('-j NUM', '--jobs', Integer, 'The NUM of jobs to execute in parallel.'){ |v| config[:jobs] = v }
80
+ parser.on('-E KEY=VAL', '--env', /\w+=[^=]+/, 'Environment variables.') { |v| v.split('=').tap{ |k, v| config[:env].update(k.to_sym => v) } }
82
81
 
83
82
  config[:patterns] = parser.parse!(args)
84
83
  config[:inventory] << DEFAULT_INVENTORY if config[:inventory].empty?
85
84
  config[:template] << DEFAULT_TEMPLATE if config[:template].empty?
86
85
 
87
86
  Xlogin.configure do
88
- set Hash[config[:env].map{ |v| v.split('=') }]
89
-
87
+ set enable: config[:enable]
90
88
  source *config[:inventory].map{ |e| File.expand_path(e, ENV['PWD']) }
91
89
  template *config[:template].map { |e| File.expand_path(e, ENV['PWD']) }
92
90
  end
@@ -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,21 +10,21 @@ module Xlogin
10
10
  class << self
11
11
  include Rake::DSL
12
12
 
13
- def all(*patterns, &block)
13
+ def all(*patterns, **opts, &block)
14
14
  description = Rake.application.last_description
15
15
  task all: Xlogin.list(*patterns).map{ |e| e[:name] }
16
16
 
17
17
  desc description
18
- generate(*patterns, &block)
18
+ generate(*patterns, **opts, &block)
19
19
  end
20
20
 
21
- def generate(*patterns, &block)
21
+ def generate(*patterns, **opts, &block)
22
22
  description = Rake.application.last_description
23
23
 
24
24
  hostnames = Xlogin.list(*patterns).map{ |e| e[:name] }
25
25
  hostnames.each do |hostname|
26
26
  desc "#{description} - #{hostname}" if description
27
- RakeTask.new(hostname, &block)
27
+ RakeTask.new(hostname, **opts, &block)
28
28
  end
29
29
  end
30
30
 
@@ -40,14 +40,13 @@ module Xlogin
40
40
  attr_reader :name
41
41
  attr_accessor :log
42
42
  attr_accessor :lock
43
- attr_accessor :timeout
44
43
  attr_accessor :silent
45
44
  attr_accessor :fail_on_error
46
45
 
47
- def initialize(name)
46
+ def initialize(name, **opts)
48
47
  @name = name
48
+ @opts = opts
49
49
  @runner = nil
50
- @timeout = nil
51
50
  @silent ||= Rake.application.options.silent
52
51
  @fail_on_error = true
53
52
 
@@ -66,8 +65,10 @@ module Xlogin
66
65
 
67
66
  private
68
67
  def define
69
- mkdir_p(File.dirname(log), verbose: Rake.application.options.trace) if log
70
- mkdir_p(File.dirname(lock), verbose: Rake.application.options.trace) if lock
68
+ self.log = File.join(Dir.pwd, 'log', name + '.log') if self.log == true
69
+ self.lock = File.join(Dir.pwd, 'lock', name_with_scope) if self.lock == true
70
+ mkdir_p(File.dirname(self.log), verbose: Rake.application.options.trace) if self.log
71
+ mkdir_p(File.dirname(self.lock), verbose: Rake.application.options.trace) if self.lock
71
72
 
72
73
  if lock
73
74
  task(name => lock)
@@ -89,7 +90,7 @@ module Xlogin
89
90
  loggers << log if log
90
91
  loggers << STDOUT if !silent && !Rake.application.options.always_multitask
91
92
 
92
- session = Xlogin.get(name, log: loggers, timeout: timeout)
93
+ session = Xlogin.get(name, log: loggers, **@opts)
93
94
  instance_exec(session, &@runner)
94
95
 
95
96
  print(buffer.string) if !silent && Rake.application.options.always_multitask
@@ -98,27 +99,25 @@ module Xlogin
98
99
  RakeTask.shutdown! if fail_on_error
99
100
 
100
101
  session.log_message(e.to_s.colorize(color: :red)) if session
101
- print(buffer.string + "\n", color: :red) if Rake.application.options.always_multitask
102
+ print(buffer.string.colorize(color: :red) + "\n") if Rake.application.options.always_multitask
102
103
  return false
103
104
  ensure
104
105
  session.close rescue nil
105
106
  end
106
107
 
107
- def puts(text, **opts)
108
- return text.each { |e| puts(e, **opts) } if text.kind_of?(Array)
109
-
110
- text = text.to_s
111
- text = text + "\n" unless text[-1] == "\n"
112
- print(text, **opts)
108
+ def puts(text)
109
+ strio = StringIO.new.tap{ |io| io.puts text }
110
+ print(strio.string)
113
111
  end
114
112
 
115
- def print(text, **opts)
116
- text = text.to_s
113
+ def print(text)
114
+ text = text.to_s.gsub("\r", '')
117
115
  return if text.empty?
118
116
 
119
- text = text.gsub("\r", '')
120
- text = text.lines.map{ |text| "#{name}\t|#{text}" }.join if Rake.application.options.always_multitask
121
- text = text.colorize(**opts)
117
+ if Rake.application.options.always_multitask
118
+ strio = StringIO.new.tap{ |io| io.puts text.lines.map{ |line| "#{name}\t|#{line}" } }
119
+ text = strio.string
120
+ end
122
121
  $stdout.print(text)
123
122
  end
124
123
 
@@ -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] || ENV['USERINFO'] || uri.userinfo).to_s.split(':')
29
29
 
30
30
  begin
31
31
  args = Hash.new
@@ -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)
@@ -1,3 +1,3 @@
1
1
  module Xlogin
2
- VERSION = "0.16.2"
2
+ VERSION = "0.16.7"
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.16.2
4
+ version: 0.16.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-11 00:00:00.000000000 Z
11
+ date: 2021-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-telnet
@@ -193,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
195
  requirements: []
196
- rubygems_version: 3.0.3
196
+ rubygems_version: 3.0.0
197
197
  signing_key:
198
198
  specification_version: 4
199
199
  summary: rancid clogin alternative