pve/lib/pve/cli.rb

160 lines
3.9 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'
class UsageError <RuntimeError
end
class PVE::Cli
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), [], [], false,
config_file, symbolize_names: true
@cli = DenCli.new File.basename($0), "PVE CLI"
@interactive = false
prepare
end
def interactive?
@interactive
end
def enter host, *args
r = host.enter *args
STDERR.puts "! #{$?.exitstatus}" unless r
end
def wait_state host, state, timeout: nil, lock: nil
spinners = %w[- / | \\]
spin = 0
r =
host.wait state, lock: lock, timeout: timeout, secs: 0.2 do |state, lock|
lock = " (locked: #{lock})" if lock
STDERR.printf "\r[%s] %s Still %s%s...\e[J\n", host.name, spinners[spin = (spin + 1) % 4], state, lock
end
STDERR.printf "\r\e[J"
exit 1 unless interactive? and r
end
def wait task, secs: nil, text: nil
secs ||= 0.1
spinners, spin, logn = "▖▘▝▗", 0, 0
STDERR.puts task.upid
loop do
s = task.status
if s[:exitstatus]
STDERR.printf "\r[%s] %s %s\e[J\n",
task.host ? task.host.name : s[:id],
case s[:exitstatus]
when "OK" then "\e[32;1m✓\e[0m"
else "\e[31;1m✗\e[0m"
end,
s[:status]
return task
end
log = task.log start: logn
log = [] if [{n: 1, t: 'no content'}] == log
unless log.empty?
STDERR.printf "\r\e[J"
log.each {|l| puts l[:t] }
logn = log.last[:n]
end
STDERR.printf "\r[%s] \e[33;1m%s\e[0m %s...\e[J", task.host ? task.host.name : s[:id], spinners[spin = (spin + 1) % 4], text || "Working"
sleep secs
end
end
def start host, node: nil, timeout: nil, fire: nil, secs: nil
timeout ||= 30
if node
task = host.migrate Proxmox::Node.find_by_name!( node)
wait task, text: "Migrating"
end
if host.running?
STDERR.puts "Already running."
return
end
task = host.start
unless fire
wait task, text: "Starting"
end
end
def stop host, timeout: nil, fire: nil, secs: nil
timeout ||= 30
if host.stopped?
STDERR.puts "Already stopped."
return
end
task = host.stop
unless fire
wait task, text: "Stopping"
end
end
def create klass, template, timeout: nil, fire: nil, secs: nil, start: nil, **options
options[:start] = true if fire and start
task = klass.create template, **options
return if fire
wait task, text: "Creating"
host = task.host.refresh!
start host, timeout: timeout, secs: secs
end
def destroy ct, timeout: nil, fire: nil, secs: nil
task = ct.destroy
unless fire
wait task, text: "Destroying"
end
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 prepare
cli_node
cli_ct
cli_qm
cli_task
cli_ha
cli_base
end
def call *argv
cli.call *argv
rescue RestClient::ExceptionWithResponse
STDERR.puts "#$! - #{$!.response} (#{$!.class})", $!.backtrace.map {|b|" #{b}"}
rescue UsageError, DenCli::UsageError
STDERR.puts $!
exit 1
end
end