Exemplo n.º 1
0
    def delete(self, app_name):
        fmlogging.debug("Received DELETE request for app %s" % app_name)
        resp_data = {}

        response = jsonify(**resp_data)
        app_info = {}

        app_obj = app_db.App().get_by_name(app_name)
        if app_obj:
            app_info['target'] = app_obj.dep_target
            app_info['app_name'] = app_obj.name
            app_info['app_location'] = app_obj.location
            app_info['app_version'] = app_obj.version
            app_info['env_id'] = app_obj.env_id

            request_handler_thread = app_handler.AppHandler(app_obj.id,
                                                            app_info,
                                                            action='delete')
            thread.start_new_thread(start_thread, (request_handler_thread, ))
            response.status_code = 202
            # TODO(devdatta) Let the user know that the image for GCR needs to be deleted manually.
            if app_obj.dep_target == 'gcloud':
                resp_data = {}
                response = jsonify(**resp_data)
                response.status_code = 303
        else:
            response.status_code = 404
        return response
Exemplo n.º 2
0
 def get(self):
     fmlogging.debug("Received GET request for all apps")
     resp_data = {}
     all_apps = app_db.App().get_all()
     resp_data['data'] = [app_db.App.to_json(app) for app in all_apps]
     response = jsonify(**resp_data)
     response.status_code = 200
     return response
Exemplo n.º 3
0
    def get(self, app_name):
        resp_data = {}
        response = jsonify(**resp_data)
        app = app_db.App().get_by_name(app_name)
        if app:
            resp_data['data'] = app_db.App.to_json(app)
            response = jsonify(**resp_data)
            response.status_code = 200
        else:
            response.status_code = 404

        return response
Exemplo n.º 4
0
    def put(self, app_name):
        fmlogging.debug("Received PUT request to redeploy app")

        args = request.get_json(force=True)

        response = jsonify()
        response.status_code = 202

        args_dict = dict(args)
        app_obj = app_db.App().get_by_name(app_name)
        try:
            if 'app_info' not in args_dict:
                response.status_code = 400
            else:
                if app_obj:
                    app_info = args_dict['app_info']
                    app_name = app_obj.name
                    app_tar_name = app_info['app_tar_name']
                    content = app_info['app_content']
                    app_info['app_name'] = app_name

                    cloud = app_obj.dep_target
                    app_info['target'] = cloud
                    app_info['env_id'] = app_obj.env_id

                    app_version = app_obj.version
                    app_location, _ = common_functions.store_app_contents(
                        app_name,
                        app_tar_name,
                        content,
                        app_version=app_version)
                    app_info['app_location'] = app_location
                    app_info['app_version'] = app_version
                    request_handler_thread = app_handler.AppHandler(
                        app_obj.id, app_info, action='redeploy')
                    thread.start_new_thread(start_thread,
                                            (request_handler_thread, ))
                    response.headers['location'] = ('/apps/{app_name}').format(
                        app_name=app_name)
                else:
                    response.status_code = 404
        except Exception as e:
            fmlogging.error(e)
            response.status_code = 500

        return response
Exemplo n.º 5
0
    def get(self, env_name):
        resp_data = {}
        response = jsonify(**resp_data)
        env = env_db.Environment().get_by_name(env_name)
        if env:
            app_json = ''
            app_list = app_db.App().get_apps_for_env(env.id)
            if app_list:
                app_json = [
                    app_db.App.to_json_restricted(app) for app in app_list
                ]

            resp_data['data'] = env_db.Environment.to_json(env, app_json)
            response = jsonify(**resp_data)
            response.status_code = 200
        else:
            response.status_code = 404
        return response
Exemplo n.º 6
0
    def delete(self, env_name):
        fmlogging.debug("Received DELETE request for environment %s" %
                        env_name)
        resp_data = {}
        response = jsonify(**resp_data)

        environment_info = {}

        env = env_db.Environment().get_by_name(env_name)
        if env:
            if not request.args.get("force"):
                app_list = app_db.App().get_apps_for_env(env.id)
                if app_list and len(app_list) > 0:
                    response.status_code = 412
                    response.status_message = 'Environment cannot be deleted as there are applications still running on it.'
                    return response
            environment_name = env.name
            environment_def = env.env_definition
            environment_info['name'] = environment_name
            environment_info['location'] = env.location
            request_handler_thread = environment_handler.EnvironmentHandler(
                env.id, environment_def, environment_info, action='delete')
            thread.start_new_thread(start_thread, (request_handler_thread, ))

            response.headers['location'] = ('/environments/{env_name}').format(
                env_name=environment_name)
            response.status_code = 202

            # Check if this was GKE environment. If so, notify user that the VPC network needs to be deleted manually.
            env_dict = ast.literal_eval(environment_def)
            cloud = env_dict['environment']['app_deployment']['target']
            if cloud == 'gcloud':
                resp_data = {}
                response = jsonify(**resp_data)
                response.status_code = 303
        else:
            response.status_code = 404
        return response
