Beispiel #1
0
    def deploy_logs(self, env_id):
        env_id = env_id.lower()
        http = HttpResponse()
        docker_utils = DockerUtils()
        env_id = env_id.strip()
        env_id_dir = EnvInit.init.get(EnvConstants.DEPLOY_PATH) + f"/{env_id}"
        file = f"{env_id_dir}/docker-compose.yml"

        try:
            status = docker_utils.logs(file)
            app.logger.debug({"msg": status})
            if status.get('err'):
                raise Exception(status.get('err'))
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.GET_LOGS_FAILED.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.GET_LOGS_FAILED.value) %
                env_id, e)

        return Response(json.dumps(
            http.response(ApiCode.SUCCESS.value,
                          ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                          status.get('out'))),
                        200,
                        mimetype="application/json")
Beispiel #2
0
    def before_request(self, name, *args, **kwargs):
        ctx = app.app_context()
        ctx.g.xid = token_hex(8)
        http = HttpResponse()
        request_uri = request.full_path
        # add here your custom header to be logged with fluentd
        self.message_dumper.set_header(
            HeaderConstants.X_REQUEST_ID,
            request.headers.get(HeaderConstants.X_REQUEST_ID) if
            request.headers.get(HeaderConstants.X_REQUEST_ID) else ctx.g.xid)
        self.message_dumper.set_header(HeaderConstants.REQUEST_URI,
                                       request_uri)

        response = self.fluentd.emit(
            tag="api", msg=self.message_dumper.dump(request=request))
        app.logger.debug(response)
        if not str(request.headers.get(HeaderConstants.TOKEN)) == str(
                EnvStartupSingleton.get_instance().get_config_env_vars().get(
                    EnvConstants.HTTP_AUTH_TOKEN)):
            if not ("/apidocs" in request_uri or "/swagger/swagger.json"
                    in request_uri):  # exclude swagger
                headers = {
                    HeaderConstants.X_REQUEST_ID:
                    self.message_dumper.get_header(
                        HeaderConstants.X_REQUEST_ID)
                }
                return Response(json.dumps(
                    http.response(
                        ApiCode.UNAUTHORIZED.value,
                        ErrorMessage.HTTP_CODE.get(ApiCode.UNAUTHORIZED.value),
                        "Invalid Token")),
                                401,
                                mimetype="application/json",
                                headers=headers)
Beispiel #3
0
    def deploy_logs(self, deployment):
        http = HttpResponse()
        kubectl_utils = KubectlUtils()
        deployment = deployment.strip()
        header_key = 'K8s-Namespace'

        if not request.headers.get(f"{header_key}"):
            raise ApiExceptionKubectl(
                ApiCode.HTTP_HEADER_NOT_PROVIDED.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.HTTP_HEADER_NOT_PROVIDED.value) % header_key,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.HTTP_HEADER_NOT_PROVIDED.value) % header_key)

        try:
            if request.headers.get(f"{header_key}"):
                namespace = request.headers.get(f"{header_key}")
            status = kubectl_utils.logs(deployment, namespace)
            if status.get('err'):
                raise Exception(status)
        except Exception as e:
            raise ApiExceptionKubectl(
                ApiCode.GET_LOGS_FAILED.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.GET_LOGS_FAILED.value) %
                deployment, e)

        return Response(json.dumps(
            http.response(ApiCode.SUCCESS.value,
                          ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                          status.get('out'))),
                        200,
                        mimetype="application/json")
Beispiel #4
0
def set_env():
    http = HttpResponse()
    input_data = request.data.decode("UTF-8", "replace").strip()

    try:
        env_vars_attempted = json.loads(input_data)
    except Exception as e:
        raise ApiException(
            ApiCode.INVALID_JSON_PAYLOAD.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.INVALID_JSON_PAYLOAD.value) %
            str(input_data), e)

    try:
        for key, value in env_vars_attempted.items():
            env.set_env_var(key, value)
        env_vars_added = {
            key: value
            for key, value in env_vars_attempted.items()
            if key in env.get_virtual_env()
        }
    except Exception as e:
        raise ApiException(
            ApiCode.SET_ENV_VAR_FAILURE.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.SET_ENV_VAR_FAILURE.value) %
            str(input_data), e)
    return Response(json.dumps(
        http.response(ApiCode.SUCCESS.value,
                      ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                      env_vars_added)),
                    200,
                    mimetype="application/json")
