2021-04-19 20:35:39 +02:00
class PVE :: Cli
def cli_qm
2022-10-05 12:55:03 +02:00
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 |
2021-04-19 20:35:39 +02:00
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 }
}
2022-10-05 12:55:03 +02:00
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 |
2022-01-12 20:07:52 +01:00
hosting_table target : target , state : state , sort : sort do | push |
node_opt ( node ) .
each ( & push ) . lazy .
2022-10-05 12:55:03 +02:00
flat_map { | n | [ Thread . new ( n , & :qemu ) ] } .
2022-01-12 20:07:52 +01:00
each { | n | n . value . each & push }
end
2021-12-13 14:28:52 +01:00
} ) .
2022-01-12 20:07:52 +01:00
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) " )
2021-04-19 20:35:39 +02:00
2022-10-05 12:55:03 +02:00
qm_cli . cmd :exec , " Executes Command in VM via qemu-guest-agent " , min : 4 , & lambda { | name_or_id , * command |
2021-04-19 20:35:39 +02:00
connect
STDERR . puts " ! #{ $? . exitstatus } " unless Proxmox :: Qemu . find! ( name_or_id ) . exec * command
}
2022-10-05 12:55:03 +02:00
qm_cli . cmd ( :resize , 'Resize a disk' , & lambda { | name_or_id , disk , size |
2021-06-23 23:42:29 +02:00
connect
qm = Proxmox :: Qemu . find! name_or_id
task = qm . resize disk , size
wait task , text : " Resizing #{ qm . sid } #{ disk } to #{ size } "
} )
2022-10-05 12:55:03 +02:00
qm_cli . cmd 'help' , '' , aliases : [ '-h' , '--help' ] , & lambda { | * args | help qm_cli , * args }
2021-04-19 20:35:39 +02:00
}
end
end