cli: Thread -> pmap. exc2warn. ruby>=2.7

This commit is contained in:
Denis Knauf 2024-09-21 14:34:32 +02:00
parent f780eccc6a
commit f268fcc5f7
4 changed files with 21 additions and 15 deletions

View file

@ -2,6 +2,13 @@ require 'pmap'
class PVE::Cli class PVE::Cli
def exc2warn dret, exc = Exception
yield
rescue exc
warn "#{$!} (#{$!.class})"
dret
end
using IPAddress::ToSWithNetmaskForNetworks using IPAddress::ToSWithNetmaskForNetworks
def cli_base def cli_base
@ -18,8 +25,9 @@ def cli_base
cli.cmd( :status, "Lists Nodes/VMs/CTs with status", &lambda {|target=nil, sort:, node: nil, status: nil, tags: nil| cli.cmd( :status, "Lists Nodes/VMs/CTs with status", &lambda {|target=nil, sort:, node: nil, status: nil, tags: nil|
hosting_table target: target, status: status, sort: sort, tags: tags do |push| hosting_table target: target, status: status, sort: sort, tags: tags do |push|
node_opt( node). node_opt( node).
each( &push).lazy. each( &push).
flat_map {|n| [ Thread.new( n, &:lxc), Thread.new( n, &:qemu) ] }. flat_map {|n| [ n.method(:lxc), n.method(:qemu) ] }.
flat_pmap {|m| m.call }.
each {|n| n.value.each &push } each {|n| n.value.each &push }
end end
}). }).

View file

@ -4,16 +4,14 @@ def cli_ct
ct_cli.cmd :list, "List CT-IDs", aliases: ['ls'], &lambda {|node=nil| ct_cli.cmd :list, "List CT-IDs", aliases: ['ls'], &lambda {|node=nil|
connect connect
nodes = node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all nodes = node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all
nodes.flat_map do |n| nodes.flat_pmap do |n|
n.lxc.map {|c| c.vmid.to_i } exc2warn( []) { n.lxc.map {|c| c.vmid.to_i } }
end.sort.each {|c| puts c } end.sort.each {|c| puts c }
} }
ct_cli.cmd( :status, "Lists CTs with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil, tags: nil| ct_cli.cmd( :status, "Lists CTs with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil, tags: nil|
hosting_table target: target, status: status, sort: sort, tags: tags do |push| hosting_table target: target, status: status, sort: sort, tags: tags do |push|
node_opt( node).lazy. node_opt( node).peach {|n| exc2warn( nil) { n.lxc.each &push } }
map {|n| Thread.new n, &:lxc }.
each {|n| n.value.each &push }
end end
}). }).
opt( :sort, '-s', '--sort=COLUMNS', "Sort by COLUMNs eg hn for host and name ([s]tatus, h[a], [i]d, [n]ame (default), [h]ost, [u]ptime, [c]pu, [m]em, [d]isk)", default: 'n'). opt( :sort, '-s', '--sort=COLUMNS', "Sort by COLUMNs eg hn for host and name ([s]tatus, h[a], [i]d, [n]ame (default), [h]ost, [u]ptime, [c]pu, [m]em, [d]isk)", default: 'n').

View file

@ -173,7 +173,7 @@ module Proxmox
class <<self class <<self
def __new__ data def __new__ data
n = allocate n = allocate
n.send :__update__, data n.send :__update__, **data
end end
private :__new__ private :__new__
@ -194,7 +194,7 @@ module Proxmox
end end
end end
def __update__ data def __update__ **data
instance_variables.each do |k| instance_variables.each do |k|
remove_instance_variable k remove_instance_variable k
end end
@ -207,7 +207,7 @@ module Proxmox
private :__update__ private :__update__
def refresh! def refresh!
__update__ rest_get( @rest_prefix) __update__ **rest_get( @rest_prefix)
end end
end end
@ -301,7 +301,7 @@ module Proxmox
def refresh! def refresh!
d = rest_get @rest_prefix d = rest_get @rest_prefix
d[:starttime] &&= Time.at d[:starttime] d[:starttime] &&= Time.at d[:starttime]
__update__ d.merge( node: @node, upid: @upid, task: @task) __update__ **d.merge( node: @node, upid: @upid, task: @task)
end end
def __update__ **data def __update__ **data
@ -406,7 +406,7 @@ module Proxmox
end end
def refresh! def refresh!
__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 __update__ **data def __update__ **data
@ -701,7 +701,7 @@ module Proxmox
#}.map {|k,v| "#{k}=#{v}" }.join( ','), #}.map {|k,v| "#{k}=#{v}" }.join( ','),
swap: tmplt.swap, swap: tmplt.swap,
unprivileged: tmplt.unprivileged, unprivileged: tmplt.unprivileged,
features: tmplt.features ? Proxmox::hash2cnfstr( tmplt.features) : nil, features: tmplt.features ? Proxmox::hash2cnfstr( *tmplt.features) : nil,
}.delete_if {|k,v| v.nil? } }.delete_if {|k,v| v.nil? }
temp = LXC.send :__new__, node: node, vmid: options[:vmid], name: name, hostname: options[:hostname] temp = LXC.send :__new__, node: node, vmid: options[:vmid], name: name, hostname: options[:hostname]
@ -763,7 +763,7 @@ module Proxmox
def refresh! def refresh!
virt = @virt virt = @virt
__update__( {state: nil}.merge( rest_get( "/cluster/ha/resources/#{virt.sid}")).merge( virt: virt)) __update__ **{state: nil}.merge( rest_get( "/cluster/ha/resources/#{virt.sid}")).merge( virt: virt)
self self
rescue RestClient::InternalServerError rescue RestClient::InternalServerError
__update__ digest: nil, state: nil, virt: virt __update__ digest: nil, state: nil, virt: virt

View file

@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
# PVE-7 based on debian-11, so ruby 2.7 is default. # PVE-7 based on debian-11, so ruby 2.7 is default.
# No support for ruby 2.5 anymore (PVE-6 based on debian-10) # No support for ruby 2.5 anymore (PVE-6 based on debian-10)
# Old behaviour will be droped. # Old behaviour will be droped.
spec.required_ruby_version = Gem::Requirement.new "~> 2.7" spec.required_ruby_version = Gem::Requirement.new ">= 2.7"
spec.metadata["homepage_uri"] = spec.homepage spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage spec.metadata["source_code_uri"] = spec.homepage