Compare commits

...

28 commits

Author SHA1 Message Date
Denis Knauf
9cd41ec8c6 small fix (ruby-3: **opts mandatory) 2025-07-10 23:02:16 +02:00
Denis Knauf
7b824577d0 refactored cli.rb 2025-07-10 22:52:49 +02:00
Denis Knauf
5ac5c4bc02 small fixes relatied to refactoring proxmox.rb 2025-07-10 21:22:02 +02:00
Denis Knauf
b72da462fe small fixes/improvements arounds ct_templates. 2025-07-10 21:20:36 +02:00
Denis Knauf
553197800f proxmox.rb refactored: many smaller files 2025-07-10 21:15:19 +02:00
Denis Knauf
71381946e1 ct create: bugfix hash2cnfstr. (expects "**options", older versions were working also with "*") 2024-11-22 10:36:30 +01:00
Denis Knauf
b7610df328 help-text for fire more positive and generic 2024-09-22 13:51:00 +02:00
Denis Knauf
9ff1c8efee require ~>ruby-3.1 2024-09-21 21:25:30 +02:00
Denis Knauf
1c4bbbdb7d use features of ruby-3.1 (lambda(&exe), **kwargs) 2024-09-21 21:23:46 +02:00
Denis Knauf
9786e145c1 more cli: Thread -> pmap. exc2warn 2024-09-21 14:54:42 +02:00
Denis Knauf
ba7fbed101 bump 0.4.0 - first draw 2024-09-21 14:41:36 +02:00
Denis Knauf
f268fcc5f7 cli: Thread -> pmap. exc2warn. ruby>=2.7 2024-09-21 14:34:32 +02:00
Denis Knauf
f780eccc6a version 0.3.3 typo bugfix 2024-03-28 23:31:01 +01:00
Denis Knauf
5eb6cd52eb typo fixed 2024-03-28 17:48:03 +01:00
Denis Knauf
d6e7d84afc version 0.3.2 bugfix for correct handling "already started" 2023-10-21 00:14:26 +02:00
Denis Knauf
e2b38ca779 debug-output removed 2023-09-21 19:21:18 +02:00
Denis Knauf
82b3d3cda2 bump v0.3.1 2023-09-21 18:02:44 +02:00
Denis Knauf
6530c40749 UPID-Parser, Task status & monitoring 2023-09-21 14:55:24 +02:00
Denis Knauf
83211a817f qm/ct status shows only qms/cts, no nodes. 2023-01-16 21:37:38 +01:00
Denis Knauf
20db6bd9b2 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.
2023-01-16 17:30:23 +01:00
Denis Knauf
d558af77b6 v0.3.0: >=ruby2.7. PVE-6 not supported anymore. small fixes. more comfort. commands on more than one machine at once. 2022-10-05 12:55:03 +02:00
Denis Knauf
93889f9dab colored host hostnames - determined by config 2022-01-12 23:33:01 +01:00
Denis Knauf
4f6479a0d2 v0.2.4 2022-01-12 21:03:48 +01:00
Denis Knauf
8994faad9e Set min for commands with same (sort) aliases. Table improvements. 2022-01-12 20:07:52 +01:00
Denis Knauf
efe4f41eda v0.2.2 2021-12-13 14:29:51 +01:00
Denis Knauf
ce7a95945c status-output: colored time, nodes (re)added 2021-12-13 14:28:52 +01:00
Denis Knauf
b2d78971b0 Gemfile.lock removed and ignored 2021-12-13 14:24:59 +01:00
Denis Knauf
45646994ff 0.2.2 2021-12-08 22:38:03 +01:00
30 changed files with 1767 additions and 1048 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
*.sw[pomnqrst]
*.gem
Gemfile.lock
.rake_tasks

View file

@ -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

View file

