From f268fcc5f76e0641fa00a0ed7cb01d582832a2ea Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Sat, 21 Sep 2024 14:34:32 +0200 Subject: [PATCH] cli: Thread -> pmap. exc2warn. ruby>=2.7 --- lib/pve/cli/base.rb | 12 ++++++++++-- lib/pve/cli/ct.rb | 8 +++----- lib/pve/proxmox.rb | 14 +++++++------- pve.gemspec | 2 +- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/pve/cli/base.rb b/lib/pve/cli/base.rb index 9c23cc5..3b244a9 100644 --- a/lib/pve/cli/base.rb +++ b/lib/pve/cli/base.rb @@ -2,6 +2,13 @@ require 'pmap' class PVE::Cli + def exc2warn dret, exc = Exception + yield + rescue exc + warn "#{$!} (#{$!.class})" + dret + end + using IPAddress::ToSWithNetmaskForNetworks 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| hosting_table target: target, status: status, sort: sort, tags: tags do |push| node_opt( node). - each( &push).lazy. - flat_map {|n| [ Thread.new( n, &:lxc), Thread.new( n, &:qemu) ] }. + each( &push). + flat_map {|n| [ n.method(:lxc), n.method(:qemu) ] }. + flat_pmap {|m| m.call }. each {|n| n.value.each &push } end }). diff --git a/lib/pve/cli/ct.rb b/lib/pve/cli/ct.rb index e7debb0..170fad3 100644 --- a/lib/pve/cli/ct.rb +++ b/lib/pve/cli/ct.rb @@ -4,16 +4,14 @@ def cli_ct ct_cli.cmd :list, "List CT-IDs", aliases: ['ls'], &lambda {|node=nil| connect nodes = node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all - nodes.flat_map do |n| - n.lxc.map {|c| c.vmid.to_i } + nodes.flat_pmap do |n| + exc2warn( []) { n.lxc.map {|c| c.vmid.to_i } } 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| hosting_table target: target, status: status, sort: sort, tags: tags do |push| - node_opt( node).lazy. - map {|n| Thread.new n, &:lxc }. - each {|n| n.value.each &push } + node_opt( node).peach {|n| exc2warn( nil) { n.lxc.each &push } } 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'). diff --git a/lib/pve/proxmox.rb b/lib/pve/proxmox.rb index f335140..98b7550 100644 --- a/lib/pve/proxmox.rb +++ b/lib/pve/proxmox.rb @@ -173,7 +173,7 @@ module Proxmox class < 2.7" + spec.required_ruby_version = Gem::Requirement.new ">= 2.7" spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = spec.homepage