Beispiel #1
0
def upload_icon_img(request, app_id, context):
    app_id = long(app_id)
    app = App.get_by_id(app_id)
    if app.developer_id != context["developer"].key.id():
        # hoge = app.developer_id
        # fuga = context["developer"].key.id()
        return HttpResponseRedirect(reverse(index))
    view_url = reverse(upload_img)
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            params = form.cleaned_data
            image = AppImage.getEntity(app_id)
            image.image = params["file"]["img"]
            image.content_type = params["file"]["content_type"]
            image.put()
            context["debug"] = image
            return HttpResponseRedirect(reverse(index))

    upload_url, upload_data = prepare_upload(request, view_url)
    form = UploadForm()
    context["form"] = form
    context["upload_url"] = upload_url
    context["upload_data"] = upload_data
    return render_to_response('webfront/upload_form.html', context)
Beispiel #2
0
    def post(self, project_id, app_id):
        """
        """

        app_memory_schema = MetricsSchema()
        app_query_data = request.get_json()

        validated_query_data, errors = app_memory_schema.load(app_query_data)

        if errors:
            return dict(status='fail', message=errors), 400

        current_time = datetime.datetime.now()
        yesterday_time = current_time + datetime.timedelta(days=-1)

        start = validated_query_data.get('start', yesterday_time.timestamp())
        end = validated_query_data.get('end', current_time.timestamp())
        step = validated_query_data.get('step', '1h')

        current_user_id = get_jwt_identity()
        current_user_roles = get_jwt_claims()['roles']

        project = Project.get_by_id(project_id)

        if not project:
            return dict(status='fail',
                        message=f'project {project_id} not found'), 404

        app = App.get_by_id(app_id)

        if not app:
            return dict(status='fail', message=f'App {app_id} not found'), 404

        if not is_owner_or_admin(project, current_user_id, current_user_roles):
            return dict(status='fail', message='Unauthorised'), 403

        app_alias = app.alias
        namespace = project.alias

        prom_memory_data = prometheus.query_rang(
            start=start,
            end=end,
            step=step,
            metric=
            'sum(rate(container_memory_usage_bytes{container_name!="POD", image!="",pod=~"'
            + app_alias + '.*", namespace="' + namespace + '"}[5m]))')

        new_data = json.loads(prom_memory_data)
        final_data_list = []
        try:
            for value in new_data["data"]["result"][0]["values"]:
                mem_case = {
                    'timestamp': float(value[0]),
                    'value': float(value[1])
                }
                final_data_list.append(mem_case)
        except:
            return dict(status='fail', message='No values found'), 404

        return dict(status='success', data=dict(values=final_data_list)), 200
Beispiel #3
0
    def post(self, project_id, app_id):

        current_user_id = get_jwt_identity()
        current_user_roles = get_jwt_claims()['roles']

        app_memory_schema = MetricsSchema()
        app_cpu_data = request.get_json()

        validated_query_data, errors = app_memory_schema.load(app_cpu_data)

        if errors:
            return dict(status='fail', message=errors), 400

        project = Project.get_by_id(project_id)

        if not project:
            return dict(status='fail',
                        message=f'project {project_id} not found'), 404

        if not is_owner_or_admin(project, current_user_id, current_user_roles):
            return dict(status='fail', message='unauthorised'), 403

        # Check app from db
        app = App.get_by_id(app_id)

        if not app:
            return dict(status='fail', message=f'app {app_id} not found'), 404

        # Get current time
        current_time = datetime.datetime.now()
        yesterday = current_time + datetime.timedelta(days=-1)
        namespace = project.alias
        app_alias = app.alias

        prometheus = Prometheus()

        start = validated_query_data.get('start', yesterday.timestamp())
        end = validated_query_data.get('end', current_time.timestamp())
        step = validated_query_data.get('step', '1h')

        prom_data = prometheus.query_rang(
            start=start,
            end=end,
            step=step,
            metric=
            'sum(rate(container_cpu_usage_seconds_total{container!="POD", image!="", namespace="'
            + namespace + '", pod=~"' + app_alias + '.*"}[5m]))')
        #  change array values to json"values"
        new_data = json.loads(prom_data)
        cpu_data_list = []
        try:
            for value in new_data["data"]["result"][0]["values"]:
                case = {'timestamp': float(value[0]), 'value': float(value[1])}
                cpu_data_list.append(case)
        except:
            return dict(status='fail', message='No values found'), 404

        return dict(status='success', data=dict(values=cpu_data_list)), 200