@ -10,9 +10,9 @@ require_relative 'cli/task'
require_relative 'cli/qm'
require_relative 'cli/node'
require_relative 'cli/storage'
class UsageError <RuntimeError
end
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
@ -39,10 +39,12 @@ 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
@out = Output.new "<>"
prepare
end
@ -51,38 +53,37 @@ 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 = %w[- / | \\]
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] %s Still %s%s...\e[J\n", host.name, spinners[spin = (spin + 1) % 4], state, lock
@out.progress "Still %s%s...", state, lock ? " (locked: #{lock})" : ''
end
STDERR.printf "\r\e[J"
exit 1 unless interactive? and r
@out.reset
exit 1 if 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?
STDERR.printf "\r\e[J"
@out.reset
log.each {|l| puts l[:t] }
logn = log.last[:n]
end
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
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,48 +93,53 @@ 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
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."
ConditionViolation "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
timeout ||= 30
if host.stopped?
STDERR.puts "Already stopped."
preset_error "Already stopped."
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
@ -153,6 +159,150 @@ 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?
@ -162,7 +312,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
@ -208,10 +358,32 @@ class PVE::Cli
def call *argv
cli.call *argv
rescue RestClient::ExceptionWithResponse
STDERR.puts "#$! - #{$!.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|
@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

View file

@ -2,10 +2,17 @@ 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'], &lambda {|target=nil|
cli.cmd :list, "List CT/VM-IDs", aliases: ['ls'] do |target=nil|
connect
nodes = Proxmox::Node.all
nodes.
@ -13,27 +20,28 @@ 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: '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")
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
def prepare_show_config cnf
r = {}
@ -45,18 +53,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
@ -64,28 +69,29 @@ 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
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."
@ -94,17 +100,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}"
raise UsageError, "Expect config 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
@ -129,82 +144,104 @@ 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
th.cnfset **opts
show_config th.config, old
}
end
ccli.cmd :show, "Show Config of CT/VM", aliases: %w[s], &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 {|name_or_id|
cli.cmd :run, "Starts CT/VM", aliases: %w[start star] do |*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
end.
completion do |*pre, arg|
completion_helper *pre, arg do |f|
complete_lxc( f) + complete_qemu( f)
end
end
#cli.cmd :reboot, "Reboot CT/VM (not implemented, yet)", min: 6, &lambda {|name_or_id|
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|
# 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 {|name_or_id|
cli.cmd :stop, "Stops CT/VM", min: 4 do |*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
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

View file

@ -1,58 +1,77 @@
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_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 }
}
end
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)
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
to.print order: [3]
}
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
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
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
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
end
ct_cli.cmd( :start, "Starts CT", min: 3, &lambda {|name_or_id, node: nil, 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|
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|
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 }
ct_volume_move ct, volume, destination
end
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 do |*names_or_ids, node: nil, fire:, timeout:, secs:|
connect
ct = Proxmox::LXC.find! name_or_id
stop ct, fire: fire, timeout: timeout, secs: secs
}).tap {|c| opts_wait c }
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_cli.cmd( :wait, "Wait till CT is in state", &lambda {|name_or_id, state, timeout: nil, secs: nil|
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|
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", &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"
@ -96,28 +115,28 @@ EOU
end.parse! options
connect
create Proxmox::LXC, template, **ctopts
})
end
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 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 }

22
lib/pve/cli/exceptions.rb Normal file
View file

@ -0,0 +1,22 @@
class PVE::Cli
class RuntimeError <::RuntimeError
end
class UsageError <RuntimeError
end
def UsageError msg
raise UsageError, msg
end
class OperationalError <RuntimeError
end
def OperationalError msg
raise OperationalError, msg
end
class ConditionViolation <RuntimeError
end
def ConditionViolation msg
raise ConditionViolation, msg
end
end

View file

