xlogin 0.16.1 → 0.16.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/xlogin.rb +7 -13
- data/lib/xlogin/cli.rb +10 -12
- data/lib/xlogin/factory.rb +10 -6
- data/lib/xlogin/rake_task.rb +28 -24
- data/lib/xlogin/session.rb +7 -9
- data/lib/xlogin/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7bc26b26eaaf13f1577ebe322b44a888dd5c83426f81d484372e582bb8a1182
|
4
|
+
data.tar.gz: d3bfdb0e28a4857f2411c59567e0a54e70b94dc580f64b6c963b8e439919f85f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
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,
|
74
|
-
parser.on('-t PATH', '--template', String,
|
75
|
-
parser.on('-L PATH', '--log-dir', String,
|
76
|
-
|
77
|
-
parser.on('
|
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
|
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
|
data/lib/xlogin/factory.rb
CHANGED
@@ -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)
|
data/lib/xlogin/rake_task.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
65
|
-
|
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,
|
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"
|
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
|
103
|
-
|
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
|
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
|
-
|
115
|
-
|
116
|
-
|
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
|
|
data/lib/xlogin/session.rb
CHANGED
@@ -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
|
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
|
-
|
69
|
-
|
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)
|
data/lib/xlogin/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2021-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-telnet
|