def copy_lab_source(self, vm_id, lab_repo_name, git_clone_loc): directories = git_clone_loc.split("/") labs_dir = directories[-2] src_dir = None if base_config.ADS_ON_CONTAINER: src_dir = "%s%s%s%s%s%s" % (base_config.VM_ROOT_DIR, base_config.ADS_SERVER_VM_ID, base_config.VM_DEST_DIR, labs_dir, "/", lab_repo_name) else: src_dir = "%s%s%s%s" % (base_config.VM_DEST_DIR, labs_dir, "/", lab_repo_name) dest_dir = "%s%s%s" % (base_config.VM_ROOT_DIR, vm_id, base_config.VM_DEST_DIR + "labs") logger.debug("vm_id = %s, src_dir=%s, dest_dir=%s" % (vm_id, src_dir, dest_dir)) try: return self.copy_files(src_dir, dest_dir) except Exception, e: logger.error("ERROR = %s" % str(e)) return False
def create_vm(self, lab_spec): # vm_spec is a json string # Allocate a vm_id: not required as platform adapter will allocate it. # Invoke platform adapter server (POST) logger.debug("VMPool: create_vm(); poolID=%s, Desciption=%s" "AdapterIP=%s, AdapterPort=%s, CreatePath=%s" "DestroyPath=%s" % (self.vmpool_id, self.vm_description, self.adapter_ip, self.adapter_port, self.create_path, self.destroy_path)) adapter_url = "%s:%s%s" % (self.adapter_ip, self.adapter_port, self.create_path) payload = {'lab_spec': json.dumps(lab_spec)} logger.debug("VMPool: create_vm(); adapter_url = %s, payload = %s" % (adapter_url, str(payload))) try: result = requests.post(url=adapter_url, data=payload) logger.debug("VMPool: create_vm(): Response text from adapter: " + result.text) if result.status_code == requests.codes.ok: vm_id = result.json()["vm_id"] vm_ip = result.json()["vm_ip"] vm_port = result.json()["vm_port"] return self.construct_state(lab_spec, vm_id, vm_ip, vm_port) else: raise Exception("VMPool: create_vm(): Error creating VM: " + result.text) except Exception, e: logger.error("VMPool: create_vm(): Error communicating with" + "adapter: " + str(e)) raise Exception("VMPool: create_vm(): Error creating VM: " + str(e))
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 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 test_get_lab_reqs(): lab_src_url = "https://github.com/Virtual-Labs/computer-programming-iiith.git" labmgr = LabManager() try: lab_spec = labmgr.get_lab_reqs(lab_src_url, version=None) logger.debug("Lab spec: %s" % str(lab_spec)) except Exception, e: logger.error("Test failed with error: " + str(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 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 get_lab_spec(self, repo_name): spec_file_path = self.get_spec_path(repo_name) + self.lab_spec_file logger.debug("spec_file_path: %s" % spec_file_path) if not os.path.exists(spec_file_path): logger.error("Lab spec file not found") raise LabSpecInvalid("Lab spec file not found") try: return json.loads(open(spec_file_path).read()) except Exception, e: logger.error("Lab spec JSON invalid: " + str(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 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 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 test_labmgr_test_lab(): vmmgrurl = "http://172.16.0.2" lab_src_url = \ "https://github.com/Virtual-Labs/computer-programming-iiith.git" port = "9089" revision_tag = None labmgr = LabManager() try: (ret_val, ret_str) = labmgr.test_lab(vmmgrurl, port, lab_src_url, revision_tag) except Exception, e: logger.error("test_lab(); Test failed with error: " + str(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 get_used_pools(self, lab_id): used_pools = [] logger.debug("VMPoolManager: get_used_pools()") deployment_record = self.state.get_record(lab_id) logger.debug("Deployment Record = %s" % deployment_record) if deployment_record is not None: # Currently there is only pool where the lab is deployed used_pools.append(deployment_record['vmpool_info']['vmpool_id']) return used_pools else: logger.error("No record found with the lab id = %s" % lab_id) raise RecordNotFoundError(lab_id)
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 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 execute_command(cmd): logger.debug("command: %s" % cmd) return_code = -1 output = None try: output = subprocess.check_output(cmd, shell=True) return_code = 0 except subprocess.CalledProcessError as cpe: logger.error("Called Process Error Message: %s" % cpe.output) raise cpe except OSError as ose: logger.error("OSError: %s" % ose.output) raise ose return (return_code, output)
def _copy_lab_source(self, ip_addr, lab_repo_name): src_dir = str(self.git.git_clone_loc) + "/" + lab_repo_name dest_dir = "{0}@{1}:{2}labs/".format(self.VM_USER, ip_addr, base_config.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 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 copy_ovpl_source(self, vm_id): src_dir = "%s%s%s" % (base_config.VM_ROOT_DIR, base_config.ADS_SERVER_VM_ID, base_adapter.OVPL_DIR_PATH) dest_dir = "%s%s%s" % (base_config.VM_ROOT_DIR, vm_id, base_config.VM_DEST_DIR) logger.debug("vm_id = %s, src_dir=%s, dest_dir=%s" % (vm_id, src_dir, dest_dir)) try: return self.copy_files(str(src_dir), str(dest_dir)) except Exception, e: logger.error("ERROR = %s" % 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 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 _copy_ovpl_source(self, ip_addr): # env = EnvSetUp() src_dir = base_adapter.OVPL_DIR_PATH dest_dir = "{0}@{1}:{2}".format(self.VM_USER, ip_addr, base_config.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 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 get_lab_reqs(self, lab_src_url, version=None): logger.debug("Will return lab spec") try: repo_name = self.git.construct_repo_name(lab_src_url) if version is None: version = "master" if self.git.repo_exists(repo_name): self.git.reset_repo(repo_name) self.git.checkout_version(repo_name, version) self.git.pull_repo(repo_name) else: self.git.clone_repo(lab_src_url, repo_name) self.git.checkout_version(repo_name, version) return self.git.get_lab_spec(repo_name) except Exception, e: logger.error("Error: %s" % str(e)) raise e
class CentOSBridgeVZAdapter(object): time_before_next_retry = None git = None env = None def __init__(self): self.env = EnvSetUp.Instance() self.git = GitCommands() self.time_before_next_retry = 5 """ Every newly created container needs to be set with IP_ADDRESS in the interfaces file. This acheived by modifying the x.x.x.x variable in the template file with the container IP_ADDRESS and then copying it to /etc/network/interfaces. """ 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)) text_to_search = 'x.x.x.x' text_to_replace = IP_ADDRESS file_to_search = dest_file fd = open(file_to_search, 'r+') for line in fileinput.input(file_to_search): fd.write(line.replace(text_to_search, text_to_replace)) fd.close() src_file = "/vz/private/" + base_config.ADS_SERVER_VM_ID + \ base_adapter.OVPL_DIR_PATH + config.BRIDGE_NETWORK_SETUP_PATH + \ "interfaces" dest_file = "/vz/private/" + vm_id + "/etc/network/interfaces" logger.debug("vm_id = %s, src_file=%s, dest_file=%s" % (vm_id, src_file, dest_file)) try: return self.copy_files(str(src_file), str(dest_file)) except Exception, e: logger.error("ERROR = %s" % str(e)) return False
def destroy_vm(self, vm_id): # Invoke platform adapter # Delete entry from VMs list logger.debug("VMPool.destroy_vm()") adapter_url = "%s:%s:%s" % (self.adapter_ip, self.adapter_port, self.destrpy_path) payload = {'vm_id': vm_id} try: result = requests.post(url=adapter_url, data=payload) logger.debug("Response text from adapter: " + result.text) if (result.status_code == requests.codes.ok and "Success" in result.text): logger.debug("VMPool.destroy_vm()") return True else: logger.error("Error destroying vm: " + result.text) except Exception, e: logger.error("Error communicating with adapter: " + str(e))
def get_lab_reqs(self, lab_src_url, version=None): logger.debug("Will return lab spec") try: repo_name = self.git.construct_repo_name(lab_src_url) if version is None: version = "master" if self.git.repo_exists(repo_name): self.git.reset_repo(repo_name) self.git.checkout_version(repo_name, version) self.git.pull_repo(repo_name, version) else: self.git.clone_repo(lab_src_url, repo_name) self.git.checkout_version(repo_name, version) return self.git.get_lab_spec(repo_name) except Exception, e: logger.error("Error: %s" % str(e)) raise e
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 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 _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 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