Beispiel #5
0
    def get_deployment_status(self, pod):
        http = HttpResponse()
        pod = pod.strip()
        kubectl_utils = KubectlUtils()
        header_keys = ["Label-Selector", 'K8s-Namespace']

        for header_key in header_keys:
            if not request.headers.get(f"{header_key}"):
                raise ApiExceptionKubectl(
                    ApiCode.HTTP_HEADER_NOT_PROVIDED.value,
                    ErrorMessage.HTTP_CODE.get(
                        ApiCode.HTTP_HEADER_NOT_PROVIDED.value) % header_key,
                    ErrorMessage.HTTP_CODE.get(
                        ApiCode.HTTP_HEADER_NOT_PROVIDED.value) % header_key)

        try:
            label_selector = request.headers.get(f"{header_keys[0]}")
            namespace = request.headers.get(f"{header_keys[1]}")
            deployment = kubectl_utils.get_active_pod(pod, label_selector,
                                                      namespace)
        except Exception as e:
            raise ApiExceptionKubectl(
                ApiCode.DEPLOY_STATUS_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.DEPLOY_STATUS_FAILURE.value), e)

        return Response(json.dumps(
            http.response(ApiCode.SUCCESS.value,
                          ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                          deployment)),
                        200,
                        mimetype="application/json")
Beispiel #6
0
    def delete_deployment(self, deployment):
        http = HttpResponse()
        deployment = deployment.strip()
        kubectl_utils = KubectlUtils()
        header_key = 'K8s-Namespace'
        fluentd_tag = "deploy_stop"

        if not request.headers.get(f"{header_key}"):
            raise ApiExceptionKubectl(
                ApiCode.HTTP_HEADER_NOT_PROVIDED.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.HTTP_HEADER_NOT_PROVIDED.value) % header_key,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.HTTP_HEADER_NOT_PROVIDED.value) % header_key)

        try:
            namespace = request.headers.get(f"{header_key}")
            status = kubectl_utils.down(deployment, namespace)
            self.fluentd.emit(tag=fluentd_tag, msg={"msg": status})
            if "Error".lower() in status.get('err').lower():
                raise Exception(status.get('err'))
            result = status.get('out').split("\n")[1:]
        except Exception as e:
            raise ApiExceptionKubectl(
                ApiCode.DEPLOY_STOP_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.DEPLOY_STOP_FAILURE.value),
                e)

        return Response(json.dumps(
            http.response(ApiCode.SUCCESS.value,
                          ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                          result)),
                        200,
                        mimetype="application/json")
Beispiel #7
0
    def deploy_start(self):
        http = HttpResponse()
        kubectl_utils = KubectlUtils()
        fluentd_tag = "deploy_start"
        token = token_hex(8)
        deploy_dir = f"{EnvInit.init.get(EnvConstants.DEPLOY_PATH)}/{token}"
        file = f"{deploy_dir}/k8s-deployment.yml"

        try:
            IOUtils.create_dir(deploy_dir)
            input_data = request.data.decode('utf-8')
            IOUtils.write_to_file(file, input_data)
            status = kubectl_utils.up(f"{file}")
            self.fluentd.emit(tag=fluentd_tag, msg={"msg": status})
            if status.get('err'):
                raise Exception(status.get('err'))
        except Exception as e:
            raise ApiExceptionKubectl(
                ApiCode.DEPLOY_START_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.DEPLOY_START_FAILURE.value),
                e)

        return Response(json.dumps(
            http.response(ApiCode.SUCCESS.value,
                          ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                          token)),
                        200,
                        mimetype="application/json")
