174 lines
4.2 KiB
Ruby
174 lines
4.2 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 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"
|
|
log.each {|l| puts l[:t] }
|
|
logn = log.last[:n]
|
|
end
|
|
logn
|
|
end
|
|
|
|
def wait task, secs: nil, text: nil
|
|
secs ||= 0.1
|
|
spinners, spin, logn = "▖▘▝▗", 0, 0
|
|
STDERR.puts task.upid
|
|
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
|
|
STDERR.printf "\r[%s] %s %s %s\e[J\n",
|
|
host || s.id,
|
|
s.successfull? ? "\e[32;1m✓\e[0m" : "\e[31;1m✗\e[0m",
|
|
text && "#{text}:",
|
|
s.stopped? ? :finished : s.status
|
|
return s
|
|
end
|
|
STDERR.printf "\r[%s] \e[33;1m%s\e[0m %s...\e[J",
|
|
host || s[:id],
|
|
spinners[spin = (spin + 1) % 4],
|
|
text || "Working"
|
|
sleep secs
|
|
end
|
|
end
|
|
|
|
def 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
|
|
wait task, text: "Starting" unless fire
|
|
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] = 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 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
|