From 6d84fe68c865defa648d130f82be78bff1620117 Mon Sep 17 00:00:00 2001 From: Yogeshiiiith Date: Thu, 5 Dec 2013 11:42:15 +0530 Subject: [PATCH 01/10] Modified VMManager.py --- VMManager.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/VMManager.py b/VMManager.py index 4ae8d26..e105f4a 100644 --- a/VMManager.py +++ b/VMManager.py @@ -1,5 +1,5 @@ -# Author: Chandan Gupta -# Contact: chandan@vlabs.ac.in +# Author: Yogesh Agrawal +# Contact: yogesh@vlabs.ac.in """ An interface for managing VMs for a selected platform. """ @@ -19,8 +19,26 @@ def execute(command): subprocess.check_call(command, shell=True) +def running_time(): + execute("uptime") + return 0 +def mem_usage(): + execute("free -mg") + return 0 -if __name__ == "__main__": - execute("ls -l") +def disk_usage(): + execute("df -h") + return 0 + +def running_process(): + execute("ps -e -o command") + return 0 + +def cpu_load(): + execute("ps -e -o pcpu | awk '{s+=$1} END {print s\"%\"}'") + return 0 + +if __name__ == "__main__": + cpu_load() From 64cd89b207ac8e19e5d96623768a485a7030accf Mon Sep 17 00:00:00 2001 From: Yogeshiiiith Date: Thu, 5 Dec 2013 11:46:06 +0530 Subject: [PATCH 02/10] modified VMManager.py --- VMManager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/VMManager.py b/VMManager.py index e105f4a..810b803 100644 --- a/VMManager.py +++ b/VMManager.py @@ -1,5 +1,5 @@ -# Author: Yogesh Agrawal -# Contact: yogesh@vlabs.ac.in +# Author: Chandan Gupta +# Contact: chandan@vlabs.ac.in """ An interface for managing VMs for a selected platform. """ From 6c64af62d9916e928f4d7539756b4a057e0b10cd Mon Sep 17 00:00:00 2001 From: Yogeshiiiith Date: Thu, 5 Dec 2013 12:34:24 +0530 Subject: [PATCH 03/10] Modified VMManager.py --- VMManager.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/VMManager.py b/VMManager.py index 810b803..80d1cf0 100644 --- a/VMManager.py +++ b/VMManager.py @@ -11,34 +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(): - execute("uptime") - return 0 + return execute("uptime") def mem_usage(): - execute("free -mg") - return 0 + return execute("free -mg") def disk_usage(): - execute("df -h") - return 0 + return execute("df -h") def running_process(): - execute("ps -e -o command") - return 0 + return execute("ps -e -o command") def cpu_load(): - execute("ps -e -o pcpu | awk '{s+=$1} END {print s\"%\"}'") - return 0 + return execute("ps -e -o pcpu | awk '{s+=$1} END {print s\"%\"}'") if __name__ == "__main__": - cpu_load() + print cpu_load() + print mem_usage() From 97f256f1779dada0d6b42ae6f4b602c73e5dfd33 Mon Sep 17 00:00:00 2001 From: Yogeshiiiith Date: Thu, 5 Dec 2013 23:28:30 +0530 Subject: [PATCH 04/10] Modified CentOSVZAdapter.py --- CentOSVZAdapter.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CentOSVZAdapter.py b/CentOSVZAdapter.py index a8ca2d9..6c40c06 100644 --- a/CentOSVZAdapter.py +++ b/CentOSVZAdapter.py @@ -121,7 +121,15 @@ 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 + " | grep running ", shell=True) + if status == "": + return "no" + elif status == "running": + return "yes" + + except subprocess.CalledProcessError, e: + raise e def get_vm_ip(vm_id): vm_id = validate_vm_id(vm_id) From 49b944f563ceafabc4477aaf0ad53c9c67da975b Mon Sep 17 00:00:00 2001 From: Yogeshiiiith Date: Mon, 9 Dec 2013 19:47:34 +0530 Subject: [PATCH 05/10] Modified CentOSVZAdapter.py --- CentOSVZAdapter.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CentOSVZAdapter.py b/CentOSVZAdapter.py index 6c40c06..ef07421 100644 --- a/CentOSVZAdapter.py +++ b/CentOSVZAdapter.py @@ -146,7 +146,12 @@ 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) From 55313112e66463ccfc5cb36f49bab6c50baaa206 Mon Sep 17 00:00:00 2001 From: Yogeshiiiith Date: Tue, 10 Dec 2013 11:28:50 +0530 Subject: [PATCH 06/10] Modified CenteOSVZAdapter.py --- CentOSKVMAdapter.py | 3 +++ CentOSVZAdapter.py | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CentOSKVMAdapter.py b/CentOSKVMAdapter.py index e69de29..ac3fb17 100644 --- a/CentOSKVMAdapter.py +++ b/CentOSKVMAdapter.py @@ -0,0 +1,3 @@ +# Author: Yogesh Agrawal +# Contact: yogesh@vlabs.ac.in/ yogeshiiith@gmail.com + diff --git a/CentOSVZAdapter.py b/CentOSVZAdapter.py index ef07421..3d07bf2 100644 --- a/CentOSVZAdapter.py +++ b/CentOSVZAdapter.py @@ -146,10 +146,9 @@ def get_vm_ip(vm_id): def migrate_vm(vm_id, destination): vm_id = validate_vm_id(vm_id) - try: + 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 From c8385e2f2cf2ed393d76d5ca446065e7634745f1 Mon Sep 17 00:00:00 2001 From: Yogeshiiiith Date: Tue, 10 Dec 2013 12:25:38 +0530 Subject: [PATCH 07/10] Modified is_running_vm function, using cut --- CentOSKVMAdapter.py | 237 +++++++++++++++++++++++++++++++++++++++++++- CentOSVZAdapter.py | 16 +-- 2 files changed, 245 insertions(+), 8 deletions(-) diff --git a/CentOSKVMAdapter.py b/CentOSKVMAdapter.py index ac3fb17..fbdc973 100644 --- a/CentOSKVMAdapter.py +++ b/CentOSKVMAdapter.py @@ -1,3 +1,238 @@ # Author: Yogesh Agrawal -# Contact: yogesh@vlabs.ac.in/ yogeshiiith@gmail.com +# Contact: yogesh@vlabs.ac.in or yogeshiiith@gmail.com +""" A module for managing VMs on CentOS - OpenVZ platform. """ + +__all__ = [ + 'create_vm', + 'start_vm', + 'stop_vm', + 'restart_vm', + 'start_vm_manager', + 'destroy_vm', + 'is_running_vm', + 'migrate_vm', + 'get_resource_utilization', + 'take_snapshot', + 'InvalidVMIDException', + ] + +# 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 +NAME_SERVER = "10.10.10.10" +SUBNET = ["10.1.10.0/24", "10.1.11.0/24"] +VZCTL = "/usr/sbin/vzctl" +VZLIST = "/usr/sbin/vzlist -a" +HOST_NAME = "vlabs.ac.in" +LAB_ID = "engg01" +MAX_vm_id = 2147483644 # 32-bit; exact value based on trial-and-error +VM_MANAGER_PORT = 8089 +VM_MANAGER_DIR = "/root/vm_manager" +OS = "Ubuntu" +OS_VERSION = "12.04" +IP_ADDRESS_REGEX = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" +#IP_ADDRESS_REGEX = +# "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"; + +class InvalidVMIDException(Exception): + def __init__(msg): + Exception.__init__(msg) + + +def create_vm(vm_id, vm_spec): + """ VM_specification is a VMSpec object """ + # Create the VM + vm_id = validate_vm_id(vm_id) + (vm_create_args, vm_set_args) = construct_vzctl_args(vm_spec) + try: + subprocess.check_call(VZCTL + " create " + vm_id + vm_create_args, shell=True) + subprocess.check_call(VZCTL + " start " + vm_id, shell=True) + subprocess.check_call(VZCTL + " set " + vm_id + vm_set_args, shell=True) + except subprocess.CalledProcessError, e: + raise e + return start_vm_manager(vm_id) + +def restart_vm(vm_id): + vm_id = validate_vm_id(vm_id) + try: + subprocess.check_call(VZCTL + " restart " + vm_id, shell=True) + except subprocess.CalledProcessError, e: + raise e + return start_vm_manager(vm_id) + +# Function alias +start_vm = restart_vm + +def start_vm_manager(vm_id): + # Copy the VMManager package to the VM + # Start the VMManager on a chosen port + # + # Return the VM's IP and port info + return (get_vm_ip(vm_id), VM_MANAGER_PORT) + +def get_resource_utilization(): + pass + +def stop_vm(vm_id): + vm_id = validate_vm_id(vm_id) + try: + subprocess.check_call(VZCTL + " stop " + vm_id, shell=True) + except subprocess.CalledProcessError, e: + raise e + # Return success or failure + +def destroy_vm(vm_id): + vm_id = validate_vm_id(vm_id) + try: + subprocess.check_call(VZCTL + " stop " + vm_id, shell=True) + subprocess.check_call(VZCTL + " destroy " + vm_id, shell=True) + except subprocess.CalledProcessError, e: + raise e + # Return success or failure + +def is_running_vm(vm_id): + vm_id = validate_vm_id(vm_id) + try: + status = subprocess.check_output(VZCTL + " status " + vm_id + " | grep running ", shell=True) + if status == "": + return "no" + elif status == "running": + return "yes" + + except subprocess.CalledProcessError, e: + raise e + +def get_vm_ip(vm_id): + vm_id = validate_vm_id(vm_id) + try: + vzlist = subprocess.check_output(VZLIST + " | grep " + vm_id, shell=True) + if vzlist == "": + return # raise exception? + ip_address = re.match(r'[\s*\w+]*\s+(%s)' % IP_ADDRESS_REGEX, vzlist) + if ip_address != None: + ip_address = ip_address.group(0) + return ip_address + except subprocess.CalledProcessError, e: + raise e + +def migrate_vm(vm_id, destination): + vm_id = validate_vm_id(vm_id) + 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) + pass + +def construct_vzctl_args(vm_spec): + """ Returns a tuple of vzctl create arguments and set arguments """ + lab_ID = LAB_ID if vm_spec.lab_ID == "" else vm_spec.lab_ID + host_name = lab_ID + "." + HOST_NAME + ip_address = find_available_ip() + os_template = find_os_template(vm_spec.os, vm_spec.os_version) + (ram, swap) = VMUtils.get_ram_swap(vm_spec.ram, vm_spec.swap) + (disk_soft, disk_hard) = VMUtils.get_disk_space(vm_spec.diskspace) + vm_create_args = " --ostemplate " + os_template + \ + " --ipadd " + ip_address + \ + " --diskspace " + disk_soft + ":" + disk_hard + \ + " --hostname " + host_name + # Note to self: check ram format "0:256M" vs "256M" + vm_set_args = " --nameserver " + NAME_SERVER + \ + " --ram " + ram + \ + " --swap " + swap + \ + " --onboot yes" + \ + " --save" + return (vm_create_args, vm_set_args) + +def find_available_ip(): + # not designed to be concurrent? + used_ips = subprocess.check_output(VZLIST, shell=True) + for subnet in SUBNET: + ip_network = netaddr.IPNetwork(subnet) + for ip in list(ip_network): + if ip == ip_network.network or ip == ip_network.broadcast: + # e.g. 192.0.2.0 or 192.0.2.255 for subnet 192.0.2.0/24 + continue + else: + ip_address = str(ip) + if ip_address not in used_ips: + return ip_address + +def find_os_template(os, os_version): + # What to do when request comes for unavailable OS/version? + os = OS.upper() if os == "" else os.strip().upper() + os_version = OS_VERSION if os_version == "" else os_version.strip() + if os == "UBUNTU": + if os_version == "12.04" or os_version == "12": + return "ubuntu-12.04-x86_64" + elif os_version == "11.10" or os_version == "11": + return "ubuntu-11.10-x86_64" + elif os == "CENTOS": + if os_version == "6.3": + return "centos-6.3-x86_64" + elif os_version == "6.2": + return "centos-6.2-x86_64" + elif os == "DEBIAN": + if os_version == "6.0" or os_version == "6": + return "debian-6.0-x86_64" + else: + pass + +def validate_vm_id(vm_id): + vm_id = str(vm_id).strip() + m = re.match(r'^([0-9]+)$', vm_id) + if m == None: + raise InvalidVMIDException("Invalid VM ID. VM ID must be numeric.") + vm_id = int(m.group(0)) + if vm_id <= 100: + raise InvalidVMIDException("Invalid VM ID. VM ID must be greater than 100.") + if vm_id > MAX_vm_id: + raise InvalidVMIDException("Invalid VM ID. Specify a smaller VM ID.") + return str(vm_id) + + +if __name__ == "__main__": + # 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) + #create_vm("99102", vm_spec) + #create_vm("99103", vm_spec) + destroy_vm("99100") + destroy_vm("99101") + #destroy_vm("99102") + #destroy_vm("99103") diff --git a/CentOSVZAdapter.py b/CentOSVZAdapter.py index 3d07bf2..47c9a43 100644 --- a/CentOSVZAdapter.py +++ b/CentOSVZAdapter.py @@ -122,8 +122,9 @@ def destroy_vm(vm_id): def is_running_vm(vm_id): vm_id = validate_vm_id(vm_id) try: - status = subprocess.check_output(VZCTL + " status " + vm_id + " | grep running ", shell=True) - if status == "": + status = subprocess.check_output(VZCTL + " status " + vm_id + " | cut -d\ -f5 ", shell=True) + status = status.strip("\n") + if status == "down": return "no" elif status == "running": return "yes" @@ -227,12 +228,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") From 30ec610ad8978a22ec293f776a56ba460baae1d7 Mon Sep 17 00:00:00 2001 From: Yogeshiiiith Date: Tue, 10 Dec 2013 12:37:20 +0530 Subject: [PATCH 08/10] modified is_running_vm function, put simple if condition --- CentOSVZAdapter.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/CentOSVZAdapter.py b/CentOSVZAdapter.py index 47c9a43..656e7ad 100644 --- a/CentOSVZAdapter.py +++ b/CentOSVZAdapter.py @@ -122,13 +122,11 @@ def destroy_vm(vm_id): def is_running_vm(vm_id): vm_id = validate_vm_id(vm_id) try: - status = subprocess.check_output(VZCTL + " status " + vm_id + " | cut -d\ -f5 ", shell=True) - status = status.strip("\n") - if status == "down": - return "no" - elif status == "running": + 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 From 67c175936af01aa0d283a88a21ad6797e98ba5b7 Mon Sep 17 00:00:00 2001 From: Yogeshiiiith Date: Sun, 22 Dec 2013 16:10:33 +0530 Subject: [PATCH 09/10] Created CentOSKVMAdapter.py, more functions needs to be added I have coded some --- CentOSKVMAdapter.py | 151 +++++++++++++++----------------------------- 1 file changed, 51 insertions(+), 100 deletions(-) diff --git a/CentOSKVMAdapter.py b/CentOSKVMAdapter.py index fbdc973..612bf16 100644 --- a/CentOSKVMAdapter.py +++ b/CentOSKVMAdapter.py @@ -1,7 +1,7 @@ # Author: Yogesh Agrawal -# Contact: yogesh@vlabs.ac.in or yogeshiiith@gmail.com +# Contact: yogesh@vlabs.ac.in -""" A module for managing VMs on CentOS - OpenVZ platform. """ +""" A module for managing VMs on Windows - KVM platform. """ __all__ = [ 'create_vm', @@ -68,84 +68,68 @@ def __init__(msg): Exception.__init__(msg) -def create_vm(vm_id, vm_spec): +def create_vm(vm_name): """ VM_specification is a VMSpec object """ # Create the VM - vm_id = validate_vm_id(vm_id) - (vm_create_args, vm_set_args) = construct_vzctl_args(vm_spec) +# vm_id = validate_vm_id(vm_id) try: - subprocess.check_call(VZCTL + " create " + vm_id + vm_create_args, shell=True) - subprocess.check_call(VZCTL + " start " + vm_id, shell=True) - subprocess.check_call(VZCTL + " set " + vm_id + vm_set_args, shell=True) + subprocess.check_call(" mkdir /mnt/das1/virtual_hosts/" + vm_name, shell=True) + subprocess.check_call(" virt-clone -o windowsvm -n " + vm_name + " -f /mnt/das1/virtual_hosts/" + vm_name + "/" + vm_name , shell=True) + subprocess.check_call(" virsh start " + vm_name, shell=True) except subprocess.CalledProcessError, e: raise e - return start_vm_manager(vm_id) + return -def restart_vm(vm_id): - vm_id = validate_vm_id(vm_id) +def is_running_vm(vm_name): +# vm_id = validate_vm_id(vm_id) try: - subprocess.check_call(VZCTL + " restart " + vm_id, shell=True) + 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 - return start_vm_manager(vm_id) + +def restart_vm(vm_name): +# vm_id = validate_vm_id(vm_id) + 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 start_vm_manager(vm_id): - # Copy the VMManager package to the VM - # Start the VMManager on a chosen port - # - # Return the VM's IP and port info - return (get_vm_ip(vm_id), VM_MANAGER_PORT) - -def get_resource_utilization(): - pass - -def stop_vm(vm_id): - vm_id = validate_vm_id(vm_id) - try: - subprocess.check_call(VZCTL + " stop " + vm_id, shell=True) - except subprocess.CalledProcessError, e: - raise e - # Return success or failure - -def destroy_vm(vm_id): - vm_id = validate_vm_id(vm_id) - try: - subprocess.check_call(VZCTL + " stop " + vm_id, shell=True) - subprocess.check_call(VZCTL + " destroy " + vm_id, shell=True) - except subprocess.CalledProcessError, e: - raise e - # Return success or failure - -def is_running_vm(vm_id): - vm_id = validate_vm_id(vm_id) +def stop_vm(vm_name): +# vm_id = validate_vm_id(vm_id) + 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] + print vm_id + subprocess.check_call(" virsh shutdown " + vm_id, shell=True) + except subprocess.CalledProcessError, e: + raise e + return "VM successfully stoped" + +def destroy_vm(vm_name): +# vm_id = validate_vm_id(vm_id) try: - status = subprocess.check_output(VZCTL + " status " + vm_id + " | grep running ", shell=True) - if status == "": - return "no" - elif status == "running": - return "yes" - + subprocess.check_call(" virsh undefine " + vm_name, shell=True) except subprocess.CalledProcessError, e: raise e + return "Vm destroyed" -def get_vm_ip(vm_id): - vm_id = validate_vm_id(vm_id) - try: - vzlist = subprocess.check_output(VZLIST + " | grep " + vm_id, shell=True) - if vzlist == "": - return # raise exception? - ip_address = re.match(r'[\s*\w+]*\s+(%s)' % IP_ADDRESS_REGEX, vzlist) - if ip_address != None: - ip_address = ip_address.group(0) - return ip_address - except subprocess.CalledProcessError, e: - raise e def migrate_vm(vm_id, destination): - vm_id = validate_vm_id(vm_id) +# vm_id = validate_vm_id(vm_id) try: subprocess.check_call(VZCTL + " stop " + vm_id, shell=True) subprocess.check_call(VZCTL + " vzmigrate " + destination + " " + vm_id, shell=True) @@ -156,40 +140,6 @@ def take_snapshot(vm_id): vm_id = validate_vm_id(vm_id) pass -def construct_vzctl_args(vm_spec): - """ Returns a tuple of vzctl create arguments and set arguments """ - lab_ID = LAB_ID if vm_spec.lab_ID == "" else vm_spec.lab_ID - host_name = lab_ID + "." + HOST_NAME - ip_address = find_available_ip() - os_template = find_os_template(vm_spec.os, vm_spec.os_version) - (ram, swap) = VMUtils.get_ram_swap(vm_spec.ram, vm_spec.swap) - (disk_soft, disk_hard) = VMUtils.get_disk_space(vm_spec.diskspace) - vm_create_args = " --ostemplate " + os_template + \ - " --ipadd " + ip_address + \ - " --diskspace " + disk_soft + ":" + disk_hard + \ - " --hostname " + host_name - # Note to self: check ram format "0:256M" vs "256M" - vm_set_args = " --nameserver " + NAME_SERVER + \ - " --ram " + ram + \ - " --swap " + swap + \ - " --onboot yes" + \ - " --save" - return (vm_create_args, vm_set_args) - -def find_available_ip(): - # not designed to be concurrent? - used_ips = subprocess.check_output(VZLIST, shell=True) - for subnet in SUBNET: - ip_network = netaddr.IPNetwork(subnet) - for ip in list(ip_network): - if ip == ip_network.network or ip == ip_network.broadcast: - # e.g. 192.0.2.0 or 192.0.2.255 for subnet 192.0.2.0/24 - continue - else: - ip_address = str(ip) - if ip_address not in used_ips: - return ip_address - def find_os_template(os, os_version): # What to do when request comes for unavailable OS/version? os = OS.upper() if os == "" else os.strip().upper() @@ -227,12 +177,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("newvm") + #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") + print stop_vm("newvm") #destroy_vm("99102") #destroy_vm("99103") From 5860ee9476c13ebc62f6882d0f645b25ada9928b Mon Sep 17 00:00:00 2001 From: Yogesh Date: Wed, 29 Jan 2014 13:14:26 +0530 Subject: [PATCH 10/10] Added CentOSKVMAdapter.py --- CentOSKVMAdapter.py | 98 +++++++++------------------------------------ 1 file changed, 18 insertions(+), 80 deletions(-) diff --git a/CentOSKVMAdapter.py b/CentOSKVMAdapter.py index 612bf16..ecee231 100644 --- a/CentOSKVMAdapter.py +++ b/CentOSKVMAdapter.py @@ -5,16 +5,15 @@ __all__ = [ 'create_vm', - 'start_vm', - 'stop_vm', + 'is_running_vm', 'restart_vm', - 'start_vm_manager', + 'stop_vm', 'destroy_vm', - 'is_running_vm', 'migrate_vm', + 'start_vm', + 'start_vm_manager', 'get_resource_utilization', 'take_snapshot', - 'InvalidVMIDException', ] # Standard Library imports @@ -48,42 +47,21 @@ def f(*popenargs, **kwargs): # Globals -NAME_SERVER = "10.10.10.10" -SUBNET = ["10.1.10.0/24", "10.1.11.0/24"] -VZCTL = "/usr/sbin/vzctl" -VZLIST = "/usr/sbin/vzlist -a" -HOST_NAME = "vlabs.ac.in" -LAB_ID = "engg01" -MAX_vm_id = 2147483644 # 32-bit; exact value based on trial-and-error -VM_MANAGER_PORT = 8089 -VM_MANAGER_DIR = "/root/vm_manager" -OS = "Ubuntu" -OS_VERSION = "12.04" -IP_ADDRESS_REGEX = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" -#IP_ADDRESS_REGEX = -# "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"; - -class InvalidVMIDException(Exception): - def __init__(msg): - Exception.__init__(msg) +VH_DIR = "/mnt/das1/virtual_hosts/" def create_vm(vm_name): - """ VM_specification is a VMSpec object """ - # Create the VM -# vm_id = validate_vm_id(vm_id) try: - subprocess.check_call(" mkdir /mnt/das1/virtual_hosts/" + vm_name, shell=True) - subprocess.check_call(" virt-clone -o windowsvm -n " + vm_name + " -f /mnt/das1/virtual_hosts/" + vm_name + "/" + vm_name , shell=True) - subprocess.check_call(" virsh start " + vm_name, shell=True) + 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): -# vm_id = validate_vm_id(vm_id) try: - status = subprocess.check_output(" virsh list | grep " + vm_name, shell=True) + status = subprocess.check_output("virsh list | grep " + vm_name, shell=True) if "running" in status: return "yes" else: @@ -92,10 +70,9 @@ def is_running_vm(vm_name): raise e def restart_vm(vm_name): -# vm_id = validate_vm_id(vm_id) if(is_running_vm(vm_name) == "yes"): try: - subprocess.check_call(" virsh reboot " + vm_name, shell=True) + subprocess.check_call("virsh reboot " + vm_name, shell=True) except subprocess.CalledProcessError, e: raise e else: @@ -106,77 +83,38 @@ def restart_vm(vm_name): start_vm = restart_vm def stop_vm(vm_name): -# vm_id = validate_vm_id(vm_id) 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] - print vm_id - subprocess.check_call(" virsh shutdown " + vm_id, shell=True) + subprocess.check_call("virsh shutdown " + vm_id, shell=True) except subprocess.CalledProcessError, e: raise e - return "VM successfully stoped" + return "VM successfully stopped" def destroy_vm(vm_name): -# vm_id = validate_vm_id(vm_id) try: - subprocess.check_call(" virsh undefine " + vm_name, shell=True) + subprocess.check_call("virsh undefine " + vm_name, shell=True) except subprocess.CalledProcessError, e: raise e return "Vm destroyed" - def migrate_vm(vm_id, destination): -# vm_id = validate_vm_id(vm_id) 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) - pass - -def find_os_template(os, os_version): - # What to do when request comes for unavailable OS/version? - os = OS.upper() if os == "" else os.strip().upper() - os_version = OS_VERSION if os_version == "" else os_version.strip() - if os == "UBUNTU": - if os_version == "12.04" or os_version == "12": - return "ubuntu-12.04-x86_64" - elif os_version == "11.10" or os_version == "11": - return "ubuntu-11.10-x86_64" - elif os == "CENTOS": - if os_version == "6.3": - return "centos-6.3-x86_64" - elif os_version == "6.2": - return "centos-6.2-x86_64" - elif os == "DEBIAN": - if os_version == "6.0" or os_version == "6": - return "debian-6.0-x86_64" - else: - pass - -def validate_vm_id(vm_id): - vm_id = str(vm_id).strip() - m = re.match(r'^([0-9]+)$', vm_id) - if m == None: - raise InvalidVMIDException("Invalid VM ID. VM ID must be numeric.") - vm_id = int(m.group(0)) - if vm_id <= 100: - raise InvalidVMIDException("Invalid VM ID. VM ID must be greater than 100.") - if vm_id > MAX_vm_id: - raise InvalidVMIDException("Invalid VM ID. Specify a smaller VM ID.") - return str(vm_id) - +def start_vm_manager(): + try: + subprocess.check_call("python VMManager.py", shell=True) + except subprocess.CalledProcessError, e: + raise e if __name__ == "__main__": - # Start an HTTP server and wait for invocation - # Parse the invocation command and route to - # appropriate methods. # vm_spec = VMSpec.VMSpec() # create_vm("newvm") #create_vm("99101", vm_spec)