diff --git a/.gitignore b/.gitignore index d033fef..de3303e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ *.sw[pomnqrst] *.gem -Gemfile.lock -.rake_tasks diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..1fc249a --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,70 @@ +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 diff --git a/lib/pve/cli.rb b/lib/pve/cli.rb index 6c53c08..0f999e3 100644 --- a/lib/pve/cli.rb +++ b/lib/pve/cli.rb @@ -10,9 +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 UsageError " prepare end @@ -53,37 +51,38 @@ class PVE::Cli end def enter host, *args - @out.host = host.name r = host.enter *args - operational_error "Exitstatus: #{$?.exitstatus}" unless r + STDERR.puts "! #{$?.exitstatus}" unless r end def wait_state host, state, timeout: nil, lock: nil - @out.host = host.name + spinners = %w[- / | \\] + spin = 0 r = host.wait state, lock: lock, timeout: timeout, secs: 0.2 do |state, lock| - @out.progress "Still %s%s...", state, lock ? " (locked: #{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 end - @out.reset - exit 1 if interactive? and !r + STDERR.printf "\r\e[J" + exit 1 unless interactive? and r end def task_log task, logn, limit = 1024 log = task.log start: logn, limit: limit log = [] if [{n: 1, t: 'no content'}] == log unless log.empty? - @out.reset + STDERR.printf "\r\e[J" log.each {|l| puts l[:t] } logn = log.last[:n] end logn end - def wait task, secs: nil, text: nil, timeout: nil + def wait task, secs: nil, text: nil secs ||= 0.1 - logn = 0 - STDOUT.puts task.upid - @out.host = host = task.host&.name || '<>' + spinners, spin, logn = "▖▘▝▗", 0, 0 + STDERR.puts task.upid + host = task.host&.name loop do s = task.status logn = self.task_log task, logn @@ -93,53 +92,48 @@ class PVE::Cli break if 0 == logn - r logn = r end - 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 + 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 return s end - @out.progress text || "Working" + STDERR.printf "\r[%s] \e[33;1m%s\e[0m %s...\e[J", + host || s[:id], + spinners[spin = (spin + 1) % 4], + text || "Working" sleep secs end end - def migrate host, node, timeout: nil, fire: nil, secs: nil, online: nil - timeout ||= 30 - 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 + timeout ||= 30 + if node + task = host.migrate Proxmox::Node.find_by_name!( node) + wait task, text: "Migrating" + end if host.running? - ConditionViolation "Already running." + STDERR.puts "Already running." return end task = host.start - 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 + wait task, text: "Starting" unless fire end def stop host, timeout: nil, fire: nil, secs: nil timeout ||= 30 if host.stopped? - preset_error "Already stopped." + STDERR.puts "Already stopped." return end task = host.stop - t = Time.now unless fire - wait task, text: "Stopping", timeout: timeout - wait_state host, :stopped, timeout: timeout-(Time.now-t) if host.ha.exist? and not host.stopped? + wait task, text: "Stopping" end end - def create klass, template, timeout: nil, fire: nil, start: nil, **options + def create klass, template, timeout: nil, fire: nil, secs: nil, start: nil, **options options[:start] = fire && start task = klass.create template, **options return if fire @@ -159,150 +153,6 @@ 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 - - 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 help cl, *args STDERR.puts cl.help( *args) exit 1 unless interactive? @@ -312,7 +162,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", "Return immidiatly, instead of waiting till finished", default: false) + opt( :fire, "-f", "--[no-]fire", "Do not wait till running", default: false) end def complete_lxc f @@ -358,32 +208,10 @@ class PVE::Cli def call *argv cli.call *argv rescue RestClient::ExceptionWithResponse - @out.failed "%s: %s - %s (%s)", $!.request, $!, $!.response, $!.class - #STDERR.puts $!.backtrace.map {|b|" #{b}"} + STDERR.puts "#$! - #{$!.response} (#{$!.class})" #, $!.backtrace.map {|b|" #{b}"} + rescue UsageError, DenCli::UsageError + STDERR.puts $! exit 1 - rescue DenCli::UsageError, RuntimeError - @out.failed "%s", $! - exit 1 - end - - def per_argument arguments, print: nil, &exe - arguments.each do |argument| - @out.host = argument - @out.info "\e[1;34m#{print}\e[0m", argument if print - begin - yield argument - rescue RestClient::ExceptionWithResponse - @out.failed "%s: %s - %s", $!.request, $!, JSON.parse( $!.response.body) - rescue DenCli::UsageError, RuntimeError - @out.failed "%s", $! - rescue RestClient::BadRequest - @out.failed "%p", $!.message - rescue Interrupt - @out.failed "Interrupted by user" - rescue SystemExit - @out.failed "Exitcode: %d", $!.status if 0 < $!.status - end - end end def appliances node, regexp, system, applications diff --git a/lib/pve/cli/base.rb b/lib/pve/cli/base.rb index e21f1ac..86f81f6 100644 --- a/lib/pve/cli/base.rb +++ b/lib/pve/cli/base.rb @@ -2,17 +2,10 @@ require 'pmap' class PVE::Cli - def exc2warn dret, exc = Exception - yield - rescue exc - warn "#{$!} (#{$!.class})" - dret - end - using IPAddress::ToSWithNetmaskForNetworks def cli_base - cli.cmd :list, "List CT/VM-IDs", aliases: ['ls'] do |target=nil| + cli.cmd :list, "List CT/VM-IDs", aliases: ['ls'], &lambda {|target=nil| connect nodes = Proxmox::Node.all nodes. @@ -20,28 +13,27 @@ 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" 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)"). - opt( :tags, '-t', '--tags=TAGS', "Filter by comma-seperated tags. All tags must be present for Machine") - - def val2str v - case v - when true then 1 - when false then 0 - else v - end - end + 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. + 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)) } + }). + 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") def prepare_show_config cnf r = {} @@ -53,15 +45,18 @@ def cli_base net. reject {|k, v| :card == k }. sort_by {|k, v| :name == k ? :AAAAAAAAA : k }. - map {|k, v| "#{k}=#{val2str v}" } - r[net[:card].to_sym] = s.join ',' + 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(",") end when :sshkeys - r[k] = CGI.unescape( v).gsub( /^/, " "*14).gsub /\A {14}|\n\z/, '' - when :features - r[k] = v.map {|k, v| "#{k}=#{val2str v}" }.join ',' + r[k] = CGI.unescape(v).gsub( /^/, " "*14).gsub /\A {14}|\n\z/, '' else - r[k] = val2str( v).to_s.gsub( /$^/, " "*14).gsub /\n\z/, '' + case v + when true then v = 1 + when false then v = 0 + end + r[k] = v.to_s.gsub( /$^/, " "*14).gsub /\n\z/, '' end end r @@ -69,29 +64,28 @@ 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}:#{' ' * (l-k.length)} #{v}" + puts "#{k}:#{' ' * (12-k.length)} #{v}" else - 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? + 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? end end else - cnf.sort_by {|k,_| k }.each do |k,v| - puts "#{k}:#{' ' * (l-k.length)} #{v}" + cnf.sort_by{|k,v|k}.each do |k,v| + puts "#{k}:#{' ' * (12-k.length)} #{v}" end end end cli.sub :config, "CT/VM Configuration", min: 2, aliases: %w[cnf] do |ccli| - ccli.cmd :help, '', aliases: [nil, '-h', '--help'] do |*args| help ccli, *args end + ccli.cmd :help, '', aliases: [nil, '-h', '--help'], &lambda {|*args| help ccli, *args } - ccli.cmd :set, "Set Configs for CT/VM", min: 3 do |name_or_id, *args| + ccli.cmd :set, "Set Configs for CT/VM", min: 3, &lambda {|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." @@ -100,26 +94,17 @@ 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/ - n = $1.to_sym - opts[n] = - if args.empty? or /\A--/ == args.first - true - else opts[n] = args.shift - end + opts[$1.to_sym] = args.shift else - raise UsageError, "Expect config option to set. What do you mean with `#{arg}`?" + raise UsageError, "Expection option to set. What do you mean with: #{arg}" end end opts.each do |k, v| opts[k] = - case v + case v = opts[k] when '' then nil else v end @@ -144,104 +129,82 @@ def cli_base next unless opts.has_key? k opts[k] = case v = opts[k] - 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 *%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 '', '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 + th.cnfset opts show_config th.config, old - end + } - ccli.cmd :show, "Show Config of CT/VM" do |name_or_id| + ccli.cmd :show, "Show Config of CT/VM", aliases: %w[s], &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 + } end - cli.cmd :enter, "Enter Console of CT/Node" do |name_or_id| + cli.cmd( :enter, "Enter Console of CT/Node", &lambda {|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] do |*names_or_ids| + cli.cmd( :run, "Starts CT/VM", aliases: %w[start star], &lambda {|name_or_id| 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| - 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 - end. + 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 + }). 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 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| - 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 - end.tap {|c| opts_wait c } - - #cli.cmd :reboot, "Reboot CT/VM (not implemented, yet)", min: 6 do |name_or_id| + #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) # raise UsageError, "Container or Node not found: #{name_or_id}" unless th # reboot th - #end + #} - cli.cmd :stop, "Stops CT/VM", min: 4 do |*names_or_ids| + cli.cmd( :stop, "Stops CT/VM", min: 4, &lambda {|name_or_id| 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. + 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 + }). completion do |*pre, arg| completion_helper *pre, arg do |f| complete_lxc( f) + complete_qemu( f) end end - cli.cmd :help, '', aliases: ['-h', '--help'] do |*args, full:| + cli.cmd( :help, '', aliases: ['-h', '--help'], &lambda {|*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] do + cli.cmd :cli, 'Opens interactive console', min: 3, aliases: [nil], &lambda { @interactive = true - cli.interactive( File.basename( $0, '.rb')).run - end + cli.interactive( File.basename($0,'.rb')).run + } end end diff --git a/lib/pve/cli/ct.rb b/lib/pve/cli/ct.rb index e1fce32..31bc3d8 100644 --- a/lib/pve/cli/ct.rb +++ b/lib/pve/cli/ct.rb @@ -1,77 +1,58 @@ 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'] do |node=nil| + 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_pmap do |n| - exc2warn( []) { n.lxc.map {|c| c.vmid.to_i } } + nodes.flat_map do |n| + 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] 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 } } + ct_cli.cmd :status, "List CTs with status", aliases: [nil], &lambda {|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 - 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") + to.print order: [3] + } - ct_cli.cmd :enter, "Enter Console of CT" do |name_or_id| + ct_cli.cmd :enter, "Enter Console of CT", &lambda {|name_or_id| connect - @out.host = name_or_id - @out.failed "Exitstatus #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).enter - end + STDERR.puts "! #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).enter + } - ct_cli.cmd :exec, "Executes Command in CT", min: 4 do |name_or_id, *command| + ct_cli.cmd :exec, "Executes Command in CT", min: 4, &lambda {|name_or_id, *command| connect - @out.host = name_or_id - @out.failed "Exitstatus #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).exec *command - end + STDERR.puts "! #{$?.exitstatus}" unless Proxmox::LXC.find!( name_or_id).exec *command + } - 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| - ct = Proxmox::LXC.find! name_or_id - task = ct.migrate node - wait task, text: "Migrating", timeout: timeout unless fire - end - 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] do |name_or_id, volume, destination| + ct_cli.cmd( :start, "Starts CT", min: 3, &lambda {|name_or_id, node: nil, fire:, timeout:, secs:| connect ct = Proxmox::LXC.find! name_or_id - ct_volume_move ct, volume, destination - end + 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 } - ct_cli.cmd :start, "Starts CT(s)", min: 4 do |*names_or_ids, node: nil, fire:, timeout:, secs:| + ct_cli.cmd( :stop, "Stops CT", min: 3, &lambda {|name_or_id, fire: nil, 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 - end.tap {|c| opts_wait c } + ct = Proxmox::LXC.find! name_or_id + stop ct, fire: fire, timeout: timeout, secs: secs + }).tap {|c| opts_wait c } - 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 - end.tap {|c| opts_wait c } - - ct_cli.cmd :wait, "Wait till CT is in state" do |name_or_id, state, timeout: nil, secs: nil| + ct_cli.cmd( :wait, "Wait till CT is in state", &lambda {|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 do |template, *options| #, fire:, timeout:, secs:, start:| + ct_cli.cmd( :create, "Creates a new container", &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" @@ -115,28 +96,28 @@ EOU end.parse! options connect create Proxmox::LXC, template, **ctopts - end + }) - ct_cli.cmd :config, 'Shows current config', aliases: %w[cnf], min: 2 do |name_or_id| + ct_cli.cmd( :config, 'Shows current config', aliases: %w[cnf], &lambda {|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' do |name_or_id, disk, size| + ct_cli.cmd( :resize, 'Resize a disk', &lambda {|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 do |name_or_id, fire:, secs:, timeout:, i_really_want_to_destroy:| + ct_cli.cmd( :destroy, '', min: 7, &lambda {|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 - end.tap {|c| opts_wait c }. + }).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/exceptions.rb b/lib/pve/cli/exceptions.rb deleted file mode 100644 index 3c10ad9..0000000 --- a/lib/pve/cli/exceptions.rb +++ /dev/null @@ -1,22 +0,0 @@ -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 - diff --git a/lib/pve/cli/task.rb b/lib/pve/cli/task.rb index caa901a..b1c420d 100644 --- a/lib/pve/cli/task.rb +++ b/lib/pve/cli/task.rb @@ -1,45 +1,16 @@ 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 do |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 - ] - end - to.print order: order - end - def cli_task cli.sub :task, "Inspect tasks" do |tcli| - tcli.cmd :list, "List done tasks" do |node=nil| + tcli.cmd :list, "List done tasks", &lambda {|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 + end.sort.each {|upid| puts upid } + } - tcli.cmd :get, "Inspect a task" do |upid| + tcli.cmd :get, "Inspect a task", &lambda {|upid| connect Proxmox::Node.all.each do |n| n.tasks.each do |t| @@ -49,41 +20,7 @@ def cli_task return end end - end - - 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" do |node: nil| - connect - nodes = Proxmox::Node.all - nodes = nodes.select {|n| node == n.name } if node - tasks = {} - 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 - end - - tcli.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help ct_cli, *args } + } end end end diff --git a/lib/pve/helper.rb b/lib/pve/helper.rb index 68e21ea..365d2c0 100644 --- a/lib/pve/helper.rb +++ b/lib/pve/helper.rb @@ -13,24 +13,17 @@ 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 "%d B" % v if 512 > v %w[KiB MiBy GiByt TiByte ExiByte PetiByte].each_with_index do |m| v /= 1024 - return units '%.1f ' % v, m if 512 > v + #return "%.1f %s" % [v, m] if 10 > v + #return "%d %s" % [v, m] if 512 > v + return "%.1f %s" % [v, m] if 512 > v end - units '%d ' % v, :PetiByte + "%d PetiByte" % v end def bytes2 v @@ -47,28 +40,21 @@ 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 V.new "\e[1;30m·\e[0m", 1 if 0 == i - return units '%d ' % i, :s if 90 > i + return '·' if 0 == i + return "%d s" % i if 90 > i i /= 60 - return units '%d ' % i, :mi if 90 > i + return "%d mi" % i if 90 > i i /= 60 - return units '%d ' % i, :hou if 36 > i + return "%d hou" % i if 36 > i i /= 24 - return units '%d ' % i, :days if 14 > i + return "%d days" % i if 14 > i j = i / 7 - 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 + return "%d weeks" % j if 25 > j + i /= 365 + return "%.1f years" if 550 > i + "%dy" % i end end end @@ -86,14 +72,17 @@ 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 end + class TablizedOutput def initialize header, stdout: nil, format: nil @header = header.map &:to_s @@ -112,7 +101,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.to_s end + def to_s() @s end def length() @s.length end def inspect() "#" end end @@ -124,14 +113,12 @@ class TablizedOutput def inspect() "#" end def to_s - 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]}" + #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 @@ -152,6 +139,7 @@ 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__ @@ -161,28 +149,36 @@ class TablizedOutput } EOC end - @stdout.puts \ - @header.each_with_index.map {|s, i| - #"#{' ' * (@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 \ - l.each_with_index.map {|s, i| - pad = ' ' * (@maxs[i] - s.length) - case @format[i] - when '<' - "#{s}#{pad}" - else - "#{pad}#{s}" - end - }.join( "\e[3#{i.even? ? 6 : 3}m | \e[0m") - 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] } } + 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 diff --git a/lib/pve/proxmox.rb b/lib/pve/proxmox.rb index a2e6842..c2ab96d 100644 --- a/lib/pve/proxmox.rb +++ b/lib/pve/proxmox.rb @@ -1,6 +1,699 @@ -require_relative 'proxmox/node' -require_relative 'proxmox/task' -require_relative 'proxmox/qemu' -require_relative 'proxmox/ha' -require_relative 'proxmox/storage' -require_relative 'proxmox/apl_info' +require 'rest_client' +require 'cgi' +require 'json' +require 'ipaddress' +require 'shellwords' +require 'active_support/all' + +module Proxmox + class Exception < ::Exception + end + class NotFound < Exception + end + class AlreadyExists < Exception + 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) || Proxmox::Node.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? + __response__ Proxmox.connection[path].get( __headers__( :'Content-Type' => 'application/json')) + end + + def rest_put path, **data, &exe + __response__ Proxmox.connection[path].put( __data__( data), __headers__( :'Content-Type' => 'application/json')) + end + + def rest_del path, &exe + __response__ Proxmox.connection[path].delete( __headers__( :'Content-Type' => 'application/json')) + end + + def rest_post path, **data, &exe + __response__ Proxmox.connection[path].post( __data__( data), __headers__( :'Content-Type' => 'application/json')) + 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 rest_prefix + @rest_prefix = "/nodes/#{@node.node}/tasks/#{upid}" + end + + def initialize + rest_prefix + @sid = 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 refresh! + __update__ rest_get( "#{@rest_prefix}/status/current").merge( node: @node, t: @t) + end + + def === t + @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 + + 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 = {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[:firewall] &&= 1 == nc[:firewall].to_i + nc + end + cnf[:unprivileged] &&= 1 == cnf[:unprivileged] + cnf[:memory] &&= cnf[:memory].to_i + cnf[:cores] &&= cnf[:cores].to_i + 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 + else r[k] = v + end + end + r.delete :delete if r[:delete].empty? + rest_put "#{@rest_prefix}/config", r + 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, + }.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 + 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 deleted file mode 100644 index 0a08c4c..0000000 --- a/lib/pve/proxmox/module.rb +++ /dev/null @@ -1,72 +0,0 @@ -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 deleted file mode 100644 index dc97555..0000000 --- a/lib/pve/proxmox/node.rb +++ /dev/null @@ -1,87 +0,0 @@ -require_relative 'base' -require_relative 'exceptions' -require_relative 'lxc' -require_relative 'apl_info' -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 deleted file mode 100644 index f1ff4b3..0000000 --- a/lib/pve/proxmox/storage.rb +++ /dev/null @@ -1,27 +0,0 @@ -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 deleted file mode 100644 index e52b4d2..0000000 --- a/lib/pve/proxmox/task.rb +++ /dev/null @@ -1,110 +0,0 @@ -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 diff --git a/lib/pve/ct_templates.rb b/lib/pve/templates.rb similarity index 89% rename from lib/pve/ct_templates.rb rename to lib/pve/templates.rb index 1c946d4..617271c 100644 --- a/lib/pve/ct_templates.rb +++ b/lib/pve/templates.rb @@ -20,18 +20,7 @@ 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 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 unprivileged() options.unprivileged() || 1 end def ssh_public_keys options[:'ssh-public-keys'] || @@ -93,15 +82,13 @@ 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 class Datacenter < Base def self.help - <<~EOF + <<-EOF.gsub /^ {6}/, '' Datacenter provides an interface for special network-settings. Networks in Datacenters are often based on this behaviour: A Network has an ID like 99. @@ -135,14 +122,12 @@ 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 @@ -180,7 +165,7 @@ module PVE::CTTemplate end def vmid - super || ((0...100).map {|i| "#{100*network_id+i}".to_i } - @virts.map( &:vmid)).first + super || ((0...100).map {|i| "#{100*network_id+i}" } - @virts.map( &:vmid)).first end def network diff --git a/lib/pve/version.rb b/lib/pve/version.rb index d254033..e19a049 100644 --- a/lib/pve/version.rb +++ b/lib/pve/version.rb @@ -1,3 +1,3 @@ module PVE - VERSION = '0.4.0' + VERSION = '0.2.1' end diff --git a/pve.gemspec b/pve.gemspec index ce0ac7e..05eefc0 100644 --- a/pve.gemspec +++ b/pve.gemspec @@ -12,10 +12,8 @@ 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-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 "~> 3.1" + # PVE based on debian, so ruby 2.5 is default. + spec.required_ruby_version = Gem::Requirement.new "~> 2.5.0" spec.metadata["homepage_uri"] = spec.homepage spec.metadata["source_code_uri"] = spec.homepage