コード例 #1
0
ファイル: dev.py プロジェクト: pistatium/houkago_app
def app_update(request, app_id, context={}):
    context["title"] = u"アプリ情報更新"
    context["description"] = u"こちらで登録したアプリの情報を修正できます。\n修正がサイト上で反映されるまでしばらく時間がかかります。\n\n多くのフォームは任意入力ですが、アプリについてより沢山の情報を入力することで検索による集客が見込めます。"
    app = App.getById(int(app_id))
    if app is None:
        return HttpResponseRedirect(reverse(app_regist))

     # POST
    if request.method == 'POST':
        #developer = models.DeveloperModel()
        form = AppFormUpdate(request.POST)
        if form.is_valid():
            params = form.cleaned_data
            params["developer_id"] = (context["developer"]).key.id()
            app = App.save(params, instance=app)
            app.put()
            return HttpResponseRedirect(reverse(index))
        else:
            context["form"] = form
            return render_to_response('webfront/regist_form.html', context)
    # GET
    else:
        form = AppFormUpdate()
        form.setParams(app)

        context["form"] = form
        return render_to_response('webfront/regist_form.html', context)
コード例 #2
0
ファイル: home.py プロジェクト: pistatium/houkago_app
def index(request, context):
    context["developers"] = Developer.getQuery().fetch(4)
    context["recent_apps"] = {}
    pickups = App.getPickup()
    context["pickup_apps"] = pickups[0:2]
    context["pickup_apps_after"] = pickups[2:]
    context["has_more"] = {}
    context["apps"] = App.getRecentQuery().fetch(18)
    return render_to_response("webfront/index.html", context)
コード例 #3
0
ファイル: dev.py プロジェクト: pistatium/houkago_app
def update_push(request, context):
    if request.method == 'POST':
        
        form = PushForm(request.POST)
        if form.is_valid():
            params = form.cleaned_data
            context["debug"] = params 
            developer_id = (context["developer"]).key.id()
            App.update_push(developer_id, params)
    return HttpResponseRedirect(reverse(index))
コード例 #4
0
ファイル: home.py プロジェクト: pistatium/houkago_app
def app_detail(request, app_id, context):
    app = App.getById(int(app_id))
    if not app or app.status != 1:
        raise Http404
    context["app"] = app
    developer = Developer.get_by_id(app.developer_id)
    # if not developer.status != 1:
    #    raise Http404
    context["developer"] = developer
    context["push_app"] = App.getPush(app.developer_id, app.platform)
    # 自身が含まれている可能性があるので +1件
    context["related_app"] = App.getRecentQuery(app.platform, app.category).fetch(3 + 1)
    return render_to_response("webfront/app_detail.html", context)
コード例 #5
0
ファイル: dev.py プロジェクト: pistatium/houkago_app
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)
コード例 #6
0
ファイル: aff.py プロジェクト: pistatium/houkago_app
def to_home(request, app_id):
    app = App.getById(int(app_id))
    if app:
        app.affiriate_point_total += 2
        app.affiriate_point += 2
        app.put()
    return HttpResponseRedirect(reverse(views.home.index))
コード例 #7
0
ファイル: app.py プロジェクト: crane-cloud/backend
    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
コード例 #8
0
ファイル: aff.py プロジェクト: pistatium/houkago_app
def to_app(request, app_id):
    app = App.getById(int(app_id))
    if app:
        app.affiriate_point_total += 1
        app.affiriate_point += 1
        app.put()
    return HttpResponseRedirect(reverse(views.home.app_detail, args=[app_id]))
コード例 #9
0
ファイル: app.py プロジェクト: crane-cloud/backend
    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
コード例 #10
0
ファイル: home.py プロジェクト: pistatium/houkago_app
def user(request, user_alias, context):
    developer = Developer.getByAlias(user_alias)
    if not developer:
        raise Http404
    app = App.getQueryByDeveloper(developer.key.id())
    context["developer"] = developer
    context["apps"] = app
    context["platforms"] = platforms
    return render_to_response("webfront/developer_detail.html", context)
コード例 #11
0
ファイル: home.py プロジェクト: pistatium/houkago_app
def app_list(request, plat_str, page, context):
    COUNT = 24
    platform = get_platform_id(plat_str)
    query = App.getRecentQuery(platform)
    p = GAEPaginator(query, COUNT)
    context["plat_str"] = plat_str
    context["page"] = page
    context["categories"] = categories
    context["apps"] = p.page(page)
    return render_to_response("webfront/app_list.html", context)
