pve/lib/pve/cli/task.rb

90 lines
2.6 KiB
Ruby

class PVE::Cli
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
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
end.sort_by(&:upid).each {|upid| puts upid }
}
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] }
return
end
end
}
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|
Proxmox::Node.all.each {|n| n.tasks.each &push }
end
})
tcli.cmd( :monitor, "Monitors running tasks", &lambda {|node: nil|
connect
nodes = Proxmox::Node.all
nodes = nodes.select {|n| node == n.name } if node
tasks = {}
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
rescue Interrupt
STDERR.print "\e[2J\e[1;1H"
end
})
tcli.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help ct_cli, *args }
end
end
end