mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-03 04:12:31 +01:00
CLOUDSTACK-6257: Adding function to check state of VM
This commit is contained in:
parent
9e98cbf1c1
commit
59d0b75e79
@ -31,6 +31,11 @@
|
||||
"""
|
||||
|
||||
RUNNING = "Running"
|
||||
STOPPED = "Stopped"
|
||||
STOPPING = "Stopping"
|
||||
STARTING = "Starting"
|
||||
DESTROYED = "Destroyed"
|
||||
EXPUNGING = "Expunging"
|
||||
RECURRING = "RECURRING"
|
||||
ENABLED = "Enabled"
|
||||
NETWORK_OFFERING = "network_offering"
|
||||
|
||||
@ -22,7 +22,9 @@
|
||||
import marvin
|
||||
from utils import is_server_ssh_ready, random_gen
|
||||
from marvin.cloudstackAPI import *
|
||||
from marvin.codes import FAILED, PASS
|
||||
from marvin.codes import (FAILED, FAIL, PASS, RUNNING, STOPPED,
|
||||
STARTING, DESTROYED, EXPUNGING,
|
||||
STOPPING)
|
||||
from marvin.cloudstackException import GetDetailExceptionInfo
|
||||
from marvin.lib.utils import validateList
|
||||
# Import System modules
|
||||
@ -222,6 +224,16 @@ class User:
|
||||
class VirtualMachine:
|
||||
"""Manage virtual machine lifecycle"""
|
||||
|
||||
'''Class level variables'''
|
||||
# Variables denoting VM state - start
|
||||
STOPPED = STOPPED
|
||||
RUNNING = RUNNING
|
||||
DESTROYED = DESTROYED
|
||||
EXPUNGING = EXPUNGING
|
||||
STOPPING = STOPPING
|
||||
STARTING = STARTING
|
||||
# Varibles denoting VM state - end
|
||||
|
||||
def __init__(self, items, services):
|
||||
self.__dict__.update(items)
|
||||
if "username" in services:
|
||||
@ -470,6 +482,10 @@ class VirtualMachine:
|
||||
if forced:
|
||||
cmd.forced = forced
|
||||
apiclient.stopVirtualMachine(cmd)
|
||||
response = self.getState(apiclient, VirtualMachine.STOPPED)
|
||||
if response[0] == FAIL:
|
||||
raise Exception(response[1])
|
||||
return
|
||||
|
||||
def reboot(self, apiclient):
|
||||
"""Reboot the instance"""
|
||||
@ -521,6 +537,33 @@ class VirtualMachine:
|
||||
)
|
||||
return self.ssh_client
|
||||
|
||||
def getState(self, apiclient, state, timeout=600):
|
||||
"""List VM and check if its state is as expected
|
||||
@returnValue - List[Result, Reason]
|
||||
1) Result - FAIL if there is any exception
|
||||
in the operation or VM state does not change
|
||||
to expected state in given time else PASS
|
||||
2) Reason - Reason for failure"""
|
||||
|
||||
returnValue = [FAIL, "VM state not trasited to %s,\
|
||||
operation timed out" % state]
|
||||
|
||||
while timeout>0:
|
||||
try:
|
||||
vms = VirtualMachine.list(apiclient, id=self.id, listAll=True)
|
||||
validationresult = validateList(vms)
|
||||
if validationresult[0] == FAIL:
|
||||
raise Exception("VM list validation failed: %s" % validationresult[2])
|
||||
elif str(vms[0].state).lower() == str(state).lower():
|
||||
returnValue = [PASS, None]
|
||||
break
|
||||
except Exception as e:
|
||||
returnValue = [FAIL, e]
|
||||
break
|
||||
time.sleep(60)
|
||||
timeout -= 60
|
||||
return returnValue
|
||||
|
||||
def resetSshKey(self, apiclient, **kwargs):
|
||||
"""Resets SSH key"""
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user