Perform automatic shutdown/stop on VM disable

Instead of requiring the VM to already be stopped, instead allow disable
state changes to perform a shutdown first. Also add a force option which
will do a hard stop instead of a shutdown.

References #148
This commit is contained in:
Joshua Boniface 2021-11-06 03:53:44 -04:00
parent 16544227eb
commit 739b60b91e
4 changed files with 30 additions and 10 deletions

View File

@ -1868,6 +1868,7 @@ class API_VM_State(Resource):
"helptext": "A valid state must be specified", "helptext": "A valid state must be specified",
"required": True, "required": True,
}, },
{"name": "force"},
{"name": "wait"}, {"name": "wait"},
] ]
) )
@ -1890,6 +1891,10 @@ class API_VM_State(Resource):
- stop - stop
- restart - restart
- disable - disable
- in: query
name: force
type: boolean
description: Whether to force stop instead of shutdown VM during disable
- in: query - in: query
name: wait name: wait
type: boolean type: boolean
@ -1907,6 +1912,7 @@ class API_VM_State(Resource):
id: Message id: Message
""" """
state = reqargs.get("state", None) state = reqargs.get("state", None)
force = bool(strtobool(reqargs.get("force", "false")))
wait = bool(strtobool(reqargs.get("wait", "false"))) wait = bool(strtobool(reqargs.get("wait", "false")))
if state == "start": if state == "start":
@ -1918,7 +1924,7 @@ class API_VM_State(Resource):
if state == "restart": if state == "restart":
return api_helper.vm_restart(vm, wait) return api_helper.vm_restart(vm, wait)
if state == "disable": if state == "disable":
return api_helper.vm_disable(vm) return api_helper.vm_disable(vm, force)
abort(400) abort(400)

View File

@ -719,11 +719,11 @@ def vm_start(zkhandler, name):
@ZKConnection(config) @ZKConnection(config)
def vm_restart(zkhandler, name, wait): def vm_restart(zkhandler, name, wait=False):
""" """
Restart a VM in the PVC cluster. Restart a VM in the PVC cluster.
""" """
retflag, retdata = pvc_vm.restart_vm(zkhandler, name, wait) retflag, retdata = pvc_vm.restart_vm(zkhandler, name, wait=wait)
if retflag: if retflag:
retcode = 200 retcode = 200
@ -767,11 +767,11 @@ def vm_stop(zkhandler, name):
@ZKConnection(config) @ZKConnection(config)
def vm_disable(zkhandler, name): def vm_disable(zkhandler, name, force=False):
""" """
Disable a (stopped) VM in the PVC cluster. Disable (shutdown or force stop if required)a VM in the PVC cluster.
""" """
retflag, retdata = pvc_vm.disable_vm(zkhandler, name) retflag, retdata = pvc_vm.disable_vm(zkhandler, name, force=force)
if retflag: if retflag:
retcode = 200 retcode = 200

View File

@ -837,21 +837,29 @@ def stop_vm(zkhandler, domain):
return True, 'Forcibly stopping VM "{}".'.format(domain) return True, 'Forcibly stopping VM "{}".'.format(domain)
def disable_vm(zkhandler, domain): def disable_vm(zkhandler, domain, force=False):
# Validate that VM exists in cluster # Validate that VM exists in cluster
dom_uuid = getDomainUUID(zkhandler, domain) dom_uuid = getDomainUUID(zkhandler, domain)
if not dom_uuid: if not dom_uuid:
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain) return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
# Get state and verify we're OK to proceed # Get state and perform a shutdown/stop if VM is online
current_state = zkhandler.read(("domain.state", dom_uuid)) current_state = zkhandler.read(("domain.state", dom_uuid))
if current_state != "stop": if current_state != "stop":
return False, 'ERROR: VM "{}" must be stopped before disabling!'.format(domain) if force:
change_state(zkhandler, dom_uuid, "stop")
# Wait for the command to be registered by the node
time.sleep(0.5)
else:
change_state(zkhandler, dom_uuid, "shutdown")
# Wait for the shutdown to complete
while zkhandler.read(("domain.state", dom_uuid)) != "stop":
time.sleep(0.5)
# Set the VM to disable # Set the VM to disable
change_state(zkhandler, dom_uuid, "disable") change_state(zkhandler, dom_uuid, "disable")
return True, 'Marked VM "{}" as disable.'.format(domain) return True, 'Disabled VM "{}".'.format(domain)
def update_vm_sriov_nics(zkhandler, dom_uuid, source_node, target_node): def update_vm_sriov_nics(zkhandler, dom_uuid, source_node, target_node):

View File

@ -6680,6 +6680,12 @@
"required": true, "required": true,
"type": "string" "type": "string"
}, },
{
"description": "Whether to force stop instead of shutdown VM during disable",
"in": "query",
"name": "force",
"type": "boolean"
},
{ {
"description": "Whether to block waiting for the state change to complete", "description": "Whether to block waiting for the state change to complete",
"in": "query", "in": "query",