def __generate_sliver_urn(self, vm, slice_leaf=None): if slice_leaf: return hrn_to_urn( self.__config.CM_HRN + "." + slice_leaf + "." + str(vm.name), "sliver") else: return hrn_to_urn(self.__config.CM_HRN + "." + str(vm.name), "sliver")
def run(self): while True: print "Background VM Expiration Thread starting..." expirations = ExpiringComponents.objects.all() try: for e in expirations: exp_date = re.sub(r'[\+].+', "", e.expires) exp_date = exp_date.replace("Z", "").replace("T", " ") try: exp_time = datetime.strptime(exp_date, '%Y-%m-%d %H:%M:%S.%f') except: exp_time = datetime.strptime(exp_date, '%Y-%m-%d %H:%M:%S') current_time = datetime.utcnow() time_to_expire = current_time - exp_time if exp_time < current_time: vms = VirtualMachine.objects.filter( sliceName=e.slice, projectName=e.authority) for vm in vms: try: sliver_urn = hrn_to_urn( e.slice + "." + vm.name, "sliver") print "VM (urn=%s) has expired on time=%s. Proceeding to its deletion" % \ (sliver_urn, e.expires) self.vt_driver.delete_vm(sliver_urn) except: pass e.delete() # If one hour or less is left for expiring, notify it to experimenter elif time_to_expire.days == 0 and time_to_expire.seconds <= 60 * 60: vms = VirtualMachine.objects.filter( sliceName=e.slice, projectName=e.authority) slice_urn = hrn_to_urn(e.slice, "slice") for vm in vms: sliver_urn = hrn_to_urn(e.slice + "." + vm.name, "sliver") send_mail( settings.ISLAND_NAME + ": sliver expiring at %s island" % settings.ISLAND_NAME, "Sliver %s from slice %s, is going to expire in approximately 1 hour.\n\nPlease renew it or delete / let it expire if you are not using it anymore." % (sliver_urn, slice_urn), from_email=settings.DEFAULT_FROM_EMAIL, # TODO Should be set to the maintainer's e-mail (maybe getting that from creds?) recipient_list=[settings.ROOT_EMAIL], ) except Exception as e: print "Expiratio Monitoring failed, cause:", str(e) time.sleep(self.period)
def __convert_to_resources_with_slivers(self, server, vms, expiration=None): """ Always return a list of slivers, independently of the number of resources inside. """ resource = self.__convert_to_resource(server) resources = list() for vm in vms: if not expiration: try: expiration = ExpiringComponents.objects.filter(slice=vm.sliceName, authority=vm.projectName)[0].expires except Exception as e: print e new_resource = copy.deepcopy(resource) new_resource.set_id(vm.name) sliver = Sliver() sliver.set_type("VM") sliver.set_expiration(expiration) sliver.set_client_id(vm.name) sliver.set_urn(self.__generate_sliver_urn(vm, server.name + "." + vm.sliceName.split(".")[-1])) sliver.set_slice_urn(hrn_to_urn(vm.sliceName, "slice")) if isinstance(vm, VirtualMachine): sliver.set_services(self.__generate_vt_am_services(vm)) sliver.set_allocation_status(self.__translate_to_allocation_state(vm)) sliver.set_operational_status(self.__translate_to_operational_state(vm)) sliver.set_expiration(expiration) new_resource.set_sliver(sliver) resources.append(copy.deepcopy(new_resource)) return resources
def reserve_vms(self, slice_urn, reservation, expiration=None, users=list()): # URNs of foreign RMs are not served current_cm_hrn = self.__config.CM_HRN cm_id = getattr(reservation, "get_component_manager_id") if callable(cm_id): cm_id = urn_to_hrn(cm_id())[0] if current_cm_hrn != cm_id: # No reservation is provided for URNs of other CMs return None # VMs are dynamic resource -> no collision will happen slice_hrn, hrn_type = urn_to_hrn(slice_urn) if not reservation.get_component_id() == None: server_hrn, hrn_type = urn_to_hrn(reservation.get_component_id()) server_name = server_hrn.split(".")[-1] VTAMDriver.logger.debug("server_name = " + server_name + ", server_hrn = " + server_hrn) else: server_name = self.__get_best_server() server_hrn = self.__config.CM_HRN + "." + server_name VTAMDriver.logger.debug("server_name = " + server_name + ", server_hrn = " + server_hrn) server = VTServer.objects.get(name=server_name).getChildObject() server_id = server.id if reservation.get_id(): if Reservation.objects.filter(sliceName=slice_hrn, projectName=slice_hrn, name=reservation.get_id()) or VirtualMachine.objects.filter(sliceName=slice_hrn, projectName=slice_hrn, name=reservation.get_id()): raise Exception("There is another VM with client id %s on this slice already <GENI PROVISIONED> or <GENI ALLOCATED>" %reservation.get_id()) reservation_name = reservation.get_id() else: reservation_name = str(random.randint(0,1000*1000)) if expiration == None: expiration = datetime.utcnow() + timedelta(hours=168) reserved_vm = Reservation() #reserved_vm.reservation_id = random.randint(0,1000) reserved_vm.server_id = server_id reserved_vm.set_slice_name(slice_hrn) reserved_vm.set_project_name(slice_hrn) reserved_vm.set_name(reservation_name) reserved_vm.set_valid_until(str(expiration)) reserved_vm.uuid = str(uuid.uuid4()) reserved_vm.set_disk_image(reservation.get_disk_image()) reserved_vm.save() if not reservation.get_sliver(): reservation.set_sliver(Sliver()) # Set information for sliver sliver_urn = hrn_to_urn(server_hrn+"." + slice_hrn.split(".")[-1] + "." +str(reservation_name), "sliver") VTAMDriver.logger.debug("XXX sliver_urn = " + sliver_urn) reservation.get_sliver().set_urn(sliver_urn) reservation.get_sliver().set_allocation_status(self.GENI_ALLOCATED) reservation.get_sliver().set_expiration(expiration) reservation.get_sliver().set_operational_status(self.GENI_NOT_READY) reservation.get_sliver().set_client_id(reservation_name) reservation.set_allocation_status = self.GENI_ALLOCATED #vm_params = self.__urn_to_vm_params(slice_urn) #self.__store_user_keys(users, vm_params) return reservation
def run(self): while True: print "Background VM Expiration Thread starting..." expirations = ExpiringComponents.objects.all() try: for e in expirations: exp_date = re.sub(r'[\+].+', "", e.expires) exp_date = exp_date.replace("Z", "").replace("T", " ") try: exp_time = datetime.strptime(exp_date, '%Y-%m-%d %H:%M:%S.%f') except: exp_time = datetime.strptime(exp_date, '%Y-%m-%d %H:%M:%S') current_time = datetime.utcnow() time_to_expire = current_time - exp_time if exp_time < current_time: vms = VirtualMachine.objects.filter(sliceName=e.slice, projectName=e.authority) for vm in vms: try: sliver_urn = hrn_to_urn(e.slice + "." + vm.name, "sliver") print "VM (urn=%s) has expired on time=%s. Proceeding to its deletion" % \ (sliver_urn, e.expires) self.vt_driver.delete_vm(sliver_urn) except: pass e.delete() # If one hour or less is left for expiring, notify it to experimenter elif time_to_expire.days == 0 and time_to_expire.seconds <= 60*60: vms = VirtualMachine.objects.filter(sliceName=e.slice, projectName=e.authority) slice_urn = hrn_to_urn(e.slice, "slice") for vm in vms: sliver_urn = hrn_to_urn(e.slice + "." + vm.name, "sliver") send_mail( settings.ISLAND_NAME + ": sliver expiring at %s island" % settings.ISLAND_NAME, "Sliver %s from slice %s, is going to expire in approximately 1 hour.\n\nPlease renew it or delete / let it expire if you are not using it anymore." % (sliver_urn, slice_urn), from_email=settings.DEFAULT_FROM_EMAIL, # TODO Should be set to the maintainer's e-mail (maybe getting that from creds?) recipient_list=[settings.ROOT_EMAIL], ) except Exception as e: print "Expiratio Monitoring failed, cause:", str(e) time.sleep(self.period)
def __convert_to_resources_with_slivers(self, server, vms, expiration=None): """ Always return a list of slivers, independently of the number of resources inside. """ resource = self.__convert_to_resource(server) resources = list() for vm in vms: if not expiration: try: expiration = ExpiringComponents.objects.filter( slice=vm.sliceName, authority=vm.projectName)[0].expires except Exception as e: print e new_resource = copy.deepcopy(resource) new_resource.set_id(vm.name) sliver = Sliver() sliver.set_type("VM") sliver.set_expiration(expiration) sliver.set_client_id(vm.name) sliver.set_urn( self.__generate_sliver_urn( vm, server.name + "." + vm.sliceName.split(".")[-1])) sliver.set_slice_urn(hrn_to_urn(vm.sliceName, "slice")) if isinstance(vm, VirtualMachine): sliver.set_services(self.__generate_vt_am_services(vm)) sliver.set_allocation_status( self.__translate_to_allocation_state(vm)) sliver.set_operational_status( self.__translate_to_operational_state(vm)) sliver.set_expiration(expiration) new_resource.set_sliver(sliver) resources.append(copy.deepcopy(new_resource)) return resources
def test_should_get_hrn_sliver(self): sliver_urn = hrn_to_urn(self.sliver_hrn, self.sliver_type) self.assertEquals(self.sliver_urn_template, sliver_urn)
def test_should_get_hrn_authority(self): authority_urn = hrn_to_urn(self.authority_hrn, self.authority_type) self.assertEquals(self.authority_urn_template, authority_urn)
def generate_sliver_urn(self, vm, slice_leaf=None): if slice_leaf: return hrn_to_urn(self.__config.CM_HRN + "." + slice_leaf + "." + str(vm.name), "sliver") else: return hrn_to_urn(self.__config.CM_HRN + "." + str(vm.name), "sliver")
def __generate_component_name(self, server): return hrn_to_urn(self.__config.CM_HRN+"."+str(server.name),"node")
def __generate_component_manager_name(self, server): return hrn_to_urn(self.__config.CM_HRN, "authority+cm")
def reserve_vms(self, slice_urn, reservation, expiration=None, users=list()): # URNs of foreign RMs are not served current_cm_hrn = self.__config.CM_HRN cm_id = getattr(reservation, "get_component_manager_id") if callable(cm_id): cm_id = urn_to_hrn(cm_id())[0] if current_cm_hrn != cm_id: # No reservation is provided for URNs of other CMs return None # VMs are dynamic resource -> no collision will happen slice_hrn, hrn_type = urn_to_hrn(slice_urn) if not reservation.get_component_id() == None: server_hrn, hrn_type = urn_to_hrn(reservation.get_component_id()) server_name = server_hrn.split(".")[-1] else: server_name = self.__get_best_server() server_hrn = self.__config.CM_HRN + "." + server_name server = VTServer.objects.get(name=server_name).getChildObject() server_id = server.id if reservation.get_id(): if Reservation.objects.filter(sliceName=slice_hrn, projectName=slice_hrn, name=reservation.get_id() ) or VirtualMachine.objects.filter( sliceName=slice_hrn, projectName=slice_hrn, name=reservation.get_id()): raise Exception( "There is another VM with client id %s on this slice already <GENI PROVISIONED> or <GENI ALLOCATED>" % reservation.get_id()) reservation_name = reservation.get_id() else: reservation_name = str(random.randint(0, 1000 * 1000)) if expiration == None: expiration = datetime.utcnow() + timedelta(hours=1) reserved_vm = Reservation() #reserved_vm.reservation_id = random.randint(0,1000) reserved_vm.server_id = server_id reserved_vm.set_slice_name(slice_hrn) reserved_vm.set_project_name(slice_hrn) reserved_vm.set_name(reservation_name) reserved_vm.set_valid_until(str(expiration)) reserved_vm.uuid = str(uuid.uuid4()) reserved_vm.save() if not reservation.get_sliver(): reservation.set_sliver(Sliver()) # Set information for sliver reservation.get_sliver().set_urn( hrn_to_urn( server_hrn + "." + slice_hrn.split(".")[-1] + "." + str(reservation_name), "sliver")) reservation.get_sliver().set_allocation_status(self.GENI_ALLOCATED) reservation.get_sliver().set_expiration(expiration) reservation.get_sliver().set_operational_status(self.GENI_NOT_READY) reservation.get_sliver().set_client_id(reservation_name) reservation.set_allocation_status = self.GENI_ALLOCATED #vm_params = self.__urn_to_vm_params(slice_urn) #self.__store_user_keys(users, vm_params) return reservation
def test_should_get_hrn_node(self): node_urn = hrn_to_urn(self.node_hrn, self.node_type) self.assertEquals(self.node_urn_template, node_urn)
def __get_link_urn(self, source, dst, server_name): name = self.__correct_iface_name(source) return hrn_to_urn( self.__config.CM_HRN + "." + str(server_name) + "." + str(name) + "-" + str(dst), "link")
def __get_foreign_urn(self, dpid): return hrn_to_urn(self.__config.FOREIGN_HRN + "." + str(dpid), "datapath")
def __generate_component_name(self, server): return hrn_to_urn(self.__config.CM_HRN + "." + str(server.name), "node")
def __get_port_urn(self, port, server_name): name = self.__correct_iface_name(port) # return hrn_to_urn(self.__config.CM_HRN + "." + str(server_name) + "." + str(name),"interface") return hrn_to_urn(self.__config.CM_HRN + "." + str(server_name), "node") + "+interface+" + str(name)
def test_should_get_hrn_component_manager(self): component_urn = hrn_to_urn(self.component_hrn, self.component_type) self.assertEquals(self.component_urn_template, component_urn)
def __get_link_urn(self, source, dst, server_name): name = self.__correct_iface_name(source) return hrn_to_urn(self.__config.CM_HRN + "." + str(server_name)+ "." + str(name) + "-" + str(dst),"link")
def __get_foreign_urn(self, dpid): return hrn_to_urn(self.__config.FOREIGN_HRN + "." + str(dpid),"datapath")