コード例 #12
0
ファイル: api.py プロジェクト: pistatium/houkago_app
def _detail_developer(request, option={}):
    if "developer_id" in option:
        developer = Developer.get_by_id(option["developer_id"])
    else:
        developer = Developer.getByAlias(option["developer_alias"])
    if not developer:
        return {"status": -2, "error": "invalid developer"}
    app = App.getQueryByDeveloper(developer.key.id())
    context["developer"] = developer
    context["apps"] = app
    context["platforms"] = platforms
コード例 #13
0
ファイル: api.py プロジェクト: pistatium/houkago_app
def _recent_app(request, option = {}):
    platform = arrays.get_platform_id(option["plat_str"])
    params = request.GET.copy()
    params["platform"] = platform
    form = apiform.RecentAppForm(params)
    if not form.is_valid():
        return {"status": -1, "thread_id":'', "error": form.errors}
    params = form.cleaned_data
    return {
        "status": 1,
        "apps": App.getRecentQuery(platform).fetch(params["count"], offset=params["offset"])
    }
コード例 #14
0
ファイル: app.py プロジェクト: crane-cloud/backend
    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
コード例 #15
0
ファイル: api.py プロジェクト: pistatium/houkago_app
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,
    }
コード例 #16
0
ファイル: api.py プロジェクト: pistatium/houkago_app
def _developer_apps(request, option={}):
    developer = Developer.getByAlias(option["developer_alias"])
    if not developer:
        return {"status": -2, "error": "invalid developer"}
    platform = arrays.get_platform_id(option["plat_str"])
    params = request.GET.copy()
    params["platform"] = platform
    form = apiform.RecentAppForm(params)
    if not form.is_valid():
        return {"status": -1, "thread_id":'', "error": form.errors}
    params = form.cleaned_data
    app = App.getQueryByDeveloper(developer.key.id(), params["platform"])
    return {
        "status": 1,
        "apps": sorted(app.fetch(params["count"], offset=params["offset"]), key=lambda x:x.created_at, reverse=True)
    }
コード例 #17
0
ファイル: dev.py プロジェクト: pistatium/houkago_app
def app_regist(request, context):
     # POST
    context["title"] = u"アプリ情報登録"
    context["description"] = u"こちらからアプリの詳細を入力してください。\n一部フォームは任意入力ですが、アプリについてより多くの情報を入力すると検索などから流入増加が見込めます。"
    if request.method == 'POST':
        #developer = models.DeveloperModel()
        form = AppForm(request.POST)
        if form.is_valid():
            params = form.cleaned_data
            params["developer_id"] = context["developer"].key.id()
            app = App.create(params)
            app.put()
            return HttpResponseRedirect(reverse(index) + "?app_registed=1")
        else:
            context["form"] = form
            return render_to_response('webfront/regist_form.html', context)
    # GET
    else:
        form = AppForm(initial={
        })
        context["form"] = form
        return render_to_response('webfront/regist_form.html', context)
