pve/lib/pve/cli/task.rb

90 lines
2.6 KiB
Ruby
Raw Normal View History

2021-04-19 20:35:39 +02:00
class PVE::Cli
2023-09-21 14:55:24 +02:00
def task_table order: nil, &exe
to = TablizedOutput.new %w[S Starttime Node SID Type UPID], format: '<<<<<<'
hosted = {}
Proxmox::Hosted.all.each {|h| hosted[h.vmid.to_i] = h }
hosted.delete nil
exe.call lambda {|t|
u = t.upid
v = u.id ? hosted[u.id.to_i] : t.node
to.push [
case t.status.state
when :running then ColoredString.new '...', '30'
when :success then ColoredString.new 'OK', '32'
when :failed then ColoredString.new 'failed', '31'
end,
u.starttime.strftime( '%Y-%m-%d %H:%M:%S'),
u.node,
case v&.t
when 'nd' then ColoredString.new v.sid, '33'
when 'qm' then ColoredString.new v.sid, '35'
when 'ct' then ColoredString.new v.sid, '36'
when nil then u.id.inspect
else v.sid
end,
u.dtype, u.upid
]
}
to.print order: order
end
2021-04-19 20:35:39 +02:00
def cli_task
cli.sub :task, "Inspect tasks" do |tcli|
tcli.cmd :list, "List done tasks", &lambda {|node=nil|
connect
nodes = Proxmox::Node.all
nodes = nodes.select {|n| node == n.name } if node
nodes.flat_map do |n|
n.tasks.map &:upid
2023-09-21 14:55:24 +02:00
end.sort_by(&:upid).each {|upid| puts upid }
2021-04-19 20:35:39 +02:00
}
tcli.cmd :get, "Inspect a task", &lambda {|upid|
connect
Proxmox::Node.all.each do |n|
n.tasks.each do |t|
next unless t.upid == upid
puts t.upid
t.log( start: 0, limit: 1024).each {|l| puts l[:t] }
2021-04-19 20:35:39 +02:00
return
end
end
}
2023-09-21 14:55:24 +02:00
tcli.cmd( :status, "Lists tasks with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil|
connect
task_table order: [2] do |push|
2023-09-21 19:21:18 +02:00
Proxmox::Node.all.each {|n| n.tasks.each &push }
2023-09-21 14:55:24 +02:00
end
})
2023-09-21 14:55:24 +02:00
tcli.cmd( :monitor, "Monitors running tasks", &lambda {|node: nil|
connect
nodes = Proxmox::Node.all
nodes = nodes.select {|n| node == n.name } if node
tasks = {}
2023-09-21 14:55:24 +02:00
nodes.each {|n| n.tasks.each {|t| tasks[t.upid.upid] = true } }
begin
loop do
task_table order: [2] do |push|
begin
nodes.
flat_map {|n| n.tasks }.
select {|t| tasks[t.upid.upid] != true or t.running? }.
each( &push)
STDERR.print "\e[2J\e[1;1H"
rescue RestClient::InternalServerError
end
end
sleep 1
end
2023-09-21 14:55:24 +02:00
rescue Interrupt
STDERR.print "\e[2J\e[1;1H"
end
2023-09-21 14:55:24 +02:00
})
tcli.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help ct_cli, *args }
2021-04-19 20:35:39 +02:00
end
end
end