Beispiel #4
0
    def post(self, project_id, app_id):

        current_user_id = get_jwt_identity()
        current_user_roles = get_jwt_claims()['roles']

        project = Project.get_by_id(project_id)

        if not project:
            return dict(status='fail',
                        message=f'project {project_id} not found'), 404

        if not is_owner_or_admin(project, current_user_id, current_user_roles):
            return dict(status='fail', message='unauthorised'), 403

        # Check app from db
        app = App.get_by_id(app_id)

        if not app:
            return dict(status='fail', message=f'app {app_id} not found'), 404

        namespace = project.alias
        app_alias = app.alias

        prometheus = Prometheus()

        try:
            prom_data = prometheus.query(
                metric=
                'sum(kube_persistentvolumeclaim_resource_requests_storage_bytes{namespace="'
                + namespace + '", persistentvolumeclaim=~"' + app_alias +
                '.*"})')
            #  change array values to json
            new_data = json.loads(prom_data)
            values = new_data["data"]

            percentage_data = prometheus.query(
                metric='100*(kubelet_volume_stats_used_bytes{namespace="' +
                namespace + '", persistentvolumeclaim=~"' + app_alias +
                '.*"}/kubelet_volume_stats_capacity_bytes{namespace="' +
                namespace + '", persistentvolumeclaim=~"' + app_alias +
                '.*"})')

            data = json.loads(percentage_data)
            volume_perc_value = data["data"]
        except:
            return dict(status='fail', message='No values found'), 404

        return dict(status='success',
                    data=dict(storage_capacity=values,
                              storage_percentage_usage=volume_perc_value)), 200
Beispiel #5
0
def _detail_app(request, option = {}):
    app_id = long(option["app_id"])
    params = request.GET.copy()
    form = apiform.DetailAppForm(params)
    if not form.is_valid():
        return {"status": -1, "error": form.errors}
    params = form.cleaned_data
    app = App.get_by_id(app_id)
    if not app or app.status != 1:
        return {"status": -2, "error": "invalid app_id"}
    developer = Developer.get_by_id(app.developer_id)
    return {
        "status": 1,
        "app": app,
        "developer": developer,
    }
Beispiel #6
0
    def delete(self, app_id):
        """
        """

        try:

            current_user_id = get_jwt_identity()
            current_user_roles = get_jwt_claims()['roles']

            app = App.get_by_id(app_id)

            if not app:
                return dict(status='fail',
                            message=f'app {app_id} not found'), 404

            project = app.project

            if not project:
                return dict(status='fail',
                            message='Internal server error'), 500

            if not is_owner_or_admin(project, current_user_id,
                                     current_user_roles):
                return dict(status='fail', message='Unauthorised'), 403

            cluster = project.cluster
            namespace = project.alias

            if not cluster or not namespace:
                return dict(status='fail',
                            message='Internal server error'), 500

            kube_host = cluster.host
            kube_token = cluster.token

            kube_client = create_kube_clients(kube_host, kube_token)

            # delete deployment and service for the app
            deployment_name = f'{app.alias}-deployment'
            service_name = f'{app.alias}-service'
            deployment = kube_client.appsv1_api.read_namespaced_deployment(
                name=deployment_name, namespace=namespace)

            if deployment:
                kube_client.appsv1_api.delete_namespaced_deployment(
                    name=deployment_name, namespace=namespace)

            service = kube_client.kube.read_namespaced_service(
                name=service_name, namespace=namespace)

            if service:
                kube_client.kube.delete_namespaced_service(name=service_name,
                                                           namespace=namespace)

            #delete pvc
            # pvc_name = f'{app.alias}-pvc'

            # pvc = kube_client.kube.read_namespaced_persistent_volume_claim(
            #     name=pvc_name,
            #     namespace=namespace
            # )

            # if pvc:
            #     kube_client.kube.delete_namespaced_persistent_volume_claim(
            #         name=pvc_name,
            #         namespace=namespace
            #     )

            # delete the app from the database
            deleted = app.delete()

            if not deleted:
                return dict(status='fail',
                            message='Internal server error'), 500

            return dict(status='success',
                        message=f'App {app_id} deleted successfully'), 200

        except client.rest.ApiException as e:
            return dict(status='fail', message=json.loads(e.body)), 500

        except Exception as e:
            return dict(status='fail', message=str(e)), 500