Beispiel #8
0
    def get_deployment_info(self):
        http = HttpResponse()
        kubectl_utils = KubectlUtils()
        header_keys = ["Label-Selector", 'K8s-Namespace']

        for header_key in header_keys:
            if not request.headers.get(f"{header_key}"):
                raise ApiExceptionKubectl(
                    ApiCode.HTTP_HEADER_NOT_PROVIDED.value,
                    ErrorMessage.HTTP_CODE.get(
                        ApiCode.HTTP_HEADER_NOT_PROVIDED.value) % header_key,
                    ErrorMessage.HTTP_CODE.get(
                        ApiCode.HTTP_HEADER_NOT_PROVIDED.value) % header_key)
        label_selector = request.headers.get(f"{header_keys[0]}")
        namespace = request.headers.get(f"{header_keys[1]}")
        active_pods = kubectl_utils.get_active_pods(label_selector, namespace)
        app.logger.debug(
            {"msg": {
                "active_deployments": f"{len(active_pods)}"
            }})
        return Response(json.dumps(
            http.response(ApiCode.SUCCESS.value,
                          ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                          active_pods)),
                        200,
                        mimetype="application/json")
Beispiel #9
0
    def set_env(self):
        http = HttpResponse()
        input_data = request.data.decode("UTF-8", "replace").strip()

        try:
            env_vars_attempted = json.loads(input_data)
        except Exception as e:
            raise ApiExceptionKubectl(
                ApiCode.INVALID_JSON_PAYLOAD.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.INVALID_JSON_PAYLOAD.value)
                % str(input_data), e)

        try:
            env_vars_added = EnvironmentSingleton.get_instance().set_env_vars(
                env_vars_attempted)
        except Exception as e:
            raise ApiExceptionKubectl(
                ApiCode.SET_ENV_VAR_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.SET_ENV_VAR_FAILURE.value) %
                str(input_data), e)
        return Response(json.dumps(
            http.response(ApiCode.SUCCESS.value,
                          ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                          env_vars_added)),
                        200,
                        mimetype="application/json")
Beispiel #10
0
    def container_docker_network_connect(self, env_id):
        env_id = env_id.lower()
        http = HttpResponse()
        docker_utils = DockerUtils()
        headers = request.headers
        service_name = "container"

        if request.args.get('service') is not None:
            service_name = request.args.get('service')
        container_id = f"{env_id}_{service_name}_1"

        try:
            status = CmdUtils.run_cmd_shell_false([
                "docker", "network", "ls", "--filter",
                "name={}".format("deployer")
            ])
            app.logger.debug({"msg": status})
            if not status.get('out'):
                raise Exception(status.get('err'))
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.GET_DEPLOYER_NETWORK_FAILED.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.GET_DEPLOYER_NETWORK_FAILED.value), e)

        try:
            deployer_network = status.get('out').split("\n")[1].split(
                " ")[0].strip()
            if headers.get("Docker-Network"):
                deployer_network = headers.get("Docker-Network")
            status = docker_utils.network_connect(deployer_network,
                                                  container_id)

            if "already exists in network".lower() in status.get(
                    'err').lower():
                return Response(json.dumps(
                    http.response(
                        ApiCode.SUCCESS.value,
                        ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                        "Success, already connected: " + status.get('err'))),
                                200,
                                mimetype="application/json")

            if "Error response from daemon".lower() in status.get(
                    'err').lower():
                raise Exception(status.get('err'))
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.CONTAINER_NET_CONNECT_FAILED.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.CONTAINER_NET_CONNECT_FAILED.value), e)
        return Response(json.dumps(
            http.response(ApiCode.SUCCESS.value,
                          ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                          status.get('out'))),
                        200,
                        mimetype="application/json")
Beispiel #11
0
 def get_env_vars(self):
     return Response(json.dumps(HttpResponse().response(
         ApiCode.SUCCESS.value,
         ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
         EnvironmentSingleton.get_instance().get_env_and_virtual_env())),
                     200,
                     mimetype="application/json")
Beispiel #12
0
def handle_api_error(e):
    return Response(json.dumps(HttpResponse().response(
        code=e.code,
        message=e.message,
        description="Exception({})".format(e.exception.__str__()))),
                    500,
                    mimetype="application/json")
