xlogin 0.16.0 → 0.16.5

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: aae85b41321af336f469aa967987f88912d22087748c6b646adb699688c0b17f
4
- data.tar.gz: 26dcf9ed1c173bc43d247ab1a1339c8df0586c9734ea0679f5dd5e7fd70bd83a
3
+ metadata.gz: 6ef44033bfbc542654526a3a009b4400b813b3919beeac902aa38b4a68937ed6
4
+ data.tar.gz: 5d71ff7e0f4acd38e0370fec946ed13165615bfc4d955047fb29a8f63296cef9
5
5
  SHA512:
6
- metadata.gz: f7eeeecc2b10c5bb0a6bbca0ba80a7b71611cb04733d59271370641930c391aa604bdd06ca96cfbbfad05b32a745d62869688a3e703330a1742f7bab5df5c59c
7
- data.tar.gz: a27419c164f9387f8633fa7610c7507d5febd8f9ae1d1d3eb77bf2ab6b04a96133c83e40265192927432b92994c02c3c8386b1088882fc5679041aea9d6f9134
6
+ metadata.gz: 47d8c407ed8167173641c237eb07a0874d995b53b96191104961a29628b0514caa5bfc5b1c547afceba3bbf388fd31c64ae301e7e7dd4068ee744ed3825c3fb8
7
+ data.tar.gz: 7a42ce312379c15c7faf1845dbd76dc29c9c1b407bfcdd684ba9563d259f201ef06a3feadab90139c490ca2bdf952efcbd4695cd302b5e8bfb577dba19cbc8e9
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,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)
@@ -91,22 +100,25 @@ module Xlogin
91
100
  RakeTask.shutdown! if fail_on_error
92
101
 
93
102
  session.log_message(e.to_s.colorize(color: :red)) if session
94
- print(buffer.string + "\n", color: :red) if Rake.application.options.always_multitask
103
+ print(buffer.string.colorize(color: :red) + "\n") if Rake.application.options.always_multitask
95
104
  return false
96
105
  ensure
97
106
  session.close rescue nil
98
107
  end
99
108
 
100
- def puts(text, **opt)
101
- print(text + "\n", **opt)
109
+ def puts(text)
110
+ strio = StringIO.new.tap{ |io| io.puts text }
111
+ print(strio.string)
102
112
  end
103
113
 
104
- def print(text, **opt)
114
+ def print(text)
115
+ text = text.to_s.gsub("\r", '')
105
116
  return if text.empty?
106
117
 
107
- text = text.gsub("\r", '')
108
- text = text.lines.map{ |line| "#{name}\t|#{line}" }.join if Rake.application.options.always_multitask
109
- text = text.colorize(**opt)
118
+ if Rake.application.options.always_multitask
119
+ strio = StringIO.new.tap{ |io| io.puts text.lines.map{ |line| "#{name}\t|#{line}" } }
120
+ text = strio.string
121
+ end
110
122
  $stdout.print(text)
111
123
  end
112
124
 
@@ -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.0"
2
+ VERSION = "0.16.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.16.0
4
+ version: 0.16.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-21 00:00:00.000000000 Z
11
+ date: 2021-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-telnet