コード例 #18
0
ファイル: app.py プロジェクト: crane-cloud/backend
    def post(self, project_id):
        """
        """

        resource_registry = {
            'db_deployment': False,
            'db_service': False,
            'image_pull_secret': False,
            'app_deployment': False,
            'app_service': False,
            'ingress_entry': False
        }

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

        app_schema = AppSchema()

        app_data = request.get_json()

        validated_app_data, errors = app_schema.load(app_data,
                                                     partial=("project_id", ))

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

        existing_app = App.find_first(name=validated_app_data['name'],
                                      project_id=project_id)

        if existing_app:
            return dict(
                status='fail',
                message=
                f'App with name {validated_app_data["name"]} already exists'
            ), 409

        validated_app_data['port'] = validated_app_data.get('port', 80)

        app_name = validated_app_data['name']
        app_alias = create_alias(validated_app_data['name'])
        app_image = validated_app_data['image']
        command = validated_app_data.get('command', None)
        # env_vars = validated_app_data['env_vars']
        env_vars = validated_app_data.get('env_vars', None)
        private_repo = validated_app_data.get('private_image', False)
        docker_server = validated_app_data.get('docker_server', None)
        docker_username = validated_app_data.get('docker_username', None)
        docker_password = validated_app_data.get('docker_password', None)
        docker_email = validated_app_data.get('docker_email', None)
        replicas = validated_app_data.get('replicas', 1)
        app_port = validated_app_data.get('port', None)
        image_pull_secret = None

        command = command.split() if command else None

        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

        cluster = project.cluster
        namespace = project.alias

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

        # check if app already exists
        app = App.find_first(**{'name': app_name})

        if app:
            return dict(status='fail',
                        message=f'App {app_name} already exists'), 409

        kube_host = cluster.host
        kube_token = cluster.token
        service_host = urlsplit(kube_host).hostname

        kube_client = create_kube_clients(kube_host, kube_token)

        try:

            # create the app
            new_app = App(name=app_name,
                          image=app_image,
                          project_id=project_id,
                          alias=app_alias,
                          port=app_port)

            if private_repo:

                # handle gcr credentials
                if 'gcr' in docker_server and docker_username == '_json_key':
                    docker_password = json.dumps(
                        json.loads(base64.b64decode(docker_password)))

                # create image pull secrets
                authstring = base64.b64encode(
                    f'{docker_username}:{docker_password}'.encode("utf-8"))

                secret_dict = dict(
                    auths={
                        docker_server: {
                            "username": docker_username,
                            "password": docker_password,
                            "email": docker_email,
                            "auth": str(authstring, "utf-8")
                        }
                    })

                secret_b64 = base64.b64encode(
                    json.dumps(secret_dict).encode("utf-8"))

                secret_body = client.V1Secret(
                    metadata=client.V1ObjectMeta(name=app_alias),
                    type='kubernetes.io/dockerconfigjson',
                    data={'.dockerconfigjson': str(secret_b64, "utf-8")})

                kube_client.kube.create_namespaced_secret(
                    namespace=namespace,
                    body=secret_body,
                    _preload_content=False)

                # update registry
                resource_registry['image_pull_secret'] = True

                image_pull_secret = client.V1LocalObjectReference(
                    name=app_alias)

            # create app deployment's pvc meta and spec
            # pvc_name = f'{app_alias}-pvc'
            # pvc_meta = client.V1ObjectMeta(name=pvc_name)

            # access_modes = ['ReadWriteOnce']
            # storage_class = 'openebs-standard'
            # resources = client.V1ResourceRequirements(
            #     requests=dict(storage='1Gi'))

            # pvc_spec = client.V1PersistentVolumeClaimSpec(
            #     access_modes=access_modes, resources=resources, storage_class_name=storage_class)

            # Create a PVC
            # pvc = client.V1PersistentVolumeClaim(
            #     api_version="v1",
            #     kind="PersistentVolumeClaim",
            #     metadata=pvc_meta,
            #     spec=pvc_spec
            # )

            # kube_client.kube.create_namespaced_persistent_volume_claim(
            #     namespace=namespace,
            #     body=pvc
            # )

            # create deployment
            dep_name = f'{app_alias}-deployment'

            # # EnvVar
            env = []
            if env_vars:
                for key, value in env_vars.items():
                    env.append(client.V1EnvVar(name=str(key),
                                               value=str(value)))

            # pod template
            container = client.V1Container(
                name=app_alias,
                image=app_image,
                ports=[client.V1ContainerPort(container_port=app_port)],
                env=env,
                command=command
                # volume_mounts=[client.V1VolumeMount(mount_path="/data", name=dep_name)]
            )

            #pod volumes
            # volumes = client.V1Volume(
            #     name=dep_name
            #     # persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(claim_name=pvc_name)
            # )

            # spec
            template = client.V1PodTemplateSpec(
                metadata=client.V1ObjectMeta(labels={'app': app_alias}),
                spec=client.V1PodSpec(containers=[container],
                                      image_pull_secrets=[image_pull_secret]
                                      # volumes=[volumes]
                                      ))

            # spec of deployment
            spec = client.V1DeploymentSpec(
                replicas=replicas,
                template=template,
                selector={'matchLabels': {
                    'app': app_alias
                }})

            # Instantiate the deployment
            deployment = client.V1Deployment(
                api_version="apps/v1",
                kind="Deployment",
                metadata=client.V1ObjectMeta(name=dep_name),
                spec=spec)

            # create deployment in  cluster

            kube_client.appsv1_api.create_namespaced_deployment(
                body=deployment, namespace=namespace, _preload_content=False)

            # update registry
            resource_registry['app_deployment'] = True

            # create service in the cluster
            service_name = f'{app_alias}-service'

            service_meta = client.V1ObjectMeta(name=service_name,
                                               labels={'app': app_alias})

            service_spec = client.V1ServiceSpec(
                type='ClusterIP',
                ports=[client.V1ServicePort(port=3000, target_port=app_port)],
                selector={'app': app_alias})

            service = client.V1Service(metadata=service_meta,
                                       spec=service_spec)

            kube_client.kube.create_namespaced_service(namespace=namespace,
                                                       body=service,
                                                       _preload_content=False)

            # update resource registry
            resource_registry['app_service'] = True

            # subdomain for the app
            # sub_domain = f'{app_alias}.cranecloud.io'
            sub_domain = get_app_subdomain(app_alias)

            # create new ingres rule for the application
            new_ingress_backend = client.ExtensionsV1beta1IngressBackend(
                service_name=service_name, service_port=3000)

            new_ingress_rule = client.ExtensionsV1beta1IngressRule(
                host=sub_domain,
                http=client.ExtensionsV1beta1HTTPIngressRuleValue(paths=[
                    client.ExtensionsV1beta1HTTPIngressPath(
                        path="", backend=new_ingress_backend)
                ]))

            ingress_name = f'{project.alias}-ingress'

            # Check if there is an ingress resource in the namespace, create if not

            ingress_list = kube_client.extension_api.list_namespaced_ingress(
                namespace=namespace).items

            if not ingress_list:

                ingress_meta = client.V1ObjectMeta(name=ingress_name)

                ingress_spec = client.ExtensionsV1beta1IngressSpec(
                    # backend=ingress_backend,
                    rules=[new_ingress_rule])

                ingress_body = client.ExtensionsV1beta1Ingress(
                    metadata=ingress_meta, spec=ingress_spec)

                kube_client.extension_api.create_namespaced_ingress(
                    namespace=namespace, body=ingress_body)

                # update registry
                resource_registry['ingress_entry'] = True
            else:
                # Update ingress with new entry
                ingress = ingress_list[0]

                ingress.spec.rules.append(new_ingress_rule)

                kube_client.extension_api.patch_namespaced_ingress(
                    name=ingress_name, namespace=namespace, body=ingress)

            service_url = f'https://{sub_domain}'

            new_app.url = service_url

            saved = new_app.save()

            if not saved:
                return dict(status='fail',
                            message='Internal Server Error'), 500

            new_app_data, _ = app_schema.dump(new_app)

            return dict(status='success', data=dict(app=new_app_data)), 201

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

        except Exception as e:
            resource_clean_up(resource_registry, app_alias, namespace,
                              kube_client)
            return dict(status='fail', message=str(e)), 500