Beispiel #13
0
    def execute_command(self):
        input_data = request.data.decode("UTF-8", "replace").strip()

        if not input_data:
            raise ApiExceptionDocker(
                ApiCode.EMPTY_REQUEST_BODY_PROVIDED.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.EMPTY_REQUEST_BODY_PROVIDED.value),
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.EMPTY_REQUEST_BODY_PROVIDED.value))
        try:
            input_data_list = input_data.split("\n")
            input_data_list = list(map(lambda x: x.strip(), input_data_list))
            command_in_memory = CommandInMemory()
            response = command_in_memory.run_commands(input_data_list)
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.COMMAND_EXEC_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.COMMAND_EXEC_FAILURE.value),
                e)
        return Response(json.dumps(HttpResponse().response(
            ApiCode.SUCCESS.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value), response)),
                        200,
                        mimetype="application/json")
Beispiel #14
0
def get_env(name):
    return Response(json.dumps(HttpResponse().response(
        ApiCode.SUCCESS.value,
        ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
        env.get_env_and_virtual_env().get(name))),
                    200,
                    mimetype="application/json")
Beispiel #15
0
def get_vars():
    return Response(json.dumps(HttpResponse().response(
        code=ApiCode.SUCCESS.value,
        message=ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
        description=env.get_env_and_virtual_env())),
                    200,
                    mimetype="application/json")
Beispiel #16
0
    def delete_deployment_id(self, depl_id):
        depl_id = depl_id.strip()
        docker_utils = DockerUtils()
        depl_folder = f"{EnvInit.init.get(EnvConstants.DEPLOY_PATH)}/{depl_id}"
        file = f"{depl_folder}/docker-compose.yml"
        try:
            status = docker_utils.down(file)
            if "Cannot connect to the Docker daemon".lower() in status.get(
                    'err').lower():
                raise Exception(status.get('err'))
            app.logger.debug({"msg": status})
            status = docker_utils.ps(depl_id)
            result = status.get('out').split("\n")[1:]
            DeploymentMetadataSingleton.get_instance(
            ).delete_metadata_for_deployment(depl_id)
            IOUtils.remove_directory(f"{depl_folder}")
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.DEPLOY_STOP_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.DEPLOY_STOP_FAILURE.value),
                e)

        return Response(json.dumps(HttpResponse().response(
            ApiCode.SUCCESS.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value), result)),
                        200,
                        mimetype="application/json")
Beispiel #17
0
    def delete_all_deployments(self):
        docker_utils = DockerUtils()
        try:
            active_deployments = docker_utils.get_active_deployments()
            for deployment in active_deployments:
                depl_id = deployment.get('id')
                depl_folder = f"{EnvInit.init.get(EnvConstants.DEPLOY_PATH)}/{depl_id}"
                status = docker_utils.down(f"{depl_folder}/docker-compose.yml")
                if "Cannot connect to the Docker daemon".lower() in status.get(
                        'err').lower():
                    raise Exception(status.get('err'))
                app.logger.debug({"msg": status})
                DeploymentMetadataSingleton.get_instance(
                ).delete_metadata_for_deployment(depl_id)

            DockerUtils.folder_clean_up()
            result = docker_utils.get_active_deployments()
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.DEPLOY_STOP_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.DEPLOY_STOP_FAILURE.value),
                e)

        return Response(json.dumps(HttpResponse().response(
            ApiCode.SUCCESS.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value), result)),
                        200,
                        mimetype="application/json")
Beispiel #18
0
def get_eureka_apps():
    http = HttpResponse()
    eureka = Eureka(
        EnvStartupSingleton.get_instance().get_config_env_vars().get(
            EnvConstants.EUREKA_SERVER))

    try:
        apps_list = eureka.get_eureka_apps()
    except Exception as e:
        raise ApiException(
            ApiCode.GET_EUREKA_APPS_FAILED.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.GET_EUREKA_APPS_FAILED.value) %
            eureka.get_eureka_host(), e)

    return Response(json.dumps(
        http.response(ApiCode.SUCCESS.value,
                      ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                      apps_list)),
                    200,
                    mimetype="application/json")
