From 45646994ff4ccc9ad52d6b25077b97bbfed031c4 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 8 Dec 2021 22:38:03 +0100 Subject: [PATCH 01/28] 0.2.2 --- lib/pve/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pve/version.rb b/lib/pve/version.rb index e19a049..a304544 100644 --- a/lib/pve/version.rb +++ b/lib/pve/version.rb @@ -1,3 +1,3 @@ module PVE - VERSION = '0.2.1' + VERSION = '0.2.2' end From b2d78971b084f5f4f68b2fb9d9470089b242beb3 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Mon, 13 Dec 2021 14:24:59 +0100 Subject: [PATCH 02/28] Gemfile.lock removed and ignored --- .gitignore | 1 + Gemfile.lock | 70 ---------------------------------------------------- 2 files changed, 1 insertion(+), 70 deletions(-) delete mode 100644 Gemfile.lock diff --git a/.gitignore b/.gitignore index de3303e..1977fa1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.sw[pomnqrst] *.gem +Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index 1fc249a..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,70 +0,0 @@ -PATH - remote: . - specs: - pve (0.2.1) - activesupport (>= 2) - dencli (~> 0.4) - ipaddress (~> 0.8.3) - pmap (~> 1.1) - rest-client (~> 2.1) - -GEM - remote: https://rubygems.org/ - specs: - activesupport (6.1.4.1) - concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (>= 1.6, < 2) - minitest (>= 5.1) - tzinfo (~> 2.0) - zeitwerk (~> 2.3) - concurrent-ruby (1.1.9) - dencli (0.5.3) - diff-lcs (1.4.4) - domain_name (0.5.20190701) - unf (>= 0.0.5, < 1.0.0) - http-accept (1.7.0) - http-cookie (1.0.4) - domain_name (~> 0.5) - i18n (1.8.11) - concurrent-ruby (~> 1.0) - ipaddress (0.8.3) - mime-types (3.4.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2021.1115) - minitest (5.14.4) - netrc (0.11.0) - pmap (1.1.1) - rest-client (2.1.0) - http-accept (>= 1.7.0, < 2.0) - http-cookie (>= 1.0.2, < 2.0) - mime-types (>= 1.16, < 4.0) - netrc (~> 0.8) - rspec (3.10.0) - rspec-core (~> 3.10.0) - rspec-expectations (~> 3.10.0) - rspec-mocks (~> 3.10.0) - rspec-core (3.10.1) - rspec-support (~> 3.10.0) - rspec-expectations (3.10.1) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.10.0) - rspec-mocks (3.10.2) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.10.0) - rspec-support (3.10.3) - tzinfo (2.0.4) - concurrent-ruby (~> 1.0) - unf (0.1.4) - unf_ext - unf_ext (0.0.8) - zeitwerk (2.5.1) - -PLATFORMS - x86_64-linux - -DEPENDENCIES - pve! - rspec (~> 3.2) - -BUNDLED WITH - 2.2.15 From ce7a95945c5475750a03449d4c3deba213b72f64 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Mon, 13 Dec 2021 14:28:52 +0100 Subject: [PATCH 03/28] status-output: colored time, nodes (re)added --- lib/pve/cli/base.rb | 1 + lib/pve/cli/ct.rb | 27 ++++++++++++------ lib/pve/cli/qm.rb | 27 ++++++++++++------ lib/pve/helper.rb | 68 ++++++++++++++++++++++++++++++--------------- 4 files changed, 82 insertions(+), 41 deletions(-) diff --git a/lib/pve/cli/base.rb b/lib/pve/cli/base.rb index 86f81f6..8000d5d 100644 --- a/lib/pve/cli/base.rb +++ b/lib/pve/cli/base.rb @@ -27,6 +27,7 @@ def cli_base lambda {|n| to.virt n } end nodes = node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all + nodes.each &push nodes. flat_map {|n| [ Thread.new( n, &:lxc), Thread.new( n, &:qemu) ] }. each {|n| n.value.each &push } diff --git a/lib/pve/cli/ct.rb b/lib/pve/cli/ct.rb index 31bc3d8..a50387d 100644 --- a/lib/pve/cli/ct.rb +++ b/lib/pve/cli/ct.rb @@ -9,16 +9,25 @@ def cli_ct end.sort.each {|c| puts c } } - ct_cli.cmd :status, "List CTs with status", aliases: [nil], &lambda {|node=nil| + ct_cli.cmd( :status, "Lists CTs with status", aliases: [nil], &lambda {|target=nil, sort: 'n', node: nil| connect - to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU Mem/MiB Disk/MiB] - nodes = Proxmox::Node.all - nodes = nodes.select {|n| node == n.name } if node - nodes.each do |n| - n.lxc.each &to.method( :virt) - end - to.print order: [3] - } + node &&= /\A#{node}\z/ + to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU/% Mem/MiB Mem/% Disk/MiB Disk/%] + push = + if target + target = /\A#{target}\z/ + lambda {|n| to.virt n if n === target } + else + lambda {|n| to.virt n } + end + nodes = node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all + nodes. + map {|n| Thread.new( n, &:lxc) }. + each {|n| n.value.each &push } + to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) } + }). + 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)"). + opt( :node, '-n', '--node=NODE', "List only hosted by this NODE") ct_cli.cmd :enter, "Enter Console of CT", &lambda {|name_or_id| connect diff --git a/lib/pve/cli/qm.rb b/lib/pve/cli/qm.rb index 4f36d7c..40944fe 100644 --- a/lib/pve/cli/qm.rb +++ b/lib/pve/cli/qm.rb @@ -10,16 +10,25 @@ def cli_qm end.sort.each {|c| puts c } } - qm.cmd :status, "List VMs with status", aliases: [nil], &lambda {|node=nil| + qm.cmd( :status, "Lists CTs with status", aliases: [nil], &lambda {|target=nil, sort: 'n', node: nil| connect - to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU Mem/MiB Disk/MiB] - nodes = Proxmox::Node.all - nodes = nodes.select {|n| node == n.name } if node - nodes.each do |n| - n.qemu.each &to.method( :virt) - end - to.print order: [3] - } + node &&= /\A#{node}\z/ + to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU/% Mem/MiB Mem/% Disk/MiB Disk/%] + push = + if target + target = /\A#{target}\z/ + lambda {|n| to.virt n if n === target } + else + lambda {|n| to.virt n } + end + nodes = node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all + nodes. + map {|n| Thread.new( n, &:qemu) }. + each {|n| n.value.each &push } + to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) } + }). + 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)"). + opt( :node, '-n', '--node=NODE', "List only hosted by this NODE") qm.cmd :exec, "Executes Command in VM via qemu-guest-agent", min: 4, &lambda {|name_or_id, *command| connect diff --git a/lib/pve/helper.rb b/lib/pve/helper.rb index 365d2c0..2aaa9a5 100644 --- a/lib/pve/helper.rb +++ b/lib/pve/helper.rb @@ -13,17 +13,24 @@ end class Measured + class V + attr_reader :value, :length + + def initialize( value, length = nil) @value, @length = value, length || value.length end + def inspect() "#" end + alias :to_s :value + alias :to_str :value + end + class < v + return units '%d ' % v , :B if 512 > v %w[KiB MiBy GiByt TiByte ExiByte PetiByte].each_with_index do |m| v /= 1024 - #return "%.1f %s" % [v, m] if 10 > v - #return "%d %s" % [v, m] if 512 > v - return "%.1f %s" % [v, m] if 512 > v + return units '%.1f ' % v, m if 512 > v end - "%d PetiByte" % v + units '%d ' % v, :PetiByte end def bytes2 v @@ -40,21 +47,28 @@ class Measured end alias bytes bytes2 + def units val, unit + v = "#{val}\e[1;30m#{unit}\e[0m" + V.new v, v.length - 11 + end + def seconds i i = i.to_i - return '·' if 0 == i - return "%d s" % i if 90 > i + return V.new "\e[1;30m·\e[0m", 1 if 0 == i + return units '%d ' % i, :s if 90 > i i /= 60 - return "%d mi" % i if 90 > i + return units '%d ' % i, :mi if 90 > i i /= 60 - return "%d hou" % i if 36 > i + return units '%d ' % i, :hou if 36 > i i /= 24 - return "%d days" % i if 14 > i + return units '%d ' % i, :days if 14 > i j = i / 7 - return "%d weeks" % j if 25 > j - i /= 365 - return "%.1f years" if 550 > i - "%dy" % i + return units '%d ' % j, :weeks if 8 > j + j = i / 30 + return units '%d ' % j, :months if 11 > j + i /= 365.0 + return units '%.2f ' % i, :years if 550 > i + units '%d ' % i, :years end end end @@ -72,10 +86,8 @@ class ColoredString def length() @string.length end alias size length - #def to_str() self end def to_s() "\e[#{@color_codes}m#{@string}\e[0m" end alias to_str to_s - #alias inspect to_str include Comparable def <=>(o) @string <=> o.string end @@ -101,7 +113,7 @@ class TablizedOutput class V < B attr_reader :v, :s def initialize( v, s=nil) @v, @s = v, s || "#{v}" end - def to_s() @s end + def to_s() @s.to_s end def length() @s.length end def inspect() "#" end end @@ -113,11 +125,9 @@ class TablizedOutput def inspect() "#" end def to_s - #y = w - (v*w).round y = (v*w).round x = (100*v).round r = "%*s" % [w, 0==x ? '·' : x] - #"\e[0m#{r[0...y]}\e[1;4;#{0.75>v ? 32 : 31}m#{r[y..-1]}\e[0m" "\e[1;4;#{0.75>v ? 32 : 31}m#{r[0...y]}\e[0m#{r[y..-1]}" end end @@ -139,7 +149,6 @@ class TablizedOutput end def print order: nil - format = "#{@format.map {|f| "\e[%%sm%%#{case f when '<' then '-' else ' ' end}%ds\e[0m"}.join( ' ') % @maxs}\n" ls = @lines if order eval <<-EOC, binding, __FILE__, 1+__LINE__ @@ -149,9 +158,22 @@ class TablizedOutput } EOC end - #ls = ls.sort_by {|e| p e; order.map &e.method(:[]) } if order - @stdout.printf format, *@header.flat_map {|s|['',s]} - ls.each {|l| @stdout.printf format, *l.flat_map {|s| s.is_a?(ColoredString) ? [s.color_codes, s.string] : ["", s.to_s] } } + @stdout.puts \ + @header.each_with_index.map {|s, i| + "#{' ' * (@maxs[i] - s.length)}#{s}" + }.join( ' ') + ls.each_with_index do |l| + @stdout.puts \ + l.each_with_index.map {|s, i| + pad = ' ' * (@maxs[i] - s.length) + case @format[i] + when '<' + "#{s}#{pad}" + else + "#{pad}#{s}" + end + }.join( ' ') + end end def virt v From efe4f41edab4f4449747f45a4c66c0b32bc9f429 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Mon, 13 Dec 2021 14:29:51 +0100 Subject: [PATCH 04/28] v0.2.2 --- lib/pve/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pve/version.rb b/lib/pve/version.rb index a304544..ebeb6da 100644 --- a/lib/pve/version.rb +++ b/lib/pve/version.rb @@ -1,3 +1,3 @@ module PVE - VERSION = '0.2.2' + VERSION = '0.2.3' end From 8994faad9e20b4abd41bb1e81ab4e402ae9e07f6 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 12 Jan 2022 20:07:52 +0100 Subject: [PATCH 05/28] Set min for commands with same (sort) aliases. Table improvements. --- lib/pve/cli.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/pve/cli/base.rb | 31 +++++++++++-------------------- lib/pve/cli/ct.rb | 34 +++++++++++++--------------------- lib/pve/cli/ha.rb | 6 +++--- lib/pve/cli/node.rb | 2 +- lib/pve/cli/qm.rb | 28 ++++++++++------------------ lib/pve/cli/storage.rb | 20 ++++++++++---------- lib/pve/helper.rb | 19 +++++++++++-------- 8 files changed, 101 insertions(+), 81 deletions(-) diff --git a/lib/pve/cli.rb b/lib/pve/cli.rb index 0f999e3..b2bf2cc 100644 --- a/lib/pve/cli.rb +++ b/lib/pve/cli.rb @@ -153,6 +153,48 @@ class PVE::Cli end end + def node_opt node = nil + node &&= /\A#{node}\z/ + node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all + end + + def target_opt target = nil, &exe + if target + target = /\A#{target}\z/ + lambda {|n| exe.call n if n === target } + else + exe + end + end + + def hosting_table target:, status:, sort: + connect + to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU/% Mem/MiB Mem/% Disk/MiB Disk/%], format: '<<<<<>>>>>>' + target &&= /\A#{target}\z/i + status = + case status + when /\Asta(r(t(ed?)?)?)?\z/i, /\Aon(l(i(ne?)?)?)?\z/i, /\Ar(u(n(n(i(ng?)?)?)?)?)?\z/i, '1' + %i[started online running] + when /\Asto(p(p(ed?)?)?)?\z/i, /\Aof(f(l(i(ne?)?)?)?)?\z/i, '0' + %i[stopped offline] + when nil, '', /\Aa(ll?)?\z/i then nil + else + raise DenKn::UsageError, "Unknown state #{status}" + end + push = + if target and status + lambda {|n| to.virt n if n === target and status.include?( n.state) } + elsif target + lambda {|n| to.virt n if n === target } + elsif status + lambda {|n| to.virt n if status.include? n.state } + else + to.method :virt + end + yield push + to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) } + end + def help cl, *args STDERR.puts cl.help( *args) exit 1 unless interactive? diff --git a/lib/pve/cli/base.rb b/lib/pve/cli/base.rb index 8000d5d..8c031bc 100644 --- a/lib/pve/cli/base.rb +++ b/lib/pve/cli/base.rb @@ -15,26 +15,17 @@ def cli_base each {|c| puts c } } - cli.cmd( :status, "Lists Nodes/VMs/CTs with status", &lambda {|target=nil, sort: 'n', node: nil| - connect - node &&= /\A#{node}\z/ - to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU/% Mem/MiB Mem/% Disk/MiB Disk/%] - push = - if target - target = /\A#{target}\z/ - lambda {|n| to.virt n if n === target } - else - lambda {|n| to.virt n } - end - nodes = node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all - nodes.each &push - nodes. - flat_map {|n| [ Thread.new( n, &:lxc), Thread.new( n, &:qemu) ] }. - each {|n| n.value.each &push } - to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) } + cli.cmd( :status, "Lists Nodes/VMs/CTs with status", &lambda {|target=nil, sort:, node: nil, status: nil| + hosting_table target: target, status: status, sort: sort do |push| + node_opt( node). + each( &push).lazy. + flat_map {|n| [ Thread.new( n, &:lxc), Thread.new( n, &:qemu) ] }. + each {|n| n.value.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)"). - opt( :node, '-n', '--node=NODE', "List only hosted by this NODE") + 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( :node, '-n', '--node=NODE', "List only hosted by this NODE"). + opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)") def prepare_show_config cnf r = {} @@ -144,7 +135,7 @@ def cli_base show_config th.config, old } - ccli.cmd :show, "Show Config of CT/VM", aliases: %w[s], &lambda {|name_or_id| + ccli.cmd :show, "Show Config of CT/VM", &lambda {|name_or_id| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) show_config th.config diff --git a/lib/pve/cli/ct.rb b/lib/pve/cli/ct.rb index a50387d..a1fb79b 100644 --- a/lib/pve/cli/ct.rb +++ b/lib/pve/cli/ct.rb @@ -9,25 +9,17 @@ def cli_ct end.sort.each {|c| puts c } } - ct_cli.cmd( :status, "Lists CTs with status", aliases: [nil], &lambda {|target=nil, sort: 'n', node: nil| - connect - node &&= /\A#{node}\z/ - to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU/% Mem/MiB Mem/% Disk/MiB Disk/%] - push = - if target - target = /\A#{target}\z/ - lambda {|n| to.virt n if n === target } - else - lambda {|n| to.virt n } - end - nodes = node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all - nodes. - map {|n| Thread.new( n, &:lxc) }. - each {|n| n.value.each &push } - to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) } + ct_cli.cmd( :status, "Lists CTs with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil| + hosting_table target: target, status: status, sort: sort do |push| + node_opt( node). + each( &push).lazy. + map {|n| Thread.new n, &:lxc }. + each {|n| n.value.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)"). - opt( :node, '-n', '--node=NODE', "List only hosted by this NODE") + 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( :node, '-n', '--node=NODE', "List only hosted by this NODE"). + opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)") ct_cli.cmd :enter, "Enter Console of CT", &lambda {|name_or_id| connect @@ -39,7 +31,7 @@ def cli_ct STDERR.puts "! #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).exec *command } - ct_cli.cmd( :start, "Starts CT", min: 3, &lambda {|name_or_id, node: nil, fire:, timeout:, secs:| + ct_cli.cmd( :start, "Starts CT", min: 4, &lambda {|name_or_id, node: nil, fire:, timeout:, secs:| connect ct = Proxmox::LXC.find! name_or_id start ct, node: node, fire: fire, timeout: timeout, secs: secs @@ -61,7 +53,7 @@ def cli_ct opt( :timeout, "-tTIMEOUT", "--timeout=TIMEOUT", "Wait for max TIMEOUT seconds (default: endless)", default: nil). opt( :secs, "-sSECONDS", "--seconds=SECONDS", "Check every SECONDS for state (default: 0.2)", default: 0.2) - ct_cli.cmd( :create, "Creates a new container", &lambda {|template, *options| #, fire:, timeout:, secs:, start:| + ct_cli.cmd( :create, "Creates a new container", min: 2, &lambda {|template, *options| #, fire:, timeout:, secs:, start:| if %w[-h --help].include? template STDERR.puts "Usage: ct create TEMPLATE -h # Shows template-related options" STDERR.puts " ct create TEMPLATE [OPTIONS] # Creates a container" @@ -107,7 +99,7 @@ EOU create Proxmox::LXC, template, **ctopts }) - ct_cli.cmd( :config, 'Shows current config', aliases: %w[cnf], &lambda {|name_or_id| + ct_cli.cmd( :config, 'Shows current config', aliases: %w[cnf], min: 2, &lambda {|name_or_id| connect ct = Proxmox::LXC.find! name_or_id STDOUT.puts JSON.dump( ct.config) diff --git a/lib/pve/cli/ha.rb b/lib/pve/cli/ha.rb index 04b3b7f..8f37708 100644 --- a/lib/pve/cli/ha.rb +++ b/lib/pve/cli/ha.rb @@ -9,7 +9,7 @@ def opts_ha cl end def cli_ha - cli.sub :ha, "Inspect High-Availability" do |hacli| + cli.sub :ha, "Inspect High-Availability", min: 2 do |hacli| hacli.cmd( :create, "Create HA for CT/VM", &lambda {|name_or_id, group:, comment: nil, max_relocate:, max_restart:, state:| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) @@ -19,7 +19,7 @@ def cli_ha ha.create group: group, comment: comment, max_relocate: max_relocate, max_restart: max_restart }).tap {|cl| opts_ha cl } - hacli.cmd :remove, "Remove CT/VM from HA", &lambda {|name_or_id| + hacli.cmd :remove, "Remove CT/VM from HA", min: 5, &lambda {|name_or_id| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th @@ -63,7 +63,7 @@ def cli_ha ha.stopped! } - hacli.cmd :reset, "If state of CT/VM is failed, Proxmox will not start/stop it anyway. You have to reset state (state=disabled), first", &lambda {|name_or_id| + hacli.cmd :reset, "If state of CT/VM is failed, Proxmox will not start/stop it anyway. You have to reset state (state=disabled), first", min: 3, aliases: [:rst], &lambda {|name_or_id| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th diff --git a/lib/pve/cli/node.rb b/lib/pve/cli/node.rb index 99dee17..f87b6bb 100644 --- a/lib/pve/cli/node.rb +++ b/lib/pve/cli/node.rb @@ -30,7 +30,7 @@ def cli_node end nod_cli.sub :task, "Inspect tasks" do |tcli| - tcli.cmd :list, "List done tasks", aliases: [nil, 'ls'], &lambda {|node| + tcli.cmd :list, "List done tasks", aliases: [:ls], &lambda {|node| connect Proxmox::Node.find_by_name!( node). tasks. diff --git a/lib/pve/cli/qm.rb b/lib/pve/cli/qm.rb index 40944fe..58dfb95 100644 --- a/lib/pve/cli/qm.rb +++ b/lib/pve/cli/qm.rb @@ -10,25 +10,17 @@ def cli_qm end.sort.each {|c| puts c } } - qm.cmd( :status, "Lists CTs with status", aliases: [nil], &lambda {|target=nil, sort: 'n', node: nil| - connect - node &&= /\A#{node}\z/ - to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU/% Mem/MiB Mem/% Disk/MiB Disk/%] - push = - if target - target = /\A#{target}\z/ - lambda {|n| to.virt n if n === target } - else - lambda {|n| to.virt n } - end - nodes = node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all - nodes. - map {|n| Thread.new( n, &:qemu) }. - each {|n| n.value.each &push } - to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) } + qm.cmd( :status, "Lists CTs with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil| + hosting_table target: target, state: state, sort: sort do |push| + node_opt( node). + each( &push).lazy. + flat_map {|n| [ Thread.new( n, &:lxc), Thread.new( n, &:qemu) ] }. + each {|n| n.value.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)"). - opt( :node, '-n', '--node=NODE', "List only hosted by this NODE") + 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( :node, '-n', '--node=NODE', "List only hosted by this NODE"). + opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)") qm.cmd :exec, "Executes Command in VM via qemu-guest-agent", min: 4, &lambda {|name_or_id, *command| connect diff --git a/lib/pve/cli/storage.rb b/lib/pve/cli/storage.rb index 26b06ae..5503ac5 100644 --- a/lib/pve/cli/storage.rb +++ b/lib/pve/cli/storage.rb @@ -53,24 +53,24 @@ def cli_storage connect appliances node, regexp, system, applications }). - opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil). - opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil). - opt( :system, '-s', '--system', 'Only system templates', default: nil). - opt( :applications, '-a', '--applications', 'Only applications (non system) templates', default: nil) + opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil). + opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil). + opt( :system, '-s', '--system', 'Only system templates', default: nil). + opt( :applications, '-a', '--applications', 'Only applications (non system) templates', default: nil) - cli_apl.cmd( :system, "Table of provided systems", aliases: [nil], &lambda {|node:, regexp:| + cli_apl.cmd( :system, "Table of provided systems", &lambda {|node:, regexp:| connect appliances node, regexp, true, nil }). - opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil). - opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil) + opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil). + opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil) - cli_apl.cmd( :applications, "Table of provided applications", aliases: [nil], &lambda {|node:, regexp:| + cli_apl.cmd( :applications, "Table of provided applications", &lambda {|node:, regexp:| connect appliances node, regexp, nil, true }). - opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil). - opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil) + opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil). + opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil) cli_apl.cmd( :list, "List provided appliances", aliases: ['ls'], &lambda {|node=nil, regexp:| connect diff --git a/lib/pve/helper.rb b/lib/pve/helper.rb index 2aaa9a5..3c92065 100644 --- a/lib/pve/helper.rb +++ b/lib/pve/helper.rb @@ -94,7 +94,6 @@ class ColoredString end - class TablizedOutput def initialize header, stdout: nil, format: nil @header = header.map &:to_s @@ -125,10 +124,14 @@ class TablizedOutput def inspect() "#" end def to_s - y = (v*w).round - x = (100*v).round - r = "%*s" % [w, 0==x ? '·' : x] - "\e[1;4;#{0.75>v ? 32 : 31}m#{r[0...y]}\e[0m#{r[y..-1]}" + vw = v*w + percent = (100*v).round + vwi = vw.to_i + rounded = (vw+0.5).to_i + s = "%*s" % [w, 0==percent ? '·' : percent] + pre = "\e[1;4;#{0.75>v ? 32 : 31}m" + mid = (vw % 1) > 0.5 ? "\e[2m" : "\e[0m" + "#{pre}#{s[0...vwi]}#{mid}#{s[vwi]}\e[0m#{s[(vwi+1)..-1]}" end end @@ -161,8 +164,8 @@ class TablizedOutput @stdout.puts \ @header.each_with_index.map {|s, i| "#{' ' * (@maxs[i] - s.length)}#{s}" - }.join( ' ') - ls.each_with_index do |l| + }.join( ' | ') + ls.each_with_index do |l, i| @stdout.puts \ l.each_with_index.map {|s, i| pad = ' ' * (@maxs[i] - s.length) @@ -172,7 +175,7 @@ class TablizedOutput else "#{pad}#{s}" end - }.join( ' ') + }.join( "\e[3#{i.even? ? 6 : 3}m | \e[0m") end end From 4f6479a0d2bda3f9b94c1592b987db438d986ccc Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 12 Jan 2022 21:03:48 +0100 Subject: [PATCH 06/28] v0.2.4 --- .gitignore | 1 + lib/pve/version.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1977fa1..d033fef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.sw[pomnqrst] *.gem Gemfile.lock +.rake_tasks diff --git a/lib/pve/version.rb b/lib/pve/version.rb index ebeb6da..4b73043 100644 --- a/lib/pve/version.rb +++ b/lib/pve/version.rb @@ -1,3 +1,3 @@ module PVE - VERSION = '0.2.3' + VERSION = '0.2.4' end From 93889f9daba8bfc957f244423587d4865a2093d8 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 12 Jan 2022 23:33:01 +0100 Subject: [PATCH 07/28] colored host hostnames - determined by config --- lib/pve/cli.rb | 51 +++++++++++++++++++++++++++++++++++++++++++---- lib/pve/helper.rb | 28 -------------------------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/lib/pve/cli.rb b/lib/pve/cli.rb index b2bf2cc..5c232ca 100644 --- a/lib/pve/cli.rb +++ b/lib/pve/cli.rb @@ -183,13 +183,13 @@ class PVE::Cli end push = if target and status - lambda {|n| to.virt n if n === target and status.include?( n.state) } + lambda {|n| to.push tablized_virt( n) if n === target and status.include?( n.state) } elsif target - lambda {|n| to.virt n if n === target } + lambda {|n| to.push tablized_virt( n) if n === target } elsif status - lambda {|n| to.virt n if status.include? n.state } + lambda {|n| to.push tablized_virt( n) if status.include? n.state } else - to.method :virt + lambda {|n| to.push tablized_virt( n) } end yield push to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) } @@ -274,4 +274,47 @@ class PVE::Cli end to.print order: [1,2] end + + COLORS = %w[black red green yellow blue magenta cyan white].each_with_index.to_h + def fgcolor color + if /\Abright[-_]?(.*)\z/ =~ color + c = COLORS[$1] + c.nil? ? nil : "1;3#{c}" + else + c = COLORS[color] + c.nil? ? nil : "3#{c}" + end + end + + def tablized_virt v + ha = v.respond_to?( :ha) ? v.ha : nil + unknown = TablizedOutput::V.new 0, '-' + node = v.node.is_a?(String) ? v.node : v.node.node + if color = @cfg[:hosts]&.[](node.to_sym)&.[](:color) + node = ColoredString.new node, fgcolor( color) + end + [ + case v.status + when "running", "online" then ColoredString.new v.status, "32" + when "stopped" then ColoredString.new v.status, "31" + else v.status + end, + ha&.state || '·', + 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" + else v.sid + end, + v.name, node, + v.respond_to?(:uptime) ? TablizedOutput::V.new( v.uptime, Measured.seconds( v.uptime)) : unknown, + v.respond_to?(:cpu) ? TablizedOutput::Percentage.new( v.cpu) : unknown, + v.respond_to?(:mem) ? TablizedOutput::V.new( v.mem, Measured.bytes( v.mem)) : unknown, + v.respond_to?(:maxmem) ? TablizedOutput::Percentage.new( v.mem/v.maxmem.to_f) : unknown, + v.respond_to?(:disk) ? TablizedOutput::V.new( v.disk.to_i, Measured.bytes( v.disk.to_i)) : unknown, + if v.respond_to?(:maxdisk) and 0 < v.maxdisk.to_i + TablizedOutput::Percentage.new( v.disk.to_f/v.maxdisk.to_f) + else unknown end, + ] + end end diff --git a/lib/pve/helper.rb b/lib/pve/helper.rb index 3c92065..45f0515 100644 --- a/lib/pve/helper.rb +++ b/lib/pve/helper.rb @@ -178,32 +178,4 @@ class TablizedOutput }.join( "\e[3#{i.even? ? 6 : 3}m | \e[0m") end end - - def virt v - ha = v.respond_to?( :ha) ? v.ha : nil - unknown = V.new 0, '-' - push [ - case v.status - when "running", "online" then ColoredString.new v.status, "32" - when "stopped" then ColoredString.new v.status, "31" - else v.status - end, - ha&.state || '·', - 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" - else v.sid - end, - v.name, v.node.is_a?(String) ? v.node : v.node.node, - v.respond_to?(:uptime) ? V.new( v.uptime, Measured.seconds( v.uptime)) : unknown, - v.respond_to?(:cpu) ? Percentage.new( v.cpu) : unknown, - v.respond_to?(:mem) ? V.new( v.mem, Measured.bytes( v.mem)) : unknown, - v.respond_to?(:maxmem) ? Percentage.new( v.mem/v.maxmem.to_f) : unknown, - v.respond_to?(:disk) ? V.new( v.disk.to_i, Measured.bytes( v.disk.to_i)) : unknown, - if v.respond_to?(:maxdisk) and 0 < v.maxdisk.to_i - Percentage.new( v.disk.to_f/v.maxdisk.to_f) - else unknown end, - ] - end end From d558af77b6669431cfdef75b7bb63a7b8b3ca909 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 5 Oct 2022 12:55:03 +0200 Subject: [PATCH 08/28] v0.3.0: >=ruby2.7. PVE-6 not supported anymore. small fixes. more comfort. commands on more than one machine at once. --- lib/pve/cli.rb | 61 +++++++++++++++------- lib/pve/cli/base.rb | 89 +++++++++++++++++++++++---------- lib/pve/cli/ct.rb | 30 +++++++---- lib/pve/cli/qm.rb | 55 +++++++++++++++++--- lib/pve/cli/storage.rb | 2 +- lib/pve/cli/task.rb | 16 ++++++ lib/pve/proxmox.rb | 111 +++++++++++++++++++++++++++++++---------- lib/pve/templates.rb | 23 +++++++-- lib/pve/version.rb | 2 +- pve.gemspec | 6 ++- 10 files changed, 302 insertions(+), 93 deletions(-) diff --git a/lib/pve/cli.rb b/lib/pve/cli.rb index 5c232ca..c9c68e5 100644 --- a/lib/pve/cli.rb +++ b/lib/pve/cli.rb @@ -39,8 +39,9 @@ class PVE::Cli def initialize config_file = nil config_file ||= '/etc/pve/pvecli.yml' @cfg = - YAML.safe_load File.read( config_file), [], [], false, - config_file, symbolize_names: true + YAML.safe_load File.read( config_file), permitted_classes: [], + permitted_symbols: [], aliases: false, filename: config_file, + symbolize_names: true @cli = DenCli.new File.basename($0), "PVE CLI" @interactive = false prepare @@ -56,15 +57,14 @@ class PVE::Cli end def wait_state host, state, timeout: nil, lock: nil - spinners = %w[- / | \\] - spin = 0 + spinners, spin = "▖▘▝▗", 0 r = host.wait state, lock: lock, timeout: timeout, secs: 0.2 do |state, lock| lock = " (locked: #{lock})" if lock - STDERR.printf "\r[%s] %s Still %s%s...\e[J\n", host.name, spinners[spin = (spin + 1) % 4], state, lock + STDERR.printf "\r[%s] \e[33;1m%s\e[0m Still %s%s...\e[J", host.name, spinners[spin = (spin + 1) % 4], state, lock end STDERR.printf "\r\e[J" - exit 1 unless interactive? and r + exit 1 if interactive? and !r end def task_log task, logn, limit = 1024 @@ -78,7 +78,7 @@ class PVE::Cli logn end - def wait task, secs: nil, text: nil + def wait task, secs: nil, text: nil, timeout: nil secs ||= 0.1 spinners, spin, logn = "▖▘▝▗", 0, 0 STDERR.puts task.upid @@ -107,18 +107,24 @@ class PVE::Cli end end - def start host, node: nil, timeout: nil, fire: nil, secs: nil + def migrate host, node, timeout: nil, fire: nil, secs: nil, online: nil timeout ||= 30 - if node - task = host.migrate Proxmox::Node.find_by_name!( node) - wait task, text: "Migrating" - end + task = host.migrate Proxmox::Node.find_by_name!( node), online: online || false + wait task, text: "Migrating", timeout: timeout if fire + end + + def start host, node: nil, timeout: nil, fire: nil, secs: nil + timeout ||= 60 if host.running? STDERR.puts "Already running." return end task = host.start - wait task, text: "Starting" unless fire + t = Time.now + unless fire + wait task, text: "Starting", timeout: timeout + wait_state host, :running, timeout: timeout-(Time.now-t) if host.ha.exist? and not host.running? + end end def stop host, timeout: nil, fire: nil, secs: nil @@ -128,12 +134,14 @@ class PVE::Cli return end task = host.stop + t = Time.now unless fire - wait task, text: "Stopping" + wait task, text: "Stopping", timeout: timeout + wait_state host, :stopped, timeout: timeout-(Time.now-t) if host.ha.exist? and not host.stopped? end end - def create klass, template, timeout: nil, fire: nil, secs: nil, start: nil, **options + def create klass, template, timeout: nil, fire: nil, start: nil, **options options[:start] = fire && start task = klass.create template, **options return if fire @@ -154,8 +162,7 @@ class PVE::Cli end def node_opt node = nil - node &&= /\A#{node}\z/ - node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all + node ? [Proxmox::Node.find_by_name!( node)] : Proxmox::Node.all end def target_opt target = nil, &exe @@ -250,12 +257,29 @@ class PVE::Cli def call *argv cli.call *argv rescue RestClient::ExceptionWithResponse - STDERR.puts "#$! - #{$!.response} (#{$!.class})" #, $!.backtrace.map {|b|" #{b}"} + STDERR.puts "#{$!.request}: #{$!.to_s} - #{$!.response} (#{$!.class})" #, $!.backtrace.map {|b|" #{b}"} rescue UsageError, DenCli::UsageError STDERR.puts $! exit 1 end + def per_argument arguments, print: nil, &exe + arguments.each do |argument| + STDERR.printf print, argument if print + begin + yield argument + rescue RestClient::ExceptionWithResponse + STDERR.puts "#{$!.request}: #{$!.to_s} - #{JSON.parse( $!.response.body)}" + rescue UsageError, DenCli::UsageError + STDERR.puts $! + rescue RestClient::BadRequest + STDERR.puts $!.message.inspect + rescue SystemExit + STDERR.puts "Failed with exit code: #{$!.status}" if 0 < $!.status + end + end + end + def appliances node, regexp, system, applications system = applications = true if system.nil? and applications.nil? node = node ? Proxmox::Node.find_by_name!( node) : Proxmox::Node.all.first @@ -277,6 +301,7 @@ class PVE::Cli COLORS = %w[black red green yellow blue magenta cyan white].each_with_index.to_h def fgcolor color + color = color.to_s if /\Abright[-_]?(.*)\z/ =~ color c = COLORS[$1] c.nil? ? nil : "1;3#{c}" diff --git a/lib/pve/cli/base.rb b/lib/pve/cli/base.rb index 8c031bc..00599a6 100644 --- a/lib/pve/cli/base.rb +++ b/lib/pve/cli/base.rb @@ -27,6 +27,14 @@ def cli_base opt( :node, '-n', '--node=NODE', "List only hosted by this NODE"). opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)") + def val2str v + case v + when true then 1 + when false then 0 + else v + end + end + def prepare_show_config cnf r = {} cnf.each do |k,v| @@ -37,18 +45,15 @@ def cli_base net. reject {|k, v| :card == k }. sort_by {|k, v| :name == k ? :AAAAAAAAA : k }. - map {|k, v| case v when true then [k,1] when false then [k,0] else [k,v] end }. - map {|k, v| "#{k}=#{v}" } - r[net[:card].to_sym] = s.join(",") + map {|k, v| "#{k}=#{val2str v}" } + r[net[:card].to_sym] = s.join ',' end when :sshkeys - r[k] = CGI.unescape(v).gsub( /^/, " "*14).gsub /\A {14}|\n\z/, '' + r[k] = CGI.unescape( v).gsub( /^/, " "*14).gsub /\A {14}|\n\z/, '' + when :features + r[k] = v.map {|k, v| "#{k}=#{val2str v}" }.join ',' else - case v - when true then v = 1 - when false then v = 0 - end - r[k] = v.to_s.gsub( /$^/, " "*14).gsub /\n\z/, '' + r[k] = val2str( v).to_s.gsub( /$^/, " "*14).gsub /\n\z/, '' end end r @@ -56,20 +61,21 @@ def cli_base def show_config cnf, old = nil cnf = prepare_show_config cnf + l = cnf.keys.map( &:length).max if old old = prepare_show_config old (cnf.keys+old.keys).uniq.sort.each do |k| v, o = cnf[k], old[k] if v == o - puts "#{k}:#{' ' * (12-k.length)} #{v}" + puts "#{k}:#{' ' * (l-k.length)} #{v}" else - puts "\e[31m#{k}:#{' ' * (12-k.length)} #{o}\e[0m" unless o.nil? - puts "\e[32m#{k}:#{' ' * (12-k.length)} #{v}\e[0m" unless v.nil? + puts "\e[31m#{k}:#{' ' * (l-k.length)} #{o}\e[0m" unless o.nil? + puts "\e[32m#{k}:#{' ' * (l-k.length)} #{v}\e[0m" unless v.nil? end end else - cnf.sort_by{|k,v|k}.each do |k,v| - puts "#{k}:#{' ' * (12-k.length)} #{v}" + cnf.sort_by {|k,_| k }.each do |k,v| + puts "#{k}:#{' ' * (l-k.length)} #{v}" end end end @@ -86,17 +92,26 @@ def cli_base opts = {} until args.empty? case arg = args.shift + when /\A--no-(\w+)\z/ + opts[$1.to_sym] = false when /\A--(\w+)=(.*)\z/ opts[$1.to_sym] = $2 + when /\A--(\w+)!\z/, /\A--del-(\w+)\z/ + opts[$1.to_sym] = nil when /\A--(\w+)\z/ - opts[$1.to_sym] = args.shift + n = $1.to_sym + opts[n] = + if args.empty? or /\A--/ == args.first + true + else opts[n] = args.shift + end else raise UsageError, "Expection option to set. What do you mean with: #{arg}" end end opts.each do |k, v| opts[k] = - case v = opts[k] + case v when '' then nil else v end @@ -121,14 +136,15 @@ def cli_base next unless opts.has_key? k opts[k] = case v = opts[k] - when *%w[1 T TRUE t true True Y YES y yes Yes] then true - when *%w[0 F FALSE f false False N NO n no No] then false + when true, *%w[1 T TRUE t true True Y YES y yes Yes] then true + when false, *%w[0 F FALSE f false False N NO n no No] then false when '', 'nil', nil then nil else raise UsageError, "Boolean expected, given: #{v.inspect}" end end connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) + raise UsageError, "Container or Node not found: #{name_or_id}" unless th old = th.config opts[:digest] ||= old[:digest] th.cnfset opts @@ -138,6 +154,7 @@ def cli_base ccli.cmd :show, "Show Config of CT/VM", &lambda {|name_or_id| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) + raise UsageError, "Container or Node not found: #{name_or_id}" unless th show_config th.config } end @@ -154,11 +171,14 @@ def cli_base end end - cli.cmd( :run, "Starts CT/VM", aliases: %w[start star], &lambda {|name_or_id| + cli.cmd( :run, "Starts CT/VM", aliases: %w[start star], &lambda {|*names_or_ids| connect - th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) - raise UsageError, "Container or Node not found: #{name_or_id}" unless th - start th + raise UsageError, "Nothing to start?" if names_or_ids.empty? + per_argument names_or_ids, print: "\e[34;1mRunning CT/VM\e[0m %s:\n" do |name_or_id| + th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) + raise UsageError, "Container or Node not found: #{name_or_id}" unless th + start th + end }). completion do |*pre, arg| completion_helper *pre, arg do |f| @@ -166,6 +186,21 @@ def cli_base end end + cli.cmd( :migrate, "Migrates (halted) CTs/VMs to an other host", min: 2, &lambda {|target, *names_or_ids, fire:, timeout:, secs:| + connect + node = Proxmox::Node.find_by_name! target + per_argument names_or_ids, print: "\e[1;34mCT/VM(s)\e[0m %s:\n" do |name_or_id| + th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) + raise UsageError, "CT/VM not found: #{name_or_id}" unless th + unless th.stopped? + raise UsageError, "VM #{name_or_id} is running. Shutdown or for VM-online-migration see `help qm migrate`" if Proxmox::Qemu === th + raise UsageError, "CT #{name_or_id} is running. You have to shutdown it." + end + task = th.migrate node + wait task, text: "Migrating", timeout: timeout unless fire + end + }).tap {|c| opts_wait c } + #cli.cmd :reboot, "Reboot CT/VM (not implemented, yet)", min: 6, &lambda {|name_or_id| # connect # th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) @@ -173,11 +208,13 @@ def cli_base # reboot th #} - cli.cmd( :stop, "Stops CT/VM", min: 4, &lambda {|name_or_id| + cli.cmd( :stop, "Stops CT/VM", min: 4, &lambda {|*names_or_ids| connect - th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) - raise UsageError, "Container or Node not found: #{name_or_id}" unless th - stop th + per_argument names_or_ids, print: "\e[34;1mStopping CT/VM\e[0m %s:\n" do |name_or_id| + th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) + raise UsageError, "Container or Node not found: #{name_or_id}" unless th + stop th + end }). completion do |*pre, arg| completion_helper *pre, arg do |f| diff --git a/lib/pve/cli/ct.rb b/lib/pve/cli/ct.rb index a1fb79b..0371394 100644 --- a/lib/pve/cli/ct.rb +++ b/lib/pve/cli/ct.rb @@ -31,18 +31,30 @@ def cli_ct STDERR.puts "! #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).exec *command } - ct_cli.cmd( :start, "Starts CT", min: 4, &lambda {|name_or_id, node: nil, fire:, timeout:, secs:| + ct_cli.cmd( :migrate, "Migrates halted CT(s) to an other host", min: 2, &lambda {|target, *names_or_ids, fire:, timeout:, secs:| connect - ct = Proxmox::LXC.find! name_or_id - start ct, node: node, fire: fire, timeout: timeout, secs: secs - }). - opt( :node, "-nNODE", "--node=NODE", "On NODE (default, as is, so without migration)"). - tap {|c| opts_wait c } + node = Proxmox::Node.find_by_name! target + per_argument names_or_ids, print: "\e[1;34mMigrate CT %s:\e[0m" do |name_or_id| + ct = Proxmox::LXC.find! name_or_id + task = ct.migrate node + wait task, text: "Migrating", timeout: timeout unless fire + end + }).tap {|c| opts_wait c } - ct_cli.cmd( :stop, "Stops CT", min: 3, &lambda {|name_or_id, fire: nil, timeout:, secs:| + ct_cli.cmd( :start, "Starts CT(s)", min: 4, &lambda {|*names_or_ids, node: nil, fire:, timeout:, secs:| connect - ct = Proxmox::LXC.find! name_or_id - stop ct, fire: fire, timeout: timeout, secs: secs + per_argument names_or_ids, print: "\e[1;34mStart CT %s:\e[0m" do |name_or_id| + ct = Proxmox::LXC.find! name_or_id + start ct, node: node, fire: fire, timeout: timeout, secs: secs + end + }).tap {|c| opts_wait c } + + ct_cli.cmd( :stop, "Stops CT(s)", min: 3, &lambda {|*names_or_ids, fire: nil, timeout:, secs:| + connect + per_argument names_or_ids, print: "\e[1;34mStart CT %s:\e[0m" do |name_or_id| + ct = Proxmox::LXC.find! name_or_id + stop ct, fire: fire, timeout: timeout, secs: secs + end }).tap {|c| opts_wait c } ct_cli.cmd( :wait, "Wait till CT is in state", &lambda {|name_or_id, state, timeout: nil, secs: nil| diff --git a/lib/pve/cli/qm.rb b/lib/pve/cli/qm.rb index 58dfb95..351c2e3 100644 --- a/lib/pve/cli/qm.rb +++ b/lib/pve/cli/qm.rb @@ -1,7 +1,7 @@ class PVE::Cli def cli_qm - cli.sub :qm, "Virtual Machines", aliases: %w[v vm qemu], &lambda {|qm| - qm.cmd :list, "List VM-IDs", aliases: ['ls'], &lambda {|node=nil| + cli.sub :qm, "Virtual Machines", aliases: %w[v vm qemu], &lambda {|qm_cli| + qm_cli.cmd :list, "List VM-IDs", aliases: ['ls'], &lambda {|node=nil| connect nodes = Proxmox::Node.all nodes = nodes.select {|n| node == n.name } if node @@ -10,11 +10,52 @@ def cli_qm end.sort.each {|c| puts c } } - qm.cmd( :status, "Lists CTs with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil| + qm_cli.cmd( :migrate, "Migrates VM(s) to an other host", min: 2, &lambda {|target, *names_or_ids, fire:, timeout:, secs:|#, online:, restart:| + #if online and restart + # raise UsageError, "You have to decide for one migration-mode: --restart or --online." + #end + #online = !restart or online + connect + node = Proxmox::Node.find_by_name! target + per_argument names_or_ids, print: "\e[1;34mVM\e[0m %s:\n" do |name_or_id| + qm = Proxmox::Qemu.find! name_or_id + task = qm.migrate node #, online: online + wait task, text: "Migrating", timeout: timeout unless fire + end + }). + #opt( :online, '-o', '--online', "Online-migration: Does not shutdown or interrupt running VM. Opposite of --online, Default", default: nil). + #opt( :restart, '-r', '--restart', "Restart-migration: Does shutdown or interrupt running VM. Opposite of --restart", default: nil). + tap {|c| opts_wait c } + + qm_cli.cmd( :start, "Starts VM(s)", min: 4, &lambda {|*names_or_ids, node: nil, fire:, timeout:, secs:| + connect + per_argument names_or_ids, print: "\e[1;34mStart VM %s:\e[0m" do |name_or_id| + qm = Proxmox::Qemu.find! name_or_id + start qm, node: node, fire: fire, timeout: timeout, secs: secs + end + }).tap {|c| opts_wait c } + + qm_cli.cmd( :stop, "Stops VM(s)", min: 3, &lambda {|*names_or_ids, fire: nil, timeout:, secs:| + connect + per_argument names_or_ids, print: "\e[1;34mStart VM %s:\e[0m" do |name_or_id| + qm = Proxmox::Qemu.find! name_or_id + stop qm, fire: fire, timeout: timeout, secs: secs + end + }).tap {|c| opts_wait c } + + qm_cli.cmd( :wait, "Wait till VM is in state", &lambda {|name_or_id, state, timeout: nil, secs: nil| + connect + qm = Proxmox::Qemu.find! name_or_id + wait qm, state, timeout: timeout, secs: secs + }). + opt( :timeout, "-tTIMEOUT", "--timeout=TIMEOUT", "Wait for max TIMEOUT seconds (default: endless)", default: nil). + opt( :secs, "-sSECONDS", "--seconds=SECONDS", "Check every SECONDS for state (default: 0.2)", default: 0.2) + + qm_cli.cmd( :status, "Lists VMs with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil| hosting_table target: target, state: state, sort: sort do |push| node_opt( node). each( &push).lazy. - flat_map {|n| [ Thread.new( n, &:lxc), Thread.new( n, &:qemu) ] }. + flat_map {|n| [ Thread.new( n, &:qemu) ] }. each {|n| n.value.each &push } end }). @@ -22,19 +63,19 @@ def cli_qm opt( :node, '-n', '--node=NODE', "List only hosted by this NODE"). opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)") - qm.cmd :exec, "Executes Command in VM via qemu-guest-agent", min: 4, &lambda {|name_or_id, *command| + qm_cli.cmd :exec, "Executes Command in VM via qemu-guest-agent", min: 4, &lambda {|name_or_id, *command| connect STDERR.puts "! #{$?.exitstatus}" unless Proxmox::Qemu.find!( name_or_id).exec *command } - qm.cmd( :resize, 'Resize a disk', &lambda {|name_or_id, disk, size| + qm_cli.cmd( :resize, 'Resize a disk', &lambda {|name_or_id, disk, size| connect qm = Proxmox::Qemu.find! name_or_id task = qm.resize disk, size wait task, text: "Resizing #{qm.sid} #{disk} to #{size}" }) - qm.cmd 'help', '', aliases: ['-h', '--help'], &lambda {|*args| help qm, *args } + qm_cli.cmd 'help', '', aliases: ['-h', '--help'], &lambda {|*args| help qm_cli, *args } } end end diff --git a/lib/pve/cli/storage.rb b/lib/pve/cli/storage.rb index 5503ac5..32cf5b3 100644 --- a/lib/pve/cli/storage.rb +++ b/lib/pve/cli/storage.rb @@ -40,7 +40,7 @@ def cli_storage storage = node.storage.select {|sm| storage == sm.storage }.first storage.content.each {|c| puts c.to_s } } - cli_cnt.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help cli_cnt, *args } + cli_cnt.cmd( :help, '', aliases: ['-h', '--help', nil]) {|*args| help cli_cnt, *args } end cli_sm.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help cli_sm, *args } diff --git a/lib/pve/cli/task.rb b/lib/pve/cli/task.rb index b1c420d..3b1d201 100644 --- a/lib/pve/cli/task.rb +++ b/lib/pve/cli/task.rb @@ -21,6 +21,22 @@ def cli_task end end } + + tcli.cmd :status, "Shows tasks states", &lambda {|upid| + } + + tcli.cmd :monitor, "Monitors running tasks", &lambda {|node: nil| + connect + nodes = Proxmox::Node.all + nodes = nodes.select {|n| node == n.name } if node + tasks = {} + loop do + nodes.flat_map do |n| + n.tasks.map &:upid + end.sort.each do |upid| + end + end + } end end end diff --git a/lib/pve/proxmox.rb b/lib/pve/proxmox.rb index c2ab96d..45e5aed 100644 --- a/lib/pve/proxmox.rb +++ b/lib/pve/proxmox.rb @@ -5,6 +5,13 @@ require 'ipaddress' require 'shellwords' require 'active_support/all' +class RestClient::Exception + attr_reader :request + def to_s + "#{request|"?"}: #{message}: #{response}" + end +end + module Proxmox class Exception < ::Exception end @@ -13,6 +20,30 @@ module Proxmox class AlreadyExists < Exception end + def self.cnfstr2hash str + str. + split( ','). + map do |o| + k, v = o.split( '=', 2) + [k.to_sym, v] + end. + to_h + end + + def self.hash2cnfstr **opts + opts. + map do |k, v| + v = + case v + when true then 1 + when false then 0 + else v.to_s + end + "#{k}=#{v}" + end. + join( ',') + end + def self.connect username, password, realm: nil, verify_tls: nil, uri: nil uri ||= 'https://localhost:8006/api2/json' cred = @@ -95,19 +126,37 @@ module Proxmox def rest_get path, **data, &exe data = data.delete_if {|k,v|v.nil?} path += "#{path.include?( ??) ? ?& : ??}#{data.map{|k,v|"#{CGI.escape k.to_s}=#{CGI.escape v.to_s}"}.join '&'}" unless data.empty? + STDERR.puts "GET #{path} <= #{data}" if $DEBUG __response__ Proxmox.connection[path].get( __headers__( :'Content-Type' => 'application/json')) + rescue RestClient::Exception + $!.instance_variable_set :@request, "GET #{path}" + raise end def rest_put path, **data, &exe - __response__ Proxmox.connection[path].put( __data__( data), __headers__( :'Content-Type' => 'application/json')) + data = __data__( data) + STDERR.puts "PUT #{path} <= #{data}" if $DEBUG + __response__ Proxmox.connection[path].put( data, __headers__( :'Content-Type' => 'application/json')) + rescue RestClient::Exception + $!.instance_variable_set :@request, "PUT #{path}" + raise end def rest_del path, &exe + STDERR.puts "DELETE #{path}" if $DEBUG __response__ Proxmox.connection[path].delete( __headers__( :'Content-Type' => 'application/json')) + rescue RestClient::Exception + $!.instance_variable_set :@request, "DELETE #{path}" + raise end def rest_post path, **data, &exe - __response__ Proxmox.connection[path].post( __data__( data), __headers__( :'Content-Type' => 'application/json')) + data = __data__( data) + STDERR.puts "POST #{path} <= #{data}" if $DEBUG + __response__ Proxmox.connection[path].post( data, __headers__( :'Content-Type' => 'application/json')) + rescue RestClient::Exception + $!.instance_variable_set :@request, "POST #{path}" + raise end end @@ -295,15 +344,6 @@ module Proxmox @name =~ t or @vmid.to_s =~ t or @sid =~ t end - def migrate node - node = - case node - when Node then node - else Node.find!( node.to_s) - end - Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/migrate", target: node.node) - end - def start Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/status/start") end @@ -350,23 +390,22 @@ module Proxmox map( &:to_s). grep( /\Anet\d+\z/). map do |k| - nc = {card: k} - cnf.delete( k.to_sym). - split( ','). - each do |f| - k, v = f.split( '=', 2) - nc[k.to_sym] = v - end - nc[:ip] &&= IPAddress::IPv4.new nc[:ip] - nc[:gw] &&= IPAddress::IPv4.new nc[:gw] - nc[:mtu] &&= nc[:mtu].to_i - nc[:tag] &&= nc[:tag].to_i + nc = Proxmox::cnfstr2hash cnf.delete( k.to_sym) + nc[:card] = k + nc[:ip] &&= IPAddress::IPv4.new nc[:ip] + nc[:gw] &&= IPAddress::IPv4.new nc[:gw] + nc[:mtu] &&= nc[:mtu].to_i + nc[:tag] &&= nc[:tag].to_i nc[:firewall] &&= 1 == nc[:firewall].to_i nc end + cnf[:features] &&= + Proxmox::cnfstr2hash( cnf[:features]). + tap {|f| f[:nesting] &&= '1' == f[:nesting] } cnf[:unprivileged] &&= 1 == cnf[:unprivileged] - cnf[:memory] &&= cnf[:memory].to_i - cnf[:cores] &&= cnf[:cores].to_i + cnf[:memory] &&= cnf[:memory].to_i + cnf[:cores] &&= cnf[:cores].to_i + cnf.update cnf.delete( :lxc).map{|k,v|[k.to_sym,v]}.to_h if cnf[:lxc] cnf end @@ -381,6 +420,7 @@ module Proxmox end end r.delete :delete if r[:delete].empty? + STDERR.puts r.inspect rest_put "#{@rest_prefix}/config", r end @@ -461,6 +501,15 @@ module Proxmox upid = rest_put "#{@rest_prefix}/resize", disk: disk, size: size Task.send :__new__, node: @node, host: self, upid: upid end + + def migrate target, online: nil + target = + case target + when Node then target + else Node.find!( target.to_s) + end + Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/migrate", target: target.node, online: ! !online) + end end class LXC < Hosted @@ -551,6 +600,7 @@ module Proxmox #}.map {|k,v| "#{k}=#{v}" }.join( ','), swap: tmplt.swap, unprivileged: tmplt.unprivileged, + features: tmplt.features ? Proxmox::hash2cnfstr( tmplt.features) : nil, }.delete_if {|k,v| v.nil? } temp = LXC.send :__new__, node: node, vmid: options[:vmid], name: name, hostname: options[:hostname] @@ -579,6 +629,15 @@ module Proxmox def enter node.enter 'pct', 'enter', vmid end + + def migrate target + target = + case target + when Node then target + else Node.find!( target.to_s) + end + Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/migrate", target: target.node) + end end class HA < Base @@ -628,7 +687,8 @@ module Proxmox end def state= state - rest_put "#{@rest_prefix}", state: state.to_s + r = rest_put "#{@rest_prefix}", state: state.to_s + pp r: r refresh! end @@ -651,6 +711,7 @@ module Proxmox def stopped?() 'stopped' == self.state end def error?() 'error' == self.state end def active?() ! ! digest end + def exist?() ! ! digest end end class Storage < Base diff --git a/lib/pve/templates.rb b/lib/pve/templates.rb index 617271c..31beaba 100644 --- a/lib/pve/templates.rb +++ b/lib/pve/templates.rb @@ -20,7 +20,18 @@ module PVE::CTTemplate def hostname() options.hostname || name end def memory() options.memory || 1024 end def swap() options.swap || 0 end - def unprivileged() options.unprivileged() || 1 end + def unprivileged() options.unprivileged || 1 end + + def features + r = + if options.features + Proxmox::cnfstr2hash( options.features). + tap {|f| f[:nesting] &&= 1 == f[:nesting] } + else {} + end + r[:nesting] = options.nesting.nil? ? true : options.nesting + r + end def ssh_public_keys options[:'ssh-public-keys'] || @@ -82,6 +93,8 @@ module PVE::CTTemplate ipv6: [:string, false, "IPv6-Address with net-size or auto."], gateway6: [:string, false, "IPv6-Address of gateway."], storage: [:string, false, "Device will be create on this Storage (default: local"], + features: [:string, false, "Features"], + nesting: [:boolean, false, "Support nested containers (->Features)"], } end end @@ -122,12 +135,14 @@ module PVE::CTTemplate gateway6: [:string, false, "IPv6-Address of gateway."], storage: [:string, false, "Device will be create on this Storage (default: root)"], ostemplate: [:string, false, "OS-Template eg. local:vztmpl/superlinux-1.2-amd64.tar.xz"], + features: [:string, false, "Features"], + nesting: [:boolean, false, "Support nested containers (->Features)"], } end - def node() options.node || 'svc1' end - def ostype() options.ostype || 'debian' end - def memory() options.memory || 2048 end + def node() options.node || 'svc1' end + def ostype() options.ostype || 'debian' end + def memory() options.memory || 2048 end def storage() options.storage || 'root' end def network_id diff --git a/lib/pve/version.rb b/lib/pve/version.rb index 4b73043..96003f2 100644 --- a/lib/pve/version.rb +++ b/lib/pve/version.rb @@ -1,3 +1,3 @@ module PVE - VERSION = '0.2.4' + VERSION = '0.3.0' end diff --git a/pve.gemspec b/pve.gemspec index 05eefc0..79680a8 100644 --- a/pve.gemspec +++ b/pve.gemspec @@ -12,8 +12,10 @@ Gem::Specification.new do |spec| spec.homepage = "https://git.denkn.at/deac/pve" # Ruby3 interpret **opts in another way than before. # 2.7.0 should work with both(?) behaviours. - # PVE based on debian, so ruby 2.5 is default. - spec.required_ruby_version = Gem::Requirement.new "~> 2.5.0" + # 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) + # Old behaviour will be droped. + spec.required_ruby_version = Gem::Requirement.new "~> 2.7" spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = spec.homepage From 20db6bd9b2a7fcb114ba980b9d8b5698771b8612 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Mon, 16 Jan 2023 17:30:23 +0100 Subject: [PATCH 09/28] Tags, ct volume_move, PVE::Output, small fixes. Proxmox supports tags, so class Proxmox supports tags, too. It is possible to show and change tags in config and `e status` prints tags. The new command `e ct volume_move NAMEORID DISK POOL` "moves" the disk of a CT to an other RBD-pool. It only supports RBD, yet. It uses XFS on the new device. The CT must be stopped on the same machine, you are calling the command. The class PVE::Output helps to print status. Supports info, failed and progress (with a spinner). Monitoring tasks used a custom spinner, which is now part of the new PVE::Output. The status of CTs/VMs/Nodes are symbols only. Strings shouldn't be used anymore. --- lib/pve/cli.rb | 304 +++++++++++++++++++++++++++++++++++++------- lib/pve/cli/base.rb | 7 +- lib/pve/cli/ct.rb | 25 ++-- lib/pve/cli/qm.rb | 7 +- lib/pve/helper.rb | 9 +- lib/pve/proxmox.rb | 66 ++++++++-- 6 files changed, 349 insertions(+), 69 deletions(-) diff --git a/lib/pve/cli.rb b/lib/pve/cli.rb index c9c68e5..172fbbb 100644 --- a/lib/pve/cli.rb +++ b/lib/pve/cli.rb @@ -11,9 +11,6 @@ require_relative 'cli/qm' require_relative 'cli/node' require_relative 'cli/storage' -class UsageError " prepare end @@ -52,18 +117,18 @@ class PVE::Cli end def enter host, *args + @out.host = host.name r = host.enter *args - STDERR.puts "! #{$?.exitstatus}" unless r + operational_error "Exitstatus: #{$?.exitstatus}" unless r end def wait_state host, state, timeout: nil, lock: nil - spinners, spin = "▖▘▝▗", 0 + @out.host = host.name r = host.wait state, lock: lock, timeout: timeout, secs: 0.2 do |state, lock| - lock = " (locked: #{lock})" if lock - STDERR.printf "\r[%s] \e[33;1m%s\e[0m Still %s%s...\e[J", host.name, spinners[spin = (spin + 1) % 4], state, lock + @out.progress "Still %s%s...", state, lock ? " (locked: #{lock})" : '' end - STDERR.printf "\r\e[J" + @out.reset exit 1 if interactive? and !r end @@ -71,7 +136,7 @@ class PVE::Cli log = task.log start: logn, limit: limit log = [] if [{n: 1, t: 'no content'}] == log unless log.empty? - STDERR.printf "\r\e[J" + @out.reset log.each {|l| puts l[:t] } logn = log.last[:n] end @@ -80,9 +145,9 @@ class PVE::Cli def wait task, secs: nil, text: nil, timeout: nil secs ||= 0.1 - spinners, spin, logn = "▖▘▝▗", 0, 0 - STDERR.puts task.upid - host = task.host&.name + logn = 0 + STDOUT.puts task.upid + @out.host = host = task.host&.name || '<>' loop do s = task.status logn = self.task_log task, logn @@ -92,17 +157,14 @@ class PVE::Cli break if 0 == logn - r logn = r end - 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 + if s.successfull? + @out.success "%s %s", text && "#{text}:", s.stopped? ? :finished : s.status + else + @out.failed "%s %s", text && "#{text}:", s.stopped? ? :finished : s.status + end return s end - STDERR.printf "\r[%s] \e[33;1m%s\e[0m %s...\e[J", - host || s[:id], - spinners[spin = (spin + 1) % 4], - text || "Working" + @out.progress text || "Working" sleep secs end end @@ -116,7 +178,7 @@ class PVE::Cli def start host, node: nil, timeout: nil, fire: nil, secs: nil timeout ||= 60 if host.running? - STDERR.puts "Already running." + condition_violation "Already running." return end task = host.start @@ -130,7 +192,7 @@ class PVE::Cli def stop host, timeout: nil, fire: nil, secs: nil timeout ||= 30 if host.stopped? - STDERR.puts "Already stopped." + preset_error "Already stopped." return end task = host.stop @@ -161,6 +223,137 @@ class PVE::Cli end end + def rbd_device_map image_spec, &exe + dev = IO.popen( [*%w[rbd device map], image_spec]) {|io| io.read }.chomp + raise OperationalError, "Device mapping #{image_spec} localy failed." unless $?.success? + begin + yield dev + ensure + system *%w[rbd device unmap], image_spec + raise OperationalError, "Device unmapping #{image_spec} failed." unless $?.success? + end + end + + def rbd_devices_maps spec, *specs, &exe + rbd_device_map spec do |dev| + if specs.empty? + yield dev + else + rbd_devices_maps *specs do |*devs| + yield dev, *devs + end + end + end + end + + def mount dev, mp, &exe + system 'mount', dev.to_s, mp.to_s + begin yield + ensure + # it could be possible, that a process is working in fs, yet. + sleep 1 + system 'umount', mp.to_s + end + end + + def ct_volume_move ct, disk, destination + @out.host = ct.name + host = Proxmox::Node.find_by_name! Socket.gethostname + unless host.node == ct.node.node + ConditionViolation( "CT is hosted on #{ct.node.node}, not local (#{Socket.gethostname})") + end + # Lock, we do not want changes, while we check/prepare, + # these checks could be obsolete while moving. + configupdate = {} + ct.lock :migrate do + disk = + case disk + when 'rootfs', /\Amp\d+\z/ then disk.to_sym + else UsageError( "Unknown disk #{disk}") + end + ctcnf = ct.config + UsageError( "CT has no disk #{disk}") if ctcnf[disk].nil? + UsageError( "CT not stopped, yet.") unless ct.stopped? + dest = host.storage.find {|x| destination == x.storage } + ConditionViolation( "Destination storage #{dest.storage} disabled") unless dest.enabled? + ConditionViolation( "Destination storage #{dest.storage} not active") unless dest.active? + + unless /\A([^:]+):([^,]+)(,.+)?\z/ =~ ctcnf[disk] + OperationalError( "disk-specification cannot be parsed: [#{disk}: #{ctcnf[disk]}]") + end + source, name, diskopts = $1, $2, $3.to_s + src = host.storage.find {|x| source == x.storage } + + ConditionViolation( "Source storage #{dest.storage} disabled") unless dest.enabled? + ConditionViolation( "Source storage #{dest.storage} not active") unless dest.active? + case src.type + when 'rbd' + else ConditionViolation( "Storage type #{src.type} not supported as source. (supported: rbd only, yet)") + end + #unless c = src.content.find {|x| x.name == name } + # usage_error "Source storage #{src.storage} has no disk named #{name}" + #end + case dest.type + when 'rbd' + else ConditionViolation( "Storage type #{dest.type} not supported as destination. (supported: rbd only, yet)") + end + #if c = dest.content.find {|x| x.name == name } + # usage_error "Destination storage #{dest.storage} has already a disk named #{name}" + #end + + src_image_spec, dest_image_spec = "#{source}/#{name}", "#{destination}/#{name}" + mp_path = Pathname.new( "/var/lib/lxc/").join ct.vmid.to_s + mp_path.mkdir unless mp_path.exist? + src_mp, dest_mp = mp_path + src_image_spec.gsub('/','-'), mp_path + dest_image_spec.gsub('/','-') + + src_mp.mkdir unless src_mp.exist? + dest_mp.mkdir unless dest_mp.exist? + # We check, if something is mounted already on our mountpoints. + # We check it later, too, but later cleaning up would be impossible. + # We do not trust only this checks, so we check later, too. + ConditionViolation( "Something already mounted at #{src_mp}") if src_mp.mountpoint? + ConditionViolation( "Something already mounted at #{dest_mp}") if dest_mp.mountpoint? + + rbd_image_info = JSON.parse IO.popen( [*%w[rbd info --format json], src_image_spec]) {|io| io.read } + OperationalError( "Couldn't determine size of #{src_image_spec}.") unless $?.success? + + # checks and preparation done. + size = rbd_image_info['size']/1024/1024 + + unless system *%w[rbd create -s], size.to_s, dest_image_spec + OperationalError( "Creating disk #{dest_image_spec} failed.") + end + @out.progress "Map devices %s, %s \e[J", ct.name, dest_image_spec, src_image_spec + rbd_devices_maps dest_image_spec, src_image_spec do |dest_dev, src_dev| + @out.info "Maped source device %s => %s => %s", src_image_spec, src_dev, src_mp + @out.info "Maped destination device %s => %s => %s", dest_image_spec, dest_dev, dest_mp + @out.info "Formatting destination disk" + unless system *%w[mkfs.xfs -mreflink=1 -bsize=4096 -ssize=4096], dest_dev + OperationalError( "Formatting #{dest_image_spec} failed.") + end + OperationalError( "Something already mounted at #{src_mp}") if src_mp.mountpoint? + @out.progress "Mounting source disk" + mount src_dev, src_mp do + OperationalError( "Something already mounted at #{src_mp}") if dest_mp.mountpoint? + @out.progress "Mounting destination disk" + mount dest_dev, dest_mp do + #@out.info "rsyncing..." + #system *%w[rsync -aHAX --info=progress2], "#{src_mp}/", "#{dest_mp}/" + IO.popen %w[rsync -ahHAX --info=progress2] + ["#{src_mp}/", "#{dest_mp}/"] do |io| + io.each_line("\r") {|l| @out.progress 'rsync|%s', l.chomp } + end + OperationalError( "rsync had an error. [#{$?.exitcode}]") unless $?.success? + end + end + end + + unusedfield = (0..20).map{|i| "unused#{i}" }.find {|n| ctcnf[n].nil? } + configupdate = {unusedfield => "#{source}:#{name}", disk => "#{destination}:#{name}#{diskopts}"} + end + ct.cnfset **configupdate + @out.success "disk moved." + end + def node_opt node = nil node ? [Proxmox::Node.find_by_name!( node)] : Proxmox::Node.all end @@ -174,10 +367,17 @@ class PVE::Cli end end - def hosting_table target:, status:, sort: + def hosting_table target:, status:, sort:, tags: connect - to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU/% Mem/MiB Mem/% Disk/MiB Disk/%], format: '<<<<<>>>>>>' + to = TablizedOutput.new %w[Status HA ID Name Host Uptime CPU/% Mem/MiB Mem/% Disk/MiB Disk/% Tags], format: '<<<<<>>>>>><' target &&= /\A#{target}\z/i + nottags = nil + if tags + tags = tags.split /,/ + nottags = tags.grep /\A-/ + tags -= nottags + nottags.map! {|x| x[1..-1] } + end status = case status when /\Asta(r(t(ed?)?)?)?\z/i, /\Aon(l(i(ne?)?)?)?\z/i, /\Ar(u(n(n(i(ng?)?)?)?)?)?\z/i, '1' @@ -186,17 +386,25 @@ class PVE::Cli %i[stopped offline] when nil, '', /\Aa(ll?)?\z/i then nil else - raise DenKn::UsageError, "Unknown state #{status}" + usage_error "Unknown state #{status}" end push = - if target and status - lambda {|n| to.push tablized_virt( n) if n === target and status.include?( n.state) } - elsif target - lambda {|n| to.push tablized_virt( n) if n === target } - elsif status - lambda {|n| to.push tablized_virt( n) if status.include? n.state } - else - lambda {|n| to.push tablized_virt( n) } + begin + condition = [] + condition.push lambda {|n| n === target } if target + if status + condition.push lambda {|n| !n.respond_to?( :status) or status.include?( n.status) } + end + if tags + condition.push lambda {|n| + if n.respond_to?( :tags) and n.tags + nt = n.tags + [] == tags - nt and nottags == nottags - nt + else false + end + } + end + lambda {|n| to.push tablized_virt( n) if condition.all? {|c| c.call n} } end yield push to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) } @@ -257,25 +465,30 @@ class PVE::Cli def call *argv cli.call *argv rescue RestClient::ExceptionWithResponse - STDERR.puts "#{$!.request}: #{$!.to_s} - #{$!.response} (#{$!.class})" #, $!.backtrace.map {|b|" #{b}"} - rescue UsageError, DenCli::UsageError - STDERR.puts $! + @out.failed "%s: %s - %s (%s)", $!.request, $!, $!.response, $!.class + #STDERR.puts $!.backtrace.map {|b|" #{b}"} + exit 1 + rescue DenCli::UsageError, RuntimeError + @out.failed "%s", $! exit 1 end def per_argument arguments, print: nil, &exe arguments.each do |argument| - STDERR.printf print, argument if print + @out.host = argument + @out.info "\e[1;34m#{print}\e[0m", argument if print begin yield argument rescue RestClient::ExceptionWithResponse - STDERR.puts "#{$!.request}: #{$!.to_s} - #{JSON.parse( $!.response.body)}" - rescue UsageError, DenCli::UsageError - STDERR.puts $! + @out.failed "%s: %s - %s", $!.request, $!, JSON.parse( $!.response.body) + rescue DenCli::UsageError, RuntimeError + @out.failed "%s", $! rescue RestClient::BadRequest - STDERR.puts $!.message.inspect + @out.failed "%p", $!.message + rescue Interrupt + @out.failed "Interrupted by user" rescue SystemExit - STDERR.puts "Failed with exit code: #{$!.status}" if 0 < $!.status + @out.failed "Exitcode: %d", $!.status if 0 < $!.status end end end @@ -320,8 +533,8 @@ class PVE::Cli end [ case v.status - when "running", "online" then ColoredString.new v.status, "32" - when "stopped" then ColoredString.new v.status, "31" + when :running, :online then ColoredString.new v.status, "32" + when :stopped, :offline then ColoredString.new v.status, "31" else v.status end, ha&.state || '·', @@ -340,6 +553,7 @@ class PVE::Cli if v.respond_to?(:maxdisk) and 0 < v.maxdisk.to_i TablizedOutput::Percentage.new( v.disk.to_f/v.maxdisk.to_f) else unknown end, + v.respond_to?(:tags) ? v.tags.join(', ') : '', ] end end diff --git a/lib/pve/cli/base.rb b/lib/pve/cli/base.rb index 00599a6..9c23cc5 100644 --- a/lib/pve/cli/base.rb +++ b/lib/pve/cli/base.rb @@ -15,8 +15,8 @@ def cli_base each {|c| puts c } } - cli.cmd( :status, "Lists Nodes/VMs/CTs with status", &lambda {|target=nil, sort:, node: nil, status: nil| - hosting_table target: target, status: status, sort: sort do |push| + 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) ] }. @@ -25,7 +25,8 @@ def cli_base }). 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( :node, '-n', '--node=NODE', "List only hosted by this NODE"). - opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)") + opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)"). + opt( :tags, '-t', '--tags=TAGS', "Filter by comma-seperated tags. All tags must be present for Machine") def val2str v case v diff --git a/lib/pve/cli/ct.rb b/lib/pve/cli/ct.rb index 0371394..353c8df 100644 --- a/lib/pve/cli/ct.rb +++ b/lib/pve/cli/ct.rb @@ -9,8 +9,8 @@ def cli_ct 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| - hosting_table target: target, status: status, sort: sort do |push| + 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). each( &push).lazy. map {|n| Thread.new n, &:lxc }. @@ -19,31 +19,40 @@ def cli_ct }). 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( :node, '-n', '--node=NODE', "List only hosted by this NODE"). - opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)") + opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)"). + opt( :tags, '-t', '--tags=TAGS', "Filter by comma-seperated tags. All tags must be present for CT") ct_cli.cmd :enter, "Enter Console of CT", &lambda {|name_or_id| connect - STDERR.puts "! #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).enter + @out.host = name_or_id + @out.failed "Exitstatus #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).enter } ct_cli.cmd :exec, "Executes Command in CT", min: 4, &lambda {|name_or_id, *command| connect - STDERR.puts "! #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).exec *command + @out.host = name_or_id + @out.failed "Exitstatus #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).exec *command } ct_cli.cmd( :migrate, "Migrates halted CT(s) to an other host", min: 2, &lambda {|target, *names_or_ids, fire:, timeout:, secs:| connect node = Proxmox::Node.find_by_name! target - per_argument names_or_ids, print: "\e[1;34mMigrate CT %s:\e[0m" do |name_or_id| + per_argument names_or_ids, print: "Migrate CT:" do |name_or_id| ct = Proxmox::LXC.find! name_or_id task = ct.migrate node wait task, text: "Migrating", timeout: timeout unless fire end }).tap {|c| opts_wait c } + ct_cli.cmd( :volume_move, "Moves volume to an other storage/pool and marks old volume as unused.", min: 11, aliases: %i[volmv mv], &lambda {|name_or_id, volume, destination| + connect + ct = Proxmox::LXC.find! name_or_id + ct_volume_move ct, volume, destination + }) + ct_cli.cmd( :start, "Starts CT(s)", min: 4, &lambda {|*names_or_ids, node: nil, fire:, timeout:, secs:| connect - per_argument names_or_ids, print: "\e[1;34mStart CT %s:\e[0m" do |name_or_id| + per_argument names_or_ids, print: "Start CT:" do |name_or_id| ct = Proxmox::LXC.find! name_or_id start ct, node: node, fire: fire, timeout: timeout, secs: secs end @@ -51,7 +60,7 @@ def cli_ct ct_cli.cmd( :stop, "Stops CT(s)", min: 3, &lambda {|*names_or_ids, fire: nil, timeout:, secs:| connect - per_argument names_or_ids, print: "\e[1;34mStart CT %s:\e[0m" do |name_or_id| + per_argument names_or_ids, print: "Start CT:" do |name_or_id| ct = Proxmox::LXC.find! name_or_id stop ct, fire: fire, timeout: timeout, secs: secs end diff --git a/lib/pve/cli/qm.rb b/lib/pve/cli/qm.rb index 351c2e3..94f2bbc 100644 --- a/lib/pve/cli/qm.rb +++ b/lib/pve/cli/qm.rb @@ -51,8 +51,8 @@ def cli_qm opt( :timeout, "-tTIMEOUT", "--timeout=TIMEOUT", "Wait for max TIMEOUT seconds (default: endless)", default: nil). opt( :secs, "-sSECONDS", "--seconds=SECONDS", "Check every SECONDS for state (default: 0.2)", default: 0.2) - qm_cli.cmd( :status, "Lists VMs with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil| - hosting_table target: target, state: state, sort: sort do |push| + qm_cli.cmd( :status, "Lists VMs 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). each( &push).lazy. flat_map {|n| [ Thread.new( n, &:qemu) ] }. @@ -61,7 +61,8 @@ def cli_qm }). 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( :node, '-n', '--node=NODE', "List only hosted by this NODE"). - opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)") + opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)"). + opt( :tags, '-t', '--tags=TAGS', "Filter by comma-seperated tags. All tags must be present for CT") qm_cli.cmd :exec, "Executes Command in VM via qemu-guest-agent", min: 4, &lambda {|name_or_id, *command| connect diff --git a/lib/pve/helper.rb b/lib/pve/helper.rb index 45f0515..68e21ea 100644 --- a/lib/pve/helper.rb +++ b/lib/pve/helper.rb @@ -163,7 +163,14 @@ class TablizedOutput end @stdout.puts \ @header.each_with_index.map {|s, i| - "#{' ' * (@maxs[i] - s.length)}#{s}" + #"#{' ' * (@maxs[i] - s.length)}#{s}" + pad = ' ' * (@maxs[i] - s.length) + case @format[i] + when '<' + "#{s}#{pad}" + else + "#{pad}#{s}" + end }.join( ' | ') ls.each_with_index do |l, i| @stdout.puts \ diff --git a/lib/pve/proxmox.rb b/lib/pve/proxmox.rb index 45e5aed..e7dda5a 100644 --- a/lib/pve/proxmox.rb +++ b/lib/pve/proxmox.rb @@ -19,6 +19,8 @@ module Proxmox end class AlreadyExists < Exception end + class AlreadyLocked < Exception + end def self.cnfstr2hash str str. @@ -134,7 +136,7 @@ module Proxmox end def rest_put path, **data, &exe - data = __data__( data) + data = __data__( **data) STDERR.puts "PUT #{path} <= #{data}" if $DEBUG __response__ Proxmox.connection[path].put( data, __headers__( :'Content-Type' => 'application/json')) rescue RestClient::Exception @@ -227,6 +229,12 @@ module Proxmox attr_reader :name + def __update__ **data + data[:status] &&= data[:status].to_sym + super **data + end + private :__update__ + def === t @name =~ t or @vmid.to_s =~ t or @sid =~ t end @@ -241,8 +249,8 @@ module Proxmox @name = @node end - def offline?() @status.nil? or 'offline' == @status end - def online?() 'online' == @status end + def offline?() @status.nil? or :offline == @status end + def online?() :online == @status end def lxc return [] if offline? @@ -292,7 +300,12 @@ module Proxmox 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) + __update__ d.merge( node: @node, upid: @upid, task: @task) + end + + def __update__ **data + data = data.merge t: 'status' + super **data end def initialize @@ -337,9 +350,16 @@ module Proxmox class Hosted < Base def refresh! - __update__ rest_get( "#{@rest_prefix}/status/current").merge( node: @node, t: @t) + __updata__ rest_get( "#{@rest_prefix}/status/current").merge( node: @node, t: @t) end + def __update__ **data + data[:tags] = (data[:tags]||'').split ';' + data[:status] &&= data[:status].to_sym + super **data + end + private :__update__ + def === t @name =~ t or @vmid.to_s =~ t or @sid =~ t end @@ -405,6 +425,7 @@ module Proxmox cnf[:unprivileged] &&= 1 == cnf[:unprivileged] cnf[:memory] &&= cnf[:memory].to_i cnf[:cores] &&= cnf[:cores].to_i + cnf[:tags] &&= cnf[:tags].split ';' cnf.update cnf.delete( :lxc).map{|k,v|[k.to_sym,v]}.to_h if cnf[:lxc] cnf end @@ -416,12 +437,38 @@ module Proxmox when true then r[k] = 1 when false then r[k] = 0 when nil then r[:delete].push k + when Array + case k + when :tags + r[k] = v.join ';' + else r[k] = v + end else r[k] = v end end r.delete :delete if r[:delete].empty? - STDERR.puts r.inspect - rest_put "#{@rest_prefix}/config", r + rest_put "#{@rest_prefix}/config", **r + end + + def lock reason, &exe + cnf = config + raise Proxmox::AlreadyLocked, "Machine #{self} Already locked for: #{cnf[:lock].inspect}" unless cnf[:lock].nil? + r = rest_put "#{@rest_prefix}/config", {lock: reason} + if block_given? + begin + return yield + ensure + unlock + end + else + r + end + end + + def unlock + system 'pct', 'unlock', @vmid.to_s + raise Proxmox::UnlockFailed unless $?.success? + #rest_put "#{@rest_prefix}/config", {delete: %w[lock]} end def resize disk, size @@ -726,8 +773,9 @@ module Proxmox end def initialize() rest_prefix end - - def to_s() "#{@node.node}:#{@storage}" end + def to_s() "#{@node.node}:#{@storage}" end + def active?() 1 == @active end + def enabled?() 1 == @enabled end class Content < Base def rest_prefix From 83211a817f14874b595f697d541b9c40e15c3476 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Mon, 16 Jan 2023 21:37:38 +0100 Subject: [PATCH 10/28] qm/ct status shows only qms/cts, no nodes. --- lib/pve/cli/ct.rb | 3 +-- lib/pve/cli/qm.rb | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/pve/cli/ct.rb b/lib/pve/cli/ct.rb index 353c8df..e7debb0 100644 --- a/lib/pve/cli/ct.rb +++ b/lib/pve/cli/ct.rb @@ -11,8 +11,7 @@ def cli_ct 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). - each( &push).lazy. + node_opt( node).lazy. map {|n| Thread.new n, &:lxc }. each {|n| n.value.each &push } end diff --git a/lib/pve/cli/qm.rb b/lib/pve/cli/qm.rb index 94f2bbc..0635c8d 100644 --- a/lib/pve/cli/qm.rb +++ b/lib/pve/cli/qm.rb @@ -53,8 +53,7 @@ def cli_qm qm_cli.cmd( :status, "Lists VMs 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). - each( &push).lazy. + node_opt( node).lazy. flat_map {|n| [ Thread.new( n, &:qemu) ] }. each {|n| n.value.each &push } end From 6530c40749ccafc20d8e4767850458fe546841dc Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 21 Sep 2023 14:55:24 +0200 Subject: [PATCH 11/28] UPID-Parser, Task status & monitoring --- lib/pve/cli.rb | 14 ++++---- lib/pve/cli/task.rb | 65 ++++++++++++++++++++++++++++++----- lib/pve/proxmox.rb | 82 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 131 insertions(+), 30 deletions(-) diff --git a/lib/pve/cli.rb b/lib/pve/cli.rb index 172fbbb..836f302 100644 --- a/lib/pve/cli.rb +++ b/lib/pve/cli.rb @@ -380,9 +380,9 @@ class PVE::Cli end status = case status - when /\Asta(r(t(ed?)?)?)?\z/i, /\Aon(l(i(ne?)?)?)?\z/i, /\Ar(u(n(n(i(ng?)?)?)?)?)?\z/i, '1' + when /\Asta(?:r(?:t(?:ed?)?)?)?\z/i, /\Aon(?:l(?:i(?:ne?)?)?)?\z/i, /\Ar(?:u(?:n(?:n(?:i(?:ng?)?)?)?)?)?\z/i, '1' %i[started online running] - when /\Asto(p(p(ed?)?)?)?\z/i, /\Aof(f(l(i(ne?)?)?)?)?\z/i, '0' + when /\Asto(?:p(?:p(?:ed?)?)?)?\z/i, /\Aof(?:f(?:l(?:i(?:ne?)?)?)?)?\z/i, '0' %i[stopped offline] when nil, '', /\Aa(ll?)?\z/i then nil else @@ -533,7 +533,7 @@ class PVE::Cli end [ case v.status - when :running, :online then ColoredString.new v.status, "32" + when :running, :online then ColoredString.new v.status, "32" when :stopped, :offline then ColoredString.new v.status, "31" else v.status end, @@ -546,14 +546,14 @@ class PVE::Cli end, v.name, node, v.respond_to?(:uptime) ? TablizedOutput::V.new( v.uptime, Measured.seconds( v.uptime)) : unknown, - v.respond_to?(:cpu) ? TablizedOutput::Percentage.new( v.cpu) : unknown, - v.respond_to?(:mem) ? TablizedOutput::V.new( v.mem, Measured.bytes( v.mem)) : unknown, + v.respond_to?(:cpu) ? TablizedOutput::Percentage.new( v.cpu) : unknown, + v.respond_to?(:mem) ? TablizedOutput::V.new( v.mem, Measured.bytes( v.mem)) : unknown, v.respond_to?(:maxmem) ? TablizedOutput::Percentage.new( v.mem/v.maxmem.to_f) : unknown, - v.respond_to?(:disk) ? TablizedOutput::V.new( v.disk.to_i, Measured.bytes( v.disk.to_i)) : unknown, + v.respond_to?(:disk) ? TablizedOutput::V.new( v.disk.to_i, Measured.bytes( v.disk.to_i)) : unknown, if v.respond_to?(:maxdisk) and 0 < v.maxdisk.to_i TablizedOutput::Percentage.new( v.disk.to_f/v.maxdisk.to_f) else unknown end, - v.respond_to?(:tags) ? v.tags.join(', ') : '', + v.respond_to?(:tags) ? v.tags.join(', ') : '', ] end end diff --git a/lib/pve/cli/task.rb b/lib/pve/cli/task.rb index 3b1d201..6ec4492 100644 --- a/lib/pve/cli/task.rb +++ b/lib/pve/cli/task.rb @@ -1,4 +1,33 @@ 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| @@ -7,7 +36,7 @@ def cli_task nodes = nodes.select {|n| node == n.name } if node nodes.flat_map do |n| n.tasks.map &:upid - end.sort.each {|upid| puts upid } + end.sort_by(&:upid).each {|upid| puts upid } } tcli.cmd :get, "Inspect a task", &lambda {|upid| @@ -22,21 +51,39 @@ def cli_task end } - tcli.cmd :status, "Shows tasks states", &lambda {|upid| - } + 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 {|t| p t; push.call t } } + end + }) - tcli.cmd :monitor, "Monitors running tasks", &lambda {|node: nil| + tcli.cmd( :monitor, "Monitors running tasks", &lambda {|node: nil| connect nodes = Proxmox::Node.all nodes = nodes.select {|n| node == n.name } if node tasks = {} - loop do - nodes.flat_map do |n| - n.tasks.map &:upid - end.sort.each do |upid| + 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 diff --git a/lib/pve/proxmox.rb b/lib/pve/proxmox.rb index e7dda5a..08d840f 100644 --- a/lib/pve/proxmox.rb +++ b/lib/pve/proxmox.rb @@ -21,6 +21,8 @@ module Proxmox end class AlreadyLocked < Exception end + class UnparsableUPID < Exception + end def self.cnfstr2hash str str. @@ -84,7 +86,7 @@ module Proxmox end def self.find_by_vmid vmid - Proxmox::LXC.find_by_vmid( vmid) || Proxmox::Qemu.find_by_vmid( vmid) || Proxmox::Node.find_by_vmid( vmid) + Proxmox::LXC.find_by_vmid( vmid) || Proxmox::Qemu.find_by_vmid( vmid) end def self.find name_or_id @@ -299,12 +301,22 @@ module Proxmox def refresh! d = rest_get @rest_prefix d[:starttime] &&= Time.at d[:starttime] - d = {exitstatus: nil}.merge d __update__ d.merge( node: @node, upid: @upid, task: @task) end def __update__ **data data = data.merge t: 'status' + data[:status] = data[:status]&.to_sym + data[:exitstatus] = data[:exitstatus]&.to_sym + data[:state] = + case data[:status] + when :running then :running + when :stopped + case data[:exitstatus] + when :OK then :success + else :failed + end + end super **data end @@ -319,11 +331,51 @@ module Proxmox "#<#{self.class.name}|#{@upid} node=#{@node.node} #{h.join ' '}>" end - def running?() 'running' == @status end - def finished?() 'stopped' == @status 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 + def successfull?() stopped? ? :OK == @exitstatus : nil end + def failed?() stopped? ? :OK != @exitstatus : nil end + end + + def running?() status.running? end + def finished?() status.finished? end + alias stopped? finished? + def successfull?() status.successfull? end + def failed?() status.failed? end + + class UPID + RE = + /^ + UPID : + (?[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?) : + (?[0-9A-Fa-f]{8}) : + (?[0-9A-Fa-f]{8,9}) : + (?[0-9A-Fa-f]{8}) : + (?[^:\s]+) : + (?[^:\s]*) : + (?[^:\s]+) : + $/x + + attr_reader :upid, :node, :pid, :pstart, :starttime, :dtype, :id, :user + alias to_s upid + + def initialize upid + m = RE.match upid + raise UnparsableUPID, "UPID cannot be parsed: #{upid.inspect} (#{upid.class})" unless m + @upid = upid + @node = m[:node] + @pid = m[:pid].to_i 16 + @pstart = m[:pstart].to_i 16 + @starttime = Time.at m[:starttime].to_i( 16) + @dtype = m[:dtype] + @id = case m[:id] when '' then nil else m[:id].to_i end + @user = m[:user] + end + + def inspect + "#<#{self.class.name} #{@upid}>" + end end def rest_prefix @@ -332,7 +384,7 @@ module Proxmox def initialize rest_prefix - @sid = upid + @sid, @upid = upid, UPID.new( upid) end def inspect @@ -349,6 +401,10 @@ module Proxmox end class Hosted < Base + def self.all + Node.all.flat_map {|n| n.qemu + n.lxc } + end + def refresh! __updata__ rest_get( "#{@rest_prefix}/status/current").merge( node: @node, t: @t) end @@ -450,25 +506,23 @@ module Proxmox rest_put "#{@rest_prefix}/config", **r end + # it is only allowed to lock local guests, because it is only possible to unlock local guests. def lock reason, &exe cnf = config raise Proxmox::AlreadyLocked, "Machine #{self} Already locked for: #{cnf[:lock].inspect}" unless cnf[:lock].nil? r = rest_put "#{@rest_prefix}/config", {lock: reason} if block_given? - begin - return yield - ensure - unlock + begin return yield + ensure unlock end - else - r + else r end end def unlock system 'pct', 'unlock', @vmid.to_s raise Proxmox::UnlockFailed unless $?.success? - #rest_put "#{@rest_prefix}/config", {delete: %w[lock]} + #rest_put "#{@rest_prefix}/config", {delete: %w[lock], skiplock: 1} end def resize disk, size From 82b3d3cda28e99e923d61a3d1b9c05de5431784d Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 21 Sep 2023 18:02:44 +0200 Subject: [PATCH 12/28] bump v0.3.1 --- lib/pve/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pve/version.rb b/lib/pve/version.rb index 96003f2..21516f6 100644 --- a/lib/pve/version.rb +++ b/lib/pve/version.rb @@ -1,3 +1,3 @@ module PVE - VERSION = '0.3.0' + VERSION = '0.3.1' end From e2b38ca779f3a9096d533068a1eb33a83b9a2e31 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 21 Sep 2023 19:21:18 +0200 Subject: [PATCH 13/28] debug-output removed --- lib/pve/cli/task.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pve/cli/task.rb b/lib/pve/cli/task.rb index 6ec4492..e1a811d 100644 --- a/lib/pve/cli/task.rb +++ b/lib/pve/cli/task.rb @@ -54,7 +54,7 @@ def cli_task 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 {|t| p t; push.call t } } + Proxmox::Node.all.each {|n| n.tasks.each &push } end }) From d6e7d84afc3132ef8ccdf4a046d9746f15998566 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Sat, 21 Oct 2023 00:14:26 +0200 Subject: [PATCH 14/28] version 0.3.2 bugfix for correct handling "already started" --- lib/pve/cli.rb | 2 +- lib/pve/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pve/cli.rb b/lib/pve/cli.rb index 836f302..00b01e1 100644 --- a/lib/pve/cli.rb +++ b/lib/pve/cli.rb @@ -178,7 +178,7 @@ class PVE::Cli def start host, node: nil, timeout: nil, fire: nil, secs: nil timeout ||= 60 if host.running? - condition_violation "Already running." + ConditionViolation "Already running." return end task = host.start diff --git a/lib/pve/version.rb b/lib/pve/version.rb index 21516f6..e21549d 100644 --- a/lib/pve/version.rb +++ b/lib/pve/version.rb @@ -1,3 +1,3 @@ module PVE - VERSION = '0.3.1' + VERSION = '0.3.2' end From 5eb6cd52eb373f7d600ac0489c60fda2d5db039c Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 28 Mar 2024 17:48:03 +0100 Subject: [PATCH 15/28] typo fixed --- lib/pve/proxmox.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pve/proxmox.rb b/lib/pve/proxmox.rb index 08d840f..f335140 100644 --- a/lib/pve/proxmox.rb +++ b/lib/pve/proxmox.rb @@ -406,7 +406,7 @@ module Proxmox end def refresh! - __updata__ 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 __update__ **data From f780eccc6af0b770ce1ccd52aadcbc980ee9702c Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 28 Mar 2024 23:31:01 +0100 Subject: [PATCH 16/28] version 0.3.3 typo bugfix --- lib/pve/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pve/version.rb b/lib/pve/version.rb index e21549d..7a31e6e 100644 --- a/lib/pve/version.rb +++ b/lib/pve/version.rb @@ -1,3 +1,3 @@ module PVE - VERSION = '0.3.2' + VERSION = '0.3.3' end From f268fcc5f76e0641fa00a0ed7cb01d582832a2ea Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Sat, 21 Sep 2024 14:34:32 +0200 Subject: [PATCH 17/28] 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 From ba7fbed101ca53becd74b3abcccec8e35ac8891b Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Sat, 21 Sep 2024 14:41:36 +0200 Subject: [PATCH 18/28] bump 0.4.0 - first draw --- lib/pve/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pve/version.rb b/lib/pve/version.rb index 7a31e6e..d254033 100644 --- a/lib/pve/version.rb +++ b/lib/pve/version.rb @@ -1,3 +1,3 @@ module PVE - VERSION = '0.3.3' + VERSION = '0.4.0' end From 9786e145c13748e1d27c14e96017296b9df44e7f Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Sat, 21 Sep 2024 14:54:42 +0200 Subject: [PATCH 19/28] more cli: Thread -> pmap. exc2warn --- lib/pve/cli/base.rb | 3 +-- lib/pve/cli/qm.rb | 8 +++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/pve/cli/base.rb b/lib/pve/cli/base.rb index 3b244a9..1c5dcc9 100644 --- a/lib/pve/cli/base.rb +++ b/lib/pve/cli/base.rb @@ -27,8 +27,7 @@ def cli_base node_opt( node). each( &push). flat_map {|n| [ n.method(:lxc), n.method(:qemu) ] }. - flat_pmap {|m| m.call }. - each {|n| n.value.each &push } + peach {|m| exc2warn( nil) { m.call.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/cli/qm.rb b/lib/pve/cli/qm.rb index 0635c8d..f8b7e0e 100644 --- a/lib/pve/cli/qm.rb +++ b/lib/pve/cli/qm.rb @@ -5,8 +5,8 @@ def cli_qm connect nodes = Proxmox::Node.all nodes = nodes.select {|n| node == n.name } if node - nodes.flat_map do |n| - n.qemu.map {|c| c.vmid.to_i } + nodes.flat_pmap do |n| + exc2warn( []) { n.qemu.map {|c| c.vmid.to_i } } end.sort.each {|c| puts c } } @@ -53,9 +53,7 @@ def cli_qm qm_cli.cmd( :status, "Lists VMs 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. - flat_map {|n| [ Thread.new( n, &:qemu) ] }. - each {|n| n.value.each &push } + node_opt( node).flat_pmap( &:qemu).peach &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'). From 1c4bbbdb7d3285da1b8519cdb49f78ba6eb34244 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Sat, 21 Sep 2024 21:23:46 +0200 Subject: [PATCH 20/28] use features of ruby-3.1 (lambda(&exe), **kwargs) --- lib/pve/cli/base.rb | 48 +++++++++++++++++++------------------- lib/pve/cli/ct.rb | 52 +++++++++++++++++++++--------------------- lib/pve/cli/ha.rb | 30 ++++++++++++------------ lib/pve/cli/node.rb | 8 +++---- lib/pve/cli/qm.rb | 40 ++++++++++++++++---------------- lib/pve/cli/storage.rb | 36 ++++++++++++++--------------- lib/pve/cli/task.rb | 20 ++++++++-------- lib/pve/proxmox.rb | 2 +- 8 files changed, 118 insertions(+), 118 deletions(-) diff --git a/lib/pve/cli/base.rb b/lib/pve/cli/base.rb index 1c5dcc9..0a92e0b 100644 --- a/lib/pve/cli/base.rb +++ b/lib/pve/cli/base.rb @@ -12,7 +12,7 @@ class PVE::Cli using IPAddress::ToSWithNetmaskForNetworks def cli_base - cli.cmd :list, "List CT/VM-IDs", aliases: ['ls'], &lambda {|target=nil| + cli.cmd :list, "List CT/VM-IDs", aliases: ['ls'] do |target=nil| connect nodes = Proxmox::Node.all nodes. @@ -20,16 +20,16 @@ def cli_base flat_pmap {|m| m.call.map {|c| c.vmid.to_i } }. sort. each {|c| puts c } - } + end - 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" do |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). flat_map {|n| [ n.method(:lxc), n.method(:qemu) ] }. peach {|m| exc2warn( nil) { m.call.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( :node, '-n', '--node=NODE', "List only hosted by this NODE"). opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)"). @@ -89,9 +89,9 @@ def cli_base end cli.sub :config, "CT/VM Configuration", min: 2, aliases: %w[cnf] do |ccli| - ccli.cmd :help, '', aliases: [nil, '-h', '--help'], &lambda {|*args| help ccli, *args } + ccli.cmd :help, '', aliases: [nil, '-h', '--help'] do |*args| help ccli, *args end - ccli.cmd :set, "Set Configs for CT/VM", min: 3, &lambda {|name_or_id, *args| + ccli.cmd :set, "Set Configs for CT/VM", min: 3 do |name_or_id, *args| if %w[-h --help].include? name_or_id STDERR.puts "Usage: set -h|--help # Show help" STDERR.puts " set ct|vm --CNF1=VAL1 --CNF2=VAL2 ... # Set config-value. Empty value clears field." @@ -157,29 +157,29 @@ def cli_base opts[:digest] ||= old[:digest] th.cnfset opts show_config th.config, old - } + end - ccli.cmd :show, "Show Config of CT/VM", &lambda {|name_or_id| + ccli.cmd :show, "Show Config of CT/VM" do |name_or_id| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) raise UsageError, "Container or Node not found: #{name_or_id}" unless th show_config th.config - } + end end - cli.cmd( :enter, "Enter Console of CT/Node", &lambda {|name_or_id| + cli.cmd :enter, "Enter Console of CT/Node" do |name_or_id| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Node.find_by_name( name_or_id) raise UsageError, "Container or Node not found: #{name_or_id}" unless th STDERR.puts "! #{$?.exitstatus}" unless th.enter - }). + end. completion do |*pre, arg| completion_helper *pre, arg do |f| complete_lxc( f) + complete_node( f) end end - cli.cmd( :run, "Starts CT/VM", aliases: %w[start star], &lambda {|*names_or_ids| + cli.cmd :run, "Starts CT/VM", aliases: %w[start star] do |*names_or_ids| connect raise UsageError, "Nothing to start?" if names_or_ids.empty? per_argument names_or_ids, print: "\e[34;1mRunning CT/VM\e[0m %s:\n" do |name_or_id| @@ -187,14 +187,14 @@ def cli_base raise UsageError, "Container or Node not found: #{name_or_id}" unless th start th end - }). + end. completion do |*pre, arg| completion_helper *pre, arg do |f| complete_lxc( f) + complete_qemu( f) end end - cli.cmd( :migrate, "Migrates (halted) CTs/VMs to an other host", min: 2, &lambda {|target, *names_or_ids, fire:, timeout:, secs:| + cli.cmd :migrate, "Migrates (halted) CTs/VMs to an other host", min: 2 do |target, *names_or_ids, fire:, timeout:, secs:| connect node = Proxmox::Node.find_by_name! target per_argument names_or_ids, print: "\e[1;34mCT/VM(s)\e[0m %s:\n" do |name_or_id| @@ -207,41 +207,41 @@ def cli_base task = th.migrate node wait task, text: "Migrating", timeout: timeout unless fire end - }).tap {|c| opts_wait c } + end.tap {|c| opts_wait c } - #cli.cmd :reboot, "Reboot CT/VM (not implemented, yet)", min: 6, &lambda {|name_or_id| + #cli.cmd :reboot, "Reboot CT/VM (not implemented, yet)", min: 6 do |name_or_id| # connect # th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) # raise UsageError, "Container or Node not found: #{name_or_id}" unless th # reboot th - #} + #end - cli.cmd( :stop, "Stops CT/VM", min: 4, &lambda {|*names_or_ids| + cli.cmd :stop, "Stops CT/VM", min: 4 do |*names_or_ids| connect per_argument names_or_ids, print: "\e[34;1mStopping CT/VM\e[0m %s:\n" do |name_or_id| th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) raise UsageError, "Container or Node not found: #{name_or_id}" unless th stop th end - }). + end. completion do |*pre, arg| completion_helper *pre, arg do |f| complete_lxc( f) + complete_qemu( f) end end - cli.cmd( :help, '', aliases: ['-h', '--help'], &lambda {|*args, full:| + cli.cmd :help, '', aliases: ['-h', '--help'] do |*args, full:| if full cli.help_full *args, output: STDERR else cli.help *args, output: STDERR end - }). + end. opt( :full, '-f', '--[no-]full', 'Includes all commands of all subcommands.', default: false) - cli.cmd :cli, 'Opens interactive console', min: 3, aliases: [nil], &lambda { + cli.cmd :cli, 'Opens interactive console', min: 3, aliases: [nil] do @interactive = true - cli.interactive( File.basename($0,'.rb')).run - } + cli.interactive( File.basename( $0, '.rb')).run + end end end diff --git a/lib/pve/cli/ct.rb b/lib/pve/cli/ct.rb index 170fad3..e1fce32 100644 --- a/lib/pve/cli/ct.rb +++ b/lib/pve/cli/ct.rb @@ -1,37 +1,37 @@ class PVE::Cli def cli_ct cli.sub :ct, "Containers", aliases: %w[lx lxc] do |ct_cli| - ct_cli.cmd :list, "List CT-IDs", aliases: ['ls'], &lambda {|node=nil| + ct_cli.cmd :list, "List CT-IDs", aliases: ['ls'] do |node=nil| connect nodes = node ? Proxmox::Node.find_by_name( name) : Proxmox::Node.all nodes.flat_pmap do |n| exc2warn( []) { n.lxc.map {|c| c.vmid.to_i } } end.sort.each {|c| puts c } - } + end - 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] do |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).peach {|n| exc2warn( nil) { n.lxc.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( :node, '-n', '--node=NODE', "List only hosted by this NODE"). opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)"). opt( :tags, '-t', '--tags=TAGS', "Filter by comma-seperated tags. All tags must be present for CT") - ct_cli.cmd :enter, "Enter Console of CT", &lambda {|name_or_id| + ct_cli.cmd :enter, "Enter Console of CT" do |name_or_id| connect @out.host = name_or_id @out.failed "Exitstatus #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).enter - } + end - ct_cli.cmd :exec, "Executes Command in CT", min: 4, &lambda {|name_or_id, *command| + ct_cli.cmd :exec, "Executes Command in CT", min: 4 do |name_or_id, *command| connect @out.host = name_or_id @out.failed "Exitstatus #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).exec *command - } + end - ct_cli.cmd( :migrate, "Migrates halted CT(s) to an other host", min: 2, &lambda {|target, *names_or_ids, fire:, timeout:, secs:| + ct_cli.cmd :migrate, "Migrates halted CT(s) to an other host", min: 2 do |target, *names_or_ids, fire:, timeout:, secs:| connect node = Proxmox::Node.find_by_name! target per_argument names_or_ids, print: "Migrate CT:" do |name_or_id| @@ -39,39 +39,39 @@ def cli_ct task = ct.migrate node wait task, text: "Migrating", timeout: timeout unless fire end - }).tap {|c| opts_wait c } + end.tap {|c| opts_wait c } - ct_cli.cmd( :volume_move, "Moves volume to an other storage/pool and marks old volume as unused.", min: 11, aliases: %i[volmv mv], &lambda {|name_or_id, volume, destination| + ct_cli.cmd :volume_move, "Moves volume to an other storage/pool and marks old volume as unused.", min: 11, aliases: %i[volmv mv] do |name_or_id, volume, destination| connect ct = Proxmox::LXC.find! name_or_id ct_volume_move ct, volume, destination - }) + end - ct_cli.cmd( :start, "Starts CT(s)", min: 4, &lambda {|*names_or_ids, node: nil, fire:, timeout:, secs:| + ct_cli.cmd :start, "Starts CT(s)", min: 4 do |*names_or_ids, node: nil, fire:, timeout:, secs:| connect per_argument names_or_ids, print: "Start CT:" do |name_or_id| ct = Proxmox::LXC.find! name_or_id start ct, node: node, fire: fire, timeout: timeout, secs: secs end - }).tap {|c| opts_wait c } + end.tap {|c| opts_wait c } - ct_cli.cmd( :stop, "Stops CT(s)", min: 3, &lambda {|*names_or_ids, fire: nil, timeout:, secs:| + ct_cli.cmd :stop, "Stops CT(s)", min: 3 do |*names_or_ids, fire: nil, timeout:, secs:| connect per_argument names_or_ids, print: "Start CT:" do |name_or_id| ct = Proxmox::LXC.find! name_or_id stop ct, fire: fire, timeout: timeout, secs: secs end - }).tap {|c| opts_wait c } + end.tap {|c| opts_wait c } - ct_cli.cmd( :wait, "Wait till CT is in state", &lambda {|name_or_id, state, timeout: nil, secs: nil| + ct_cli.cmd :wait, "Wait till CT is in state" do |name_or_id, state, timeout: nil, secs: nil| connect ct = Proxmox::LXC.find! name_or_id wait ct, state, timeout: timeout, secs: secs - }). + end. opt( :timeout, "-tTIMEOUT", "--timeout=TIMEOUT", "Wait for max TIMEOUT seconds (default: endless)", default: nil). opt( :secs, "-sSECONDS", "--seconds=SECONDS", "Check every SECONDS for state (default: 0.2)", default: 0.2) - ct_cli.cmd( :create, "Creates a new container", min: 2, &lambda {|template, *options| #, fire:, timeout:, secs:, start:| + ct_cli.cmd :create, "Creates a new container", min: 2 do |template, *options| #, fire:, timeout:, secs:, start:| if %w[-h --help].include? template STDERR.puts "Usage: ct create TEMPLATE -h # Shows template-related options" STDERR.puts " ct create TEMPLATE [OPTIONS] # Creates a container" @@ -115,28 +115,28 @@ EOU end.parse! options connect create Proxmox::LXC, template, **ctopts - }) + end - ct_cli.cmd( :config, 'Shows current config', aliases: %w[cnf], min: 2, &lambda {|name_or_id| + ct_cli.cmd :config, 'Shows current config', aliases: %w[cnf], min: 2 do |name_or_id| connect ct = Proxmox::LXC.find! name_or_id STDOUT.puts JSON.dump( ct.config) - }) + end - ct_cli.cmd( :resize, 'Resize a disk', &lambda {|name_or_id, disk, size| + ct_cli.cmd :resize, 'Resize a disk' do |name_or_id, disk, size| connect ct = Proxmox::LXC.find! name_or_id task = ct.resize disk, size wait task, text: "Resizing #{ct.sid} #{disk} to #{size}" - }) + end - ct_cli.cmd( :destroy, '', min: 7, &lambda {|name_or_id, fire:, secs:, timeout:, i_really_want_to_destroy:| + ct_cli.cmd :destroy, '', min: 7 do |name_or_id, fire:, secs:, timeout:, i_really_want_to_destroy:| raise UsageError, "Name/ID is not what you want to destroy" unless name_or_id == i_really_want_to_destroy connect ct = Proxmox::LXC.find! name_or_id raise UsageError, "Container is not stopped" unless ct.stopped? destroy ct, fire: fire, timeout: timeout, secs: secs - }).tap {|c| opts_wait c }. + end.tap {|c| opts_wait c }. opt( :i_really_want_to_destroy, "--i-really-want-to-destroy=NAMEORID", "Repeat the name/ID") ct_cli.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help ct_cli, *args } diff --git a/lib/pve/cli/ha.rb b/lib/pve/cli/ha.rb index 8f37708..5975e63 100644 --- a/lib/pve/cli/ha.rb +++ b/lib/pve/cli/ha.rb @@ -10,41 +10,41 @@ end def cli_ha cli.sub :ha, "Inspect High-Availability", min: 2 do |hacli| - hacli.cmd( :create, "Create HA for CT/VM", &lambda {|name_or_id, group:, comment: nil, max_relocate:, max_restart:, state:| + hacli.cmd :create, "Create HA for CT/VM" do |name_or_id, group:, comment: nil, max_relocate:, max_restart:, state:| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th ha = th.ha raise UsageError, "#{th.sid} is already High-Available" if ha.active? ha.create group: group, comment: comment, max_relocate: max_relocate, max_restart: max_restart - }).tap {|cl| opts_ha cl } + end.tap {|cl| opts_ha cl } - hacli.cmd :remove, "Remove CT/VM from HA", min: 5, &lambda {|name_or_id| + hacli.cmd :remove, "Remove CT/VM from HA", min: 5 do |name_or_id| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th ha = th.ha raise UsageError, "#{th.sid} is not High-Available" unless ha.active? ha.delete - } + end - hacli.cmd( :active, "CT/VM should be high-available. Options are only for defaults, if not activated, yet.", &lambda {|name_or_id, group:, comment: nil, max_relocate:, max_restart:, state:| + hacli.cmd :active, "CT/VM should be high-available. Options are only for defaults, if not activated, yet." do |name_or_id, group:, comment: nil, max_relocate:, max_restart:, state:| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th ha = th.ha ha.create group: group, comment: comment, max_relocate: max_relocate, max_restart: max_restart if ha.active? - }).tap {|cl| opts_ha cl } + end.tap {|cl| opts_ha cl } - hacli.cmd :deactive, "CT/VM should NOT be high-available.", &lambda {|name_or_id| + hacli.cmd :deactive, "CT/VM should NOT be high-available." do |name_or_id| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th ha = th.ha ha.delete unless ha.active? - } + end - hacli.cmd( :started, "CT/VM should be in state started. By stopping CT/VM via pct/e state will be changed in HA, too.", &lambda {|name_or_id, force: nil| + hacli.cmd :started, "CT/VM should be in state started. By stopping CT/VM via pct/e state will be changed in HA, too." do |name_or_id, force: nil| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th @@ -52,27 +52,27 @@ def cli_ha ha = ha.create unless ha.active? ha.disabled! if force and ha.error? ha.started! - }).opt( :force, "-f", "--force", "If CT/VM is in error-state, first reset HA, than try to start.") + end.opt( :force, "-f", "--force", "If CT/VM is in error-state, first reset HA, than try to start.") - hacli.cmd :stopped, "CT/VM should be in state stopped. By starting CT/VM via pct/e state will be changed in HA, too.", min: 3, &lambda {|name_or_id| + hacli.cmd :stopped, "CT/VM should be in state stopped. By starting CT/VM via pct/e state will be changed in HA, too.", min: 3 do |name_or_id| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th ha = th.ha ha = ha.create unless ha.active? ha.stopped! - } + end - hacli.cmd :reset, "If state of CT/VM is failed, Proxmox will not start/stop it anyway. You have to reset state (state=disabled), first", min: 3, aliases: [:rst], &lambda {|name_or_id| + hacli.cmd :reset, "If state of CT/VM is failed, Proxmox will not start/stop it anyway. You have to reset state (state=disabled), first", min: 3, aliases: [:rst] do |name_or_id| connect th = Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find_by_name( name_or_id) raise UsageError, "Container or VirtualMachine not found: #{name_or_id}" unless th ha = th.ha raise UsageError, "#{th.sid} is not High-Available" if ha.active? ha.state = :disabled - } + end - hacli.cmd 'help', '', aliases: ['-h', '--help'], &lambda {|*args| help hacli, *args } + hacli.cmd 'help', '', aliases: ['-h', '--help'] do |*args| help hacli, *args end end end end diff --git a/lib/pve/cli/node.rb b/lib/pve/cli/node.rb index f87b6bb..02a3953 100644 --- a/lib/pve/cli/node.rb +++ b/lib/pve/cli/node.rb @@ -1,7 +1,7 @@ class PVE::Cli def cli_node cli.sub :node, "Nodes" do |nod_cli| - nod_cli.cmd :status, "Lists nodes with status", aliases: [nil], &lambda {|node=nil| + nod_cli.cmd :status, "Lists nodes with status", aliases: [nil] do |node=nil| connect to = TablizedOutput.new %w[Status Node Uptime CPU Mem/MiB Disk/MiB] nodes = Proxmox::Node.all @@ -17,7 +17,7 @@ def cli_node ] end to.print order: [1] - } + end nod_cli.cmd :exec, "Executes command on node", min: 4 do |name, *args| connect @@ -30,14 +30,14 @@ def cli_node end nod_cli.sub :task, "Inspect tasks" do |tcli| - tcli.cmd :list, "List done tasks", aliases: [:ls], &lambda {|node| + tcli.cmd :list, "List done tasks", aliases: [:ls] do |node| connect Proxmox::Node.find_by_name!( node). tasks. map( &:upid). sort. each {|upid| puts upid } - } + end end nod_cli.cmd( 'help', '', aliases: ['-h', '--help']) {|*args| help nod_cli, *args } diff --git a/lib/pve/cli/qm.rb b/lib/pve/cli/qm.rb index f8b7e0e..8fbcbe6 100644 --- a/lib/pve/cli/qm.rb +++ b/lib/pve/cli/qm.rb @@ -1,16 +1,16 @@ class PVE::Cli def cli_qm - cli.sub :qm, "Virtual Machines", aliases: %w[v vm qemu], &lambda {|qm_cli| - qm_cli.cmd :list, "List VM-IDs", aliases: ['ls'], &lambda {|node=nil| + cli.sub :qm, "Virtual Machines", aliases: %w[v vm qemu] do |qm_cli| + qm_cli.cmd :list, "List VM-IDs", aliases: ['ls'] do |node=nil| connect nodes = Proxmox::Node.all nodes = nodes.select {|n| node == n.name } if node nodes.flat_pmap do |n| exc2warn( []) { n.qemu.map {|c| c.vmid.to_i } } end.sort.each {|c| puts c } - } + end - qm_cli.cmd( :migrate, "Migrates VM(s) to an other host", min: 2, &lambda {|target, *names_or_ids, fire:, timeout:, secs:|#, online:, restart:| + qm_cli.cmd :migrate, "Migrates VM(s) to an other host", min: 2 do |target, *names_or_ids, fire:, timeout:, secs:|#, online:, restart:| #if online and restart # raise UsageError, "You have to decide for one migration-mode: --restart or --online." #end @@ -22,58 +22,58 @@ def cli_qm task = qm.migrate node #, online: online wait task, text: "Migrating", timeout: timeout unless fire end - }). + end. #opt( :online, '-o', '--online', "Online-migration: Does not shutdown or interrupt running VM. Opposite of --online, Default", default: nil). #opt( :restart, '-r', '--restart', "Restart-migration: Does shutdown or interrupt running VM. Opposite of --restart", default: nil). tap {|c| opts_wait c } - qm_cli.cmd( :start, "Starts VM(s)", min: 4, &lambda {|*names_or_ids, node: nil, fire:, timeout:, secs:| + qm_cli.cmd :start, "Starts VM(s)", min: 4 do |*names_or_ids, node: nil, fire:, timeout:, secs:| connect per_argument names_or_ids, print: "\e[1;34mStart VM %s:\e[0m" do |name_or_id| qm = Proxmox::Qemu.find! name_or_id start qm, node: node, fire: fire, timeout: timeout, secs: secs end - }).tap {|c| opts_wait c } + end.tap {|c| opts_wait c } - qm_cli.cmd( :stop, "Stops VM(s)", min: 3, &lambda {|*names_or_ids, fire: nil, timeout:, secs:| + qm_cli.cmd :stop, "Stops VM(s)", min: 3 do |*names_or_ids, fire: nil, timeout:, secs:| connect per_argument names_or_ids, print: "\e[1;34mStart VM %s:\e[0m" do |name_or_id| qm = Proxmox::Qemu.find! name_or_id stop qm, fire: fire, timeout: timeout, secs: secs end - }).tap {|c| opts_wait c } + end.tap {|c| opts_wait c } - qm_cli.cmd( :wait, "Wait till VM is in state", &lambda {|name_or_id, state, timeout: nil, secs: nil| + qm_cli.cmd :wait, "Wait till VM is in state" do |name_or_id, state, timeout: nil, secs: nil| connect qm = Proxmox::Qemu.find! name_or_id wait qm, state, timeout: timeout, secs: secs - }). + end. opt( :timeout, "-tTIMEOUT", "--timeout=TIMEOUT", "Wait for max TIMEOUT seconds (default: endless)", default: nil). opt( :secs, "-sSECONDS", "--seconds=SECONDS", "Check every SECONDS for state (default: 0.2)", default: 0.2) - qm_cli.cmd( :status, "Lists VMs with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil, tags: nil| + qm_cli.cmd :status, "Lists VMs with status", aliases: [nil] do |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).flat_pmap( &:qemu).peach &push + node_opt( node).peach {|n| n.qemu.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( :node, '-n', '--node=NODE', "List only hosted by this NODE"). opt( :status, '-S', '--status=STATUS', "Filter for status (running, stopped, ...) (default: no filter)"). opt( :tags, '-t', '--tags=TAGS', "Filter by comma-seperated tags. All tags must be present for CT") - qm_cli.cmd :exec, "Executes Command in VM via qemu-guest-agent", min: 4, &lambda {|name_or_id, *command| + qm_cli.cmd :exec, "Executes Command in VM via qemu-guest-agent", min: 4 do |name_or_id, *command| connect STDERR.puts "! #{$?.exitstatus}" unless Proxmox::Qemu.find!( name_or_id).exec *command - } + end - qm_cli.cmd( :resize, 'Resize a disk', &lambda {|name_or_id, disk, size| + qm_cli.cmd :resize, 'Resize a disk' do |name_or_id, disk, size| connect qm = Proxmox::Qemu.find! name_or_id task = qm.resize disk, size wait task, text: "Resizing #{qm.sid} #{disk} to #{size}" - }) + end - qm_cli.cmd 'help', '', aliases: ['-h', '--help'], &lambda {|*args| help qm_cli, *args } - } + qm_cli.cmd 'help', '', aliases: ['-h', '--help'] do |*args| help qm_cli, *args end + end end end diff --git a/lib/pve/cli/storage.rb b/lib/pve/cli/storage.rb index 32cf5b3..3dc7bb1 100644 --- a/lib/pve/cli/storage.rb +++ b/lib/pve/cli/storage.rb @@ -1,19 +1,19 @@ class PVE::Cli def cli_storage cli.sub :storage, "Storages", min: 3 do |cli_sm| - cli_sm.cmd :list, "List Storages", aliases: ['ls'], &lambda {|node=nil| + cli_sm.cmd :list, "List Storages", aliases: ['ls'] do |node=nil| connect nodes = node ? [Proxmox::Node.find_by_name!( node)] : Proxmox::Node.all - nodes.flat_map do |n| + nodes.flat_pmap do |n| n.lxc.map {|c| c.vmid.to_i } end.sort.each {|c| puts c } - } + end - cli_sm.cmd :status, "List Storages with status", aliases: [nil], &lambda {|node=nil| + cli_sm.cmd :status, "List Storages with status", aliases: [nil] do |node=nil| connect to = TablizedOutput.new %w[A E S Storage Host Type] nodes = node ? [Proxmox::Node.find_by_name!( node)] : Proxmox::Node.all - nodes.each do |n| + nodes.peach do |n| n.storage.each do |v| to.push [ case v.active @@ -31,15 +31,15 @@ def cli_storage end end to.print order: [4,5] - } + end cli_sm.sub :content, "Content of Storage", aliases: ['cnt'] do |cli_cnt| - cli_cnt.cmd :list, "List Content", aliases: ['ls'], &lambda {|node=nil, storage| + cli_cnt.cmd :list, "List Content", aliases: ['ls'] do |node=nil, storage| connect node = node ? Proxmox::Node.find_by_name!( node) : Proxmox::Node.all.first storage = node.storage.select {|sm| storage == sm.storage }.first storage.content.each {|c| puts c.to_s } - } + end cli_cnt.cmd( :help, '', aliases: ['-h', '--help', nil]) {|*args| help cli_cnt, *args } end @@ -49,39 +49,39 @@ def cli_storage cli.sub :apl, "Appliances - Downloadable container images", min: 3 do |cli_apl| - cli_apl.cmd( :content, "Table of all provided appliances", aliases: [nil], &lambda {|node:, regexp:, system:, applications:| + cli_apl.cmd :content, "Table of all provided appliances", aliases: [nil] do |node:, regexp:, system:, applications:| connect appliances node, regexp, system, applications - }). + end. opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil). opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil). opt( :system, '-s', '--system', 'Only system templates', default: nil). opt( :applications, '-a', '--applications', 'Only applications (non system) templates', default: nil) - cli_apl.cmd( :system, "Table of provided systems", &lambda {|node:, regexp:| + cli_apl.cmd :system, "Table of provided systems" do |node:, regexp:| connect appliances node, regexp, true, nil - }). + end. opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil). opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil) - cli_apl.cmd( :applications, "Table of provided applications", &lambda {|node:, regexp:| + cli_apl.cmd :applications, "Table of provided applications" do |node:, regexp:| connect appliances node, regexp, nil, true - }). + end. opt( :node, '-n=NODE', '--node', 'Ask this node for appliances (any node should list the same)', default: nil). opt( :regexp, '-r=REGEXP', '--regexp', 'Filter by template', default: nil) - cli_apl.cmd( :list, "List provided appliances", aliases: ['ls'], &lambda {|node=nil, regexp:| + cli_apl.cmd :list, "List provided appliances", aliases: ['ls'] do |node=nil, regexp:| connect node = node ? Proxmox::Node.find_by_name!( node) : Proxmox::Node.all.first node.aplinfo.each do |apl| puts apl.template end - }). + end. opt( :regexp, '-r=REGEXP', '--regexp', 'Filters by name', default: nil) - cli_apl.cmd :download, "Download appliance", aliases: ['dl'], min: 2, &lambda {|template, node, storage=nil| + cli_apl.cmd :download, "Download appliance", aliases: ['dl'], min: 2 do |template, node, storage=nil| storage ||= 'local' connect node = Proxmox::Node.find_by_name! node @@ -89,7 +89,7 @@ def cli_storage raise UsageError, "Appliance not found" unless apl task = apl.download storage wait task, text: "Download #{apl.template} on #{node.node} to #{storage}" - } + end cli_apl.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help cli_apl, *args } end diff --git a/lib/pve/cli/task.rb b/lib/pve/cli/task.rb index e1a811d..caa901a 100644 --- a/lib/pve/cli/task.rb +++ b/lib/pve/cli/task.rb @@ -4,7 +4,7 @@ class PVE::Cli hosted = {} Proxmox::Hosted.all.each {|h| hosted[h.vmid.to_i] = h } hosted.delete nil - exe.call lambda {|t| + exe.call do |t| u = t.upid v = u.id ? hosted[u.id.to_i] : t.node to.push [ @@ -24,22 +24,22 @@ class PVE::Cli end, u.dtype, u.upid ] - } + end to.print order: order end def cli_task cli.sub :task, "Inspect tasks" do |tcli| - tcli.cmd :list, "List done tasks", &lambda {|node=nil| + tcli.cmd :list, "List done tasks" do |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 } - } + end - tcli.cmd :get, "Inspect a task", &lambda {|upid| + tcli.cmd :get, "Inspect a task" do |upid| connect Proxmox::Node.all.each do |n| n.tasks.each do |t| @@ -49,16 +49,16 @@ def cli_task return end end - } + end - tcli.cmd( :status, "Lists tasks with status", aliases: [nil], &lambda {|target=nil, sort: nil, node: nil, status: nil| + tcli.cmd :status, "Lists tasks with status", aliases: [nil] do |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 - }) + end - tcli.cmd( :monitor, "Monitors running tasks", &lambda {|node: nil| + tcli.cmd :monitor, "Monitors running tasks" do |node: nil| connect nodes = Proxmox::Node.all nodes = nodes.select {|n| node == n.name } if node @@ -81,7 +81,7 @@ def cli_task rescue Interrupt STDERR.print "\e[2J\e[1;1H" end - }) + end tcli.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help ct_cli, *args } end diff --git a/lib/pve/proxmox.rb b/lib/pve/proxmox.rb index 98b7550..38f2241 100644 --- a/lib/pve/proxmox.rb +++ b/lib/pve/proxmox.rb @@ -155,7 +155,7 @@ module Proxmox end def rest_post path, **data, &exe - data = __data__( data) + data = __data__( **data) STDERR.puts "POST #{path} <= #{data}" if $DEBUG __response__ Proxmox.connection[path].post( data, __headers__( :'Content-Type' => 'application/json')) rescue RestClient::Exception From 9ff1c8efee9aa299083babc7617f5a9420b181c1 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Sat, 21 Sep 2024 21:25:30 +0200 Subject: [PATCH 21/28] require ~>ruby-3.1 --- pve.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pve.gemspec b/pve.gemspec index 7f35c90..ce0ac7e 100644 --- a/pve.gemspec +++ b/pve.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |spec| # 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) # Old behaviour will be droped. - spec.required_ruby_version = Gem::Requirement.new ">= 2.7" + spec.required_ruby_version = Gem::Requirement.new "~> 3.1" spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = spec.homepage From b7610df3285cbd65b4935446ecf63ae0727adbce Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Sun, 22 Sep 2024 13:51:00 +0200 Subject: [PATCH 22/28] help-text for fire more positive and generic --- lib/pve/cli.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pve/cli.rb b/lib/pve/cli.rb index 00b01e1..f95aa0e 100644 --- a/lib/pve/cli.rb +++ b/lib/pve/cli.rb @@ -419,7 +419,7 @@ class PVE::Cli cl. opt( :timeout, "-t", "--timeout=TIMEOUT", "Wait for max TIMEOUT seconds (default: endless)", default: nil). opt( :secs, "-s", "--seconds=SECONDS", "Check every SECONDS for state (default: 0.2)", default: 0.2). - opt( :fire, "-f", "--[no-]fire", "Do not wait till running", default: false) + opt( :fire, "-f", "--[no-]fire", "Return immidiatly, instead of waiting till finished", default: false) end def complete_lxc f From 71381946e1930673b72e4ed388939b771d0ebcf1 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Fri, 22 Nov 2024 10:36:30 +0100 Subject: [PATCH 23/28] ct create: bugfix hash2cnfstr. (expects "**options", older versions were working also with "*") --- lib/pve/proxmox.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pve/proxmox.rb b/lib/pve/proxmox.rb index 38f2241..0f8201b 100644 --- a/lib/pve/proxmox.rb +++ b/lib/pve/proxmox.rb @@ -701,7 +701,7 @@ module Proxmox #}.map {|k,v| "#{k}=#{v}" }.join( ','), swap: tmplt.swap, 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? } temp = LXC.send :__new__, node: node, vmid: options[:vmid], name: name, hostname: options[:hostname] From 553197800f96567853a07c25b7909a2f4ab1ee6d Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 10 Jul 2025 21:15:19 +0200 Subject: [PATCH 24/28] proxmox.rb refactored: many smaller files --- lib/pve/proxmox.rb | 868 +---------------------------- lib/pve/proxmox/apl_info.rb | 19 + lib/pve/proxmox/base.rb | 48 ++ lib/pve/proxmox/exceptions.rb | 21 + lib/pve/proxmox/ha.rb | 76 +++ lib/pve/proxmox/hosted.rb | 138 +++++ lib/pve/proxmox/lxc.rb | 136 +++++ lib/pve/proxmox/module.rb | 72 +++ lib/pve/proxmox/node.rb | 87 +++ lib/pve/proxmox/qemu.rb | 87 +++ lib/pve/proxmox/rest_connection.rb | 75 +++ lib/pve/proxmox/storage.rb | 27 + lib/pve/proxmox/task.rb | 110 ++++ 13 files changed, 902 insertions(+), 862 deletions(-) create mode 100644 lib/pve/proxmox/apl_info.rb create mode 100644 lib/pve/proxmox/base.rb create mode 100644 lib/pve/proxmox/exceptions.rb create mode 100644 lib/pve/proxmox/ha.rb create mode 100644 lib/pve/proxmox/hosted.rb create mode 100644 lib/pve/proxmox/lxc.rb create mode 100644 lib/pve/proxmox/module.rb create mode 100644 lib/pve/proxmox/node.rb create mode 100644 lib/pve/proxmox/qemu.rb create mode 100644 lib/pve/proxmox/rest_connection.rb create mode 100644 lib/pve/proxmox/storage.rb create mode 100644 lib/pve/proxmox/task.rb diff --git a/lib/pve/proxmox.rb b/lib/pve/proxmox.rb index 0f8201b..a2e6842 100644 --- a/lib/pve/proxmox.rb +++ b/lib/pve/proxmox.rb @@ -1,862 +1,6 @@ -require 'rest_client' -require 'cgi' -require 'json' -require 'ipaddress' -require 'shellwords' -require 'active_support/all' - -class RestClient::Exception - attr_reader :request - def to_s - "#{request|"?"}: #{message}: #{response}" - end -end - -module Proxmox - class Exception < ::Exception - end - class NotFound < Exception - end - class AlreadyExists < Exception - end - class AlreadyLocked < Exception - end - class UnparsableUPID < Exception - end - - def self.cnfstr2hash str - str. - split( ','). - map do |o| - k, v = o.split( '=', 2) - [k.to_sym, v] - end. - to_h - end - - def self.hash2cnfstr **opts - opts. - map do |k, v| - v = - case v - when true then 1 - when false then 0 - else v.to_s - end - "#{k}=#{v}" - end. - join( ',') - end - - def self.connect username, password, realm: nil, verify_tls: nil, uri: nil - uri ||= 'https://localhost:8006/api2/json' - cred = - { - username: username, - password: password, - realm: realm || 'pve' - } - @@connection = - RestClient::Resource.new( uri, verify_ssl: ! ! verify_tls).tap do |rs| - resp = rs['/access/ticket'].post cred - case resp.code - when 200 - data = JSON.parse resp.body, symbolize_names: true - @@api_ticket = - { - CSRFPreventionToken: data[:data][:CSRFPreventionToken], - cookie: "PVEAuthCookie=#{CGI.escape data[:data][:ticket]}" - } - else - raise Exception, "Authentication failed" - end - end - end - - def self.connection - @@connection - end - - def self.api_ticket - @@api_ticket - end - - def self.find_by_name name - Proxmox::LXC.find_by_name( name) || Proxmox::Qemu.find_by_name( name) || Proxmox::Node.find_by_name( name) - end - - def self.find_by_vmid vmid - Proxmox::LXC.find_by_vmid( vmid) || Proxmox::Qemu.find_by_vmid( vmid) - end - - def self.find name_or_id - Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find( name_or_id) || Proxmox::Node.find( name_or_id) - end - - module RestConnection - def __response__ resp - case resp.code - when 200 - JSON.parse( resp.body, symbolize_names: true)[:data] - when 500 - nil - else - raise "Request failed of #{req.url} [#{resp.code}]: #{resp.body.inspect}" - end - end - - def __headers__ **hdrs - Proxmox.api_ticket.merge( 'Accept' => 'application/json').merge( hdrs) - end - - def __data__ **data - case data - when String - data - else - data.to_json - end - end - - private :__response__, :__headers__, :__data__ - - def bench path - #t = Time.now - r = yield - #p path => Time.now-t - r - end - - def rest_get path, **data, &exe - data = data.delete_if {|k,v|v.nil?} - path += "#{path.include?( ??) ? ?& : ??}#{data.map{|k,v|"#{CGI.escape k.to_s}=#{CGI.escape v.to_s}"}.join '&'}" unless data.empty? - STDERR.puts "GET #{path} <= #{data}" if $DEBUG - __response__ Proxmox.connection[path].get( __headers__( :'Content-Type' => 'application/json')) - rescue RestClient::Exception - $!.instance_variable_set :@request, "GET #{path}" - raise - end - - def rest_put path, **data, &exe - data = __data__( **data) - STDERR.puts "PUT #{path} <= #{data}" if $DEBUG - __response__ Proxmox.connection[path].put( data, __headers__( :'Content-Type' => 'application/json')) - rescue RestClient::Exception - $!.instance_variable_set :@request, "PUT #{path}" - raise - end - - def rest_del path, &exe - STDERR.puts "DELETE #{path}" if $DEBUG - __response__ Proxmox.connection[path].delete( __headers__( :'Content-Type' => 'application/json')) - rescue RestClient::Exception - $!.instance_variable_set :@request, "DELETE #{path}" - raise - end - - def rest_post path, **data, &exe - data = __data__( **data) - STDERR.puts "POST #{path} <= #{data}" if $DEBUG - __response__ Proxmox.connection[path].post( data, __headers__( :'Content-Type' => 'application/json')) - rescue RestClient::Exception - $!.instance_variable_set :@request, "POST #{path}" - raise - end - end - - class Base - include RestConnection - extend RestConnection - - attr_reader :sid - - class <" - 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 running?() status.running? end - def finished?() status.finished? end - alias stopped? finished? - def successfull?() status.successfull? end - def failed?() status.failed? end - - class UPID - RE = - /^ - UPID : - (?[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?) : - (?[0-9A-Fa-f]{8}) : - (?[0-9A-Fa-f]{8,9}) : - (?[0-9A-Fa-f]{8}) : - (?[^:\s]+) : - (?[^:\s]*) : - (?[^:\s]+) : - $/x - - attr_reader :upid, :node, :pid, :pstart, :starttime, :dtype, :id, :user - alias to_s upid - - def initialize upid - m = RE.match upid - raise UnparsableUPID, "UPID cannot be parsed: #{upid.inspect} (#{upid.class})" unless m - @upid = upid - @node = m[:node] - @pid = m[:pid].to_i 16 - @pstart = m[:pstart].to_i 16 - @starttime = Time.at m[:starttime].to_i( 16) - @dtype = m[:dtype] - @id = case m[:id] when '' then nil else m[:id].to_i end - @user = m[:user] - end - - def inspect - "#<#{self.class.name} #{@upid}>" - end - end - - def rest_prefix - @rest_prefix = "/nodes/#{@node.node}/tasks/#{upid}" - end - - def initialize - rest_prefix - @sid, @upid = upid, UPID.new( upid) - end - - def inspect - "#<#{self.class.name} #{@upid}>" - end - - def status - Status.fetch node: @node, task: self, upid: @upid - end - - def log start: nil, limit: nil - rest_get( "#{@rest_prefix}/log", start: start, limit: limit) - end - end - - class Hosted < Base - def self.all - Node.all.flat_map {|n| n.qemu + n.lxc } - end - - def refresh! - __update__ **rest_get( "#{@rest_prefix}/status/current").merge( node: @node, t: @t) - end - - def __update__ **data - data[:tags] = (data[:tags]||'').split ';' - data[:status] &&= data[:status].to_sym - super **data - end - private :__update__ - - def === t - @name =~ t or @vmid.to_s =~ t or @sid =~ t - end - - def start - Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/status/start") - end - - def stop - Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/status/stop") - end - - def destroy - Task.send :__new__, node: @node, host: self, upid: rest_del( "#{@rest_prefix}") - end - - def current_status - rest_get "#{@rest_prefix}/status/current" - end - - def running? - current_status[:status] == 'running' - end - - def stopped? - current_status[:status] == 'stopped' - end - - def wait forstatus, timeout: nil, secs: nil, lock: nil, &exe - forstatus = forstatus.to_s - secs ||= 0.2 - b = Time.now + timeout.to_i + secs - loop do - d = current_status - return true if d[:status] == forstatus and (not lock or lock == d[:lock]) - exe.call d[:status], d[:lock] if exe - return false if timeout and b <= Time.now - sleep secs - end - nil - end - - def config - cnf = rest_get "#{@rest_prefix}/config" - cnf[:network] = - cnf. - keys. - map( &:to_s). - grep( /\Anet\d+\z/). - map do |k| - nc = Proxmox::cnfstr2hash cnf.delete( k.to_sym) - nc[:card] = k - nc[:ip] &&= IPAddress::IPv4.new nc[:ip] - nc[:gw] &&= IPAddress::IPv4.new nc[:gw] - nc[:mtu] &&= nc[:mtu].to_i - nc[:tag] &&= nc[:tag].to_i - nc[:firewall] &&= 1 == nc[:firewall].to_i - nc - end - cnf[:features] &&= - Proxmox::cnfstr2hash( cnf[:features]). - tap {|f| f[:nesting] &&= '1' == f[:nesting] } - cnf[:unprivileged] &&= 1 == cnf[:unprivileged] - cnf[:memory] &&= cnf[:memory].to_i - cnf[:cores] &&= cnf[:cores].to_i - cnf[:tags] &&= cnf[:tags].split ';' - cnf.update cnf.delete( :lxc).map{|k,v|[k.to_sym,v]}.to_h if cnf[:lxc] - cnf - end - - def cnfset **cnf - r = {delete: []} - cnf.each do |k,v| - case v - when true then r[k] = 1 - when false then r[k] = 0 - when nil then r[:delete].push k - when Array - case k - when :tags - r[k] = v.join ';' - else r[k] = v - end - else r[k] = v - end - end - r.delete :delete if r[:delete].empty? - rest_put "#{@rest_prefix}/config", **r - end - - # it is only allowed to lock local guests, because it is only possible to unlock local guests. - def lock reason, &exe - cnf = config - raise Proxmox::AlreadyLocked, "Machine #{self} Already locked for: #{cnf[:lock].inspect}" unless cnf[:lock].nil? - r = rest_put "#{@rest_prefix}/config", {lock: reason} - if block_given? - begin return yield - ensure unlock - end - else r - end - end - - def unlock - system 'pct', 'unlock', @vmid.to_s - raise Proxmox::UnlockFailed unless $?.success? - #rest_put "#{@rest_prefix}/config", {delete: %w[lock], skiplock: 1} - end - - def resize disk, size - upid = rest_put "#{@rest_prefix}/resize", disk: disk, size: size - Task.send :__new__, node: @node, host: self, upid: upid - end - end - - class Qemu < Hosted - class < tmplt.ssh_public_keys, - storage: tmplt.storage, - #rootfs: { - # volume: "root:vm-#{vmid}-disk-0", size: '4G' - #}.map {|k,v| "#{k}=#{v}" }.join( ','), - swap: tmplt.swap, - unprivileged: tmplt.unprivileged, - features: tmplt.features ? Proxmox::hash2cnfstr( **tmplt.features) : nil, - }.delete_if {|k,v| v.nil? } - - temp = LXC.send :__new__, node: node, vmid: options[:vmid], name: name, hostname: options[:hostname] - upid = rest_post( "/nodes/%s/lxc" % node.node, **options) - Task.send :__new__, node: node, host: temp, upid: upid - end - end - - def rest_prefix - @rest_prefix ||= "/nodes/%s/lxc/%d" % [@node.node, @vmid] - end - - def initialize - rest_prefix - @sid = "ct:#{@vmid}" - end - - def ha - HA.find self - end - - def exec *args - node.exec 'pct', 'exec', vmid, '--', *args - end - - def enter - node.enter 'pct', 'enter', vmid - end - - def migrate target - target = - case target - when Node then target - else Node.find!( target.to_s) - end - Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/migrate", target: target.node) - end - end - - class HA < Base - def rest_prefix - @rest_prefix ||= "/cluster/ha/resources/#{virt.sid}" - end - - def initialize - rest_prefix - end - - class < tmplt.ssh_public_keys, + storage: tmplt.storage, + #rootfs: { + # volume: "root:vm-#{vmid}-disk-0", size: '4G' + #}.map {|k,v| "#{k}=#{v}" }.join( ','), + swap: tmplt.swap, + unprivileged: tmplt.unprivileged, + features: tmplt.features ? Proxmox::hash2cnfstr( **tmplt.features) : nil, + }.delete_if {|k,v| v.nil? } + + temp = LXC.send :__new__, node: node, vmid: options[:vmid], name: name, hostname: options[:hostname] + upid = rest_post( "/nodes/%s/lxc" % node.node, **options) + Proxmox::Task.send :__new__, node: node, host: temp, upid: upid + end + end + + def rest_prefix + @rest_prefix ||= "/nodes/%s/lxc/%d" % [@node.node, @vmid] + end + + def initialize + rest_prefix + @sid = "ct:#{@vmid}" + end + + def ha + Proxmox::HA.find self + end + + def exec *args + node.exec 'pct', 'exec', vmid, '--', *args + end + + def enter + node.enter 'pct', 'enter', vmid + end + + def migrate target + target = + case target + when Proxmox::Node then target + else Proxmox::Node.find!( target.to_s) + end + Proxmox::Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/migrate", target: target.node) + end +end + diff --git a/lib/pve/proxmox/module.rb b/lib/pve/proxmox/module.rb new file mode 100644 index 0000000..0a08c4c --- /dev/null +++ b/lib/pve/proxmox/module.rb @@ -0,0 +1,72 @@ +require_relative 'rest_connection' + +module Proxmox + def self.cnfstr2hash str + str. + split( ','). + map do |o| + k, v = o.split( '=', 2) + [k.to_sym, v] + end. + to_h + end + + def self.hash2cnfstr **opts + opts. + map do |k, v| + v = + case v + when true then 1 + when false then 0 + else v.to_s + end + "#{k}=#{v}" + end. + join( ',') + end + + def self.connect username, password, realm: nil, verify_tls: nil, uri: nil + uri ||= 'https://localhost:8006/api2/json' + cred = + { + username: username, + password: password, + realm: realm || 'pve' + } + @@connection = + RestClient::Resource.new( uri, verify_ssl: ! ! verify_tls).tap do |rs| + resp = rs['/access/ticket'].post cred + case resp.code + when 200 + data = JSON.parse resp.body, symbolize_names: true + @@api_ticket = + { + CSRFPreventionToken: data[:data][:CSRFPreventionToken], + cookie: "PVEAuthCookie=#{CGI.escape data[:data][:ticket]}" + } + else + raise Exception, "Authentication failed" + end + end + end + + def self.connection + @@connection + end + + def self.api_ticket + @@api_ticket + end + + def self.find_by_name name + Proxmox::LXC.find_by_name( name) || Proxmox::Qemu.find_by_name( name) || Proxmox::Node.find_by_name( name) + end + + def self.find_by_vmid vmid + Proxmox::LXC.find_by_vmid( vmid) || Proxmox::Qemu.find_by_vmid( vmid) + end + + def self.find name_or_id + Proxmox::LXC.find( name_or_id) || Proxmox::Qemu.find( name_or_id) || Proxmox::Node.find( name_or_id) + end +end diff --git a/lib/pve/proxmox/node.rb b/lib/pve/proxmox/node.rb new file mode 100644 index 0000000..4a041c6 --- /dev/null +++ b/lib/pve/proxmox/node.rb @@ -0,0 +1,87 @@ +require_relative 'base' +require_relative 'exceptions' +require_relative 'lxc' +require_relative 'apl' +require_relative 'qemu' +require_relative 'storage' + +class Proxmox::Node < Proxmox::Base + class < 'application/json').merge( hdrs) + end + + def __data__ **data + case data + when String + data + else + data.to_json + end + end + + private :__response__, :__headers__, :__data__ + + def bench path + #t = Time.now + r = yield + #p path => Time.now-t + r + end + + def rest_get path, **data, &exe + data = data.delete_if {|k,v|v.nil?} + path += "#{path.include?( ??) ? ?& : ??}#{data.map{|k,v|"#{CGI.escape k.to_s}=#{CGI.escape v.to_s}"}.join '&'}" unless data.empty? + STDERR.puts "GET #{path} <= #{data}" if $DEBUG + __response__ Proxmox.connection[path].get( __headers__( :'Content-Type' => 'application/json')) + rescue RestClient::Exception + $!.instance_variable_set :@request, "GET #{path}" + raise + end + + def rest_put path, **data, &exe + data = __data__( **data) + STDERR.puts "PUT #{path} <= #{data}" if $DEBUG + __response__ Proxmox.connection[path].put( data, __headers__( :'Content-Type' => 'application/json')) + rescue RestClient::Exception + $!.instance_variable_set :@request, "PUT #{path}" + raise + end + + def rest_del path, &exe + STDERR.puts "DELETE #{path}" if $DEBUG + __response__ Proxmox.connection[path].delete( __headers__( :'Content-Type' => 'application/json')) + rescue RestClient::Exception + $!.instance_variable_set :@request, "DELETE #{path}" + raise + end + + def rest_post path, **data, &exe + data = __data__( **data) + STDERR.puts "POST #{path} <= #{data}" if $DEBUG + __response__ Proxmox.connection[path].post( data, __headers__( :'Content-Type' => 'application/json')) + rescue RestClient::Exception + $!.instance_variable_set :@request, "POST #{path}" + raise + end +end diff --git a/lib/pve/proxmox/storage.rb b/lib/pve/proxmox/storage.rb new file mode 100644 index 0000000..f1ff4b3 --- /dev/null +++ b/lib/pve/proxmox/storage.rb @@ -0,0 +1,27 @@ +require_relative 'base' + +class Proxmox::Storage < Proxmox::Base + class Content < Proxmox::Base + def rest_prefix + @rest_prefix ||= "/nodes/#{@node.node}/storage/#{@storage}/content/#{@content}" + end + + def initialize() rest_prefix end + def to_s() "#{node.node} #{volid}" end + end + + def rest_prefix + @rest_prefix ||= "/nodes/#{@node.node}/storage/#{@storage}" + end + + def content + rest_get( "#{@rest_prefix}/content").map do |c| + Content.send :__new__, c.merge( node: @node, storage: self, t: 'smc') + end + end + + def initialize() rest_prefix end + def to_s() "#{@node.node}:#{@storage}" end + def active?() 1 == @active end + def enabled?() 1 == @enabled end +end diff --git a/lib/pve/proxmox/task.rb b/lib/pve/proxmox/task.rb new file mode 100644 index 0000000..e52b4d2 --- /dev/null +++ b/lib/pve/proxmox/task.rb @@ -0,0 +1,110 @@ +require_relative 'base' +require_relative 'exceptions' + +class Proxmox::Task < Proxmox::Base + class Status < Proxmox::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] + __update__ **d.merge( node: @node, upid: @upid, task: @task) + end + + def __update__ **data + data = data.merge t: 'status' + data[:status] = data[:status]&.to_sym + data[:exitstatus] = data[:exitstatus]&.to_sym + data[:state] = + case data[:status] + when :running then :running + when :stopped + case data[:exitstatus] + when :OK then :success + else :failed + end + end + super **data + 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 running?() status.running? end + def finished?() status.finished? end + alias stopped? finished? + def successfull?() status.successfull? end + def failed?() status.failed? end + + class UPID + RE = + /^ + UPID : + (?[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?) : + (?[0-9A-Fa-f]{8}) : + (?[0-9A-Fa-f]{8,9}) : + (?[0-9A-Fa-f]{8}) : + (?[^:\s]+) : + (?[^:\s]*) : + (?[^:\s]+) : + $/x + + attr_reader :upid, :node, :pid, :pstart, :starttime, :dtype, :id, :user + alias to_s upid + + def initialize upid + m = RE.match upid + raise UnparsableUPID, "UPID cannot be parsed: #{upid.inspect} (#{upid.class})" unless m + @upid = upid + @node = m[:node] + @pid = m[:pid].to_i 16 + @pstart = m[:pstart].to_i 16 + @starttime = Time.at m[:starttime].to_i( 16) + @dtype = m[:dtype] + @id = case m[:id] when '' then nil else m[:id].to_i end + @user = m[:user] + end + + def inspect + "#<#{self.class.name} #{@upid}>" + end + end + + def rest_prefix + @rest_prefix = "/nodes/#{@node.node}/tasks/#{upid}" + end + + def initialize + rest_prefix + @sid, @upid = upid, UPID.new( upid) + end + + def inspect + "#<#{self.class.name} #{@upid}>" + end + + def status + Status.fetch node: @node, task: self, upid: @upid + end + + def log start: nil, limit: nil + rest_get( "#{@rest_prefix}/log", start: start, limit: limit) + end +end From b72da462fe73e3f8677f413a0fb78be2eaa05dd0 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 10 Jul 2025 21:20:36 +0200 Subject: [PATCH 25/28] small fixes/improvements arounds ct_templates. --- lib/pve/{templates.rb => ct_templates.rb} | 4 ++-- lib/pve/proxmox/lxc.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename lib/pve/{templates.rb => ct_templates.rb} (98%) diff --git a/lib/pve/templates.rb b/lib/pve/ct_templates.rb similarity index 98% rename from lib/pve/templates.rb rename to lib/pve/ct_templates.rb index 31beaba..1c946d4 100644 --- a/lib/pve/templates.rb +++ b/lib/pve/ct_templates.rb @@ -101,7 +101,7 @@ module PVE::CTTemplate class Datacenter < Base def self.help - <<-EOF.gsub /^ {6}/, '' + <<~EOF Datacenter provides an interface for special network-settings. Networks in Datacenters are often based on this behaviour: A Network has an ID like 99. @@ -180,7 +180,7 @@ module PVE::CTTemplate end def vmid - super || ((0...100).map {|i| "#{100*network_id+i}" } - @virts.map( &:vmid)).first + super || ((0...100).map {|i| "#{100*network_id+i}".to_i } - @virts.map( &:vmid)).first end def network diff --git a/lib/pve/proxmox/lxc.rb b/lib/pve/proxmox/lxc.rb index acd69ce..cb7dd3b 100644 --- a/lib/pve/proxmox/lxc.rb +++ b/lib/pve/proxmox/lxc.rb @@ -4,7 +4,7 @@ require_relative 'exceptions' require_relative 'qemu' require_relative 'task' require_relative 'ha' -require_relative '../../template' +require_relative '../ct_templates' class Proxmox::LXC < Proxmox::Hosted class < Date: Thu, 10 Jul 2025 21:22:02 +0200 Subject: [PATCH 26/28] small fixes relatied to refactoring proxmox.rb --- lib/pve/proxmox/base.rb | 4 ++-- lib/pve/proxmox/node.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pve/proxmox/base.rb b/lib/pve/proxmox/base.rb index 2b29343..5da8569 100644 --- a/lib/pve/proxmox/base.rb +++ b/lib/pve/proxmox/base.rb @@ -1,8 +1,8 @@ require_relative 'rest_connection' class Proxmox::Base - include RestConnection - extend RestConnection + include Proxmox::RestConnection + extend Proxmox::RestConnection attr_reader :sid diff --git a/lib/pve/proxmox/node.rb b/lib/pve/proxmox/node.rb index 4a041c6..dc97555 100644 --- a/lib/pve/proxmox/node.rb +++ b/lib/pve/proxmox/node.rb @@ -1,7 +1,7 @@ require_relative 'base' require_relative 'exceptions' require_relative 'lxc' -require_relative 'apl' +require_relative 'apl_info' require_relative 'qemu' require_relative 'storage' From 7b824577d009c58b8ad18ee110e1984a91f81f38 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 10 Jul 2025 22:52:49 +0200 Subject: [PATCH 27/28] refactored cli.rb --- lib/pve/cli.rb | 158 +-------------------------------- lib/pve/cli/exceptions.rb | 22 +++++ lib/pve/cli/output.rb | 47 ++++++++++ lib/pve/cli/tablized_virt.rb | 163 +++++++++++++++++++++++++++++++++++ 4 files changed, 235 insertions(+), 155 deletions(-) create mode 100644 lib/pve/cli/exceptions.rb create mode 100644 lib/pve/cli/output.rb create mode 100644 lib/pve/cli/tablized_virt.rb diff --git a/lib/pve/cli.rb b/lib/pve/cli.rb index f95aa0e..6c53c08 100644 --- a/lib/pve/cli.rb +++ b/lib/pve/cli.rb @@ -10,6 +10,9 @@ require_relative 'cli/task' require_relative 'cli/qm' require_relative 'cli/node' require_relative 'cli/storage' +require_relative 'cli/exceptions' +require_relative 'cli/output' +require_relative 'cli/tablized_virt' class DenCli::Sub def provide_help name: nil, aliases: nil, min: nil @@ -23,73 +26,6 @@ class DenCli::Sub end class PVE::Cli - class RuntimeError <::RuntimeError - end - - class UsageError >>>>><' - target &&= /\A#{target}\z/i - nottags = nil - if tags - tags = tags.split /,/ - nottags = tags.grep /\A-/ - tags -= nottags - nottags.map! {|x| x[1..-1] } - end - status = - case status - when /\Asta(?:r(?:t(?:ed?)?)?)?\z/i, /\Aon(?:l(?:i(?:ne?)?)?)?\z/i, /\Ar(?:u(?:n(?:n(?:i(?:ng?)?)?)?)?)?\z/i, '1' - %i[started online running] - when /\Asto(?:p(?:p(?:ed?)?)?)?\z/i, /\Aof(?:f(?:l(?:i(?:ne?)?)?)?)?\z/i, '0' - %i[stopped offline] - when nil, '', /\Aa(ll?)?\z/i then nil - else - usage_error "Unknown state #{status}" - end - push = - begin - condition = [] - condition.push lambda {|n| n === target } if target - if status - condition.push lambda {|n| !n.respond_to?( :status) or status.include?( n.status) } - end - if tags - condition.push lambda {|n| - if n.respond_to?( :tags) and n.tags - nt = n.tags - [] == tags - nt and nottags == nottags - nt - else false - end - } - end - lambda {|n| to.push tablized_virt( n) if condition.all? {|c| c.call n} } - end - yield push - to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) } - end - def help cl, *args STDERR.puts cl.help( *args) exit 1 unless interactive? @@ -511,49 +404,4 @@ class PVE::Cli end to.print order: [1,2] end - - COLORS = %w[black red green yellow blue magenta cyan white].each_with_index.to_h - def fgcolor color - color = color.to_s - if /\Abright[-_]?(.*)\z/ =~ color - c = COLORS[$1] - c.nil? ? nil : "1;3#{c}" - else - c = COLORS[color] - c.nil? ? nil : "3#{c}" - end - end - - def tablized_virt v - ha = v.respond_to?( :ha) ? v.ha : nil - unknown = TablizedOutput::V.new 0, '-' - node = v.node.is_a?(String) ? v.node : v.node.node - if color = @cfg[:hosts]&.[](node.to_sym)&.[](:color) - node = ColoredString.new node, fgcolor( color) - end - [ - case v.status - when :running, :online then ColoredString.new v.status, "32" - when :stopped, :offline then ColoredString.new v.status, "31" - else v.status - end, - ha&.state || '·', - 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" - else v.sid - end, - v.name, node, - v.respond_to?(:uptime) ? TablizedOutput::V.new( v.uptime, Measured.seconds( v.uptime)) : unknown, - v.respond_to?(:cpu) ? TablizedOutput::Percentage.new( v.cpu) : unknown, - v.respond_to?(:mem) ? TablizedOutput::V.new( v.mem, Measured.bytes( v.mem)) : unknown, - v.respond_to?(:maxmem) ? TablizedOutput::Percentage.new( v.mem/v.maxmem.to_f) : unknown, - v.respond_to?(:disk) ? TablizedOutput::V.new( v.disk.to_i, Measured.bytes( v.disk.to_i)) : unknown, - if v.respond_to?(:maxdisk) and 0 < v.maxdisk.to_i - TablizedOutput::Percentage.new( v.disk.to_f/v.maxdisk.to_f) - else unknown end, - v.respond_to?(:tags) ? v.tags.join(', ') : '', - ] - end end diff --git a/lib/pve/cli/exceptions.rb b/lib/pve/cli/exceptions.rb new file mode 100644 index 0000000..3c10ad9 --- /dev/null +++ b/lib/pve/cli/exceptions.rb @@ -0,0 +1,22 @@ +class PVE::Cli + class RuntimeError <::RuntimeError + end + + class UsageError >>>>><' + target &&= /\A#{target}\z/i + nottags = nil + if tags + tags = tags.split /,/ + nottags = tags.grep /\A-/ + tags -= nottags + nottags.map! {|x| x[1..-1] } + end + status = + case status + when /\Asta(?:r(?:t(?:ed?)?)?)?\z/i, /\Aon(?:l(?:i(?:ne?)?)?)?\z/i, /\Ar(?:u(?:n(?:n(?:i(?:ng?)?)?)?)?)?\z/i, '1' + %i[started online running] + when /\Asto(?:p(?:p(?:ed?)?)?)?\z/i, /\Aof(?:f(?:l(?:i(?:ne?)?)?)?)?\z/i, '0' + %i[stopped offline] + when nil, '', /\Aa(ll?)?\z/i then nil + else + usage_error "Unknown state #{status}" + end + push = + begin + condition = [] + condition.push lambda {|n| n === target } if target + if status + condition.push lambda {|n| !n.respond_to?( :status) or status.include?( n.status) } + end + if tags + condition.push lambda {|n| + if n.respond_to?( :tags) and n.tags + nt = n.tags + [] == tags - nt and nottags == nottags - nt + else false + end + } + end + lambda {|n| to.push tablized_virt( n) if condition.all? {|c| c.call n} } + end + yield push + to.print order: sort.each_char.map {|c| (2*c.ord[5]-1) * (' sainhucmd'.index( c.downcase)) } + end + + def tablized_virt v + TablizedVirt.new( v, @cfg).to_a + end +end + From 9cd41ec8c69edb63b5d83378430368d23919fd25 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 10 Jul 2025 23:02:16 +0200 Subject: [PATCH 28/28] small fix (ruby-3: **opts mandatory) --- lib/pve/cli/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pve/cli/base.rb b/lib/pve/cli/base.rb index 0a92e0b..e21f1ac 100644 --- a/lib/pve/cli/base.rb +++ b/lib/pve/cli/base.rb @@ -114,7 +114,7 @@ def cli_base else opts[n] = args.shift end else - raise UsageError, "Expection option to set. What do you mean with: #{arg}" + raise UsageError, "Expect config option to set. What do you mean with `#{arg}`?" end end opts.each do |k, v| @@ -155,7 +155,7 @@ def cli_base raise UsageError, "Container or Node not found: #{name_or_id}" unless th old = th.config opts[:digest] ||= old[:digest] - th.cnfset opts + th.cnfset **opts show_config th.config, old end