82 lines
3.8 KiB
Ruby
82 lines
3.8 KiB
Ruby
class PVE::Cli
|
|
def cli_qm
|
|
cli.sub :qm, "Virtual Machines", aliases: %w[v vm qemu], &lambda {|qm_cli|
|
|
qm_cli.cmd :list, "List VM-IDs", aliases: ['ls'], &lambda {|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 }
|
|
end.sort.each {|c| puts c }
|
|
}
|
|
|
|
qm_cli.cmd( :migrate, "Migrates VM(s) to an other host", min: 2, &lambda {|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
|
|
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
|
|
}).
|
|
#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_cli.cmd( :start, "Starts VM(s)", min: 4, &lambda {|*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
|
|
}).tap {|c| opts_wait c }
|
|
|
|
qm_cli.cmd( :stop, "Stops VM(s)", min: 3, &lambda {|*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
|
|
}).tap {|c| opts_wait c }
|
|
|
|
qm_cli.cmd( :wait, "Wait till VM is in state", &lambda {|name_or_id, state, timeout: nil, secs: nil|
|
|
connect
|
|
qm = Proxmox::Qemu.find! name_or_id
|
|
wait qm, state, timeout: timeout, secs: secs
|
|
}).
|
|
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], &lambda {|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).lazy.
|
|
flat_map {|n| [ Thread.new( n, &:qemu) ] }.
|
|
each {|n| n.value.each &push }
|
|
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, &lambda {|name_or_id, *command|
|
|
connect
|
|
STDERR.puts "! #{$?.exitstatus}" unless Proxmox::Qemu.find!( name_or_id).exec *command
|
|
}
|
|
|
|
qm_cli.cmd( :resize, 'Resize a disk', &lambda {|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}"
|
|
})
|
|
|
|
qm_cli.cmd 'help', '', aliases: ['-h', '--help'], &lambda {|*args| help qm_cli, *args }
|
|
}
|
|
end
|
|
end
|