Beispiel #19
0
    def get_active_deployments(self):
        docker_utils = DockerUtils()
        active_deployments = docker_utils.get_active_deployments()
        app.logger.debug(
            {"msg": {
                "active_deployments": f"{len(active_deployments)}"
            }})

        return Response(json.dumps(HttpResponse().response(
            ApiCode.SUCCESS.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
            active_deployments)),
                        200,
                        mimetype="application/json")
Beispiel #20
0
 def handle_api_error(cls, e):
     http_response = Response(json.dumps(HttpResponse().response(
         code=e.code,
         message=e.message,
         description="Exception({})".format(e.exception.__str__()))),
                              500,
                              mimetype="application/json")
     http_response.headers[
         HeaderConstants.
         X_REQUEST_ID] = DockerView.message_dumper.get_header(
             HeaderConstants.X_REQUEST_ID)
     response = DockerView.fluentd.emit(
         tag="api", msg=DockerView.message_dumper.dump(http_response))
     app.logger.debug(f"{response}")
     return http_response
Beispiel #21
0
    def upload_file(self):
        http = HttpResponse()
        io_utils = IOUtils()
        header_key = 'File-Path'
        file_content = request.get_data()
        file_path = request.headers.get(f"{header_key}")

        if not file_path:
            raise ApiExceptionDocker(
                ApiCode.HTTP_HEADER_NOT_PROVIDED.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.HTTP_HEADER_NOT_PROVIDED.value) % header_key,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.HTTP_HEADER_NOT_PROVIDED.value) % header_key)
        if not file_content:
            raise ApiExceptionDocker(
                ApiCode.EMPTY_REQUEST_BODY_PROVIDED.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.EMPTY_REQUEST_BODY_PROVIDED.value),
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.EMPTY_REQUEST_BODY_PROVIDED.value))

        try:
            io_utils.write_to_file_binary(file_path, file_content)
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.UPLOAD_FILE_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.UPLOAD_FILE_FAILURE.value),
                e)

        return Response(json.dumps(
            http.response(ApiCode.SUCCESS.value,
                          ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                          ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value))),
                        200,
                        mimetype="application/json")
Beispiel #22
0
    def cleanup_inactive_deployments(self):
        """ ! can be executed only after /deployments DELETE ! """
        try:
            deleted_folders = DockerUtils.folder_clean_up()
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.DEPLOYMENTS_FOLDER_CLEANUP_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.DEPLOYMENTS_FOLDER_CLEANUP_FAILURE.value), e)

        return Response(json.dumps(HttpResponse().response(
            ApiCode.SUCCESS.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
            deleted_folders)),
                        200,
                        mimetype="application/json")
Beispiel #23
0
    def receive_prepared_deployment_and_unpack(self):
        token = token_hex(8)
        io_utils = IOUtils()
        deployment_id = request.headers.get("Deployment-Id").lower(
        ) if request.headers.get("Deployment-Id") else token
        deploy_dir = f"{EnvInit.init.get(EnvConstants.DEPLOY_PATH)}/{deployment_id}"
        file_path = f"{deploy_dir}/archive.zip"
        file_content = request.get_data()
        # send here the complete env. The deployment template can be overridden at deploy start
        if not file_content:
            raise ApiExceptionDocker(
                ApiCode.EMPTY_REQUEST_BODY_PROVIDED.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.EMPTY_REQUEST_BODY_PROVIDED.value),
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.EMPTY_REQUEST_BODY_PROVIDED.value))

        try:
            io_utils.create_dir(deploy_dir)
            io_utils.write_to_file_binary(file_path, file_content)
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.UPLOAD_FILE_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.UPLOAD_FILE_FAILURE.value),
                e)
        try:
            shutil.unpack_archive(file_path, deploy_dir)
            io_utils.remove_file(file_path)
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.FOLDER_UNZIP_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.FOLDER_UNZIP_FAILURE.value),
                e)

        return Response(json.dumps(HttpResponse().response(
            ApiCode.SUCCESS.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value), deployment_id)),
                        200,
                        mimetype="application/json")
    def send_http_request(self, app, request_object):
        logging.debug({
            "url": f'{self.get_url(app)}{request_object.get("uri")}',
            "method": request_object.get('method'),
            "headers": request_object.get("headers"),
            "data": request_object.get("data")
        })

        try:
            response = requests.request(
                method=request_object.get('method'),
                url=f'{self.get_url(app)}{request_object.get("uri")}',
                headers=request_object.get("headers"),
                data=request_object.get('data'),
                timeout=10,
                verify=False)
        except Exception as e:
            response = HttpResponse.response(
                ApiCode.TARGET_UNREACHABLE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.TARGET_UNREACHABLE.value) %
                f'{self.get_url(app)}{request_object.get("uri")}',
                "Exception({})".format(e.__str__()))

        return response
