From c31d78c7ffef28dcd10dfab3ed9a74c5d2588291 Mon Sep 17 00:00:00 2001 From: avinassh Date: Fri, 31 Jan 2014 14:07:36 +0530 Subject: [PATCH] few more bug fixes :relieved: --- CentOSVZAdapter.py | 2 +- Controller.py | 9 ++++++--- LabManager.py | 3 ++- VMManager/VMManager.py | 36 ++++++++++++++++++++++++++---------- VMUtils.py | 2 ++ 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/CentOSVZAdapter.py b/CentOSVZAdapter.py index cb126a9..147c375 100644 --- a/CentOSVZAdapter.py +++ b/CentOSVZAdapter.py @@ -221,7 +221,7 @@ def find_os_template(os, os_version): 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-custom1-x86_64" + return "ubuntu-12.04-custom-x86_64" elif os_version == "11.10" or os_version == "11": return "ubuntu-11.10-x86_64" elif os == "CENTOS": diff --git a/Controller.py b/Controller.py index 0f855de..66d4d9f 100644 --- a/Controller.py +++ b/Controller.py @@ -22,8 +22,11 @@ def test_lab(self, lab_id, lab_src_url, version=None): lab_spec = LabManager.get_lab_reqs(lab_id, lab_src_url, version) vmpoolmgr = VMPoolManager.VMPoolManager() (ip, port) = vmpoolmgr.create_vm(lab_spec) - print 'created vm ', ip, port - LabManager.test_lab(ip, port, lab_src_url, version) + #print 'created vm ', ip, port + if LabManager.test_lab(ip, port, lab_src_url, version): + return ip + else: + return "Test failed" except Exception, e: # This should return an error json when Controller is a web service print e @@ -32,4 +35,4 @@ def test_lab(self, lab_id, lab_src_url, version=None): if __name__ == '__main__': c = Controller() #c.test_lab("asdf", "asdf") - c.test_lab("asdf", "https://github.com/vlead/simo.git") \ No newline at end of file + print c.test_lab("ovpl01", "https://github.com/nrchandan/vlab-computer-programming") \ No newline at end of file diff --git a/LabManager.py b/LabManager.py index 3b2d40c..5b294a8 100644 --- a/LabManager.py +++ b/LabManager.py @@ -61,6 +61,7 @@ def checkout_version(repo_name): raise e def get_lab_spec(repo_name): + # Allow no lab spec but not an invalid json as a lab spec spec_path = GIT_CLONE_LOC + repo_name + LAB_SPEC_LOC if not os.path.exists(spec_path): raise LabSpecInvalid("Lab spec file not found") @@ -89,7 +90,7 @@ def test_lab(ip, port, lab_src_url, version=None): payload = {"lab_src_url": lab_src_url, "version": version} url = '%s:%s%s' % (ip, port, TEST_LAB_API_URI) response = requests.post(url=url, data=payload) - print response.text + return response.text def setup_logging(): diff --git a/VMManager/VMManager.py b/VMManager/VMManager.py index e77b82e..5ae3e8d 100644 --- a/VMManager/VMManager.py +++ b/VMManager/VMManager.py @@ -12,14 +12,24 @@ # to do : handle exceptions +import os import subprocess +import logging +import shlex +import json +from logging.handlers import TimedRotatingFileHandler -from LabActionsRunner import LabActionsRunner +from LabActionRunner import LabActionRunner GIT_CLONE_LOC = "./" VMM_LOGGER = logging.getLogger('VMM') -LOG_FILENAME = 'log/vmmanager.log' # make log name a setting -LOG_FD = open(LOG_FILENAME, 'a') +LOG_FILENAME = '/root/VMManager/log/vmmanager.log' # make log name a setting +LAB_SPEC_LOC = "/scripts/labspec.json" + +class LabSpecInvalid(Exception): + def __init__(self, msg): + Exception(self, msg) + # UGLY DUCK PUNCHING: Backporting check_output from 2.7 to 2.6 if "check_output" not in dir(subprocess): @@ -65,7 +75,7 @@ def test_lab(lab_src_url, version=None): # run LabAction Runner # instantiate the object - def get_build_spec(lab_spec): + def get_build_steps_spec(lab_spec): return {"build_steps": lab_spec['lab'][u'build_requirements']['platform']['build_steps']} def get_installer_steps_spec(lab_spec): @@ -81,6 +91,7 @@ def repo_exists(repo_name): def clone_repo(repo_name): clone_cmd = shlex.split("git clone %s" % lab_src_url) + VMM_LOGGER.debug(clone_cmd) try: subprocess.check_call(clone_cmd, stdout=LOG_FD, stderr=LOG_FD) except Exception, e: @@ -89,7 +100,7 @@ def clone_repo(repo_name): raise e def pull_repo(repo_name): - pull_cmd = shlex.split("git --git-dir=%s pull" % \ + pull_cmd = shlex.split("git --git-dir=%s/.git pull" % \ (GIT_CLONE_LOC + repo_name)) try: subprocess.check_call(pull_cmd, stdout=LOG_FD, stderr=LOG_FD) @@ -110,6 +121,7 @@ def checkout_version(repo_name): def get_lab_spec(repo_name): repo_path = GIT_CLONE_LOC + repo_name + LAB_SPEC_LOC + print repo_path if not os.path.exists(repo_path): raise LabSpecInvalid("Lab spec file not found") try: @@ -126,26 +138,30 @@ def get_lab_spec(repo_name): lab_spec = get_lab_spec(repo_name) - lar = LabActionsRunner(get_installer_steps_spec(lab_spec), "") + lar = LabActionRunner(get_installer_steps_spec(lab_spec), "") lar.run_install_source() - lar = LabActionsRunner(get_build_steps_spec(lab_spec), "") + lar = LabActionRunner(get_build_steps_spec(lab_spec), "") lar.run_build_steps() def setup_logging(): VMM_LOGGER.setLevel(logging.DEBUG) # make log level a setting # Add the log message handler to the logger - handler = logging.handlers.TimedRotatingFileHandler( + myhandler = TimedRotatingFileHandler( LOG_FILENAME, when='midnight', backupCount=5) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p') - handler.setFormatter(formatter) - VMM_LOGGER.addHandler(handler) + myhandler.setFormatter(formatter) + VMM_LOGGER.addHandler(myhandler) +setup_logging() +LOG_FD = open(LOG_FILENAME, 'a') + if __name__ == "__main__": + test_lab("https://github.com/nrchandan/vlab-computer-programming") print cpu_load() print mem_usage() \ No newline at end of file diff --git a/VMUtils.py b/VMUtils.py index 0902263..ce2c498 100644 --- a/VMUtils.py +++ b/VMUtils.py @@ -49,6 +49,8 @@ def get_disk_space(disk_soft): return (disk_soft, disk_hard) def convert_to_megs(value): + if not value: + return 0 m = re.match(r'([0-9]+)\s*([a-zA-Z]+)', value) if m == None: return 0