def __filter_by_swarm(service_instance): try: LOG.debug( "[lifecycle.modules.agent_decision] [filter_by_swarm] Checking if agents support Docker Swarm ..." ) l_filtered_agents = [] try: for agent in service_instance["agents"]: if __check_swarm(agent): LOG.debug( "[lifecycle.modules.agent_decision] [filter_by_swarm] Docker Swarm supported by agent: " + str(agent)) l_filtered_agents.append(agent) except: LOG.exception( '[lifecycle.modules.agent_decision] [filter_by_swarm] Exception while processing response' ) return None service_instance['agents'] = l_filtered_agents LOG.debug( "[lifecycle.modules.agent_decision] [filter_by_swarm] service_instance=" + str(service_instance)) return service_instance except: LOG.exception( '[lifecycle.modules.agent_decision] [filter_by_swarm] Exception') return None
def terminate(service_instance_id): LOG.debug("[lifecycle.operations] [terminate] service_instance_id=" + service_instance_id) try: # 1. get service_instance object service_instance = data_adapter.get_service_instance( service_instance_id) if service_instance is None or service_instance == -1: return common.gen_response( 500, 'Error getting service instance object', 'service_instance_id', service_instance_id) # 2. updates service instance status service_instance['status'] = STATUS_TERMINATING data_adapter.update_service_instance(service_instance_id, service_instance) # 3.1 service instance status = Stopped => stop and terminate if service_instance['status'] != "Stopped": return operation_service(service_instance_id, OPERATION_STOP_TERMINATE) # 3.2 terminate else: return operation_service(service_instance_id, OPERATION_TERMINATE) except: LOG.exception('[lifecycle.operations] [terminate] Exception') return common.gen_response(500, 'Exception', 'service_instance_id', service_instance_id)
def __print_records(db): LOG.debug( '[lifecycle.data.app.lm_db] [__print_records] Retrieving records from db...' ) records = db() for r in records: LOG.debug("lm_db> " + str(r))
def genServiceDict(appname): try: dictService = { "apiVersion": "v1", "kind": "Service", "metadata": { "name": "serv_" + appname }, "spec": { "selector": { "app": appname }, "ports": [{ "name": "", "port": 50090, "protocol": "TCP", "targetPort": 80 }] } } return dictService except: LOG.error( "[lifecycle.modules.apps.kubernetes.schemas] [genServiceDict] Error during the creation of the deployment dict" )
def delete_docker_service(service_id): LOG.debug( "[lifecycle.modules.apps.swarm.adapter] [delete_docker_service] [service_name=" + service_id + "]") # connect to docker api lclient = get_client_agent_docker() try: if lclient: if lclient.remove_service(service_id): LOG.info( "[lifecycle.modules.apps.swarm.adapter] [delete_docker_service] Service '" + service_id + "' removed") return True else: LOG.error( "[lifecycle.modules.apps.swarm.adapter] [delete_docker_service] Error removing service '" + service_id + "'") return False else: LOG.error( "[lifecycle.modules.apps.swarm.adapter] [delete_docker_service] Could not connect to DOCKER API" ) return None except: LOG.exception( "[lifecycle.modules.apps.swarm.adapter] [delete_docker_service] Exception" ) return None
def get_client_agent_docker(): global client LOG.debug( "[lifecycle.modules.apps.swarm.adapter] [get_client_agent_docker] Connecting to DOCKER API [" + DOCKER_SOCKET + "], " "[SWARM MASTER? " + str(False) + "] [client=" + str(client) + "],...") try: try: if client is not None: client.version() LOG.debug( "[lifecycle.modules.apps.swarm.adapter] [get_client_agent_docker] Returning existing client [" + json.dumps(client) + "] ...") return client except: LOG.exception( "[lifecycle.modules.apps.swarm.adapter] [get_client_agent_docker] docker client gave an error. Trying to reconnect to docker..." ) client = docker.APIClient(base_url=DOCKER_SOCKET) LOG.debug( "[lifecycle.modules.apps.swarm.adapter] [get_client_agent_docker] Connected to DOCKER in [" + DOCKER_SOCKET + "]; version: " + json.dumps(client.version())) return client except: LOG.exception( "[lifecycle.modules.apps.swarm.adapter] [get_client_agent_docker] Error when connecting to DOCKER API: " + DOCKER_SOCKET) return None
def thr_submit_local(service, service_instance, agent): try: LOG.debug( "[lifecycle.deployment] [thr_submit_local] allocate service locally" ) resp_deploy = apps_adapter.deploy_service_agent( service, service_instance, agent) LOG.debug( "[lifecycle.deployment] [thr_submit_local] allocate service locally: " "[resp_deploy=" + str(resp_deploy) + "]") if agent['status'] == "waiting": LOG.debug( "[lifecycle.deployment] [thr_submit_local] execute service locally" ) # executes service apps_adapter.start_service_agent(service, agent) else: LOG.error( "[lifecycle.deployment] [thr_submit_local] allocate service locally: NOT DEPLOYED" ) agent['status'] = "not-deployed" except: LOG.exception("[lifecycle.deployment] [thr_submit_local] Exception")
def check_service_content(service): if 'category' not in service or 'exec_type' not in service or 'exec' not in service: LOG.debug( "[lifecycle.deployment] [check_service_content] fields category/exec/exec_type not found" ) return False return True
def thr_submit_remote(service, service_instance, agent): try: LOG.debug( "[lifecycle.deployment] [thr_submit_remote] allocate service in remote agent [" + agent['url'] + "]") resp_deploy = connector.lifecycle_deploy(service, service_instance, agent) if resp_deploy is not None: agent['status'] = resp_deploy['status'] agent['container_id'] = resp_deploy['container_id'] agent['ports'] = resp_deploy['ports'] LOG.debug( "[lifecycle.deployment] [thr_submit_remote] allocate service in remote agent: " "[agent=" + str(agent) + "]") # executes / starts service resp_start = connector.lifecycle_operation(service, agent, "start") if resp_start is not None: agent['status'] = resp_start['status'] LOG.debug( "[lifecycle.deployment] [thr_submit_remote] execute service in remote agent: " "[agent=" + str(agent) + "]") else: LOG.error( "[lifecycle.deployment] [thr_submit_remote] allocate service in remote agent: NOT DEPLOYED" ) agent['status'] = "not-deployed" except: LOG.exception("[lifecycle.deployment] [thr_submit_remote] Exception")
def stop_agreement(agreement_id): agreement_id = agreement_id.replace('agreement/', '') LOG.debug( "[lifecycle.connectors.atos.sla_manager] [stop_agreement] agreement_id: " + agreement_id) try: LOG.info( "[lifecycle.connectors.atos.sla_manager] [stop_agreement] HTTP PUT: " + str(config.dic['URL_PM_SLA_MANAGER']) + "/agreements/" + agreement_id + "/stop") r = requests.put(str(config.dic['URL_PM_SLA_MANAGER']) + "/agreements/" + agreement_id + "/stop", verify=config.dic['VERIFY_SSL']) LOG.debug( "[lifecycle.connectors.atos.sla_manager] [stop_agreement] response: " + str(r)) # + ", " + str(r.json())) if r.status_code >= 200 and r.status_code <= 204: return True LOG.error( "[lifecycle.connectors.atos.sla_manager] [stop_agreement] rror: status_code=" + str(r.status_code) + "; Returning False ...") except: LOG.exception( "[lifecycle.connectors.atos.sla_manager] [stop_agreement] Exception; Returning False ..." ) return False
def thr_operation_service_local(operation, service, agent): try: LOG.debug( "[lifecycle.operations] [thr_operation_service_local] service operation: " + operation + " (localhost)") if operation == OPERATION_START: LOG.debug( "[lifecycle.operations] [thr_operation_service_local] start service locally: " + str(service) + ", agent: " + str(agent)) apps_adapter.start_service_agent(service, agent) elif operation == OPERATION_STOP: LOG.debug( "[lifecycle.operations] [thr_operation_service_local] stop service locally: " + str(service) + ", agent: " + str(agent)) apps_adapter.stop_service_agent(service, agent) elif operation == OPERATION_TERMINATE: LOG.debug( "[lifecycle.operations] [thr_operation_service_local] terminate service locally: " + str(service) + ", agent: " + str(agent)) apps_adapter.terminate_service_agent(service, agent) except: LOG.exception( "[lifecycle.operations] [thr_operation_service_local] Exception")
def serv_instance_replace_service_instance_agents(self, service_instance, service, user_id, sla_template_id, agents_list): LOG.warning( "[lifecycle.data.standalone_data_adapter] [serv_instance_replace_service_instance_agents] not implemented" ) return None
def __print_records(db): LOG.log( TRACE, '[lifecycle.data.app.db] [__print_records] Retrieving records from db...' ) records = db() for r in records: LOG.log(TRACE, "db> " + str(r))
def inspect_container(id): try: lclient = get_client_agent_docker() return lclient.inspect_container(id) except: LOG.exception( "[lifecycle.modules.apps.docker.client] [inspect_container] [" + str(id) + "]: Exception") return False
def remove_container_by_id(id): try: lclient = get_client_agent_docker() lclient.remove_container(id, force=True) except: LOG.exception( "[lifecycle.modules.apps.docker.client] [remove_container_by_id] [" + id + "]: Exception") return False
def stop(service, agent): LOG.info("[lifecycle.int_operations] [stop] " + str(service) + ", agent: " + str(agent)) try: status = apps_adapter.stop_service_agent(service, agent) return common.gen_response_ok('Stop service', 'agent', str(agent), 'status', status) except: LOG.exception('[lifecycle.int_operations] [stop] Exception') return common.gen_response(500, 'Exception', 'agent', str(agent))
def start_container(id): try: lclient = get_client_agent_docker() lclient.start(id) return True except: LOG.exception( "[lifecycle.modules.apps.docker.client] [start_container] [" + id + "]: Exception") return False
def gen_response_ok(message, key, value, key2=None, value2=None): dict = {'error': False, 'message': message} dict[key] = value if not (key2 is None) and not (value2 is None): dict[key2] = value2 LOG.log( TRACE, "[lifecycle.common.common] [gen_response_ok] Generate response OK; dict=" + str(dict)) return dict
def terminate(service, agent): LOG.info("[lifecycle.int_operations] [terminate] " + str(service) + ", agent: " + str(agent)) try: status = apps_adapter.terminate_service_agent(service, agent) return common.gen_response_ok('Terminate service', 'agent', str(agent), 'status', status) except: LOG.exception('[lifecycle.int_operations] [terminate] Exception') return common.gen_response(500, 'Exception', 'agent', str(agent))
def get_um_sharing_model(): LOG.debug( "[lifecycle.data.mf2c.data_interface] [get_um_sharing_model] Getting information about current user and device..." ) # get 'my' device_id device_id = __get_current_device_id() if device_id == -1: return None else: return cimi.get_sharing_model_by_device(device_id)
def check_ip(ip_adress): try: # '-c 1' ==> linux # '-n 1' ==> windows response = os.system("ping -c 1 " + ip_adress) if response == 0: return True except: LOG.error('[lifecycle.common.common] [check_ip] Exception') return True
def __check_service_instance_id(service_instance_id): for sid in QoS_SERVICE_INSTANCES_LIST: if sid == service_instance_id: LOG.warning( "[lifecycle.events.handler_qos] [__check_notification] service instance [" + service_instance_id + "] is being processed") return False LOG.debug( "[lifecycle.events.handler_qos] [__check_notification] service instance [" + service_instance_id + "] is NOT being processed") return True
def update_service_instance(service_instance_id, service_instance): LOG.debug( "[lifecycle.data.mf2c.data_interface] [update_service_instance] Updating resource ... (" + service_instance_id + ", " + str(service_instance) + ")") res = cimi.update_service_instance(service_instance_id, service_instance) if not res: LOG.error( "[lifecycle.data.mf2c.data_interface] [update_service_instance] Error during the edition of the service_instance object" ) return None return res
def gen_response(status, message, key, value, key2=None, value2=None): dict = {'error': True, 'message': message} dict[key] = value if not (key2 is None) and not (value2 is None): dict[key2] = value2 LOG.log( TRACE, '[lifecycle.common.common] [gen_response] Generate response ' + str(status) + "; dict=" + str(dict)) return Response(json.dumps(dict), status=status, content_type='application/json')
def handle_warning(warning): try: LOG.info("[lifecycle.events.handler_um] [handle_warning] warning: " + str(warning)) # handle notification t = threading.Thread(target=thr, args=(warning,)) t.start() return common.gen_response_ok('UM Warning is being processed...', 'warning', str(warning)) except: LOG.exception('[lifecycle.events.handler_um] [handle_warning] Exception') return common.gen_response(500, 'Exception', 'warning', str(warning))
def tryPort(port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = False try: sock.bind(("0.0.0.0", port)) #sock.connect_ex(('127.0.0.1', 80)) result = True except: LOG.warning("[lifecycle.modules.apps.ports_mngr] [tryPort] Port (" + str(port) + ") is in use") sock.close() return result
def remove_container(agent): try: lclient = get_client_agent_docker() lclient.remove_container(agent['container_id'], force=True) for p in agent['ports']: pmngr.release_port(p) except: LOG.exception( "[lifecycle.modules.apps.docker.client] [remove_container] [" + str(agent) + "]: Exception") return False
def genDeploymentDict(appname): try: dictDeployment = { "apiVersion": "apps/v1", "kind": "Deployment", "metadata": { "name": appname }, "spec": { "replicas": 1, "revisionHistoryLimit": 10, "selector": { "matchLabels": { "app": appname } }, "template": { "metadata": { "labels": { "app": appname } }, "spec": { "containers": [{ "image": "", "name": "", "imagePullPolicy": "", "ports": [{ "containerPort": 80 }], "volumeMounts": [{ "name": "", "mountPath": "" }], "env": [{ "name": "", "value": "" }] }] } } } } return dictDeployment except: LOG.error( "[lifecycle.modules.apps.kubernetes.schemas] [genDeploymentDict] Error during the creation of the deployment dict" )
def __get_agent(): LOG.info( "[lifecycle.data.mf2c.data_interface] [__get_agent] Getting 'my' device ID ..." ) agent = cimi.get_agent_info() LOG.debug("[lifecycle.data.mf2c.data_interface] [__get_agent] agent=" + str(agent)) if not agent is None and agent != -1: return agent else: return None
def get_leader_ip(): agent = __get_agent() if agent is not None and 'leader_ip' in agent and agent['leader_ip'].strip( ): LOG.info( "[lifecycle.data.mf2c.data_interface] [get_leader_ip] LEADER IP from current agent = " + agent['leader_ip']) return agent['leader_ip'] else: LOG.error( "[lifecycle.data.mf2c.data_interface] [get_leader_ip] Error retrieving LEADER IP from agent. Returning None ..." ) return None