Beispiel #25
0
    def get_deployment_status(self, env_id):
        env_id = env_id.lower()
        docker_utils = DockerUtils()
        env_id = env_id.strip()

        try:
            status = docker_utils.ps(env_id)
            if "Cannot connect to the Docker daemon".lower() in status.get(
                    'err').lower():
                raise Exception(status.get('err'))
            result = status.get('out').split("\n")[1:]
            app.logger.debug({"msg": status})
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.DEPLOY_STATUS_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.DEPLOY_STATUS_FAILURE.value), e)

        return Response(json.dumps(HttpResponse().response(
            ApiCode.SUCCESS.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
            ActiveDeployment.docker_deployment(env_id, result))),
                        200,
                        mimetype="application/json")
Beispiel #26
0
 def get_init_env(self):
     return Response(json.dumps(HttpResponse().response(
         ApiCode.SUCCESS.value,
         ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value), EnvInit.init)),
                     200,
                     mimetype="application/json")
Beispiel #27
0
def agents_request(text):
    path = text.strip()
    eureka = Eureka(
        EnvStartupSingleton.get_instance().get_config_env_vars().get(
            EnvConstants.EUREKA_SERVER))
    ip_addr_port_header_key = 'IpAddr-Port'  # target specific agent
    home_page_url_header_key = 'HomePageUrl'  # target specific agent
    application = "agent"
    try:
        input_data = request.get_data()
    except:
        input_data = ""

    try:
        request_object = {
            "uri": path.lstrip("/"),
            "method": request.method,
            "headers": request.headers,
            "data": input_data
        }
        app.logger.debug({"msg": f"{request_object}"})
        agent_apps = eureka.get_type_eureka_apps(application)

        if request.headers.get(f"{ip_addr_port_header_key}"):  # not mandatory
            ip_port_list = request.headers.get(
                f"{ip_addr_port_header_key}").split(",")
            agent_apps = [
                app for app in agent_apps
                if f"{app.get('ipAddr')}:{app.get('port')}" in ip_port_list
            ]

        if request.headers.get(f"{home_page_url_header_key}"):  # not mandatory
            home_page_url_list = request.headers.get(
                f"{home_page_url_header_key}").split(",")
            agent_apps = [
                app for app in agent_apps
                if f"{app.get('homePageUrl')}" in home_page_url_list
            ]

        th_utils = ThreadUtils(apps=agent_apps, headers={})
        th_utils.spawn_threads_send_request(request_object)

    except Exception as e:
        raise ApiException(
            ApiCode.DISCOVERY_ERROR.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.DISCOVERY_ERROR.value), e)

    threads_response = th_utils.get_threads_response()

    if th_utils.is_response_zip():
        folder_path = f"{th_utils.download_folder}/{th_utils.get_source_zip_folder()}"
        archive_path = f"{th_utils.download_folder}/{th_utils.get_source_zip_folder()}"
        try:
            IOUtils.zip_file(archive_path, folder_path)
        except Exception as e:
            raise ApiException(
                ApiCode.FOLDER_ZIP_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.FOLDER_ZIP_FAILURE.value) %
                folder_path, e)
        IOUtils.delete_file(folder_path)

        return flask.send_file(f"{archive_path}.zip",
                               mimetype='application/zip',
                               as_attachment=True), 200

    return Response(json.dumps(HttpResponse().response(
        ApiCode.SUCCESS.value,
        ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value), threads_response)),
                    200,
                    mimetype="application/json")
