require 'dencli' require 'yaml' require 'pve' require_relative 'helper' require_relative 'cli/base' require_relative 'cli/ct' require_relative 'cli/ha' require_relative 'cli/task' require_relative 'cli/qm' require_relative 'cli/node' require_relative 'cli/storage' class UsageError >>>>>' 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.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) } 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? end def opts_wait cl 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) end def complete_lxc f Proxmox::LXC.all. flat_map {|x| [x.name, x.vmid.to_s] }. select {|x| f =~ x } end def complete_qemu f Proxmox::Qemu.all. flat_map {|x| [x.name, x.vmid.to_s] }. select {|x| f =~ x } end def complete_node f Proxmox::Qemu.all. map {|x| x.name }. select {|x| f =~ x } end def completion_helper *pre, arg, &exe if pre.empty? connect xs = yield /\A#{Regexp.quote arg}/ STDOUT.print "\a" if xs.empty? xs else STDOUT.print "\a" [] end end def prepare cli_node cli_ct cli_qm cli_task cli_ha cli_base cli_storage end def call *argv cli.call *argv rescue RestClient::ExceptionWithResponse STDERR.puts "#$! - #{$!.response} (#{$!.class})" #, $!.backtrace.map {|b|" #{b}"} rescue UsageError, DenCli::UsageError STDERR.puts $! exit 1 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 to = TablizedOutput.new %w
, format: %w[> > > > > <] node.aplinfo. select {|a| 'system' == a.section ? system : applications}. each do |apl| to.push [ apl.section, apl.package, apl.version, apl.os, apl.template, apl.description, ] 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