@ -9,42 +9,42 @@ def opts_ha cl
end
def cli_ha
cli.sub :ha, "Inspect High-Availability" do |hacli|
hacli.cmd( :create, "Create HA for CT/VM", &lambda {|name_or_id, group:, comment: nil, max_relocate:, max_restart:, state:|
cli.sub :ha, "Inspect High-Availability", min: 2 do |hacli|
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", &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", &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

View file

@ -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: [nil, '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 }

47
lib/pve/cli/output.rb Normal file
View file

@ -0,0 +1,47 @@
class PVE::Cli
class Output
LINERESET = "\r\e[J"
RESET = "\e[0m"
SUCCESS_ = "\e[32;1m✓"
SUCCESS = "#{LINERESET}[%s] #{SUCCESS_}#{RESET} "
FAILED_ = "\e[31;1m✗"
FAILED = "#{LINERESET}[%s] #{FAILED_}#{RESET} "
PROGRESS_ = "\e[33;1m%s"
PROGRESS = "#{LINERESET}[%s] #{PROGRESS_}#{RESET} "
INFO_ = "\e[33;1m•"
INFO = "#{LINERESET}[%s] #{INFO_}#{RESET} "
attr_accessor :host
def initialize host
@host, @spinners, @spin = host, "▖▘▝▗", 0
end
def progress format, *args, **kwargs
STDERR.printf PROGRESS, @host, @spinners[@spin = (@spin + 1) % 4]
STDERR.printf( format, *args, **kwargs)
end
def info format, *args, **kwargs
STDERR.printf INFO, @host
STDERR.printf( format, *args, **kwargs)
STDERR.puts
end
def failed format, *args, **kwargs
STDERR.printf FAILED, @host
STDERR.printf( format, *args, **kwargs)
STDERR.puts
end
def success format, *args, **kwargs
STDERR.printf SUCCESS, @host
STDERR.printf( format, *args, **kwargs)
STDERR.puts
end
def reset
STDERR.printf LINERESET
end
end
end

View file

@ -1,39 +1,79 @@
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] 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_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 }
}
end
qm.cmd :status, "List VMs with status", aliases: [nil], &lambda {|node=nil|
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
#online = !restart or online
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)
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
to.print order: [3]
}
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.cmd :exec, "Executes Command in VM via qemu-guest-agent", min: 4, &lambda {|name_or_id, *command|
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
end.tap {|c| opts_wait c }
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
end.tap {|c| opts_wait c }
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] 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| 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 do |name_or_id, *command|
connect
STDERR.puts "! #{$?.exitstatus}" unless Proxmox::Qemu.find!( name_or_id).exec *command
}
end
qm.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.cmd 'help', '', aliases: ['-h', '--help'], &lambda {|*args| help qm, *args }
}
qm_cli.cmd 'help', '', aliases: ['-h', '--help'] do |*args| help qm_cli, *args end
end
end
end

View file

@ -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,16 +31,16 @@ 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 }
}
cli_cnt.cmd( :help, '', aliases: ['-h', '--help']) {|*args| help cli_cnt, *args }
end
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 }
@ -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
}).
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)
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", aliases: [nil], &lambda {|node:, regexp:|
cli_apl.cmd :system, "Table of provided systems" do |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)
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", aliases: [nil], &lambda {|node:, regexp:|
cli_apl.cmd :applications, "Table of provided applications" do |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)
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

View file

@ -0,0 +1,163 @@
class PVE::Cli
class TablizedVirt
COLORS = %w[black red green yellow blue magenta cyan white].each_with_index.to_h
Unknown = TablizedOutput::V.new 0, '-'
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 initialize( virt, cfg) @virt, @cfg = virt, cfg end
def ha() @virt.respond_to?( :ha) ? @virt.ha : nil end
def name() @virt.name end
def ha_state() ha&.state || '·' end
def uptime
if @virt.respond_to? :uptime
TablizedOutput::V.new @virt.uptime, Measured.seconds( @virt.uptime)
else Unknown
end
end
def cpu
if @virt.respond_to? :cpu
TablizedOutput::Percentage.new @virt.cpu
else Unknown
end
end
def mem
if @virt.respond_to? :mem
TablizedOutput::V.new @virt.mem, Measured.bytes( @virt.mem)
else Unknown
end
end
def maxmem
if @virt.respond_to? :maxmem
TablizedOutput::Percentage.new @virt.mem/@virt.maxmem.to_f
else Unknown
end
end
def disk
if @virt.respond_to? :disk
TablizedOutput::V.new @virt.disk.to_i, Measured.bytes( @virt.disk.to_i)
else Unknown
end
end
def maxdisk
if @virt.respond_to?(:maxdisk) and 0 < @virt.maxdisk.to_i
TablizedOutput::Percentage.new @virt.disk.to_f/@virt.maxdisk.to_f
else Unknown
end
end
def tags
if @virt.respond_to? :tags
@virt.tags.join( ', ')
else ''
end
end
def node
node = @virt.node.is_a?(String) ? @virt.node : @virt.node.node
if color = @cfg[:hosts]&.[](node.to_sym)&.[](:color)
ColoredString.new node, fgcolor( color)
else node
end
end
def status
s = @virt.status
case s
when :running, :online then ColoredString.new s, "32"
when :stopped, :offline then ColoredString.new s, "31"
else s
end
end
def sid
case @virt.t
when "nd" then ColoredString.new @virt.sid, "33"
when "qm" then ColoredString.new @virt.sid, "35"
when "ct" then ColoredString.new @virt.sid, "36"
else @virt.sid
end
end
def to_a
[
status,
ha_state,
sid,
name,
node,
uptime,
cpu,
mem,
maxmem,
disk,
maxdisk,
tags,
]
end
end
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/% 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'
%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