Exemplo n.º 7
0
    def get(self, app_name):
        resp_data = {}
        response = jsonify(**resp_data)
        app_obj = app_db.App().get_by_name(app_name)
        if app_obj:
            app_info = {}
            app_info['target'] = app_obj.dep_target
            app_info['app_name'] = app_obj.name
            app_info['app_location'] = app_obj.location
            app_info['app_version'] = app_obj.version
            app_output_config = ast.literal_eval(app_obj.output_config)
            app_info['app_folder_name'] = app_output_config['app_folder_name']
            app_info['env_id'] = app_obj.env_id

            request_handler = app_handler.AppHandler(app_obj.id, app_info)
            logs_data = request_handler.get_logs()

            resp_data['data'] = logs_data
            response = jsonify(**resp_data)
            response.status_code = 200
        else:
            response.status_code = 404

        return response
Exemplo n.º 8
0
 def _get_coe_type_for_app(self, app_id):
     app_obj = app_db.App().get(app_id)
     coe_type = self._get_coe_type(app_obj.env_id)
     return coe_type
Exemplo n.º 9
0
    def post(self):
        fmlogging.debug("Received POST request to deploy app")
        args = request.get_json(force=True)

        response = jsonify()
        response.status_code = 201

        args_dict = dict(args)

        try:
            if 'app_info' not in args_dict:
                response.status_code = 400
            else:
                app_info = args_dict['app_info']
                app_name = app_info['app_name']
                env_name = app_info['env_name']
                env_obj = env_db.Environment().get_by_name(env_name)
                if not env_obj:
                    message = (
                        "Environment with name {env_name} does not exist"
                    ).format(env_name=env_name)
                    fmlogging.debug(message)
                    resp_data = {'error': message}
                    response = jsonify(**resp_data)
                    response.status_code = 400
                    return response
                if not env_obj.status or env_obj.status != 'available':
                    message = 'Environment not ready.'
                    fmlogging.debug(message)
                    resp_data = {'error': message}
                    response = jsonify(**resp_data)
                    response.status_code = 412
                    return response
                app_id = ''
                app_location = ''
                app_version = ''
                cloud = ''
                app_data = {}
                try:
                    app_data['name'] = app_name
                    app_data['location'] = app_location
                    app_data['version'] = app_version
                    app_data['dep_target'] = cloud
                    app_data['env_id'] = env_obj.id
                    app_data['env_name'] = env_obj.name
                    app_id = app_db.App().insert(app_data)
                except Exception as e:
                    fmlogging.debug(e)
                    raise e
                if not app_id:
                    message = (
                        "App with name {app_name} already exists. Choose different name."
                    ).format(app_name=app_name)
                    fmlogging.debug(message)
                    resp_data = {'error': message}
                    response = jsonify(**resp_data)
                    response.status_code = 400
                    return response
                app_tar_name = app_info['app_tar_name']
                content = app_info['app_content']
                if 'target' in app_info:
                    cloud = app_info['target']
                else:
                    env_dict = ast.literal_eval(env_obj.env_definition)
                    cloud = env_dict['environment']['app_deployment']['target']
                    app_info['target'] = cloud

                app_location, app_version = common_functions.store_app_contents(
                    app_name, app_tar_name, content)
                app_info['app_location'] = app_location
                app_info['app_version'] = app_version
                app_info['env_id'] = env_obj.id

                app_data['location'] = app_location
                app_data['version'] = app_version
                app_data['dep_target'] = cloud
                app_data['app_yaml_contents'] = str(
                    common_functions.read_app_yaml(app_info))
                app_db.App().update(app_id, app_data)

                try:
                    validator.validate_app_deployment(app_info, app_data,
                                                      env_obj)
                except exceptions.AppDeploymentValidationFailure as e:
                    fmlogging.error(e)
                    message = e.get_message()
                    resp_data = {'error': message}
                    response = jsonify(**resp_data)
                    response.status_code = 400
                    return response

                request_handler_thread = app_handler.AppHandler(
                    app_id, app_info, action='deploy')
                thread.start_new_thread(start_thread,
                                        (request_handler_thread, ))
                response.headers['location'] = ('/apps/{app_name}').format(
                    app_name=app_name)
        except Exception as e:
            fmlogging.error(e)
            resp_data = {'error': str(e)}
            response = jsonify(**resp_data)
            response.status_code = 500

        return response