Beispiel #28
0
 def about(self):
     return Response(json.dumps(HttpResponse().response(
         ApiCode.SUCCESS.value,
         ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value), about_system)),
                     200,
                     mimetype="application/json")
Beispiel #29
0
    def start_deployment(self):
        docker_utils = DockerUtils()
        token = token_hex(8)
        deployment_id = request.headers.get("Deployment-Id").lower(
        ) if request.headers.get("Deployment-Id") else token
        deploy_dir = f"{EnvInit.init.get(EnvConstants.DEPLOY_PATH)}/{deployment_id}"
        file = f"{deploy_dir}/docker-compose.yml"
        header_key = 'Eureka-Server'
        eureka_server_header = request.headers.get(f"{header_key}")
        config_env_vars = EnvStartupSingleton.get_instance(
        ).get_config_env_vars()
        input_data = request.data.decode('UTF-8').strip()

        status = CmdUtils.run_cmd_shell_false(["docker", "ps"])
        if "Cannot connect to the Docker daemon".lower() in status.get(
                'err').lower():
            raise ApiExceptionDocker(
                ApiCode.DOCKER_DAEMON_NOT_RUNNING.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.DOCKER_DAEMON_NOT_RUNNING.value),
                status.get('err'))

        active_deployments = docker_utils.get_active_deployments()
        if len(active_deployments) >= EnvInit.init.get(
                EnvConstants.MAX_DEPLOYMENTS):
            raise ApiExceptionDocker(
                ApiCode.MAX_DEPLOYMENTS_REACHED.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.MAX_DEPLOYMENTS_REACHED.value) %
                str(EnvInit.init.get(EnvConstants.MAX_DEPLOYMENTS)),
                active_deployments)
        try:
            template_file_name = f"deployment_{deployment_id}.yml"
            template_file_path = f"{EnvInit.init.get(EnvConstants.TEMPLATES_DIR)}/{template_file_name}"
            app.logger.debug({
                "msg": {
                    "file": template_file_path,
                    "file_content": f"{input_data}"
                }
            })
            IOUtils.write_to_file(template_file_path, input_data)

            IOUtils.create_dir(deploy_dir)
            EnvironmentSingleton.get_instance().set_env_var(
                EnvConstants.TEMPLATE, template_file_name)
            env_vars = EnvironmentSingleton.get_instance(
            ).get_env_and_virtual_env()
            render = Render(env_vars.get(EnvConstants.TEMPLATE),
                            env_vars.get(EnvConstants.VARIABLES))
            if config_env_vars.get(
                    EnvConstants.EUREKA_SERVER) and config_env_vars.get(
                        EnvConstants.APP_IP_PORT):
                # if {{app_ip_port}} and {{eureka_server}} then register that instance too
                if '{{app_ip_port}}' in input_data and '{{eureka_server}}' in input_data:
                    eureka_server = config_env_vars.get(
                        EnvConstants.EUREKA_SERVER)
                    # header value overwrite the eureka server
                    if eureka_server_header:
                        eureka_server = eureka_server_header
                    input_data = render.get_jinja2env().get_template(
                        env_vars.get(EnvConstants.TEMPLATE)).render({
                            "deployment_id":
                            f"{deployment_id}",
                            "eureka_server":
                            eureka_server,
                            "app_ip_port":
                            config_env_vars.get(
                                EnvConstants.APP_IP_PORT).split("/")[0]
                        })
            os.remove(template_file_path) if os.path.exists(
                template_file_path) else None
            app.logger.debug(
                {"msg": {
                    "file": file,
                    "file_content": f"{input_data}"
                }})
            IOUtils.write_to_file(file, input_data) if input_data else None
            CmdUtils.run_cmd_detached(
                rf'''docker-compose -f {file} pull && docker-compose -f {file} up -d'''
            )
        except Exception as e:
            app.logger.debug({"msg": docker_utils.down(file)})
            raise ApiExceptionDocker(
                ApiCode.DEPLOY_START_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.DEPLOY_START_FAILURE.value),
                e)

        DeploymentMetadataSingleton.get_instance() \
            .delete_metadata_for_inactive_deployments(DockerUtils.get_active_deployments())
        metadata = DeploymentReader.get_metadata_for_deployment(
            IOUtils.read_file(file=file))
        IOUtils.write_to_file_dict(f"{deploy_dir}/metadata.json", metadata)
        DeploymentMetadataSingleton.get_instance().set_metadata_for_deployment(
            deployment_id, metadata)

        return Response(json.dumps(HttpResponse().response(
            ApiCode.SUCCESS.value,
            ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value), deployment_id)),
                        200,
                        mimetype="application/json")
