def get_client_agent_docker(): global client LOG.debug( "[lifecycle.modules.apps.docker.client] [get_client_agent_docker] Connecting to DOCKER API [" + DOCKER_SOCKET + "], " "[client=" + str(client) + "]...") try: try: if client is not None: client.version() LOG.debug( "[lifecycle.modules.apps.docker.client] [get_client_agent_docker] Returning existing client [" + str(client) + "] ...") return client except: LOG.error( "[lifecycle.modules.apps.docker.client] [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.docker.client] [get_client_agent_docker] Connected to DOCKER in [" + DOCKER_SOCKET + "]; version: " + str(client.version())) return client except: LOG.exception( "[lifecycle.modules.apps.docker.client] [get_client_agent_docker] Error when connecting to DOCKER API: " + DOCKER_SOCKET) return None
def start_job_in_agents(service_instance, body): ceiClass = body['ceiClass'] className = body['className'] hasResult = body['hasResult'] methodName = body['methodName'] parameters = body['parameters'] LOG.debug( "[lifecycle.modules.apps.compss.adapter] [start_job_in_agents] [service_instance=" + str(service_instance) + "], [parameters=" + str(parameters) + "]") try: # create resource xml xml_resources_content = "" for agent in service_instance['agents']: if agent['status'] == STATUS_STARTED: xml_resources_content += gen_resource(agent['url'], agent['ports']) if not xml_resources_content: LOG.error( '[lifecycle.modules.apps.compss.adapter] [start_job_in_agents] xml_resources_content is empty: agents status != STATUS_STARTED' ) return False xml = "<?xml version='1.0' encoding='utf-8'?>" \ "<startApplication>" \ " <ceiClass>" + ceiClass + "</ceiClass>" \ " <className>" + className + "</className>" \ " <hasResult>" + str(hasResult) + "</hasResult>" \ " <methodName>" + methodName + "</methodName>" \ " <parameters>" + parameters + "</parameters>" \ " <resources>" + xml_resources_content + "</resources>" \ " <serviceInstanceId>" + service_instance['id'].replace("service-instance/", "") + "</serviceInstanceId>" \ "</startApplication>" LOG.debug( "[lifecycle.modules.apps.compss.adapter] [start_job_in_agents] [xml=" + xml + "]") master_agent = data_adapter.serv_instance_find_master(service_instance) compss_port = master_agent['ports'][0] res = requests.put("http://" + master_agent['url'] + ":" + str(compss_port) + "/COMPSs/startApplication", data=xml, headers={'Content-Type': 'application/xml'}) LOG.debug( "[lifecycle.modules.apps.compss.adapter] [start_job_in_agents] response: " + str(res) + ", " + str(res.json())) if res.ok: LOG.debug( "[lifecycle.modules.apps.compss.adapter] [start_job_in_agents] res.text: " + str(res.text)) data_adapter.serv_instance_store_appid_in_master( service_instance, str(res.json())) return True except: LOG.exception( '[lifecycle.modules.apps.compss.adapter] [start_job_in_agents] Exception' ) return False
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 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 get_COMPSs_port_DB_DOCKER_PORTS(lports): try: # debug DB # print_records(DB_DOCKER_PORTS) for p in lports: records = [r for r in DB_DOCKER_PORTS if r['port'] == p] LOG.debug( "[lifecycle.data.app.db] [get_COMPSs_port_DB_DOCKER_PORTS] records: " + str(records)) if len(records) >= 1: if records[0]['mapped_to'] == config.dic['PORT_COMPSs']: LOG.debug( '[lifecycle.data.app.db] [get_COMPSs_port_DB_DOCKER_PORTS] PORT_COMPSs: ' + str(records[0]['port'])) return records[0]['port'] except: LOG.exception( '[lifecycle.data.app.db] [get_COMPSs_port_DB_DOCKER_PORTS] Exception' ) LOG.error( '[lifecycle.data.app.db] [get_COMPSs_port_DB_DOCKER_PORTS] No COMPSs ports found in DB!' ) return config.dic['PORT_COMPSs']
def create_sla_agreement(template_id, user_id, service): LOG.debug( "[lifecycle.connectors.atos.sla_manager] [create_sla_agreement] template_id=" + template_id + ", user_id=" + user_id + ", service=" + service['name']) try: LOG.info( "[lifecycle.connectors.atos.sla_manager] [create_sla_agreement] HTTP POST: " + str(config.dic['URL_PM_SLA_MANAGER']) + "/mf2c/create-agreement") body = {"template_id": template_id, "parameters": {"user": user_id}} r = requests.post(str(config.dic['URL_PM_SLA_MANAGER']) + "/mf2c/create-agreement", json=body, verify=config.dic['VERIFY_SSL']) LOG.debug( "[lifecycle.connectors.atos.sla_manager] [create_sla_agreement] response: " + str(r) + ", " + str(r.json())) if r.status_code >= 200 and r.status_code <= 204: json_data = json.loads(r.text) LOG.debug( "[lifecycle.connectors.atos.sla_manager] [create_sla_agreement] json_data=" + str(json_data)) return json_data['agreement_id'] LOG.error( "[lifecycle.connectors.atos.sla_manager] [create_sla_agreement] Error: status_code=" + str(r.status_code) + "; Returning None ...") except: LOG.exception( "[lifecycle.connectors.atos.sla_manager] [create_sla_agreement] Exception; Returning None ..." ) return None
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 operation_service_agent(agent, operation): LOG.debug("LIFECYCLE: Docker Swarm: operation_service_agent [" + operation + "]: " + str(agent)) try: # connect to docker api / check existing connection if docker_client.get_client_agent_docker() is not None: if operation == OPERATION_STOP or operation == OPERATION_TERMINATE: # service ID is stored in 'container_id' field delete_docker_service(agent['container_id']) agent['status'] = STATUS_TERMINATED else: LOG.warning( "[lifecycle.modules.apps.swarm.adapter] [operation_service_agent] [" + operation + "]: " + str(agent) + ": operation not supported") # if error when connecting to agent... else: LOG.error( "[lifecycle.modules.apps.swarm.adapter] [operation_service_agent] Could not connect to DOCKER API" ) agent['status'] = STATUS_UNKNOWN # return status return agent['status'] except: agent['status'] = STATUS_ERROR LOG.exception( '[lifecycle.modules.apps.swarm.adapter] [operation_service_agent] Exception. Returning STATUS_ERROR ...' ) return STATUS_ERROR
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 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 check_agent_swarm(agent): LOG.debug("[lifecycle.connectors.atos.lifecycle] [check_agent_swarm] " + str(agent)) try: LOG.info( "[lifecycle.connectors.atos.lifecycle] [check_agent_swarm] HTTP GET: http://" + agent['url'] + ":" + str(config.dic['SERVER_PORT']) + "/api/v2/lm/check-agent-swarm") r = requests.get("http://" + agent['url'] + ":" + str(config.dic['SERVER_PORT']) + "/api/v2/lm/check-agent-swarm", verify=config.dic['VERIFY_SSL']) LOG.debug( "[lifecycle.connectors.atos.lifecycle] [check_agent_swarm] response: " + str(r) + ", " + str(r.json())) if r.status_code == 200: json_data = json.loads(r.text) LOG.debug( "[lifecycle.connectors.atos.lifecycle] [check_agent_swarm] json_data=" + str(json_data)) return json_data LOG.error( "[lifecycle.connectors.atos.lifecycle] [check_agent_swarm] Error: status_code=" + str(r.status_code) + "; Returning None ...") except: LOG.exception( "[lifecycle.connectors.atos.lifecycle] [check_agent_swarm] Exception; Returning None ..." ) return None
def start_job(service_instance, body): ceiClass = body['ceiClass'] className = body['className'] hasResult = body['hasResult'] methodName = body['methodName'] parameters = body['parameters'] service_instance_id = service_instance['id'] agent = service_instance['agents'][0] LOG.debug( "[lifecycle.modules.apps.compss.adapter] [start_job] service_instance_id=" + service_instance_id + ", agent=" + str(agent) + ", body=" + str(body)) try: # create resource xml xml_resource = gen_resource(agent['url'], agent['ports']) # create xml xml = "<?xml version='1.0' encoding='utf-8'?>" \ "<startApplication>" \ " <ceiClass>" + ceiClass + "</ceiClass>" \ " <className>" + className + "</className>" \ " <hasResult>" + str(hasResult) + "</hasResult>" \ " <methodName>" + methodName + "</methodName>" \ " <parameters>" + parameters + "</parameters>" \ " <resources>" + xml_resource + "</resources>" \ " <serviceInstanceId>" + service_instance['id'].replace("service-instance/", "") + "</serviceInstanceId>" \ "</startApplication>" LOG.debug("[lifecycle.modules.apps.compss.adapter] [start_job] [xml=" + xml + "]") master_agent = data_adapter.serv_instance_find_master(service_instance) compss_port = agent['ports'][0] LOG.debug( "[lifecycle.modules.apps.compss.adapter] [start_job] PUT http://" + agent['url'] + ":" + str(compss_port) + "/COMPSs/startApplication") res = requests.put("http://" + agent['url'] + ":" + str(compss_port) + "/COMPSs/startApplication", data=xml, headers={'Content-Type': 'application/xml'}) if res.ok: LOG.debug( "[lifecycle.modules.apps.compss.adapter] [start_job] response (json): " + str(res) + ", " + str(res.json()) + ", " + str(res.text)) data_adapter.serv_instance_store_appid_in_master( service_instance, str(res.json())) return True else: LOG.error( "[lifecycle.modules.apps.compss.adapter] [start_job] Response from COMPSs: " + str(res)) except: LOG.exception( '[lifecycsle.modules.apps.compss.adapter] [start_job] Exception') return False
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 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 __deploy_COMPSs_in_agent(service, service_instance, agent_ip): LOG.debug( "[lifecycle.events.handler_qos] [__deploy_COMPSs_in_agent] Deploying COMPSs service instance [" + service_instance['id'] + "] in new agent [" + agent_ip + "] ...") try: # 1. create NEW AGENT LOG.debug( "[lifecycle.events.handler_qos] [__deploy_COMPSs_in_agent] Creating new service instance agent [" + agent_ip + "]") new_agent = { "device_id": "-", "app_type": service['exec_type'], "ports": service['exec_ports'], "url": agent_ip, "status": STATUS_CREATED_NOT_INITIALIZED, "compss_app_id": "-", "allow": True, "container_id": "-", "master_compss": False, "agent_param": "not-defined" } # 2. DEPLOY COMPSs in NEW AGENT LOG.debug( "[lifecycle.events.handler_qos] [__deploy_COMPSs_in_agent] allocate service in remote agent [" + new_agent['url'] + "]") resp_deploy = connector.lifecycle_deploy(service, service_instance, new_agent) if resp_deploy is not None: new_agent['status'] = resp_deploy['status'] new_agent['container_id'] = resp_deploy['container_id'] new_agent['ports'] = resp_deploy['ports'] LOG.debug( "[lifecycle.events.handler_qos] [__deploy_COMPSs_in_agent] service deployed in remote agent: [agent=" + str(new_agent) + "]") # executes / starts service resp_start = connector.lifecycle_operation(service, new_agent, "start") if resp_start is not None: new_agent['status'] = resp_start['status'] LOG.debug( "[lifecycle.events.handler_qos] [__deploy_COMPSs_in_agent] execute service in remote agent: [agent=" + str(new_agent) + "]") return True, new_agent else: LOG.error( "[lifecycle.events.handler_qos] [__deploy_COMPSs_in_agent] allocate service in remote agent: NOT DEPLOYED" ) new_agent['status'] = "not-deployed" except: LOG.exception( "[lifecycle.events.handler_qos] [__deploy_COMPSs_in_agent] Exception. Returning False ..." ) return False, None
def postServiceInt(request): LOG.info("########################################################################################") LOG.info("######## DEPLOYMENT (remote call)") data = request.get_json() # check input parameters if 'service' not in data or 'service_instance' not in data or 'agent' not in data: LOG.error('[lifecycle.app_funcs] [postServiceInt] Exception - parameter not found: service / service_instance / agent') return Response(json.dumps({'error': True, 'message': 'parameter not found: service / service_instance / agent'}), status=406, content_type='application/json') # deploy operation (for internal calls - between LMs -) return operations.deploy(data['service'], data['service_instance'], data['agent'])
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_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
def __deploy_docker_compss(service, service_instance, agent): LOG.debug( "[lifecycle.modules.apps.docker.adapter] [__deploy_docker_compss] " + str(service) + ", " + str(agent)) try: # service image / location. Examples: "mf2c/compss-agent:latest", "mf2c/compss-mf2c:1.0" service_image = service['exec'] # port(s); COMPSs exposes port 8080 ports = agent['ports'] # ip ip = agent['url'] # ip_leader ip_leader = service_instance[ 'device_ip'] # TODO create a 'exec_device_ip' container1, agents_ports = docker_client.create_docker_compss_container( service_image, ip, ports, ip_leader) if container1 is not None: db.SERVICE_INSTANCES_LIST.append({ "type": SERVICE_COMPSS, "container_main": container1['Id'], "container_2": "-" }) LOG.debug( "[lifecycle.modules.apps.docker.adapter] [__deploy_docker_compss] container: " + str(container1)) # update agent properties agent['container_id'] = container1['Id'] agent['agent_param'] = "-" agent['ports'] = agents_ports agent['status'] = STATUS_WAITING return common.gen_response_ok('Deploy service in agent', 'agent', str(agent), 'service', str(service)) else: LOG.error( "[lifecycle.modules.apps.docker.adapter] [__deploy_docker_compss] Could not connect to DOCKER API" ) agent['status'] = STATUS_ERROR return common.gen_response(500, 'Error when connecting to DOCKER API', 'agent', str(agent), 'service', str(service)) except: LOG.exception( '[lifecycle.modules.apps.docker.adapter] [__deploy_docker_compss] Exception' ) return common.gen_response(500, 'Exception: __deploy_docker_compss()', 'agent', str(agent), 'service', str(service))
def create_docker_container(service_image, service_name, service_command, prts): LOG.debug( "[lifecycle.modules.apps.docker.client] [create_docker_container] [service_name=" + service_name + "], " "[service_command=" + service_command + "], [service_image=" + service_image + "], " "[ports=" + str(prts) + "]") # connect to docker api lclient = get_client_agent_docker() try: if lclient: # check if image already exists in agent or download image __download_docker_image(lclient, service_image) LOG.debug( "[lifecycle.modules.apps.docker.client] [create_docker_container] Creating container ..." ) prts_list = list(prts) ports_dict = pmngr.create_ports_dict(prts) LOG.debug( "[lifecycle.modules.apps.docker.client] [create_docker_container] ports_dict: " + str(ports_dict)) LOG.debug( "[lifecycle.modules.apps.docker.client] [create_docker_container] prts: " + str(prts)) LOG.debug( "[lifecycle.modules.apps.docker.client] [create_docker_container] prts_list: " + str(prts_list)) # create a new container: 'docker run' container = lclient.create_container( service_image, # command=service_command, name=service_name, tty=True, ports=prts_list, host_config=lclient.create_host_config( port_bindings=ports_dict)) return container else: LOG.error( "[lifecycle.modules.apps.docker.client] [create_docker_container] Could not connect to DOCKER API" ) return None except: LOG.exception( "[lifecycle.modules.apps.docker.client] [create_docker_container] Exception" ) return None
def thr(notification): try: LOG.debug( "[lifecycle.events.handler_sla] [thr] Handling SLA violations [" + str(notification) + "] ...") # TODO #... time.sleep(10) LOG.debug( "[lifecycle.events.handler_sla] [thr] SLA violations handled") except: LOG.error('[lifecycle.events.handler_sla] [thr] Exception')
def get_my_ip(): agent = __get_agent() if agent is not None and agent[ 'device_ip'] is not None and agent['device_ip'] != "": LOG.info( "[lifecycle.data.mf2c.data_interface] [get_my_ip] IP from current agent = " + agent['device_ip']) return agent['device_ip'] elif config.dic['HOST_IP'] is not None and config.dic['HOST_IP'] != "": return config.dic['HOST_IP'] else: LOG.error( "[lifecycle.data.mf2c.data_interface] [get_my_ip] Error retrieving IP from agent. Returning None ..." ) return None
def postServiceInstanceEvent(request): body = request.get_json() if not body or 'type' not in body or 'data' not in body: LOG.error('[lifecycle.app_funcs] [postServiceInstanceEvent] Exception - parameter not found: type / data') return Response(json.dumps({'error': True, 'message': 'parameter not found: type / data'}), status=406, content_type='application/json') else: if body['type'] == "sla_notification": return handler_sla.handle_sla_notification(body['data']) elif body['type'] == "um_warning": return handler_um.handle_warning(body['data']) elif body['type'] == "qos_enforcement": return handler_qos.handle_qos_notification(body['data']) LOG.error("[lifecycle.app_funcs] [postServiceInstanceEvent] type [" + body['type'] + "] not defined / implemented") return Response(json.dumps({'error': True, 'message': 'type not defined / implemented'}), status=501, content_type='application/json')
def __deploy_docker_image(service, agent): LOG.debug( "[lifecycle.modules.apps.docker.adapter] [__deploy_docker_image] " + str(service) + ", " + str(agent)) try: # service image / location. Examples: "yeasy/simple-web" service_image = service['exec'] # service_name examples: "simple-web-test" service_name = service['name'] + "-" + str(uuid.uuid4()) # command. Docker examples: "/bin/sh -c 'python index.py'" service_command = "" # port(s) ports = agent['ports'] container1 = docker_client.create_docker_container( service_image, service_name, service_command, ports) if container1 is not None: db.SERVICE_INSTANCES_LIST.append({ "type": SERVICE_DOCKER, "container_main": container1['Id'], "container_2": "-" }) LOG.debug( "[lifecycle.modules.apps.docker.adapter] [__deploy_docker_image] container: " + str(container1)) # update agent properties agent['container_id'] = container1['Id'] agent['agent_param'] = "-" agent['status'] = STATUS_WAITING return common.gen_response_ok('Deploy service in agent', 'agent', str(agent), 'service', str(service)) else: LOG.error( "[lifecycle.modules.apps.docker.adapter] [__deploy_docker_image] Could not connect to DOCKER API" ) agent['status'] = STATUS_ERROR return common.gen_response(500, 'Error when connecting to DOCKER API', 'agent', str(agent), 'service', str(service)) except: LOG.exception( '[lifecycle.modules.apps.docker.adapter] [__deploy_docker_image] Exception' ) return common.gen_response(500, 'Exception: __deploy_docker_image()', 'agent', str(agent), 'service', str(service))
def putServiceInstance(request, service_instance_id): data = request.get_json() if 'operation' not in data: LOG.error('[lifecycle.app_funcs] [putServiceInstance] Parameter not found: operation') return Response(json.dumps({'error': True, 'message': 'parameter not found: operation'}), status=406, content_type='application/json') # operations if data['operation'] == OPERATION_START: return lifecycle_ops.start(service_instance_id) elif data['operation'] == OPERATION_STOP: return lifecycle_ops.stop(service_instance_id) elif data['operation'] == OPERATION_RESTART: return lifecycle_ops.start(service_instance_id) else: LOG.error('[lifecycle.app_funcs] [putServiceInstance] operation not defined / implemented') return Response(json.dumps({'error': True, 'message': 'operation not defined / implemented'}), status=501, content_type='application/json')
def handle_qos_notification(notification): LOG.info( "########################################################################################" ) LOG.info("######## QoS ENFORCEMENT") try: global QoS_SERVICE_INSTANCES_LIST LOG.info( "[lifecycle.events.handler_qos] [handle_qos_notification] service_instance_id: notification: " + str(notification)) if not 'num_agents' in notification or not 'service_instance_id' in notification: LOG.error( "[lifecycle.events.handler_qos] [handle_qos_notification] 'num_agents' / 'service_instance_id' parameters not found in notification!" ) return common.gen_response( 406, 'Error', 'parameter num_agents / service_instance_id not found in qos notification', str(notification)) # handle notification if __check_service_instance_id(notification['service_instance_id']): LOG.info( "[lifecycle.events.handler_qos] [handle_qos_notification] Processing request..." ) QoS_SERVICE_INSTANCES_LIST.append( notification['service_instance_id']) t = threading.Thread(target=thr, args=(notification, )) t.start() return common.gen_response_ok( 'QoS Notification is being processed...', 'notification', str(notification)) LOG.info( "[lifecycle.events.handler_qos] [handle_qos_notification] Request not processed." ) return common.gen_response_ok( "QoS Notification was not processed: List of current service instances being processed: " + str(QoS_SERVICE_INSTANCES_LIST), "notification", str(notification)) except: LOG.exception( '[lifecycle.events.handler_qos] [handle_qos_notification] Exception' ) return common.gen_response(500, 'Exception', 'notification', str(notification))
def create_docker_compose_container(service_name, service_command): LOG.debug( "[lifecycle.modules.apps.docker.client] [create_docker_compose_container] Creating docker-compose containers [service_name=" + service_name + "], [service_command=" + service_command + "] ...") try: # connect to docker api lclient = get_client_agent_docker() if lclient: # check if image already exists in agent or download image __download_docker_image(lclient, config.dic['DOCKER_COMPOSE_IMAGE'], config.dic['DOCKER_COMPOSE_IMAGE_TAG']) # create a new container: 'docker run' container = lclient.create_container( config.dic['DOCKER_COMPOSE_IMAGE'], command=service_command, name=service_name, tty=True, volumes=[ config.dic['WORKING_DIR_VOLUME'], config.dic['DOCKER_SOCKET_VOLUME'] ], host_config=lclient.create_host_config( binds={ config.dic['WORKING_DIR_VOLUME']: { 'bind': config.dic['WORKING_DIR_VOLUME'], 'mode': 'rw', }, '/var/run/docker.sock': { 'bind': config.dic['DOCKER_SOCKET_VOLUME'], 'mode': 'rw', } }), working_dir=config.dic['WORKING_DIR_VOLUME']) return container else: LOG.error( "[lifecycle.modules.apps.docker.client] [create_docker_compose_container] Could not connect to DOCKER API" ) return None except: LOG.exception( "[lifecycle.modules.apps.docker.client] [create_docker_compose_container] Exception" ) return None
def get_user_profile(): LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [get_user_profile] Getting user-profile from localhost ...") try: LOG.info("[lifecycle.connectors.atos.user_manager] [get_user_profile] HTTP GET: " + str(config.dic['URL_AC_USER_MANAGEMENT']) + "/user-profile") r = requests.get(str(config.dic['URL_AC_USER_MANAGEMENT']) + "/user-profile", verify=config.dic['VERIFY_SSL']) LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [get_user_profile] response: " + str(r) + ", " + str(r.json())) json_data = json.loads(r.text) LOG.debug("[lifecycle.connectors.atos.user_manager] [get_user_profile] json_data=" + str(json_data)) if r.status_code == 200: return json_data LOG.error("[lifecycle.connectors.atos.user_manager] [get_user_profile] Error: status_code=" + str(r.status_code) + "; Returning None ...") except: LOG.exception("[lifecycle.connectors.atos.user_manager] [get_user_profile] Exception; Returning None ...") return None
def get_current(val): LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [get_current] Getting current " + val + " from localhost - UM: Checking avialability ...") try: LOG.info("[lifecycle.connectors.atos.user_manager] [get_current] HTTP GET: " + str(config.dic['URL_AC_USER_MANAGEMENT']) + "/current/" + val) r = requests.get(str(config.dic['URL_AC_USER_MANAGEMENT']) + "/current/" + val, verify=config.dic['VERIFY_SSL']) LOG.log(TRACE, "[lifecycle.connectors.atos.user_manager] [get_current] response: " + str(r) + ", " + str(r.json())) json_data = json.loads(r.text) LOG.debug("[lifecycle.connectors.atos.user_manager] [get_current] json_data=" + str(json_data)) if r.status_code == 200: return json_data LOG.error("[lifecycle.connectors.atos.user_manager] [get_current] Error: status_code=" + str(r.status_code) + "; Returning None ...") except: LOG.exception("[lifecycle.connectors.atos.user_manager] [get_current] Exception; Returning None ...") return None
def handle_sla_notification(notification): try: LOG.info( "[lifecycle.events.handler_sla] [handle_sla_notification] service_instance_id: notification: " + str(notification)) # handle notification t = threading.Thread(target=thr, args=(notification, )) t.start() return common.gen_response_ok('SLA Notification has been processed', 'notification', str(notification)) except: LOG.error( '[lifecycle.events.handler_sla] [handle_sla_notification] Exception' ) return common.gen_response(500, 'Exception', 'notification', str(notification))