xlogin 0.11.3 → 0.11.4

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: dc36e4fecf3ad53794e4a273ab58d6ab9fbaa8316c3ba196d78dffec1083ae82
4
- data.tar.gz: 42baf158104fc805c1296c21377b0df9ed7dab6c51dd58a59409949ace19dad7
3
+ metadata.gz: 2ee087d494b733693bedb67a2c81daa6e0f25dd95557712898efd03bc5486dfd
4
+ data.tar.gz: 7efa71f1ef4e3a09964b986b77c334e44664586df26cf29f561a665340dbf9d1
5
5
  SHA512:
6
- metadata.gz: 82cbc0b9f451a51186e47f49ccccc1e6ba1dd380c983518551b2b511e3bca6a1b03d8d34149c59ba1174e9dd8e7d71da08dd279f1a780d4836a9b315464a8d09
7
- data.tar.gz: b1c88a23572bccc851a13e9a22bb228fd7fe6c0889688c71447e07fee85f4f7fe600df953fbd43c7094df36036a8e6c36c3664a3377f61b592d417167b5cf013
6
+ metadata.gz: 21443be5b7f07179fd92565c4d4092137dc4b7a650fac098f9077a3644ef2d5876362f7b7325564899b8f74acbf13d5b387ac8197dc553380949b3708dd2ccf4
7
+ data.tar.gz: 6dfce221c5e716d33b1c9b3fec9a9b1443b0d2e06f5e1b5dc71f64dadd373c6c2e32281d96bdca2ccee051e95bd8aa700d2daf695e0e4507b1120097885fbe3b
data/lib/xlogin/cli.rb CHANGED
@@ -7,118 +7,118 @@ require 'readline'
7
7
  require 'stringio'
8
8
 
9
9
  module Xlogin
