def test_lab(vmmgr_ip, port, lab_src_url, version=None): # make sure VM Manager is running # the target VM should have LabActionRunner scripts # VM Manager should do the following? # or better it should invoke another script which should # clone the repo in the VM # get the lab_spec # run Lab Action Runner if not 'http' in vmmgr_ip: raise Exception('Protocol not specified in VMManager host address!!') e = EnvSetUp() logger.debug("vmmgr_ip = %s, port = %s, lab_src_url = %s" % (vmmgr_ip, port, lab_src_url)) payload = {"lab_src_url": lab_src_url, "version": version} config_spec = json.loads(open(e.get_ovpl_directory_path() + "/config/config.json").read()) TEST_LAB_API_URI = config_spec["VMMANAGER_CONFIG"]["TEST_LAB_URI"] url = '%s:%s%s' % (vmmgr_ip, port, TEST_LAB_API_URI) logger.debug("url = %s, payload = %s" % (url, str(payload))) exception_str = "" for i in (1,2,4,8,16): time.sleep(i) try: response = requests.post(url=url, data=payload) logger.debug("response = %s" % response) return ("Success" in response.text, response.text) except Exception, e: exception_str = str(e) attempts = {0:'first', 1:'second', 2:'third', 3:'fourth'} logger.error("Error installing lab on VM for the %s attempt with error: %s" % \ (attempts[math.log(i)/math.log(2)], str(e)))
def _copy_ovpl_source(self, ip_addr): env = EnvSetUp() src_dir = env.get_ovpl_directory_path() dest_dir = "{0}@{1}:{2}".format(self.VM_USER, ip_addr, settings.VM_DEST_DIR) logger.debug("ip_address = %s, src_dir=%s, dest_dir=%s" % (ip_addr, src_dir, dest_dir)) try: return self._copy_files(src_dir, dest_dir) except Exception, e: logger.error("ERROR = %s" % str(e)) print 'ERROR= %s' % (str(e)) return False
def test_lab(lab_src_url, version=None): # check out the source with version provided # is repo already exists? if yes, then do a git pull # else clone the repo # get the labspec from /scripts/lab_spec.json # get the appropriate the actions from lab_spec.json # run LabAction Runner # instantiate the object e = EnvSetUp.Instance() git = GitCommands() logger.info("Environment http_proxy = %s" % os.environ["http_proxy"]) logger.info("Environment https_proxy = %s" % os.environ["https_proxy"]) def fill_aptconf(): try: http_proxy = os.environ["http_proxy"] https_proxy = os.environ["https_proxy"] http_cmd = r'echo "Acquire::http::Proxy \"%s\";"%s' % (http_proxy, '>>/etc/apt/apt.conf') https_cmd = r'echo "Acquire::https::Proxy \"%s\";"%s' % (https_proxy, '>>/etc/apt/apt.conf') (ret_code, output) = execute_command(http_cmd) (ret_code, output) = execute_command(https_cmd) except Exception, e: logger.error("Writing to /etc/apt/apt.conf failed with error: %s" % (str(e))) raise e
def __create_logger(name): e = EnvSetUp() config_spec = json.loads(open(e.get_ovpl_directory_path() + "/config/config.json").read()) logserver_ip = config_spec["LOGGING_CONFIGURATION"]["LOGSERVER_CONFIGURATION"]["SERVER_IP"] logserver_port = config_spec["LOGGING_CONFIGURATION"]["LOGSERVER_CONFIGURATION"]["SERVER_PORT"] logserver_uri = config_spec["LOGGING_CONFIGURATION"]["LOGSERVER_CONFIGURATION"]["URI_ENDPOINT"] log_level = config_spec["LOGGING_CONFIGURATION"]["LOG_LEVEL"] logger = logging.getLogger(name) logger.setLevel(log_level) if logger.handlers == []: server = logserver_ip + ":" + str(logserver_port) print server http_handler = HTTPHandler(host=server, url=logserver_uri, method="POST") logger.addHandler(http_handler) return logger
def prepare_vm_for_bridged_network(self, vm_id): e = EnvSetUp() dirc = e.get_ovpl_directory_path() src_dirc = dirc + "/src/adapters/bridge-settings" dest_dirc = dirc + "/src/adapters/interfaces" try: copy_command = "rsync -arz --progress " + src_dirc + " " + dest_dirc logger.debug("copy command = %s" % copy_command) command = ('%s' % (copy_command)) logger.debug("Command = %s" % command) (ret_code, output) = execute_command(command) if ret_code == 0: logger.debug("Copy successful") else: logger.debug("Copy Unsuccessful, return code is %s" % str(ret_code)) except Exception, e: logger.error("ERROR = %s" % str(e))
def __init__(self): self.env = EnvSetUp.Instance() self.git_clone_loc = ( self.env.get_config_spec())["LABMANAGER_CONFIG"]["GIT_CLONE_LOC"] self.lab_spec_dir = ( self.env.get_config_spec())["LABMANAGER_CONFIG"]["LAB_SPEC_DIR"] self.lab_spec_file = ( self.env.get_config_spec())["LABMANAGER_CONFIG"]["LAB_SPEC_FILE"] logger.debug("GIT_CLONE_LOC = %s" % str(self.git_clone_loc)) logger.debug("LAB_SPEC_DIR = %s" % str(self.lab_spec_dir)) logger.debug("LAB_SPEC_FILE = %s" % str(self.lab_spec_file))
def __init__(self): """ State should be rewriten""" logger.debug("VMPoolManager: _init_()") self.system = State.Instance() self.VMPools = [] e = EnvSetUp() config_spec = json.loads(open(e.get_ovpl_directory_path() + "/config/config.json").read()) pools = config_spec["VMPOOL_CONFIGURATION"]["VMPOOLS"] create_uri = config_spec["API_ENDPOINTS"]["CREATE_URI_ADAPTER_ENDPOINT"] destroy_uri = config_spec["API_ENDPOINTS"]["DESTROY_URI_ADAPTER_ENDPOINT"] for pool in pools: self.add_vm_pool( pool["POOLID"], \ pool["DESCRIPTION"], \ pool["ADAPTERIP"], \ pool["PORT"], \ create_uri, \ destroy_uri) logger.debug("VMPoolManager: _init_(); vm_pools = %s" % (str(self.VMPools)))
def __init__(self): # check if the key_file exists, else throw an error! # The key file should not be checked in, but the deployer has to # manually copy and configure the correct location if not os.path.isfile(self.key_file_path): raise AWSKeyFileNotFound("Given key file not found!: %s" % self.key_file_path) # deduce the key file name from the key file path.. # assuming the key file ends with a .pem extension - otherwise this # won't work! self.key_name = self.key_file_path.split('/')[-1].split('.pem')[0] self.connection = self.create_connection() self.env = EnvSetUp.Instance() self.git = GitCommands() self.time_before_next_retry = 5
def __init__(self): """ State should be rewriten""" logger.debug("VMPoolManager: _init_()") self.state = State.Instance() self.vmpools = [] self.env = EnvSetUp.Instance() self.config_spec = self.env.get_config_spec() self.pools = self.config_spec["VMPOOL_CONFIGURATION"]["VMPOOLS"] for pool in self.pools: self.add_vm_pool(pool["POOLID"], pool["DESCRIPTION"], pool["ADAPTERIP"], pool["PORT"], self.config_spec["VMPOOL_CONFIGURATION"]["ADAPTER_ENDPOINTS"]["CREATE_URI"], self.config_spec["VMPOOL_CONFIGURATION"]["ADAPTER_ENDPOINTS"]["DESTROY_URI"]) logger.debug("VMPoolManager: _init_(); vm_pools = %s" % (str(self.vmpools)))
def get_adapter_details(): adapter_details = AdapterDetails() env = EnvSetUp.Instance() config_spec = env.get_config_spec() adapter_details.create_uri = \ config_spec["VMPOOL_CONFIGURATION"]["ADAPTER_ENDPOINTS"]["CREATE_URI"] adapter_details.destroy_uri = \ config_spec["VMPOOL_CONFIGURATION"]["ADAPTER_ENDPOINTS"]["DESTROY_URI"] adapter_details.restart_uri = \ config_spec["VMPOOL_CONFIGURATION"]["ADAPTER_ENDPOINTS"]["RESTART_URI"] pool_id = config_spec["ADAPTER_TO_USE"]["POOLID"] - 1 adapter_id = config_spec["ADAPTER_TO_USE"]["ADAPTERID"] - 1 adapter_details.port = \ config_spec["VMPOOL_CONFIGURATION"]["VMPOOLS"][pool_id]["PORT"] adapter_details.module_name = \ config_spec["VMPOOL_CONFIGURATION"]["VMPOOLS"][pool_id]["ADAPTERS"] \ [adapter_id]["MODULE"] adapter_details.adapter_name = \ config_spec["VMPOOL_CONFIGURATION"]["VMPOOLS"][pool_id]["ADAPTERS"] \ [adapter_id]["ADAPTER"] return adapter_details
def __create_logger(name): env = EnvSetUp.Instance() config_spec = env.get_config_spec() logserver_ip = config_spec["LOGGING_CONFIGURATION"][ "LOGSERVER_CONFIGURATION"]["SERVER_IP"] logserver_port = config_spec["LOGGING_CONFIGURATION"][ "LOGSERVER_CONFIGURATION"]["SERVER_PORT"] logserver_uri = config_spec["LOGGING_CONFIGURATION"][ "LOGSERVER_CONFIGURATION"]["URI_ENDPOINT"] log_level = config_spec["LOGGING_CONFIGURATION"]["LOG_LEVEL"] logger = logging.getLogger(name) logger.setLevel(log_level) if logger.handlers == []: server = logserver_ip + ":" + str(logserver_port) print server http_handler = HTTPHandler(host=server, url=logserver_uri, method="POST") logger.addHandler(http_handler) return logger
# logging imports import logging import logging.handlers # other impors import os from __init__ import * from utils.envsetup import EnvSetUp env = EnvSetUp.Instance() used_loggers = {} config_spec = env.get_config_spec() FILE_PATH = config_spec["LOGGING_CONFIGURATION"]["LOGSERVER_CONFIGURATION"]["FILE_PATH"] if not os.path.isdir(FILE_PATH): os.mkdir(FILE_PATH) def get_logger(name): """Returns logger object""" logger = None if name in used_loggers: logger = used_loggers[name] else: logger = logging.getLogger(name) used_loggers[name] = logger LOG_FILE_PATH = FILE_PATH + str(name) + '.log' print LOG_FILE_PATH timed_handler = logging.handlers.TimedRotatingFileHandler( LOG_FILE_PATH, when='midnight', backupCount=5) formatter = logging.Formatter('%(asctime)s: %(message)s',
def __init__(self): self.env = EnvSetUp.Instance() self.git = GitCommands()
import os import json from http_logging.http_logger import logger from utils.execute_commands import * from utils.envsetup import EnvSetUp e = EnvSetUp() config_spec = json.loads(open(e.get_ovpl_directory_path() + "/config/config.json").read()) GIT_CLONE_LOC = config_spec["LABMANAGER_CONFIG"]["GIT_CLONE_LOC"] LAB_SPEC_DIR = config_spec["LABMANAGER_CONFIG"]["LAB_SPEC_DIR"] LAB_SPEC_FILE = config_spec["LABMANAGER_CONFIG"]["LAB_SPEC_FILE"] logger.debug("GIT_CLONE_LOC = %s" % str(GIT_CLONE_LOC)) logger.debug("LAB_SPEC_DIR = %s" % str(LAB_SPEC_DIR)) logger.debug("LAB_SPEC_FILE = %s" % str(LAB_SPEC_FILE)) class LabSpecInvalid(Exception): def __init__(self, msg): Exception(self, msg) # sample lab_src_url: [email protected]:vlead/ovpl.git def construct_repo_name(lab_src_url): logger.debug("lab_src_url: %s" % lab_src_url) repo = lab_src_url.split('/')[-1] repo_name = (repo[:-4] if repo[-4:] == ".git" else repo) logger.debug("repo_name: %s" % repo_name) return str(repo_name)
# logging imports import logging import logging.handlers # other impors import json import os from utils.envsetup import EnvSetUp e = EnvSetUp() used_loggers = {} config_spec = json.loads(open(e.get_ovpl_directory_path() + "/config/config.json").read()) FILE_PATH = config_spec["LOGGING_CONFIGURATION"]["LOGSERVER_CONFIGURATION"]["FILE_PATH"] if not os.path.isdir(FILE_PATH): os.mkdir(FILE_PATH) def get_logger(name): """Returns logger object""" logger = None if name in used_loggers: logger = used_loggers[name] else: logger = logging.getLogger(name) used_loggers[name] = logger LOG_FILE_PATH = FILE_PATH + str(name) + '.log' print LOG_FILE_PATH timed_handler = logging.handlers.TimedRotatingFileHandler( LOG_FILE_PATH, when='midnight', backupCount=5) formatter = logging.Formatter('%(asctime)s: %(message)s',
def __init__(self): self.env = EnvSetUp.Instance() self.git = GitCommands() self.time_before_next_retry = 5
import netaddr import sh import re from time import sleep import socket from __init__ import * from utils.envsetup import EnvSetUp from utils.execute_commands import execute_command from httplogging.http_logger import logger from config.adapters import base_config VZCTL = "/usr/sbin/vzctl" VZLIST = "/usr/sbin/vzlist -a" OVPL_DIR_PATH = EnvSetUp.Instance().get_ovpl_directory_path() class BaseAdapter: def create_vm(lab_spec): raise Exception("BaseAdapter: unimplemented create_vm()") def init_vm(vm_id, lab_repo_name): raise Exception("BaseAdapter: unimplemented init_()") # success status, response string return (False, "unimplemented") class AdapterDetails: pass class OSNotFound(Exception):