pve/lib/pve/cli.rb

560 lines
17 KiB
Ruby

require 'dencli'
require 'yaml'
require 'pve'
require_relative 'helper'
require_relative 'cli/base'
require_relative 'cli/ct'
require_relative 'cli/ha'
require_relative 'cli/task'
require_relative 'cli/qm'
require_relative 'cli/node'
require_relative 'cli/storage'
class DenCli::Sub
def provide_help name: nil, aliases: nil, min: nil
base = self
name = :help if name.nil?
aliases = %w[-h --help] if aliases.nil?
min = 1 if min.nil?
#p name: name, aliases: aliases, min: min
cmd( name, '', aliases: aliases, min: min) {|*args| help *args }
end
end
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
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
attr_reader :cfg, :cli
def connect
@conn ||=
Proxmox.connect cfg[:auth][:username], cfg[:auth][:password],
realm: cfg[:auth][:realm], **cfg[:connect]
#RestClient.log = STDERR
@conn
end
def initialize config_file = nil
config_file ||= '/etc/pve/pvecli.yml'
@cfg =
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
def interactive?
@interactive
end
def enter host, *args
@out.host = host.name
r = host.enter *args
operational_error "Exitstatus: #{$?.exitstatus}" unless r
end
def wait_state host, state, timeout: nil, lock: nil
@out.host = host.name
r =
host.wait state, lock: lock, timeout: timeout, secs: 0.2 do |state, lock|
@out.progress "Still %s%s...", state, lock ? " (locked: #{lock})" : ''
end
@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?
@out.reset
log.each {|l| puts l[:t] }
logn = log.last[:n]
end
logn
end
def wait task, secs: nil, text: nil, timeout: nil
secs ||= 0.1
logn = 0
STDOUT.puts task.upid
@out.host = host = task.host&.name || '<>'
loop do
s = task.status
logn = self.task_log task, logn
if s.finished?
loop do
r = self.task_log task, logn
break if 0 == logn - r
logn = r
end
if s.successfull?
@out.success "%s %s", text && "#{text}:", s.stopped? ? :finished : s.status
else
@out.failed "%s %s", text && "#{text}:", s.stopped? ? :finished : s.status
end
return s
end
@out.progress text || "Working"
sleep secs
end
end
def migrate host, node, timeout: nil, fire: nil, secs: nil, online: nil
timeout ||= 30
task = host.migrate Proxmox::Node.find_by_name!( node), online: online || false
wait task, text: "Migrating", timeout: timeout if fire
end
def start host, node: nil, timeout: nil, fire: nil, secs: nil
timeout ||= 60
if host.running?
ConditionViolation "Already running."
return
end
task = host.start
t = Time.now
unless fire
wait task, text: "Starting", timeout: timeout
wait_state host, :running, timeout: timeout-(Time.now-t) if host.ha.exist? and not host.running?
end
end
def stop host, timeout: nil, fire: nil, secs: nil
timeout ||= 30
if host.stopped?
preset_error "Already stopped."
return
end
task = host.stop
t = Time.now
unless fire
wait task, text: "Stopping", timeout: timeout
wait_state host, :stopped, timeout: timeout-(Time.now-t) if host.ha.exist? and not host.stopped?
end
end
def create klass, template, timeout: nil, fire: nil, start: nil, **options
options[:start] = fire && start
task = klass.create template, **options
return if fire
status = wait task, text: "Creating"
if status.successfull?
host = task.host.refresh!
start host, timeout: timeout, secs: secs if start
elsif not interactive?
exit 1
end
end
def destroy ct, timeout: nil, fire: nil, secs: nil
task = ct.destroy
unless fire
wait task, text: "Destroying"
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 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 help cl, *args
STDERR.puts cl.help( *args)
exit 1 unless interactive?
end
def opts_wait cl
cl.
opt( :timeout, "-t", "--timeout=TIMEOUT", "Wait for max TIMEOUT seconds (default: endless)", default: nil).
opt( :secs, "-s", "--seconds=SECONDS", "Check every SECONDS for state (default: 0.2)", default: 0.2).
opt( :fire, "-f", "--[no-]fire", "Do not wait till running", default: false)
end
def complete_lxc f
Proxmox::LXC.all.
flat_map {|x| [x.name, x.vmid.to_s] }.
select {|x| f =~ x }
end
def complete_qemu f
Proxmox::Qemu.all.
flat_map {|x| [x.name, x.vmid.to_s] }.
select {|x| f =~ x }
end
def complete_node f
Proxmox::Qemu.all.
map {|x| x.name }.
select {|x| f =~ x }
end
def completion_helper *pre, arg, &exe
if pre.empty?
connect
xs = yield /\A#{Regexp.quote arg}/
STDOUT.print "\a" if xs.empty?
xs
else
STDOUT.print "\a"
[]
end
end
def prepare
cli_node
cli_ct
cli_qm
cli_task
cli_ha
cli_base
cli_storage
end
def call *argv
cli.call *argv
rescue RestClient::ExceptionWithResponse
@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
system = applications = true if system.nil? and applications.nil?
node = node ? Proxmox::Node.find_by_name!( node) : Proxmox::Node.all.first
to = TablizedOutput.new %w<Section Package Version OS Template Description>, format: %w[> > > > > <]
node.aplinfo.
select {|a| 'system' == a.section ? system : applications}.
each do |apl|
to.push [
apl.section,
apl.package,
apl.version,
apl.os,
apl.template,
apl.description,
]
end
to.print order: [1,2]
end
COLORS = %w[black red green yellow blue magenta cyan white].each_with_index.to_h
def fgcolor color
color = color.to_s
if /\Abright[-_]?(.*)\z/ =~ color
c = COLORS[$1]
c.nil? ? nil : "1;3#{c}"
else
c = COLORS[color]
c.nil? ? nil : "3#{c}"
end
end
def tablized_virt v
ha = v.respond_to?( :ha) ? v.ha : nil
unknown = TablizedOutput::V.new 0, '-'
node = v.node.is_a?(String) ? v.node : v.node.node
if color = @cfg[:hosts]&.[](node.to_sym)&.[](:color)
node = ColoredString.new node, fgcolor( color)
end
[
case v.status
when :running, :online then ColoredString.new v.status, "32"
when :stopped, :offline then ColoredString.new v.status, "31"
else v.status
end,
ha&.state || '·',
case v.t
when "nd" then ColoredString.new v.sid, "33"
when "qm" then ColoredString.new v.sid, "35"
when "ct" then ColoredString.new v.sid, "36"
else v.sid
end,
v.name, node,
v.respond_to?(:uptime) ? TablizedOutput::V.new( v.uptime, Measured.seconds( v.uptime)) : unknown,
v.respond_to?(:cpu) ? TablizedOutput::Percentage.new( v.cpu) : unknown,
v.respond_to?(:mem) ? TablizedOutput::V.new( v.mem, Measured.bytes( v.mem)) : unknown,
v.respond_to?(:maxmem) ? TablizedOutput::Percentage.new( v.mem/v.maxmem.to_f) : unknown,
v.respond_to?(:disk) ? TablizedOutput::V.new( v.disk.to_i, Measured.bytes( v.disk.to_i)) : unknown,
if v.respond_to?(:maxdisk) and 0 < v.maxdisk.to_i
TablizedOutput::Percentage.new( v.disk.to_f/v.maxdisk.to_f)
else unknown end,
v.respond_to?(:tags) ? v.tags.join(', ') : '',
]
end
end