10
- class CLI
11
-
12
- DEFAULT_INVENTORY_FILE = File.join(ENV['HOME'], '.xloginrc')
13
- DEFAULT_TEMPLATE_DIR = File.join(ENV['HOME'], '.xlogin.d')
14
-
15
- def self.run(args = ARGV)
16
- config = getopts(args)
17
- client = Xlogin::CLI.new
18
- client.method(config.task.first).call(config)
19
- end
20
-
21
- def self.getopts(args)
22
- config = OpenStruct.new(
23
- jobs: 1,
24
- auth: false,
25
- task: [:tty, nil],
26
- inventory: DEFAULT_INVENTORY_FILE,
27
- template_dir: DEFAULT_TEMPLATE_DIR,
28
- )
29
-
30
- parser = OptionParser.new
31
- parser.banner = "#{File.basename($0)} HOST_PATTERN [Options]"
32
- parser.version = Xlogin::VERSION
33
-
34
- parser.on('-i PATH', '--inventory', String, 'The PATH to the inventory file (default: $HOME/.xloginrc).') { |v| config.inventory = v }
35
- parser.on('-T PATH', '--template', String, 'The PATH to the template dir (default: $HOME/.xlogin.d).') { |v| config.template_dir = v }
36
- parser.on('-L [DIRECTORY]', '--log-dir', String, 'The PATH to the log dir (default: $PWD).') { |v| config.logdir = v || '.' }
37
-
38
- parser.on('-l', '--list', TrueClass, 'List the inventory.') { |v| config.task = [:list, nil] }
39
- parser.on('-t', '--tty', TrueClass, 'Allocate a pseudo-tty.') { |v| config.task = [:tty, nil] }
40
- parser.on('-e COMMAND', '--exec', 'Execute commands and quit.') { |v| config.task = [:exec, v] }
41
-
42
- parser.on('-j NUM', '--jobs', Integer, 'The NUM of jobs to execute in parallel(default: 1).') { |v| config.jobs = v }
43
- parser.on('-y', '--assume-yes', TrueClass, 'Automatically answer yes to prompts.') { |v| config.auth = v }
44
- parser.on('-E', '--enable', TrueClass, 'Try to gain enable priviledge.') { |v| config.enable = v }
45
-
46
- parser.parse!(args)
47
- Xlogin.configure do
48
- authorize(config.auth)
49
- source(File.expand_path(config.inventory, ENV['PWD']))
50
- template_dir(File.expand_path(config.template_dir, ENV['PWD']))
51
- end
52
-
53
- config.hosts = Xlogin.list(*args)
54
- raise "No host found: `#{args.join(', ')}`" if config.hosts.empty?
55
-
56
- return config
57
- rescue => e
58
- $stderr.puts e, '', parser
59
- exit 1
60
- end
61
-
62
- def list(config)
63
- $stdout.puts config.hosts.map { |e| e[:name] }.sort.uniq
64
- end
65
-
66
- def tty(config)
67
- Signal.trap(:INT) { exit 0 }
68
-
69
- config.hosts.each do |hostinfo|
70
- unless config.hosts.size == 1
71
- case resp = Readline.readline(">> #{hostinfo[:name]}(Y/n)? ", false).strip
72
- when /^y(es)?$/i, ''
73
- when /^n(o)?$/i then next
74
- else redo
75
- end
76
- end
77
-
78
- $stdout.puts "Trying #{hostinfo[:name]}...", "Escape character is '^]'."
79
- tty_config = OpenStruct.new(config.to_h.merge(jobs: 1, hosts: [hostinfo]))
80
-
81
- session, _ = exec(tty_config)
82
- session.interact!
83
- end
84
- end
85
-
86
- def exec(config)
87
- Signal.trap(:INT) { exit 0 }
88
-
89
- max_width = config.hosts.map { |e| e[:name].length }.max
90
- Parallel.map(config.hosts, in_threads: config.jobs) do |hostinfo|
91
- session = nil
92
- error = nil
93
-
94
- begin
95
- buffer = StringIO.new
96
-
97
- loggers = []
98
- loggers << ((config.jobs > 1)? buffer : $stdout)
99
- loggers << File.expand_path(File.join(config.logdir, "#{hostinfo[:name]}.log"), ENV['PWD']) if config.logdir
100
-
101
- session = Xlogin.get(hostinfo.merge(log: loggers))
102
- session.enable(hostinfo[:enable]) if config.enable && hostinfo[:enable]
103
-
104
- command_lines = ['', *config.task.last.to_s.split(';').map(&:strip)]
105
- command_lines.each { |line| session.cmd(line) }
106
- rescue => err
107
- error = err
108
- end
109
-
110
- if config.jobs > 1
111
- prefix = "#{hostinfo[:name].to_s.ljust(max_width)} |"
112
- output = buffer.string.lines.map { |line| prefix + line.chomp.gsub("\r", '') + "\n" }.join
113
- $stdout.print output
114
- $stderr.print prefix + "[Error] #{error}\n" if error
115
- else
116
- $stderr.print "[Error] #{error}\n" if error
117
- end
118
-
119
- session
120
- end
121
- end
122
-
123
- end
10
+ class CLI
11
+
12
+ DEFAULT_INVENTORY_FILE = File.join(ENV['HOME'], '.xloginrc')
13
+ DEFAULT_TEMPLATE_DIR = File.join(ENV['HOME'], '.xlogin.d')
14
+
15
+ def self.run(args = ARGV)
16
+ config = getopts(args)
17
+ client = Xlogin::CLI.new
18
+ client.method(config.task.first).call(config)
19
+ end
20
+
21
+ def self.getopts(args)
22
+ config = OpenStruct.new(
23
+ jobs: 1,
24
+ auth: false,
25
+ task: [:tty, nil],
26
+ inventory: DEFAULT_INVENTORY_FILE,
27
+ template_dir: DEFAULT_TEMPLATE_DIR,
28
+ )
29
+
30
+ parser = OptionParser.new
31
+ parser.banner = "#{File.basename($0)} HOST_PATTERN [Options]"
32
+ parser.version = Xlogin::VERSION
33
+
34
+ parser.on('-i PATH', '--inventory', String, 'The PATH to the inventory file (default: $HOME/.xloginrc).') { |v| config.inventory = v }
35
+ parser.on('-T PATH', '--template', String, 'The PATH to the template dir (default: $HOME/.xlogin.d).') { |v| config.template_dir = v }
36
+ parser.on('-L [DIRECTORY]', '--log-dir', String, 'The PATH to the log dir (default: $PWD).') { |v| config.logdir = v || '.' }
37
+
38
+ parser.on('-l', '--list', TrueClass, 'List the inventory.') { |v| config.task = [:list, nil] }
39
+ parser.on('-t', '--tty', TrueClass, 'Allocate a pseudo-tty.') { |v| config.task = [:tty, nil] }
40
+ parser.on('-e COMMAND', '--exec', 'Execute commands and quit.') { |v| config.task = [:exec, v] }
41
+
42
+ parser.on('-j NUM', '--jobs', Integer, 'The NUM of jobs to execute in parallel(default: 1).') { |v| config.jobs = v }
43
+ parser.on('-y', '--assume-yes', TrueClass, 'Automatically answer yes to prompts.') { |v| config.auth = v }
44
+ parser.on('-E', '--enable', TrueClass, 'Try to gain enable priviledge.') { |v| config.enable = v }
45
+
46
+ parser.parse!(args)
47
+ Xlogin.configure do
48
+ authorize(config.auth)
49
+ source(File.expand_path(config.inventory, ENV['PWD']))
50
+ template_dir(File.expand_path(config.template_dir, ENV['PWD']))
51
+ end
52
+
53
+ config.hosts = Xlogin.list(*args)
54
+ raise "No host found: `#{args.join(', ')}`" if config.hosts.empty?
55
+
56
+ return config
57
+ rescue => e
58
+ $stderr.puts e, '', parser
59
+ exit 1
60
+ end
61
+
62
+ def list(config)
63
+ $stdout.puts config.hosts.map { |e| e[:name] }.sort.uniq
64
+ end
65
+
66
+ def tty(config)
67
+ Signal.trap(:INT) { exit 0 }
68
+
69
+ config.hosts.each do |hostinfo|
70
+ unless config.hosts.size == 1
71
+ case resp = Readline.readline(">> #{hostinfo[:name]}(Y/n)? ", false).strip
72
+ when /^y(es)?$/i, ''
73
+ when /^n(o)?$/i then next
74
+ else redo
75
+ end
76
+ end
77
+
78
+ $stdout.puts "Trying #{hostinfo[:name]}...", "Escape character is '^]'."
79
+ tty_config = OpenStruct.new(config.to_h.merge(jobs: 1, hosts: [hostinfo]))
80
+
81
+ session, _ = exec(tty_config)
82
+ session.interact!
83
+ end
84
+ end
85
+
86
+ def exec(config)
87
+ Signal.trap(:INT) { exit 0 }
88
+
89
+ max_width = config.hosts.map { |e| e[:name].length }.max
90
+ Parallel.map(config.hosts, in_threads: config.jobs) do |hostinfo|
91
+ session = nil
92
+ error = nil
93
+
94
+ begin
95
+ buffer = StringIO.new
96
+
97
+ loggers = []
98
+ loggers << ((config.jobs > 1)? buffer : $stdout)
99
+ loggers << File.expand_path(File.join(config.logdir, "#{hostinfo[:name]}.log"), ENV['PWD']) if config.logdir
100
+
101
+ session = Xlogin.get(hostinfo.merge(log: loggers))
102
+ session.enable(hostinfo[:enable]) if config.enable && hostinfo[:enable]
103
+
104
+ command_lines = ['', *config.task.last.to_s.split(';').map(&:strip)]
105
+ command_lines.each { |line| session.cmd(line) }
106
+ rescue => err
107
+ error = err
108
+ end
109
+
110
+ if config.jobs > 1
111
+ prefix = "#{hostinfo[:name].to_s.ljust(max_width)} |"
112
+ output = buffer.string.lines.map { |line| prefix + line.chomp.gsub("\r", '') + "\n" }.join
113
+ $stdout.print output
114
+ $stderr.print prefix + "[Error] #{error}\n" if error
115
+ else
116
+ $stderr.print "[Error] #{error}\n" if error
117
+ end
118
+
119
+ session
120
+ end
121
+ end
122
+
123
+ end
124
124
  end