Beispiel #7
0
    def get(self, app_id):
        """
        """
        try:
            current_user_id = get_jwt_identity()
            current_user_roles = get_jwt_claims()['roles']

            app_schema = AppSchema()

            app = App.get_by_id(app_id)

            if not app:
                return dict(status='fail',
                            message=f'App {app_id} not found'), 404

            project = app.project

            if not project:
                return dict(status='fail',
                            message='Internal server error'), 500

            if not is_owner_or_admin(project, current_user_id,
                                     current_user_roles):
                return dict(status='fail', message='Unauthorised'), 403

            app_data, errors = app_schema.dumps(app)

            if errors:
                return dict(status='fail', message=errors), 500

            app_list = json.loads(app_data)

            cluster = Cluster.get_by_id(project.cluster_id)

            if not cluster:
                return dict(
                    status='fail',
                    message=
                    f'cluster with id {project.cluster_id} does not exist'
                ), 404

            kube_host = cluster.host
            kube_token = cluster.token
            kube_client = create_kube_clients(kube_host, kube_token)

            app_status_object = \
                kube_client.appsv1_api.read_namespaced_deployment_status(
                    app_list['alias']+"-deployment", project.alias)

            app_deployment_status_conditions = app_status_object.status.conditions

            for deplyoment_status_condition in app_deployment_status_conditions:
                if deplyoment_status_condition.type == "Available":
                    app_deployment_status = deplyoment_status_condition.status

            try:
                app_db_status_object = \
                    kube_client.appsv1_api.read_namespaced_deployment_status(
                        app_list['alias']+"-postgres-db", project.alias)

                app_db_state_conditions = app_db_status_object.status.conditions

                for app_db_condition in app_db_state_conditions:
                    if app_db_condition.type == "Available":
                        app_db_status = app_db_condition.status

            except client.rest.ApiException:
                app_db_status = None

            if app_deployment_status and not app_db_status:
                if app_deployment_status == "True":
                    app_list['app_running_status'] = "running"
                else:
                    app_list['app_running_status'] = "failed"
            elif app_deployment_status and app_db_status:
                if app_deployment_status == "True" and app_db_status == "True":
                    app_list['app_running_status'] = "running"
                else:
                    app_list['app_running_status'] = "failed"
            else:
                app_list['app_running_status'] = "unknown"

            return dict(status='success', data=dict(apps=app_list)), 200

        except client.rest.ApiException as exc:
            return dict(status='fail', message=exc.reason), exc.status

        except Exception as exc:
            return dict(status='fail', message=str(exc)), 500
Beispiel #8
0
    def post(self, project_id, app_id):

        current_user_id = get_jwt_identity()
        current_user_roles = get_jwt_claims()['roles']

        logs_schema = PodsLogsSchema()
        logs_data = request.get_json()

        validated_query_data, errors = logs_schema.load(logs_data)

        if errors:
            return dict(status='fail', message=errors), 400

        project = Project.get_by_id(project_id)

        if not project:
            return dict(status='fail',
                        message=f'project {project_id} not found'), 404

        if not is_owner_or_admin(project, current_user_id, current_user_roles):
            return dict(status='fail', message='unauthorised'), 403

        # Check app from db
        app = App.get_by_id(app_id)

        if not app:
            return dict(status='fail', message=f'app {app_id} not found'), 404

        cluster = project.cluster
        if not cluster:
            return dict(status='fail', message="Invalid Cluster"), 500

        kube_host = cluster.host
        kube_token = cluster.token
        kube_client = create_kube_clients(kube_host, kube_token)

        namespace = project.alias
        deployment = app.alias

        tail_lines = validated_query_data.get('tail_lines', 100)
        since_seconds = validated_query_data.get('since_seconds', 86400)
        timestamps = validated_query_data.get('timestamps', False)
        ''' Get Replicas sets'''
        replicas = kube_client.appsv1_api.list_namespaced_replica_set(
            namespace).to_dict()
        replicasList = []
        for replica in replicas['items']:
            name = replica['metadata']['name']
            if name.startswith(deployment):
                replicasList.append(name)
        ''' get pods list'''
        pods = kube_client.kube.list_namespaced_pod(namespace)
        podsList = []
        failed_pods = []
        for item in pods.items:
            item = kube_client.api_client.sanitize_for_serialization(item)
            pod_name = item['metadata']['name']

            try:
                status = item['status']['conditions'][1]['status']
            except:
                continue

            for replica in replicasList:
                if pod_name.startswith(replica):
                    if status == 'True':
                        podsList.append(pod_name)
                    else:
                        failed_pods.append(pod_name)
                    continue

            if pod_name.startswith(deployment):
                if status == 'True':
                    podsList.append(pod_name)
                else:
                    state = item['status']['containerStatuses'][0]['state']
                    failed_pods.append(state)
        ''' Get pods logs '''
        pods_logs = []

        for pod in podsList:
            podLogs = kube_client.kube.read_namespaced_pod_log(
                pod,
                namespace,
                pretty=True,
                tail_lines=tail_lines or 100,
                timestamps=timestamps or False,
                since_seconds=since_seconds or 86400)

            if podLogs == '':
                podLogs = kube_client.kube.read_namespaced_pod_log(
                    pod,
                    namespace,
                    pretty=True,
                    tail_lines=tail_lines or 100,
                    timestamps=timestamps or False)
            pods_logs.append(podLogs)

        # Get failed pods infor
        for state in failed_pods:
            if type(state) == dict:
                waiting = state.get('waiting')
                if waiting:
                    try:
                        stop = state['waiting']['message'].index('container')
                        message = state['waiting']['message'][:stop]
                    except:
                        message = state['waiting'].get('message')

                    reason = state['waiting']['reason']
                    pod_infor = f'type\tstatus\treason\t\t\tmessage\n----\t------\t------\t\t\t------\nwaiting\tfailed\t{reason}\t{message}'
                    pods_logs.append(pod_infor)

        if not pods_logs or not pods_logs[0]:
            return dict(status='fail', data=dict(message='No logs found')), 404

        return dict(status='success', data=dict(pods_logs=pods_logs)), 200