View file

@ -1,16 +1,45 @@
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", &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.each {|upid| puts 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|
@ -20,7 +49,41 @@ 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

View file

@ -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,13 +93,15 @@ 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.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.
@ -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
@ -165,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

View file

@ -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() "#<Measured::V #{@value.inspect} (#{@length})>" end
alias :to_s :value
alias :to_str :value
end
class <<self
def bytes1 v
v = v.to_f
return "%d B" % v if 512 > 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,17 +86,14 @@ 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
@ -101,7 +112,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() "#<TO:V #{@v.inspect} #{@s.inspect}>" end
end
@ -113,12 +124,14 @@ class TablizedOutput
def inspect() "#<TO:Percentage #{@v}%>" 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]}"
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
@ -139,7 +152,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,36 +161,28 @@ 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] } }
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,
]
@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
end
end

View file

@ -1,699 +1,6 @@
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 <<self
def __new__ data
n = allocate
n.send :__update__, data
end
private :__new__
def fetch predata
__new__( predata).refresh!
end
end
def respond_to? method, also_private = false
instance_variable_defined?( "@#{method}") or super(method, also_private)
end
def method_missing method, *args, &exe
if instance_variable_defined? "@#{method}"
instance_variable_get "@#{method}"
else
super
end
end
def __update__ data
instance_variables.each do |k|
remove_instance_variable k
end
data.each do |k,v|
instance_variable_set "@#{k}", v
end
initialize
self
end
private :__update__
def refresh!
__update__ rest_get( @rest_prefix)
end
end
class Node < Base
class <<self
def find_by_name name
all.each do |node|
return node if node.node == name
end
nil
end
def find_by_name! name
find_by_name( name) or raise( Proxmox::NotFound, "Node not found: #{name}")
end
def all
rest_get( '/nodes').map {|d| __new__ d.merge( t: 'nd') }
end
end
attr_reader :name
def === t
@name =~ t or @vmid.to_s =~ t or @sid =~ t
end
def rest_prefix
@rest_prefix ||= "/nodes/#{@node}"
end
def initialize
rest_prefix
@sid = "nd:#{@node}"
@name = @node
end
def offline?() @status.nil? or 'offline' == @status end
def online?() 'online' == @status end
def lxc
return [] if offline?
rest_get( "#{@rest_prefix}/lxc").map {|d| LXC.send :__new__, d.merge( node: self, t: 'ct') }
end
def aplinfo
return [] if offline?
rest_get( "#{@rest_prefix}/aplinfo").map do |d|
AplInfo.send :__new__, d.merge( node: self, t: 'apl')
end
end
def storage
return [] if offline?
rest_get( "#{@rest_prefix}/storage").map do |d|
Storage.send :__new__, d.merge( node: self, t: 'sm')
end
end
def qemu
return [] if offline?
rest_get( "#{@rest_prefix}/qemu").map {|d| Qemu.send :__new__, d.merge( node: self, t: 'qm') }
end
def tasks
return [] if offline?
rest_get( "/#{@rest_prefix}/tasks").map {|d| Task.send :__new__, d.merge( node: self, t: 'task') }
end
def enter *command
Kernel.system 'ssh', '-t', node, command.map( &:to_s).shelljoin
end
def exec *command
Kernel.system 'ssh', node, command.map( &:to_s).shelljoin
end
end
class Task < Base
class Status < 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]
d = {exitstatus: nil}.merge d
__update__ d.merge( node: @node, t: 'status', upid: @upid, task: @task)
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 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 <<self
def all
Node.all.flat_map {|n| n.qemu }
end
def find_by_vmid vmid
vmid = vmid.to_s
all.each do |l|
return l if l.vmid == vmid
end
nil
end
def find_by_name name
all.each do |l|
return l if l.name == name
end
nil
end
def find name_or_id = nil, name: nil, vmid: nil
if (name and vmid) or (name and name_or_id) or (name_or_id and vmid)
raise Proxmox::NotFound, "name xor vmid needed to find CT, not both."
elsif name
find_by_name name
elsif vmid
find_by_vmid vmid.to_i
elsif name_or_id =~ /\D/
find_by_name name_or_id
elsif name_or_id.is_a?( Numeric) or name_or_id =~ /\d/
find_by_vmid name_or_id.to_i
else
raise Proxmox::NotFound, "name xor vmid needed to find CT."
end
end
def find_by_vmid! name
find_by_vmid( name) or raise( Proxmox::NotFound, "Virtual Machine not found: #{name}")
end
def find_by_name! name
find_by_name( name) or raise( Proxmox::NotFound, "Virtual Machine not found: #{name}")
end
def find! name
find( name) or raise( Proxmox::NotFound, "Virtual Machine not found: #{name}")
end
end
def rest_prefix
@rest_prefix ||= "/nodes/%s/qemu/%d" % [@node.node, @vmid]
end
def initialize
rest_prefix
@sid = "qm:#{@vmid}"
end
def ha
HA.find self
end
def exec *args
node.exec 'qm', 'guest', 'exec', vmid, '--', *args
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 LXC < Hosted
class <<self
def all
Node.all.flat_map {|n| n.lxc }
end
def find_by_vmid vmid
vmid = vmid.to_s
Node.all.each do |n|
n.lxc.each do |l|
return l if l.vmid == vmid
end
end
nil
end
def find_by_name name
Node.all.each do |n|
n.lxc.each do |l|
return l if l.name == name
end
end
nil
end
def find name_or_id = nil, name: nil, vmid: nil
if (name and vmid) or (name and name_or_id) or (name_or_id and vmid)
raise ArgumentError, "name xor vmid needed to find CT, not both."
elsif name
find_by_name name
elsif vmid
find_by_vmid vmid.to_i
elsif name_or_id =~ /\D/
find_by_name name_or_id
elsif name_or_id.is_a?( Numeric) or name_or_id =~ /\d/
find_by_vmid name_or_id.to_i
else
raise ArgumentError, "name xor vmid needed to find CT."
end
end
def find_by_vmid! name
find_by_vmid( name) or raise( Proxmox::NotFound, "Container not found: #{name}")
end
def find_by_name! name
find_by_name( name) or raise( Proxmox::NotFound, "Container not found: #{name}")
end
def find! name
find( name) or raise( Proxmox::NotFound, "Container not found: #{name}")
end
def create template, **options
tmplt = PVE::CTTemplate.const_get( template&.classify || 'Default').new **options
name = tmplt.name
virts = Proxmox::LXC.all + Proxmox::Qemu.all
vmid = tmplt.vmid
if virt = virts.find {|v| v.vmid == vmid }
raise Proxmox::AlreadyExists, "VT/VM with vmid [#{vmid}] already exists: #{virt.name}"
end
already_exists = virts.select {|v| v.name == name }
unless already_exists.empty?
raise Proxmox::AlreadyExists, "CT/VM named [#{name}] already exists: vmid: #{already_exists.map( &:vmid).inspect}"
end
node = Proxmox::Node.find_by_name! tmplt.node
options = {
ostemplate: tmplt.ostemplate,
vmid: vmid.to_s,
arch: tmplt.arch,
cmode: tmplt.cmode,
cores: tmplt.cores.to_i,
description: tmplt.description,
hostname: tmplt.hostname,
memory: tmplt.memory,
net0: tmplt.net0&.map {|k,v| "#{k}=#{v}" }&.join(','),
net1: tmplt.net1&.map {|k,v| "#{k}=#{v}" }&.join(','),
net2: tmplt.net2&.map {|k,v| "#{k}=#{v}" }&.join(','),
net3: tmplt.net3&.map {|k,v| "#{k}=#{v}" }&.join(','),
ostype: tmplt.ostype,
:'ssh-public-keys' => 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 <<self
def find host
ha = HA.send :__new__, digest: nil, state: nil, virt: host
ha.refresh!
end
def create host, **options
find( host).create **options
end
end
def refresh!
virt = @virt
__update__( {state: nil}.merge( rest_get( "/cluster/ha/resources/#{virt.sid}")).merge( virt: virt))
self
rescue RestClient::InternalServerError
__update__ digest: nil, state: nil, virt: virt
end
def create group: nil, comment: nil, max_relocate: nil, max_restart: nil, state: nil
options = {
sid: virt.sid,
group: group,
comment: comment,
max_relocate: max_relocate,
max_restart: max_restart,
state: state
}
options.delete_if {|k,v| v.nil? }
rest_post "/cluster/ha/resources", **options
refresh!
end
def delete
rest_del "#{@rest_prefix}"
end
def state= state
rest_put "#{@rest_prefix}", state: state.to_s
refresh!
end
def started!
self.state = :started
self
end
def stopped!
self.state = :stopped
self
end
def disabled!
self.state = :disabled
self
end
def started?() 'started' == self.state end
def stopped?() 'stopped' == self.state end
def error?() 'error' == self.state end
def active?() ! ! digest end
end
class Storage < Base
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
class Content < 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
end
class AplInfo < Base
def rest_prefix
@rest_prefix ||= "/nodes/#{@node.node}/aplinfo"
end
def initialize() rest_prefix end
def name() @template end
def system?() 'system' == @section end
def debian?() %r[\Adebian-] =~ @os end
def lxc?() 'lxc' == @type end
def download storage
upid = rest_post "#{@rest_prefix}", template: @template, storage: storage.to_s
Task.send :__new__, node: @node, host: self, upid: upid
end
end
end
require_relative 'templates'
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'

View file

@ -0,0 +1,19 @@
require_relative 'base'
require_relative 'task'
class Proxmox::AplInfo < Proxmox::Base
def rest_prefix
@rest_prefix ||= "/nodes/#{@node.node}/aplinfo"
end
def initialize() rest_prefix end
def name() @template end
def system?() 'system' == @section end
def debian?() %r[\Adebian-] =~ @os end
def lxc?() 'lxc' == @type end
def download storage
upid = rest_post "#{@rest_prefix}", template: @template, storage: storage.to_s
Proxmox::Task.send :__new__, node: @node, host: self, upid: upid
end
end

48
lib/pve/proxmox/base.rb Normal file
View file

@ -0,0 +1,48 @@
require_relative 'rest_connection'
class Proxmox::Base
include Proxmox::RestConnection
extend Proxmox::RestConnection
attr_reader :sid
class <<self
def __new__ data
n = allocate
n.send :__update__, **data
end
private :__new__
def fetch predata
__new__( predata).refresh!
end
end
def respond_to? method, also_private = false
instance_variable_defined?( "@#{method}") or super(method, also_private)
end
def method_missing method, *args, &exe
if instance_variable_defined? "@#{method}"
instance_variable_get "@#{method}"
else
super
end
end
def __update__ **data
instance_variables.each do |k|
remove_instance_variable k
end
data.each do |k,v|
instance_variable_set "@#{k}", v
end
initialize
self
end
private :__update__
def refresh!
__update__ **rest_get( @rest_prefix)
end
end

View file

@ -0,0 +1,21 @@
require 'rest_client'
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
end

76
lib/pve/proxmox/ha.rb Normal file
View file

@ -0,0 +1,76 @@
require 'rest_client'
require_relative 'base'
class Proxmox::HA < Proxmox::Base
def rest_prefix
@rest_prefix ||= "/cluster/ha/resources/#{virt.sid}"
end
def initialize
rest_prefix
end
class <<self
def find host
ha = Proxmox::HA.send :__new__, digest: nil, state: nil, virt: host
ha.refresh!
end
def create host, **options
find( host).create **options
end
end
def refresh!
virt = @virt
__update__ **{state: nil}.merge( rest_get( "/cluster/ha/resources/#{virt.sid}")).merge( virt: virt)
self
rescue RestClient::InternalServerError
__update__ digest: nil, state: nil, virt: virt
end
def create group: nil, comment: nil, max_relocate: nil, max_restart: nil, state: nil
options = {
sid: virt.sid,
group: group,
comment: comment,
max_relocate: max_relocate,
max_restart: max_restart,
state: state
}
options.delete_if {|k,v| v.nil? }
rest_post "/cluster/ha/resources", **options
refresh!
end
def delete
rest_del "#{@rest_prefix}"
end
def state= state
r = rest_put "#{@rest_prefix}", state: state.to_s
pp r: r
refresh!
end
def started!
self.state = :started
self
end
def stopped!
self.state = :stopped
self
end
def disabled!
self.state = :disabled
self
end
def started?() 'started' == self.state end
def stopped?() 'stopped' == self.state end
def error?() 'error' == self.state end
def active?() ! ! digest end
def exist?() ! ! digest end
end

138
lib/pve/proxmox/hosted.rb Normal file
View file

@ -0,0 +1,138 @@
require 'ipaddress'
require_relative 'module'
require_relative 'base'
require_relative 'node'
require_relative 'task'
require_relative 'exceptions'
class Proxmox::Hosted < Proxmox::Base
def self.all
Proxmox::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
Proxmox::Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/status/start")
end
def stop
Proxmox::Task.send :__new__, node: @node, host: self, upid: rest_post( "#{@rest_prefix}/status/stop")
end
def destroy
Proxmox::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
Proxmox::Task.send :__new__, node: @node, host: self, upid: upid
end
end

136
lib/pve/proxmox/lxc.rb Normal file
View file

@ -0,0 +1,136 @@
require_relative 'hosted'
require_relative 'node'
require_relative 'exceptions'
require_relative 'qemu'
require_relative 'task'
require_relative 'ha'
require_relative '../ct_templates'
class Proxmox::LXC < Proxmox::Hosted
class <<self
def all
Proxmox::Node.all.flat_map {|n| n.lxc }
end
def find_by_vmid vmid
vmid = vmid.to_s
Proxmox::Node.all.each do |n|
n.lxc.each do |l|
return l if l.vmid == vmid
end
end
nil
end
def find_by_name name
Proxmox::Node.all.each do |n|
n.lxc.each do |l|
return l if l.name == name
end
end
nil
end
def find name_or_id = nil, name: nil, vmid: nil
if (name and vmid) or (name and name_or_id) or (name_or_id and vmid)
raise ArgumentError, "name xor vmid needed to find CT, not both."
elsif name
find_by_name name
elsif vmid
find_by_vmid vmid.to_i
elsif name_or_id =~ /\D/
find_by_name name_or_id
elsif name_or_id.is_a?( Numeric) or name_or_id =~ /\d/
find_by_vmid name_or_id.to_i
else
raise ArgumentError, "name xor vmid needed to find CT."
end
end
def find_by_vmid! name
find_by_vmid( name) or raise( Proxmox::NotFound, "Container not found: #{name}")
end
def find_by_name! name
find_by_name( name) or raise( Proxmox::NotFound, "Container not found: #{name}")
end
def find! name
find( name) or raise( Proxmox::NotFound, "Container not found: #{name}")
end
def create template, **options
tmplt = PVE::CTTemplate.const_get( template&.classify || 'Default').new **options
name = tmplt.name
virts = Proxmox::LXC.all + Proxmox::Qemu.all
vmid = tmplt.vmid
if virt = virts.find {|v| v.vmid == vmid }
raise Proxmox::AlreadyExists, "VT/VM with vmid [#{vmid}] already exists: #{virt.name}"
end
already_exists = virts.select {|v| v.name == name }
unless already_exists.empty?
raise Proxmox::AlreadyExists, "CT/VM named [#{name}] already exists: vmid: #{already_exists.map( &:vmid).inspect}"
end
node = Proxmox::Node.find_by_name! tmplt.node
options = {
ostemplate: tmplt.ostemplate,
vmid: vmid.to_s,
arch: tmplt.arch,
cmode: tmplt.cmode,
cores: tmplt.cores.to_i,
description: tmplt.description,
hostname: tmplt.hostname,
memory: tmplt.memory,
net0: tmplt.net0&.map {|k,v| "#{k}=#{v}" }&.join(','),
net1: tmplt.net1&.map {|k,v| "#{k}=#{v}" }&.join(','),
net2: tmplt.net2&.map {|k,v| "#{k}=#{v}" }&.join(','),
net3: tmplt.net3&.map {|k,v| "#{k}=#{v}" }&.join(','),
ostype: tmplt.ostype,
:'ssh-public-keys' => 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

72
lib/pve/proxmox/module.rb Normal file
View file

@ -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

87
lib/pve/proxmox/node.rb Normal file
View file

@ -0,0 +1,87 @@
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 <<self
def find_by_name name
all.each do |node|
return node if node.node == name
end
nil
end
def find_by_name! name
find_by_name( name) or raise( Proxmox::NotFound, "Node not found: #{name}")
end
def all
rest_get( '/nodes').map {|d| __new__ d.merge( t: 'nd') }
end
end
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
def rest_prefix
@rest_prefix ||= "/nodes/#{@node}"
end
def initialize
rest_prefix
@sid = "nd:#{@node}"
@name = @node
end
def offline?() @status.nil? or :offline == @status end
def online?() :online == @status end
def lxc
return [] if offline?
rest_get( "#{@rest_prefix}/lxc").map {|d| Proxmox::LXC.send :__new__, d.merge( node: self, t: 'ct') }
end
def aplinfo
return [] if offline?
rest_get( "#{@rest_prefix}/aplinfo").map do |d|
Proxmox::AplInfo.send :__new__, d.merge( node: self, t: 'apl')
end
end
def storage
return [] if offline?
rest_get( "#{@rest_prefix}/storage").map do |d|
Proxmox::Storage.send :__new__, d.merge( node: self, t: 'sm')
end
end
def qemu
return [] if offline?
rest_get( "#{@rest_prefix}/qemu").map {|d| Proxmox::Qemu.send :__new__, d.merge( node: self, t: 'qm') }
end
def tasks
return [] if offline?
rest_get( "/#{@rest_prefix}/tasks").map {|d| Proxmox::Task.send :__new__, d.merge( node: self, t: 'task') }
end
def enter *command
Kernel.system 'ssh', '-t', node, command.map( &:to_s).shelljoin
end
def exec *command
Kernel.system 'ssh', node, command.map( &:to_s).shelljoin
end
end

87
lib/pve/proxmox/qemu.rb Normal file
View file

@ -0,0 +1,87 @@
require_relative 'hosted'
require_relative 'node'
require_relative 'exceptions'
require_relative 'ha'
class Proxmox::Qemu < Proxmox::Hosted
class <<self
def all
Proxmox::Node.all.flat_map {|n| n.qemu }
end
def find_by_vmid vmid
vmid = vmid.to_s
all.each do |l|
return l if l.vmid == vmid
end
nil
end
def find_by_name name
all.each do |l|
return l if l.name == name
end
nil
end
def find name_or_id = nil, name: nil, vmid: nil
if (name and vmid) or (name and name_or_id) or (name_or_id and vmid)
raise Proxmox::NotFound, "name xor vmid needed to find CT, not both."
elsif name
find_by_name name
elsif vmid
find_by_vmid vmid.to_i
elsif name_or_id =~ /\D/
find_by_name name_or_id
elsif name_or_id.is_a?( Numeric) or name_or_id =~ /\d/
find_by_vmid name_or_id.to_i
else
raise Proxmox::NotFound, "name xor vmid needed to find CT."
end
end
def find_by_vmid! name
find_by_vmid( name) or raise( Proxmox::NotFound, "Virtual Machine not found: #{name}")
end
def find_by_name! name
find_by_name( name) or raise( Proxmox::NotFound, "Virtual Machine not found: #{name}")
end
def find! name
find( name) or raise( Proxmox::NotFound, "Virtual Machine not found: #{name}")
end
end
def rest_prefix
@rest_prefix ||= "/nodes/%s/qemu/%d" % [@node.node, @vmid]
end
def initialize
rest_prefix
@sid = "qm:#{@vmid}"
end
def ha
Proxmox::HA.find self
end
def exec *args
node.exec 'qm', 'guest', 'exec', vmid, '--', *args
end
def resize disk, size
upid = rest_put "#{@rest_prefix}/resize", disk: disk, size: size
Proxmox::Task.send :__new__, node: @node, host: self, upid: upid
end
def migrate target, online: nil
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, online: ! !online)
end
end

View file

@ -0,0 +1,75 @@
require 'rest_client'
require 'json'
require_relative 'exceptions'
require_relative 'module'
module Proxmox::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

View file

@ -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

110
lib/pve/proxmox/task.rb Normal file
View file

@ -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 :
(?<node>[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?) :
(?<pid>[0-9A-Fa-f]{8}) :
(?<pstart>[0-9A-Fa-f]{8,9}) :
(?<starttime>[0-9A-Fa-f]{8}) :
(?<dtype>[^:\s]+) :
(?<id>[^:\s]*) :
(?<user>[^:\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

View file

@ -1,3 +1,3 @@
module PVE
VERSION = '0.2.1'
VERSION = '0.4.0'
end

View file

@ -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 "~> 3.1"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage