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

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

View File

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

View File

@ -123,6 +123,10 @@ module Proxmox
n.send :__update__, data n.send :__update__, data
end end
private :__new__ private :__new__
def fetch predata
__new__( predata).refresh!
end
end end
def respond_to? method def respond_to? method
@ -147,8 +151,10 @@ module Proxmox
initialize initialize
self self
end end
private :__update__
def refresh! def refresh!
p self: self, refresh: @rest_prefix
__update__ rest_get( @rest_prefix) __update__ rest_get( @rest_prefix)
end end
end end
@ -176,9 +182,13 @@ module Proxmox
def === t def === t
@name =~ t or @vmid.to_s =~ t or @sid =~ t @name =~ t or @vmid.to_s =~ t or @sid =~ t
end end
def rest_prefix
@rest_prefix ||= "/nodes/#{@node}"
end
def initialize def initialize
@rest_prefix = "/nodes/#{@node}" rest_prefix
@sid = "nd:#{@node}" @sid = "nd:#{@node}"
@name = @node @name = @node
end end
@ -211,21 +221,51 @@ module Proxmox
end end
class Task < Base class Task < Base
def initialize 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
@sid = upid
end
def inspect
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 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}" @rest_prefix = "/nodes/#{@node.node}/tasks/#{upid}"
end
def initialize
rest_prefix
@sid = upid @sid = upid
end end
def inspect def inspect
"#<#{self.class.name} #{upid}>" "#<#{self.class.name} #{@upid}>"
end end
#def finished?
# rest_get( "/nodes/#{node}/tasks/")
#end
def status def status
rest_get( "#{@rest_prefix}/status") Status.fetch node: @node, task: self, upid: @upid
end end
def log start: nil, limit: nil def log start: nil, limit: nil
@ -235,8 +275,7 @@ module Proxmox
class Hosted < Base class Hosted < Base
def refresh! 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 end
def === t def === t
@ -392,8 +431,12 @@ module Proxmox
end end
end end
def rest_prefix
@rest_prefix ||= "/nodes/%s/qemu/%d" % [@node.node, @vmid]
end
def initialize def initialize
@rest_prefix = "/nodes/%s/qemu/%d" % [@node.node, @vmid] rest_prefix
@sid = "qm:#{@vmid}" @sid = "qm:#{@vmid}"
end end
@ -507,8 +550,12 @@ module Proxmox
end end
end end
def rest_prefix
@rest_prefix ||= "/nodes/%s/lxc/%d" % [@node.node, @vmid]
end
def initialize def initialize
@rest_prefix = "/nodes/%s/lxc/%d" % [@node.node, @vmid] rest_prefix
@sid = "ct:#{@vmid}" @sid = "ct:#{@vmid}"
end end
@ -526,8 +573,12 @@ module Proxmox
end end
class HA < Base class HA < Base
def rest_prefix
@rest_prefix ||= "/cluster/ha/resources/#{virt.sid}"
end
def initialize def initialize
@rest_prefix = "/cluster/ha/resources/#{virt.sid}" rest_prefix
end end
class <<self class <<self

View File

@ -186,7 +186,8 @@ module PVE::CTTemplate
options.ostemplate || options.ostemplate ||
case ostype case ostype
when 'debian' 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 else
raise ArgumentError, "OS-Template for ostype #{ostype} not found or ostemplate not provided." raise ArgumentError, "OS-Template for ostype #{ostype} not found or ostemplate not provided."
end end