-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.rb
executable file
·112 lines (89 loc) · 2.65 KB
/
manager.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require 'parallel'
class Manager
attr_accessor :hosts
def initialize(hosts, **kwargs)
@hosts = hosts
hostlen = hosts.map(&:length).max
@filter = Regexp.new('.')
self.set_filter(kwargs[:filter]) unless kwargs[:filter].empty?
@alias = kwargs[:alias]
@logger = kwargs[:cli_logger]
@plex = hosts.map { |h| Plex.new(h,
agent: kwargs[:agent],
logger: kwargs[:plex_logger],
mode: kwargs[:mode],
out: kwargs[:out].call(h.rjust(hostlen)),
user: kwargs[:user]) }
end
def connect
Parallel.map(@plex
.select { |p| @filter.match(p.hostname) }
.reject { |p| p.connected? }, in_threads: 12) { |p|
@logger.info("connecting to #{p.hostname}")
p.open
@logger.info("connected to #{p.hostname}")
}
end
def set_filter(regexps)
@filter = Regexp.union(regexps)
end
def process
self.connect
timestamp = Time.now
active = @plex
.select { |p| p.connected? && !p.eof? }
.select { |p| @filter.match(p.hostname) }
pending = active.dup
Signal.trap("INT") { pending.map { |p| p.etx! } }
while pending.length > 0
if Time.now - timestamp > 10
done = active.select { |p| p.eof? || p.idle? }.map(&:hostname)
wait = active.map(&:hostname) - done
@logger.info("done: #{done.join(',')}")
@logger.info("wait: #{wait.join(',')}")
timestamp = Time.now
end
p = pending.shift
p.process
next if p.eof?
next if p.idle?
pending.push(p)
end
Signal.trap("INT", "DEFAULT")
if @plex.all? { |p| p.eof? }
@logger.info("all done")
raise RuntimeError, "FIXME: handle all shell eof"
end
end
def prompt
cmd = Readline.readline("sshplex% ", true)
return if cmd.nil?
return if cmd.empty?
if ctrl_cmd = cmd[/^:(\w+)/, 1]
case ctrl_cmd
when 'on'
if patterns = cmd[/^:on (.*)$/, 1]
set_filter(patterns.split)
return
else
@logger.info("didn't update host filter")
end
else
if new_cmd = @alias[ctrl_cmd]
Readline.pre_input_hook = -> {
Readline.insert_text(new_cmd)
Readline.redisplay
Readline.pre_input_hook = nil
}
return
else
@logger.info("didn't expand alias #{ctrl_cmd}")
end
end
end
Readline::HISTORY.push cmd
@plex
.filter { |p| @filter.match(p.hostname) }
.each { |p| p.send(cmd) }
end
end