Beispiel #30
0
    def start_deployment_with_templates(self, template, variables):
        http = HttpResponse()
        docker_utils = DockerUtils()
        token = token_hex(8)
        deployment_id = request.headers.get("Deployment-Id").lower(
        ) if request.headers.get("Deployment-Id") else token
        deploy_dir = f"{EnvInit.init.get(EnvConstants.DEPLOY_PATH)}/{deployment_id}"
        file = f"{deploy_dir}/docker-compose.yml"

        try:
            input_json = request.get_json(force=True)
            for key, value in input_json.items():
                if key not in EnvironmentSingleton.get_instance().get_env():
                    EnvironmentSingleton.get_instance().set_env_var(
                        str(key), str(value))
        except Exception as e:
            app.logger.debug(
                f"Could not parse the input from the request as JSON: {e.__str__()}"
            )

        EnvironmentSingleton.get_instance().set_env_var(
            EnvConstants.TEMPLATE, template.strip())
        EnvironmentSingleton.get_instance().set_env_var(
            EnvConstants.VARIABLES, variables.strip())
        env_vars = EnvironmentSingleton.get_instance().get_env_and_virtual_env(
        )
        app.logger.debug(
            {"msg": {
                "template_file": env_vars.get(EnvConstants.TEMPLATE)
            }})
        app.logger.debug(
            {"msg": {
                "variables_file": env_vars.get(EnvConstants.VARIABLES)
            }})

        status = CmdUtils.run_cmd_shell_false(["docker", "ps"])
        if "Cannot connect to the Docker daemon".lower() in status.get(
                'err').lower():
            raise ApiExceptionDocker(
                ApiCode.DOCKER_DAEMON_NOT_RUNNING.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.DOCKER_DAEMON_NOT_RUNNING.value),
                status.get('err'))

        active_deployments = docker_utils.get_active_deployments()
        if len(active_deployments) >= EnvInit.init.get(
                EnvConstants.MAX_DEPLOYMENTS):
            raise ApiExceptionDocker(
                ApiCode.MAX_DEPLOYMENTS_REACHED.value,
                ErrorMessage.HTTP_CODE.get(
                    ApiCode.MAX_DEPLOYMENTS_REACHED.value) %
                str(EnvInit.init.get(EnvConstants.MAX_DEPLOYMENTS)),
                active_deployments)
        try:
            r = Render(env_vars.get(EnvConstants.TEMPLATE),
                       env_vars.get(EnvConstants.VARIABLES))
            IOUtils.create_dir(deploy_dir)
            IOUtils.write_to_file(file, r.rend_template())
            CmdUtils.run_cmd_detached(
                rf'''docker-compose -f {file} pull && docker-compose -f {file} up -d'''
            )
        except Exception as e:
            raise ApiExceptionDocker(
                ApiCode.DEPLOY_START_FAILURE.value,
                ErrorMessage.HTTP_CODE.get(ApiCode.DEPLOY_START_FAILURE.value),
                e)

        DeploymentMetadataSingleton.get_instance() \
            .delete_metadata_for_inactive_deployments(DockerUtils.get_active_deployments())
        metadata = DeploymentReader.get_metadata_for_deployment(
            IOUtils.read_file(file=file))
        IOUtils.write_to_file_dict(f"{deploy_dir}/metadata.json", metadata)
        DeploymentMetadataSingleton.get_instance().set_metadata_for_deployment(
            deployment_id, metadata)

        return Response(json.dumps(
            http.response(ApiCode.SUCCESS.value,
                          ErrorMessage.HTTP_CODE.get(ApiCode.SUCCESS.value),
                          deployment_id)),
                        200,
                        mimetype="application/json")