@@ -26,12 +26,12 @@ module Xlogin
26
26
  return @inventory.values if patterns.empty?
27
27
 
28
28
  values1 = patterns.map do |pattern|
29
- values2 = pattern.split(',').map do |entry|
30
- key, val = entry.to_s.split(':')
31
- key, val = 'name', key if val.nil?
32
- @inventory.values.select { |e| File.fnmatch(val, e[key.to_sym]) }
33
- end
34
- values2.reduce(&:&)
29
+ values2 = pattern.split(',').map do |entry|
30
+ key, val = entry.to_s.split(':')
31
+ key, val = 'name', key if val.nil?
32
+ @inventory.values.select { |e| File.fnmatch(val, e[key.to_sym]) }
33
+ end
34
+ values2.reduce(&:&)
35
35
  end
36
36
  values1.reduce(&:|)
37
37
  end
@@ -83,14 +83,14 @@ module Xlogin
83
83
 
84
84
  printf($stdout, buffer.string) if !silent && Rake.application.options.always_multitask
85
85
  rescue => e
86
- output($stderr, buffer.string) if !silent && Rake.application.options.always_multitask
87
- output($stderr, "[ERROR] #{e}\n")
86
+ printf($stderr, buffer.string) if !silent && Rake.application.options.always_multitask
87
+ printf($stderr, "[ERROR] #{e}\n")
88
88
 
89
89
  @@graceful_shutdown = true if fail_on_error
90
90
  end
91
91
 
92
92
  def printf(fp, text)
93
- time = Time.now.iso8061
93
+ time = Time.now.iso8601
94
94
  text.each_line do |line|
95
95
  fp.print "#{time} "
96
96
  fp.print "#{name}\t" if Rake.application.options.always_multitask
@@ -52,7 +52,8 @@ module Xlogin
52
52
  end
53
53
 
54
54
  def prompt
55
- cmd('').to_s.lines.last&.chomp
55
+ text = cmd('').to_s.lines.last
56
+ text.chomp if text
56
57
  end
57
58
 
58
59
  def duplicate
@@ -42,12 +42,12 @@ module Xlogin
42
42
  klass = Class.new(Xlogin.const_get(uri.scheme.capitalize))
43
43
  klass.class_exec(@methods) do |methods|
44
44
  methods.each do |name, block|
45
- case name.to_s
46
- when 'enable'
47
- define_method(name) { |args = nil| instance_exec(args || opts[:enable], &block) }
48
- else
49
- define_method(name, &block)
50
- end
45
+ case name.to_s
46
+ when 'enable'
47
+ define_method(name) { |args = nil| instance_exec(args || opts[:enable], &block) }
48
+ else
49
+ define_method(name, &block)
50
+ end
51
51
  end
52
52
  end
53
53
 
@@ -1,3 +1,3 @@
1
1
  module Xlogin
2
- VERSION = "0.11.3"
2
+ VERSION = "0.11.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.11.3
4
+ version: 0.11.4
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-25 00:00:00.000000000 Z
11
+ date: 2019-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-telnet