diff --git a/CentOSKVMAdapter.py b/CentOSKVMAdapter.py index e69de29..ecee231 100644 --- a/CentOSKVMAdapter.py +++ b/CentOSKVMAdapter.py @@ -0,0 +1,127 @@ +# Author: Yogesh Agrawal +# Contact: yogesh@vlabs.ac.in + +""" A module for managing VMs on Windows - KVM platform. """ + +__all__ = [ + 'create_vm', + 'is_running_vm', + 'restart_vm', + 'stop_vm', + 'destroy_vm', + 'migrate_vm', + 'start_vm', + 'start_vm_manager', + 'get_resource_utilization', + 'take_snapshot', + ] + +# Standard Library imports +import re +import subprocess +from exceptions import Exception + +# Third party imports +import netaddr + +# VLEAD imports +import VMSpec +import VMUtils +import VMManager + +# UGLY DUCK PUNCHING: Backporting check_output from 2.7 to 2.6 +if "check_output" not in dir(subprocess): + def f(*popenargs, **kwargs): + if 'stdout' in kwargs: + raise ValueError('stdout argument not allowed, it will be overridden.') + process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs) + output, unused_err = process.communicate() + retcode = process.poll() + if retcode: + cmd = kwargs.get("args") + if cmd is None: + cmd = popenargs[0] + raise subprocess.CalledProcessError(retcode, cmd) + return output + subprocess.check_output = f + + +# Globals +VH_DIR = "/mnt/das1/virtual_hosts/" + + +def create_vm(vm_name): + try: + subprocess.check_call("mkdir " + VH_DIR + vm_name, shell=True) + subprocess.check_call("virt-clone -o windowsvm -n " + vm_name + " -f " + VH_DIR + vm_name + "/" + vm_name , shell=True) + subprocess.check_call("virsh start " + vm_name, shell=True) + except subprocess.CalledProcessError, e: + raise e + return + +def is_running_vm(vm_name): + try: + status = subprocess.check_output("virsh list | grep " + vm_name, shell=True) + if "running" in status: + return "yes" + else: + return "no" + except subprocess.CalledProcessError, e: + raise e + +def restart_vm(vm_name): + if(is_running_vm(vm_name) == "yes"): + try: + subprocess.check_call("virsh reboot " + vm_name, shell=True) + except subprocess.CalledProcessError, e: + raise e + else: + return "VM not in running state so can not be rebooted" + return "VM rebooted" + +# Function alias +start_vm = restart_vm + +def stop_vm(vm_name): + status = subprocess.check_output("virsh list | grep running | grep " + vm_name, shell=True) + if vm_name not in status: + return "VM not running" + else: + try: + vm_id = status.split(" ")[1:][0] + subprocess.check_call("virsh shutdown " + vm_id, shell=True) + except subprocess.CalledProcessError, e: + raise e + return "VM successfully stopped" + +def destroy_vm(vm_name): + try: + subprocess.check_call("virsh undefine " + vm_name, shell=True) + except subprocess.CalledProcessError, e: + raise e + return "Vm destroyed" + +def migrate_vm(vm_id, destination): + try: + subprocess.check_call(VZCTL + " stop " + vm_id, shell=True) + subprocess.check_call(VZCTL + " vzmigrate " + destination + " " + vm_id, shell=True) + except subprocess.CalledProcessError, e: + raise e + +def start_vm_manager(): + try: + subprocess.check_call("python VMManager.py", shell=True) + except subprocess.CalledProcessError, e: + raise e + +if __name__ == "__main__": + # vm_spec = VMSpec.VMSpec() + # create_vm("newvm") + #create_vm("99101", vm_spec) + #create_vm("99102", vm_spec) + #create_vm("99103", vm_spec) + #destroy_vm("99100") + #destroy_vm("99101") + print stop_vm("newvm") + #destroy_vm("99102") + #destroy_vm("99103") diff --git a/CentOSVZAdapter.py b/CentOSVZAdapter.py index a8ca2d9..656e7ad 100644 --- a/CentOSVZAdapter.py +++ b/CentOSVZAdapter.py @@ -121,7 +121,14 @@ def destroy_vm(vm_id): def is_running_vm(vm_id): vm_id = validate_vm_id(vm_id) - pass + try: + status = subprocess.check_output(VZCTL + " status " + vm_id, shell=True) + if "running" in status: + return "yes" + else: + return "no" + except subprocess.CalledProcessError, e: + raise e def get_vm_ip(vm_id): vm_id = validate_vm_id(vm_id) @@ -138,7 +145,11 @@ def get_vm_ip(vm_id): def migrate_vm(vm_id, destination): vm_id = validate_vm_id(vm_id) - pass + try: + subprocess.check_call(VZCTL + " stop " + vm_id, shell=True) + subprocess.check_call(VZCTL + " vzmigrate " + destination + " " + vm_id, shell=True) + except subprocess.CalledProcessError, e: + raise e def take_snapshot(vm_id): vm_id = validate_vm_id(vm_id) @@ -215,12 +226,13 @@ def validate_vm_id(vm_id): # Start an HTTP server and wait for invocation # Parse the invocation command and route to # appropriate methods. - vm_spec = VMSpec.VMSpec() - create_vm("99100", vm_spec) - create_vm("99101", vm_spec) + # vm_spec = VMSpec.VMSpec() + #create_vm("99100", vm_spec) + #create_vm("99101", vm_spec) #create_vm("99102", vm_spec) #create_vm("99103", vm_spec) - destroy_vm("99100") - destroy_vm("99101") + #destroy_vm("99100") + #destroy_vm("99101") + is_running_vm() #destroy_vm("99102") #destroy_vm("99103") diff --git a/VMManager.py b/VMManager.py index 4ae8d26..80d1cf0 100644 --- a/VMManager.py +++ b/VMManager.py @@ -11,16 +11,44 @@ # what is your diskspace footprint? # what processes are currently running? # what is your CPU load? -# +# UGLY DUCK PUNCHING: Backporting check_output from 2.7 to 2.6 +if "check_output" not in dir(subprocess): + def f(*popenargs, **kwargs): + if 'stdout' in kwargs: + raise ValueError('stdout argument not allowed, it will be overridden.') + process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs) + output, unused_err = process.communicate() + retcode = process.poll() + if retcode: + cmd = kwargs.get("args") + if cmd is None: + cmd = popenargs[0] + raise subprocess.CalledProcessError(retcode, cmd) + return output + subprocess.check_output = f + def execute(command): # do some validation - subprocess.check_call(command, shell=True) + return subprocess.check_output(command, shell=True) +def running_time(): + return execute("uptime") +def mem_usage(): + return execute("free -mg") +def disk_usage(): + return execute("df -h") -if __name__ == "__main__": - execute("ls -l") +def running_process(): + return execute("ps -e -o command") + +def cpu_load(): + return execute("ps -e -o pcpu | awk '{s+=$1} END {print s\"%\"}'") + +if __name__ == "__main__": + print cpu_load() + print mem_usage()