コード例 #19
0
ファイル: dev.py プロジェクト: pistatium/houkago_app
def app_detail(request, app_id, context={}):
    app = App.getById(int(app_id))
    if app is None:
        return HttpResponseRedirect(reverse(app_regist))
    context["app"] = app
    return render_to_response('webfront/app_detail.html',context)        
コード例 #20
0
ファイル: dev.py プロジェクト: pistatium/houkago_app
def index(request, context):
    context["app_registed"] = request.GET.get("app_registed", None)
    apps = App.getQueryByDeveloper(context["developer"].key.id()).order(-App.created_at)
    context["apps"] = apps
    return render_to_response('webfront/dev_index.html',context)
コード例 #21
0
ファイル: home.py プロジェクト: pistatium/houkago_app
def app_rss(request, platform=None):
    context = RequestContext(request, {"platforms": platforms})
    context["apps"] = App.getRecentQuery(platform).fetch(50)
    return render_to_response("webfront/app_rss.xml", context, mimetype="application/xml")
コード例 #22
0
ファイル: home.py プロジェクト: pistatium/houkago_app
def sitemap(request):
    context = RequestContext(request, {})
    context["apps"] = App.getRecentQuery(None)
    context["developers"] = Developer.getQuery()
    return render_to_response("webfront/sitemap.xml", context, mimetype="application/xml")
コード例 #23
0
ファイル: app.py プロジェクト: crane-cloud/backend
    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
コード例 #24
0
ファイル: app.py プロジェクト: crane-cloud/backend
    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
コード例 #25
0
ファイル: app.py プロジェクト: crane-cloud/backend
    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