def copy_vm_manager_files(vm_id): logger.debug("copy_vm_manager_files(): vm_id = %s" % vm_id) current_file_path = os.path.dirname(os.path.abspath(__file__)) src_dir = current_file_path + VM_MANAGER_SRC_DIR dest_dir = "%s%s%s" % (VM_ROOT_DIR, vm_id, VM_MANAGER_DEST_DIR) logger.debug("copy_vm_manager_files(): dest_dir = %s, src_dir = %s" % (dest_dir, src_dir))
def update_lab_spec(self, lab_spec, lab_id, lab_src_url, revision_tag): lab_spec['lab']['description']['id'] = lab_spec['lab_id'] = lab_id lab_spec['lab_src_url'] = lab_src_url lab_spec['lab_repo_name'] = git_commands.construct_repo_name(lab_src_url) lab_spec['revision_tag'] = revision_tag lab_spec['lab']['runtime_requirements']['hosting'] = 'dedicated' logger.debug("lab_repo_name: %s" %(lab_spec['lab_repo_name']))
def _find_ec2_ami(self, os, os_version): """ Find a suitable AMI from the list of supported AMIs from the given OS and OS version. If not a suitable OS is found, raise appropriate Exception """ supported_amis = config.supported_amis if os == "" or os_version == "": raise AMINotFound('No corresponding AMI for the given OS found') # sanitize input os = os.strip().upper() os_version = os_version.strip() if os == 'UBUNTU' and os_version == '12': os_version = '12.04' if os == 'UBUNTU' and os_version == '14': os_version = '14.04' # filter the supported_amis list by the os and the by the version filtered_os = filter(lambda x: x['os'] == os, supported_amis) chosen_ami = filter(lambda x: x['version'] == os_version, filtered_os) if not chosen_ami or not len(chosen_ami): raise AMINotFound('No corresponding AMI for the given OS found') # chose the item; there should be only one. chosen_ami = chosen_ami[0] logger.debug("Choosing AMI: %s; based on input OS: %s, version: %s" % (chosen_ami, os, os_version)) return chosen_ami['ami_id']
def copy_public_key(vm_id): try: if settings.ADS_ON_CONTAINER: public_key_file = ("%s%s%s%s" % (settings.VM_ROOT_DIR, settings.ADS_SERVER_VM_ID, settings.VM_DEST_DIR, ".ssh/id_rsa.pub")) else: public_key_file = ("%s" % ("/root/.ssh/id_rsa.pub")) authorized_key_file = ("%s%s%s%s" % (settings.VM_ROOT_DIR, vm_id, settings.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"' % (settings.NO_STRICT_CHECKING, settings.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 create_vm(self, lab_spec, vm_id=""): logger.debug("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 = find_available_ip() # m = re.match(r'[0-9]+.[0-9]+.([0-9]+).([0-9]+)', ip_address) # if m != None: # vm_id = m.group(1) + m.group(2) # else: # ip_address = None # set the ip address to 127.0.0.1 (loopback) ip_address = "127.0.0.1" vm_id = 127 vm_id = validate_vm_id(vm_id) (vm_create_args, vm_set_args) = construct_vzctl_args(lab_spec) logger.debug("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)) # setup IP ADRESS link so that we can map IP addresses to vm id's later IP_ADDRESSES[str(vm_id)] = ip_address return vm_id
def destroy_vm(self, vm_id): vm_id = validate_vm_id(vm_id) try: command = (r'ssh -o "%s" %s "%s stop %s"' % (settings.NO_STRICT_CHECKING, settings.BASE_IP_ADDRESS, VZCTL, vm_id)) logger.debug("CentOSVZAdapter: 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"' % (settings.NO_STRICT_CHECKING, settings.BASE_IP_ADDRESS, VZCTL, vm_id)) logger.debug("CentOSVZAdapter: 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 get_instance(self, vm_id): logger.debug("AWSAdapter: get_instance(): vm_id: %s" % (vm_id)) reservations = self.connection.get_all_instances(instance_ids=[vm_id]) instance = reservations[0].instances[0] return instance
def get_vm_ip(self, vm_id): logger.debug("AWSAdapter: get_vm_ip(): vm_id: %s" % (vm_id)) instance = self.get_instance(vm_id) logger.debug("AWSAdapter: IP address of the instance is: %s" % instance.private_ip_address) return instance.private_ip_address
def pull_repo(repo_name): pull_cmd = "git --git-dir=%s/.git pull" % (GIT_CLONE_LOC + repo_name) 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 checkout_version(repo_name, version=None): if version: checkout_cmd = "git --git-dir=%s checkout %s" \ % ((GIT_CLONE_LOC + repo_name), 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(repo_name): reset_cmd = "git --git-dir=%s/.git reset --hard" % (GIT_CLONE_LOC + repo_name) 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 create_vm(self, lab_spec, vm_id=""): logger.debug("CentOSVZAdapter: 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 = BaseAdapter.find_available_ip() m = re.match(r'[0-9]+.[0-9]+.([0-9]+).([0-9]+)', ip_address) if m != None: # vm_id = str((int(m.group(1) + m.group(2)) + 10)) vm_id = m.group(1) + m.group(2) else: ip_address = None vm_id = validate_vm_id(vm_id) (vm_create_args, vm_set_args) = construct_vzctl_args(lab_spec) logger.debug("CentOSVZAdapter: 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"' % (settings.NO_STRICT_CHECKING, settings.BASE_IP_ADDRESS, VZCTL, vm_id, vm_create_args)) logger.debug("CentOSVZAdapter: 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"' % (settings.NO_STRICT_CHECKING, settings.BASE_IP_ADDRESS, VZCTL, vm_id)) logger.debug("CentOSVZAdapter: 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"' % (settings.NO_STRICT_CHECKING, settings.BASE_IP_ADDRESS, VZCTL, vm_id, vm_set_args)) logger.debug("CentOSVZAdapter: 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(repo_name): spec_file_path = get_spec_path(repo_name) + 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)) raise LabSpecInvalid("Lab spec JSON invalid: " + str(e))
def copy_ovpl_source(vm_id): src_dir = "%s%s%s%s" % (settings.VM_ROOT_DIR, settings.ADS_SERVER_VM_ID, settings.VM_DEST_DIR, "ovpl") dest_dir = "%s%s%s" % (settings.VM_ROOT_DIR, vm_id, settings.VM_DEST_DIR) logger.debug("vm_id = %s, src_dir=%s, dest_dir=%s" % (vm_id, src_dir, dest_dir)) try: return copy_files(str(src_dir), str(dest_dir)) except Exception, e: logger.error("ERROR = %s" % str(e)) return False
def restart_vm(self, vm_id): vm_id = validate_vm_id(vm_id) try: command = (r'ssh -o "%s" %s "%s restart %s"' % (settings.NO_STRICT_CHECKING, settings.BASE_IP_ADDRESS, VZCTL, vm_id)) logger.debug("CentOSVZAdapter: restart_vm(): restart command = %s" % command) (ret_code,output) = execute_command(command) except Exception, e: raise e
def init_vm(self, vm_id, lab_repo_name): logger.debug("CentOSVZAdapter: init_vm(): vm_id = %s" % vm_id) success = True success = success and copy_public_key(vm_id) success = success and copy_ovpl_source(vm_id) success = success and copy_lab_source(vm_id, lab_repo_name) success = success and self.start_vm_manager(vm_id) # Return the VM's IP and port info response = {"vm_id": vm_id, "vm_ip": get_vm_ip(vm_id), "vmm_port": settings.VM_MANAGER_PORT} logger.debug("CentOSVZAdapter: init_vm(): success = %s, response = %s" % (success, response)) return (success, response)
def __init__(self, vmpool_id, vm_description, adapter_ip, adapter_port, create_path, destroy_path): logger.debug("VMPool: __init__(); poolID=%s, Desciption=%s, AdapterIP=%s, AdapterPort=%s, CreatePath=%s, DestroyPath=%s" % \ (vmpool_id, vm_description, adapter_ip, adapter_port, create_path, destroy_path)) self.system = State.Instance() #self.vms = [] # List of VMProxy objects self.vmpool_id = vmpool_id self.vm_description = vm_description self.adapter_ip = adapter_ip self.adapter_port = adapter_port self.create_path = create_path self.destroy_path = destroy_path
def create_vm_id(vm_id): """If no vm_id is specified, it is computed using the last two segments""" """of an available IP address; vm_spec is an object """ logger.debug("create_vm_id(): vm_id = %s" % vm_id) if vm_id == "": global IP_ADDRESS IP_ADDRESS = BaseAdapter.find_available_ip() m = re.match(r'[0-9]+.[0-9]+.([0-9]+).([0-9]+)', IP_ADDRESS) if m != None: vm_id = str((int(m.group(1) + m.group(2)) + 100)) else: vm_id = validate_vm_id(vm_id) logger.debug("create_vm_id(): vm_id = %s" % vm_id) return vm_id
def test_lab(self, lab_id, lab_src_url, revision_tag=None): logger.debug("test_lab() for lab ID %s and git url %s" \ % (lab_id, lab_src_url)) try: lab_spec = LabManager.get_lab_reqs(lab_src_url, revision_tag) self.update_lab_spec(lab_spec, lab_id, lab_src_url, revision_tag) if lab_spec['lab']['runtime_requirements']['hosting'] == 'dedicated': """ TODO: Undeploy , fnd proper place to invoke undeploy""" # self.undeploy_lab(lab_id) vmpoolmgr = VMPoolManager.VMPoolManager() logger.debug("test_lab(); invoking create_vm() on vmpoolmgr") lab_state = vmpoolmgr.create_vm(lab_spec) logger.debug("test_lab(): Returned from VMPool = %s" % (str(lab_state))) ip = lab_state['vm_info']['vm_ip'] port = lab_state['vm_info']['vmm_port'] vmmgrurl = "http://" + ip logger.debug("test_lab(): vmmgrurl = %s" % (vmmgrurl)) try: (ret_val, ret_str) = LabManager.test_lab(vmmgrurl, port, lab_src_url, revision_tag) if(ret_val): self.update_state(lab_state) logger.info("test_lab(): test succcessful") return ip else: logger.error("test_lab(); Test failed with error:" + str(ret_str)) return "Test failed: See log file for errors" except Exception, e: logger.error("test_lab(); Test failed with error: " + str(e)) return "Test failed: See log file for errors" """ TODO: Garbage collection clean up for the created VM """ finally: self.system.save()
def create_vm(self, lab_spec, vm_id=""): logger.debug("BridgeVZAdapter: create_vm()") vm_id = create_vm_id(vm_id) (vm_create_args, vm_set_args) = construct_vzctl_args(lab_spec) logger.debug("BridgeVZAdapter: 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)) success = True success = success and self.create_container(vm_id, vm_create_args) success = success and self.set_container_params(vm_id, vm_set_args) success = success and self.start_container(vm_id) return (success, vm_id)
def post(self): if not self.current_user: self.redirect('/login') return post_data = dict(urlparse.parse_qsl(self.request.body)) c = Controller.Controller() # log the user who is deploying the lab.. logger.debug("Lab Deployment: deployed by: %s, lab id: %s, URL: %s" % (self.current_user, post_data['lab_id'], post_data['lab_src_url'])) self.write(c.test_lab(post_data['lab_id'], post_data['lab_src_url'], post_data.get('version', None)))
def stop_vm(self, vm_id): vm_id = validate_vm_id(vm_id) try: command = (r'ssh -o "%s" %s "%s stop %s"' % (settings.NO_STRICT_CHECKING, settings.BASE_IP_ADDRESS, VZCTL, vm_id)) logger.debug("BridgeVZAdapter: 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 get_available_pool(self, lab_spec): """Imagining four VMPools: 0. For Linux VMs in private data center (uses OpenVZ) 1. For Amazon S3 (static content hosting) 2. For Windows VMs in private data center (uses KVM) 3. For Amazon EC2 """ logger.debug("VMPoolManager: get_available_pool()") if self.is_lab_static(lab_spec): return self.VMPools[1] elif self.lab_on_windows(lab_spec): return self.VMPools[2] else: return self.VMPools[0]
def _copy_lab_source(self, ip_addr, lab_repo_name): src_dir = GIT_CLONE_LOC[:-1] + "/" + lab_repo_name dest_dir = "{0}@{1}:{2}labs/".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 is_service_up(self, vm_ip, port): logger.debug("AWSAdapter: is_service_up(): VM IP: %s" % vm_ip) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: logger.debug("AWSAdapter: trying to connect to port: %s of: %s" % (port, vm_ip)) s.connect((vm_ip, port)) logger.debug("AWSAdapter: VM %s: port: %s is up.." % (vm_ip, port)) return True except socket.error as e: logger.debug("AWSAdapter: VM %s: Error connecting to port: %s: %s" % (vm_ip, port, e)) logger.debug("AWSAdapter: retrying to reach port %s.." % port) s.close() return False
def set_container_params(self, vm_id, vm_set_args): try: command = (r'ssh -o "%s" %s "%s set %s %s"' % (settings.NO_STRICT_CHECKING, settings.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 start_container(self, vm_id): try: command = (r'ssh -o "%s" %s "%s start %s"' % (settings.NO_STRICT_CHECKING, settings.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, 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 execute_command(cmd): e = EnvSetUp() 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: %s" % cpe) raise cpe except OSError as ose: logger.error("OSError: %s" % ose) raise ose return (return_code, output)
def get_lab_reqs(lab_src_url, version=None): e = EnvSetUp() logger.debug("Will return lab spec") try: repo_name = construct_repo_name(lab_src_url) if repo_exists(repo_name): # reset_repo(repo_name) pull_repo(repo_name) else: clone_repo(lab_src_url, repo_name) checkout_version(repo_name, version) return get_lab_spec(repo_name) except Exception, e: logger.error("Error: %s" % str(e)) raise e
def test_logging(self): logger.debug("test_logging()")