Task::Status implemented. rest_prefix is own method. debian template updated. Log-printing optimized.

This commit is contained in:
Denis Knauf 2021-09-27 21:02:55 +02:00
parent 890aa2a389
commit 38ea2125ff
4 changed files with 97 additions and 36 deletions

View file

@ -55,6 +55,17 @@ class PVE::Cli
exit 1 unless interactive? and r
end
def task_log task, logn, limit = 1024
log = task.log start: logn, limit: limit
log = [] if [{n: 1, t: 'no content'}] == log
unless log.empty?
STDERR.printf "\r\e[J"
log.each {|l| puts l[:t] }
logn = log.last[:n]
end
logn
end
def wait task, secs: nil, text: nil
secs ||= 0.1
spinners, spin, logn = "▖▘▝▗", 0, 0
@ -62,23 +73,19 @@ class PVE::Cli
host = task.host&.name
loop do
s = task.status
if s[:exitstatus]
STDERR.printf "\r[%s] %s %s %s\e[J\n",
host || s[:id],
case s[:exitstatus]
when "OK" then "\e[32;1m✓\e[0m"
else "\e[31;1m✗\e[0m"
end,
text && "#{text}:",
s[:status] == 'stopped' ? :finished : s[:status]
return task
logn = self.task_log task, logn
if s.finished?
loop do
r = self.task_log task, logn
break if 0 == logn - r
logn = r
end
log = task.log start: logn
log = [] if [{n: 1, t: 'no content'}] == log
unless log.empty?
STDERR.printf "\r\e[J"
log.each {|l| puts l[:t] }
logn = log.last[:n]
STDERR.printf "\r[%s] %s %s %s\e[J\n",
host || s.id,
s.successfull? ? "\e[32;1m✓\e[0m" : "\e[31;1m✗\e[0m",
text && "#{text}:",
s.stopped? ? :finished : s.status
return s
end
STDERR.printf "\r[%s] \e[33;1m%s\e[0m %s...\e[J",
host || s[:id],
@ -99,9 +106,7 @@ class PVE::Cli
return
end
task = host.start
unless fire
wait task, text: "Starting"
end
wait task, text: "Starting" unless fire
end
def stop host, timeout: nil, fire: nil, secs: nil
@ -120,9 +125,13 @@ class PVE::Cli
options[:start] = fire && start
task = klass.create template, **options
return if fire
wait task, text: "Creating"
status = wait task, text: "Creating"
if status.successfull?
host = task.host.refresh!
start host, timeout: timeout, secs: secs if start
elsif not interactive?
exit 1
end
end
def destroy ct, timeout: nil, fire: nil, secs: nil

View file

@ -16,7 +16,7 @@ def cli_task
n.tasks.each do |t|
next unless t.upid == upid
puts t.upid
t.log.each {|l| puts l[:l] }
t.log( start: 0, limit: 1024).each {|l| puts l[:t] }
return
end
end

View file

@ -123,6 +123,10 @@ module Proxmox
n.send :__update__, data
end
private :__new__
def fetch predata
__new__( predata).refresh!
end
end
def respond_to? method
@ -147,8 +151,10 @@ module Proxmox
initialize
self
end
private :__update__
def refresh!
p self: self, refresh: @rest_prefix
__update__ rest_get( @rest_prefix)
end
end
@ -177,8 +183,12 @@ module Proxmox
@name =~ t or @vmid.to_s =~ t or @sid =~ t
end
def rest_prefix
@rest_prefix ||= "/nodes/#{@node}"
end
def initialize
@rest_prefix = "/nodes/#{@node}"
rest_prefix
@sid = "nd:#{@node}"
@name = @node
end
@ -211,21 +221,51 @@ module Proxmox
end
class Task < Base
class Status < Base
def rest_prefix
@rest_prefix ||= '/nodes/%s/tasks/%s/status' % [@node.node, @upid]
end
def refresh!
d = rest_get @rest_prefix
d[:starttime] &&= Time.at d[:starttime]
d = {exitstatus: nil}.merge d
__update__ d.merge( node: @node, t: 'status', upid: @upid, task: @task)
end
def initialize
@rest_prefix = "/nodes/#{@node.node}/tasks/#{upid}"
rest_prefix
@sid = upid
end
def inspect
"#<#{self.class.name} #{upid}>"
h = instance_variables - %i[@node @task @sid @rest_prefix @upid @t]
h.map! {|k| "#{k[1..-1]}=#{instance_variable_get(k).inspect}" }
"#<#{self.class.name}|#{@upid} node=#{@node.node} #{h.join ' '}>"
end
#def finished?
# rest_get( "/nodes/#{node}/tasks/")
#end
def running?() 'running' == @status end
def finished?() 'stopped' == @status end
alias stopped? finished?
def successfull?() stopped? ? 'OK' == @exitstatus : nil end
def failed?() stopped? ? 'OK' != @exitstatus : nil end
end
def rest_prefix
@rest_prefix = "/nodes/#{@node.node}/tasks/#{upid}"
end
def initialize
rest_prefix
@sid = upid
end
def inspect
"#<#{self.class.name} #{@upid}>"
end
def status
rest_get( "#{@rest_prefix}/status")
Status.fetch node: @node, task: self, upid: @upid
end
def log start: nil, limit: nil
@ -235,8 +275,7 @@ module Proxmox
class Hosted < Base
def refresh!
node, t = @node, @t
__update__ rest_get( "#{@rest_prefix}/status/current").merge( node: node, t: t)
__update__ rest_get( "#{@rest_prefix}/status/current").merge( node: @node, t: @t)
end
def === t
@ -392,8 +431,12 @@ module Proxmox
end
end
def rest_prefix
@rest_prefix ||= "/nodes/%s/qemu/%d" % [@node.node, @vmid]
end
def initialize
@rest_prefix = "/nodes/%s/qemu/%d" % [@node.node, @vmid]
rest_prefix
@sid = "qm:#{@vmid}"
end
@ -507,8 +550,12 @@ module Proxmox
end
end
def rest_prefix
@rest_prefix ||= "/nodes/%s/lxc/%d" % [@node.node, @vmid]
end
def initialize
@rest_prefix = "/nodes/%s/lxc/%d" % [@node.node, @vmid]
rest_prefix
@sid = "ct:#{@vmid}"
end
@ -526,8 +573,12 @@ module Proxmox
end
class HA < Base
def rest_prefix
@rest_prefix ||= "/cluster/ha/resources/#{virt.sid}"
end
def initialize
@rest_prefix = "/cluster/ha/resources/#{virt.sid}"
rest_prefix
end
class <<self

View file

@ -186,7 +186,8 @@ module PVE::CTTemplate
options.ostemplate ||
case ostype
when 'debian'
'local:vztmpl/debian-10-standard_10.5-1_amd64.tar.gz'
# TODO: how to determine which template?
'local:vztmpl/debian-10-standard_10.7-1_amd64.tar.gz'
else
raise ArgumentError, "OS-Template for ostype #{ostype} not found or ostemplate not provided."
end