def add_new_fleet_service_instance(self, fleet_service_name): if not isinstance(fleet_service_name, str): raise IllegalArgumentError("Error: Argument <fleet_service_name> not of type <str>") if fleet_service_name in self.FleetServiceDict: fleet_service = self.FleetServiceDict[fleet_service_name] new_fleet_service_instance = self.FleetServiceHandler.create_new_fleet_service_instance(fleet_service) if new_fleet_service_instance is not None: try: self.FleetServiceHandler.start(fleet_service, new_fleet_service_instance) except FleetSubmissionError as submissionError: self.FleetServiceHandler.remove_fleet_service_instance(fleet_service, new_fleet_service_instance.name, False) raise submissionError # save the updated fleet service into etcd (this is mainly done here to save the updated used_port_numbers # into etcd so that when dynamite restarts it handles those correctly self.save_fleet_service_state_to_etcd(fleet_service) fleet_service_instance_dict = new_fleet_service_instance.to_dict() fleet_service_instance_json = json.dumps(fleet_service_instance_dict) fleet_service_instance_name = fleet_service_instance_dict['name'] etcd_instance_key = ETCDCTL.etcd_key_running_services + "/" + fleet_service.name + "/" + fleet_service_instance_name etcdctl = ETCDCTL.get_etcdctl() etcdctl.write(etcd_instance_key, fleet_service_instance_json) return new_fleet_service_instance return None
def save_fleet_service_state_to_etcd(self, fleet_service): etcdctl = ETCDCTL.get_etcdctl() etcd_key = ETCDCTL.etcd_key_running_services + "/" + fleet_service.name + "/" + ETCDCTL.etcd_name_fleet_service_template fleet_service_dict = fleet_service.to_dict() fleet_service_dict['fleet_service_instances'] = {} fleet_service_dict_json = json.dumps(fleet_service_dict) etcdctl.write(etcd_key, fleet_service_dict_json)
def remove_fleet_service_instance(self, fleet_service_name): fleet_service_handler = self.FleetServiceHandler fleet_service_dict = self.FleetServiceDict for service_name, fleet_service in fleet_service_dict.items(): if fleet_service.name == fleet_service_name: name_of_deleted_fleet_service = fleet_service_handler.remove_fleet_service_instance(fleet_service) self.save_fleet_service_state_to_etcd(fleet_service) # TODO remove the deleted fleet service instance from etcd if name_of_deleted_fleet_service is not None: etcd_instance_key = ETCDCTL.etcd_key_running_services + "/" + fleet_service.name + "/" + name_of_deleted_fleet_service etcdctl = ETCDCTL.get_etcdctl() etcdctl.delete(etcd_instance_key) return None
def remove_fleet_service_instance(self, fleet_service_name, fleet_service_instance_name=None): fleet_service_handler = self.FleetServiceHandler fleet_service_dict = self.FleetServiceDict for service_name, fleet_service in fleet_service_dict.items(): if fleet_service.name == fleet_service_name: try: name_of_deleted_fleet_service = fleet_service_handler.remove_fleet_service_instance(fleet_service, fleet_service_instance_name) except requests.exceptions.HTTPError: self._logger.exception("Error removing service instance!") return False self.save_fleet_service_state_to_etcd(fleet_service) # remove the deleted fleet service instance from etcd if name_of_deleted_fleet_service is not None: etcd_instance_key = ETCDCTL.etcd_key_running_services + "/" + fleet_service.name + "/" + name_of_deleted_fleet_service etcdctl = ETCDCTL.get_etcdctl() etcdctl.delete(etcd_instance_key) return True return None