def destroy_vm(self, vm_id): vm_id = self.validate_vm_id(vm_id) try: command = (r'ssh -o "%s" %s "%s stop %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZCTL, vm_id)) logger.debug("BridgeVZAdapter: destroy_vm(): stop command = %s" % command) (ret_code, output) = execute_command(command) if ret_code == 0: command = (r'ssh -o "%s" %s "%s destroy %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZCTL, vm_id)) logger.debug("BridgeVZAdapter: destroy_vm(): destroy command = \ %s" % command) (ret_code, output) = execute_command(command) if ret_code == 0: return "Success" except Exception, e: logger.error("Error destroying VM: " + str(e)) return "Failed to destroy VM: " + str(e)
def create_vm(self, lab_spec, vm_id=""): logger.debug("centos_openvz_adapter: create_vm()") """If no vm_id is specified, it is computed using the last two segments""" """of an available IP address; vm_spec is an object """ if vm_id == "": ip_address = base_adapter.find_available_ip() m = re.match(r'[0-9]+.[0-9]+.([0-9]+).([0-9]+)', ip_address) if m is not None: vm_id = str((int(m.group(1) + m.group(2)))) # vm_id = m.group(1) + m.group(2) else: ip_address = None vm_id = self.validate_vm_id(vm_id) (vm_create_args, vm_set_args) = self.construct_vzctl_args(lab_spec) logger.debug("centos_openvz_adapter: create_vm(): ip = %s, vm_id = %s, \ vm_create_args = %s, vm_set_args = %s" % (ip_address, vm_id, vm_create_args, vm_set_args)) try: command = (r'ssh -o "%s" %s "%s create %s %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZCTL, vm_id, vm_create_args)) logger.debug("centos_openvz_adapter: create_vm(): create command = %s" % command) (ret_code, output) = execute_command(command) if ret_code == 0: command = (r'ssh -o "%s" %s "%s start %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZCTL, vm_id)) logger.debug("centos_openvz_adapter: create_vm():start command = %s" % command) (ret_code, output) = execute_command(command) if ret_code == 0: command = (r'ssh -o "%s" %s "%s set %s %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZCTL, vm_id, vm_set_args)) logger.debug("centos_openvz_adapter:create_vm():set command=%s" % command) (ret_code, output) = execute_command(command) if ret_code == 0: return (True, vm_id) except Exception, e: logger.error("Error creating VM: " + str(e)) # raise e return (False, -1)
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 copy_public_key(self, vm_id): try: public_key_file = ("%s%s%s%s" % (base_config.VM_ROOT_DIR, base_config.ADS_SERVER_VM_ID, base_config.VM_DEST_DIR, ".ssh/id_rsa.pub")) authorized_key_file = ("%s%s%s%s" % (base_config.VM_ROOT_DIR, vm_id, base_config.VM_DEST_DIR, ".ssh/authorized_keys")) logger.debug("public key location = %s, authorized key location = %s" % (public_key_file, authorized_key_file)) command = (r'ssh -o "%s" %s "%s %s > %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, "/bin/cat", public_key_file, authorized_key_file)) logger.debug("command to cpy the public key = %s" % command) (ret_code, output) = execute_command(command) return True except Exception, e: logger.error("ERROR = %s" % str(e)) return False
def fill_aptconf(lab_spec): OS = str(lab_spec['lab']['runtime_requirements']['platform']['os']) http_proxy = os.environ["http_proxy"] https_proxy = os.environ["https_proxy"] if OS == "ubuntu": try: 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 execute(command): # do some validation try: logger.info("Command executed: " + command) (ret_code, output) = execute_command(command) return output except Exception, e: logger.error("Execution failed: " + str(e)) return "Error executing the command: " + str(e)
def test_create_vm(self): test_logger.debug("test_create_vm()") (status, self.vm_id) = self.adapter.create_vm(self.lab_spec) test_logger.debug("test_create_vm(): status = %s, vm_id = %s" % (str(status), str(self.vm_id))) check_cmd = "ssh root@%s 'vzlist %s'" % (self.base_ip, self.vm_id) test_logger.debug("test_create_vm(): check_cmd = %s" % check_cmd) (return_code, output) = execute_command(check_cmd) test_logger.debug("test_create_vm(): return_code = %s" % str(return_code)) self.assertEqual(return_code, 0) test_logger.debug("test_create_vm(): Test passed")
def reset_repo(self, repo_name): repo = self.git_clone_loc + repo_name reset_cmd = "git --git-dir=%s/.git --work-tree=%s reset --hard" % (repo, repo) logger.debug("reset cmd: %s" % reset_cmd) try: (ret_code, output) = execute_command(reset_cmd) logger.debug("reset repo successful") except Exception, e: logger.error("Error Resetting the repository: " + str(e)) raise e
def pull_repo(self, repo_name): repo = self.git_clone_loc + repo_name pull_cmd = "git --git-dir=%s/.git --work-tree=%s pull" % (repo, repo) logger.debug("pull cmd: %s" % pull_cmd) try: (ret_code, output) = execute_command(pull_cmd) logger.debug("Pull repo successful") except Exception, e: logger.error("Error Pulling the repository: " + str(e)) raise e
def clone_repo(self, lab_src_url, repo_name): clone_cmd = "git clone %s %s%s" % (lab_src_url, self.git_clone_loc, repo_name) logger.debug(clone_cmd) try: (ret_code, output) = execute_command(clone_cmd) logger.debug("Clone repo successful") except Exception, e: logger.error("Error Cloning the repository: " + str(e)) raise e
def start_vm_manager(self, vm_ip_addr): logger.debug("AWSAdapter: Attempting to start VMMgr: vm_ip:%s" % (vm_ip_addr)) ssh_command = "ssh -i {0} -o StrictHostKeyChecking=no {1}@{2} ".\ format(self.key_file_path, self.VM_USER, vm_ip_addr) vmmgr_cmd = "'python {0}{1} >> vmmgr.log 2>&1 < /dev/null &'".\ format(settings.VMMANAGERSERVER_PATH, settings.VM_MANAGER_SCRIPT) command = ssh_command + vmmgr_cmd logger.debug("AWSAdapter: start_vm_manager(): command = %s" % command) try: execute_command(command) except Exception, e: logger.error("AWSAdapter: start_vm_manager(): " + "command = %s, ERROR = %s" % (command, str(e))) return False
def checkout_version(self, repo_name, version=None): repo = self.git_clone_loc + repo_name if version is None: version = "master" checkout_cmd = "git --git-dir=%s/.git --work-tree=%s checkout %s" % (repo, repo, version) try: (ret_code, output) = execute_command(checkout_cmd) logger.debug("Checkout repo successful") except Exception, e: logger.error("Error checking out the repository: " + str(e)) raise e
def reset_repo(self, repo_name): repo = self.git_clone_loc + repo_name reset_cmd = "git --git-dir=%s/.git --work-tree=%s reset --hard" % ( repo, repo) logger.debug("reset cmd: %s" % reset_cmd) try: (ret_code, output) = execute_command(reset_cmd) logger.debug("reset repo successful") except Exception, e: logger.error("Error Resetting the repository: " + str(e)) raise e
def restart_vm(self, vm_id): vm_id = self.validate_vm_id(vm_id) try: command = (r'ssh -o "%s" %s "%s restart %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZCTL, vm_id)) logger.debug("BridgeVZAdapter: restart_vm(): restart command = %s" % command) (ret_code, output) = execute_command(command) except Exception, e: raise e
def checkout_version(self, repo_name, version=None): repo = self.git_clone_loc + repo_name if version is None: version = "master" checkout_cmd = "git --git-dir=%s/.git --work-tree=%s checkout %s" % ( repo, repo, version) try: (ret_code, output) = execute_command(checkout_cmd) logger.debug("Checkout repo successful") except Exception, e: logger.error("Error checking out the repository: " + str(e)) raise e
def start_vm_manager(self, vm_ip_addr): ovpl_dir_name = base_adapter.OVPL_DIR_PATH.split("/")[-1] vm_ovpl_path = base_config.VM_DEST_DIR + ovpl_dir_name logger.debug("AWSAdapter: Attempting to start VMMgr: vm_ip:%s" % (vm_ip_addr)) ssh_command = "ssh -i {0} -o StrictHostKeyChecking=no {1}@{2} ".\ format(self.key_file_path, self.VM_USER, vm_ip_addr) vmmgr_cmd = "'python {0}{1} >> vmmgr.log 2>&1 < /dev/null &'".\ format(vm_ovpl_path, base_config.VM_MANAGER_SERVER_PATH) command = ssh_command + vmmgr_cmd logger.debug("AWSAdapter: start_vm_manager(): command = %s" % command) try: execute_command(command) except Exception, e: logger.error("AWSAdapter: start_vm_manager(): " + "command = %s, ERROR = %s" % (command, str(e))) return False
def register_lab(self, lab_id, ip_address): service_host = base_config.SERVICE_HOST service_name = "hosting_service" service_action = "register" command = 'ssh %s %s %s %s %s' % \ (service_host, service_name, service_action, lab_id, ip_address) logger.debug("Hook's service command = %s" % command) (ret_code, output) = execute_command(command) if ret_code == 0: domain_name = lab_id + "." + get_adapter_hostname() logger.debug("FQDN of lab is = %s" % domain_name) return domain_name
def _add_default_gw(self, vm_ip): if not self.default_gw: return True logger.debug("AWSAdapter: Attempting to add default gateway to VM: %s" % (vm_ip)) ssh_command = "ssh -i {0} -o StrictHostKeyChecking=no {1}@{2} ".\ format(self.key_file_path, self.VM_USER, vm_ip) add_def_gw_cmd = "'route del default; route add default gw {0}'".\ format(self.default_gw) command = ssh_command + add_def_gw_cmd logger.debug("AWSAdapter: _add_default_gw(): command = %s" % command) try: execute_command(command) except Exception, e: logger.error("AWSAdapter: _add_default_gw(): " + "command = %s, ERROR = %s" % (command, str(e))) return False
def _add_default_gw(self, vm_ip): if not self.default_gw: return True logger.debug( "AWSAdapter: Attempting to add default gateway to VM: %s" % (vm_ip)) ssh_command = "ssh -i {0} -o StrictHostKeyChecking=no {1}@{2} ".\ format(self.key_file_path, self.VM_USER, vm_ip) add_def_gw_cmd = "'route del default; route add default gw {0}'".\ format(self.default_gw) command = ssh_command + add_def_gw_cmd logger.debug("AWSAdapter: _add_default_gw(): command = %s" % command) try: execute_command(command) except Exception, e: logger.error("AWSAdapter: _add_default_gw(): " + "command = %s, ERROR = %s" % (command, str(e))) return False
def stop_vm(self, vm_id): vm_id = self.validate_vm_id(vm_id) try: command = (r'ssh -o "%s" %s "%s stop %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZCTL, vm_id)) logger.debug("centos_openvz_adapter: stop_vm(): command = %s" % command) (ret_code, output) = execute_command(command) return "Success" except Exception, e: logger.error("Error stopping VM: " + str(e)) return "Failed to stop VM: " + str(e)
def start_container(self, vm_id): try: command = (r'ssh -o "%s" %s "%s start %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZCTL, vm_id)) logger.debug("BridgeVZAdapter: start_container(): start command = \ %s" % command) (ret_code, output) = execute_command(command) if ret_code == 0: return True else: return False except Exception, e: logger.error("Error starting VM: " + str(e)) return False
def run(self): """Runs a command. Waits for the command to finish.""" if len(self._cmd) == 0: logger.error("LabActionScript::run() - No command to run") raise EmptyLabActionError("No command to run") try: # self._cmd[0] = os.path.join(self._path_prefix, self._cmd[0]) logger.debug("LabActionScript::run() - " + os.environ["http_proxy"]) logger.debug("LabActionScript::run() - " + self._cmd) logger.debug("LabActionScript::run() - " + str(os.getcwd())) (ret_code, output) = execute_command(self._cmd) self._state = LabActionScript.ACTION_COMPLETED except Exception, e: self._state = LabActionScript.ACTION_UNSUCCESSFUL logger.error("LabActionScript::run() exception is: %s" % str(e))
def get_vm_ip(self, vm_id): vm_id = self.validate_vm_id(vm_id) try: command = (r'ssh -o "%s" %s "%s | grep %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZLIST, vm_id)) (ret_code, vzlist) = execute_command(command) if vzlist == "": return # raise exception? ip_address = re.search(IP_ADDRESS_REGEX, vzlist) if ip_address is not None: ip_address = ip_address.group(0) return ip_address except Exception, e: raise e
def set_container_params(self, vm_id, vm_set_args): try: command = (r'ssh -o "%s" %s "%s set %s %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZCTL, vm_id, vm_set_args)) logger.debug("BridgeVZAdapter: set_container_params(): set command \ = %s" % command) (ret_code, output) = execute_command(command) if ret_code == 0: return self.prepare_vm_for_bridged_network(vm_id) else: return False except Exception, e: logger.error("Error setting VM: " + str(e)) return False
def is_ctid_free(ip): # to check vm_id is already exist or not m = re.match(r'[0-9]+.[0-9]+.([0-9]+).([0-9]+)', ip) vm_id = str(int(m.group(1) + m.group(2))) command = (r'ssh -o "%s" %s "%s %s| grep %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZLIST, vm_id, vm_id)) logger.debug("CentOSVZAdapter: vzlist command = %s" % command) try: (ret_code, vzlist) = execute_command(command) if ret_code == 0: return False else: return True except Exception: logger.debug("No container found with vm id = %s" % vm_id) return True
def copy_files(self, src_dir, dest_dir): try: copy_command = "rsync -arz --progress " + src_dir + " " + dest_dir logger.debug("copy command = %s" % copy_command) command = (r'ssh %s "%s"' % (base_config.BASE_IP_ADDRESS, copy_command)) logger.debug("Command = %s" % command) (ret_code, output) = execute_command(command) if ret_code == 0: logger.debug("Copy successful") return True else: logger.debug("Copy Unsuccessful, return code is %s" % str(ret_code)) return False except Exception, e: logger.error("ERROR = %s" % str(e)) return False
def _copy_files(self, src_dir, dest_dir): cmd = "rsync -azr -e 'ssh -i {0} -o StrictHostKeyChecking=no' {1} {2}".\ format(self.key_file_path, src_dir, dest_dir) logger.debug("Command = %s" % cmd) try: (ret_code, output) = execute_command(cmd) if ret_code == 0: logger.debug("Copy successful") return True else: logger.debug("Copy Unsuccessful, return code is %s" % str(ret_code)) return False except Exception, e: logger.error("ERROR = %s" % str(e)) return False
def prepare_vm_for_bridged_network(self, vm_id): src_file = base_adapter.OVPL_DIR_PATH + \ config.BRIDGE_NETWORK_SETUP_PATH + "bridge-settings" dest_file = base_adapter.OVPL_DIR_PATH + \ config.BRIDGE_NETWORK_SETUP_PATH + "interfaces" try: copy_command = "rsync -arz --progress " + src_file + " " + dest_file 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 start_vm_manager(self, vm_id): ovpl_dir_name = base_adapter.OVPL_DIR_PATH.split("/")[-1] vm_ovpl_path = base_config.VM_DEST_DIR + ovpl_dir_name start_vm_manager_command = ( "python %s%s %s" % (vm_ovpl_path, base_config.VM_MANAGER_SERVER_PATH, ">>/root/vm.log 2>&1 </dev/null &")) command = (r"ssh -o '%s' %s%s '%s'" % (base_config.NO_STRICT_CHECKING, "root@", IP_ADDRESS, start_vm_manager_command)) logger.debug("BridgeVZAdapter: start_vm_manager(): command = %s" % command) try: (ret_code, output) = execute_command(command) return True except Exception, e: logger.error("BridgeVZAdapter: start_vm_manager(): command = %s, \ ERROR = %s" % (command, str(e))) return False
def create_container(self, vm_id, vm_create_args): try: command = (r'ssh -o "%s" %s "%s create %s %s"' % (base_config.NO_STRICT_CHECKING, base_config.BASE_IP_ADDRESS, VZCTL, vm_id, vm_create_args)) logger.debug("BridgeVZAdapter: vm_create(): create command = %s" % command) (ret_code, output) = execute_command(command) if ret_code == 0: return True else: return False except Exception, e: logger.error("Error creating VM: " + str(e)) return False
def start_vm_manager(self, vm_id): ovpl_dir_name = base_adapter.OVPL_DIR_PATH.split("/")[-1] vm_ovpl_path = base_config.VM_DEST_DIR + ovpl_dir_name start_vm_manager_command = ("python %s%s %s" % (vm_ovpl_path, base_config.VM_MANAGER_SERVER_PATH, ">>/root/vm.log 2>&1 </dev/null &")) command = (r"ssh -o '%s' %s%s '%s'" % (base_config.NO_STRICT_CHECKING, "root@", IP_ADDRESS, start_vm_manager_command)) logger.debug("BridgeVZAdapter: start_vm_manager(): command = %s" % command) try: (ret_code, output) = execute_command(command) return True except Exception, e: logger.error("BridgeVZAdapter: start_vm_manager(): command = %s, \ ERROR = %s" % (command, str(e))) return False