xlogin 0.16.1 → 0.16.6

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: 2be71cfa9759e1335b4d8fb3e7c4716d0659c16fa9bc029ecfd0f27df345fea9
4
- data.tar.gz: aa0573d7f09586135a5f95bf5c64febe48436d174ec52233604fd1ed182cc9b1
3
+ metadata.gz: f7bc26b26eaaf13f1577ebe322b44a888dd5c83426f81d484372e582bb8a1182
4
+ data.tar.gz: d3bfdb0e28a4857f2411c59567e0a54e70b94dc580f64b6c963b8e439919f85f
5
5
  SHA512:
6
- metadata.gz: 7741a7cdb6aa44f52ebad609ef4ecba0fa8308834ff27f548565ed865045ca73808de07526d669ce9ddc89d174c4dd07513634d45f7b5b4e52a64c3e8804a5dc
7
- data.tar.gz: bbfc0ab5da188614ab3364653bbf15a8ea924cf4504435f02a8433b0a3b97cc7b1dfc4e62742971360bca812bb1fc4406196cb7ba440cd1690badcfbcb1e9f6b
6
+ metadata.gz: 15af0c59968c287bcfb69f422e21de121ae893848b98c6bd6629d33809d4c0939a7a70ed88dd1a49da9e00a02e67bc01cddcce6119ac97396e672a523148a1db
7
+ data.tar.gz: 49794803c350a64a7f70d13223ad736b5950eab2e53ff4c090d2673f5fb179b979040c66a67514c9a86881b0954de93a3fe68d1872241e3ac2c1555ca71afd87
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,18 +10,23 @@ module Xlogin
10
10
  class << self
11
11
  include Rake::DSL
12
12
 
13
- def all(*patterns, &block)
14
- patterns = patterns.empty? ? ENV['target'] : patterns
15
- hostnames = Xlogin.list(*patterns).map{ |e| e[:name] }
13
+ def all(*patterns, **opts, &block)
16
14
  description = Rake.application.last_description
15
+ task all: Xlogin.list(*patterns).map{ |e| e[:name] }
17
16
 
18
- task 'all' => hostnames
17
+ desc description
18
+ generate(*patterns, **opts, &block)
19
+ end
20
+
21
+ def generate(*patterns, **opts, &block)
22
+ description = Rake.application.last_description
23
+
24
+ hostnames = Xlogin.list(*patterns).map{ |e| e[:name] }
19
25
  hostnames.each do |hostname|
20
26
  desc "#{description} - #{hostname}" if description
21
- RakeTask.new(hostname, &block)
27
+ RakeTask.new(hostname, **opts, &block)
22
28
  end
23
29
  end
24
- alias_method :generate, :all
25
30
 
26
31
  def shutdown!
27
32
  @stop = true
@@ -35,14 +40,13 @@ module Xlogin
35
40
  attr_reader :name
36
41
  attr_accessor :log
37
42
  attr_accessor :lock
38
- attr_accessor :timeout
39
43
  attr_accessor :silent
40
44
  attr_accessor :fail_on_error
41
45
 
42
- def initialize(name)
46
+ def initialize(name, **opts)
43
47
  @name = name
48
+ @opts = opts
44
49
  @runner = nil
45
- @timeout = nil
46
50
  @silent ||= Rake.application.options.silent
47
51
  @fail_on_error = true
48
52
 
@@ -61,8 +65,10 @@ module Xlogin
61
65
 
62
66
  private
63
67
  def define
64
- mkdir_p(File.dirname(log), verbose: Rake.application.options.trace) if log
65
- 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
66
72
 
67
73
  if lock
68
74
  task(name => lock)
@@ -84,7 +90,7 @@ module Xlogin
84
90
  loggers << log if log
85
91
  loggers << STDOUT if !silent && !Rake.application.options.always_multitask
86
92
 
87
- session = Xlogin.get(name, log: loggers, timeout: timeout)
93
+ session = Xlogin.get(name, log: loggers, **@opts)
88
94
  instance_exec(session, &@runner)
89
95
 
90
96
  print(buffer.string) if !silent && Rake.application.options.always_multitask
@@ -93,27 +99,25 @@ module Xlogin
93
99
  RakeTask.shutdown! if fail_on_error
94
100
 
95
101
  session.log_message(e.to_s.colorize(color: :red)) if session
96
- 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
97
103
  return false
98
104
  ensure
99
105
  session.close rescue nil
100
106
  end
101
107
 
102
- def puts(text, **opts)
103
- return text.each { |e| puts(e, **opts) } if text.kind_of?(Array)
104
-
105
- text = text.to_s
106
- text = text + "\n" unless text[-1] == "\n"
107
- print(text, **opts)
108
+ def puts(text)
109
+ strio = StringIO.new.tap{ |io| io.puts text }
110
+ print(strio.string)
108
111
  end
109
112
 
110
- def print(text, **opts)
111
- text = text.to_s
113
+ def print(text)
114
+ text = text.to_s.gsub("\r", '')
112
115
  return if text.empty?
113
116
 
114
- text = text.gsub("\r", '')
115
- text = text.lines.map{ |text| "#{name}\t|#{text}" }.join if Rake.application.options.always_multitask
116
- 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
117
121
  $stdout.print(text)
118
122
  end
119
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] || 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.1"
2
+ VERSION = "0.16.6"
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.1
4
+ version: 0.16.6
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-07 00:00:00.000000000 Z
11
+ date: 2021-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-telnet