Exemplo n.º 1
0
 def __save_service_image_relation(self, tenant, service, service_image_relation):
     if service_image_relation:
         service_image_relation.pop("ID")
         new_image_relation = ImageServiceRelation(**service_image_relation)
         new_image_relation.tenant_id = tenant.tenant_id
         new_image_relation.service_id = service.service_id
         new_image_relation.save()
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        result = {}
        service_id = ""
        try:
            tenant_id = self.tenant.tenant_id
            service_id = request.POST.get("service_id", "")
            image_input = request.POST.get("image_url", "")
            service_cname = request.POST.get("create_app_name", "")
            _is, list_args, run_execs = ImageAnalyst.analystImage(image_input)
            if not run_execs:
                run_execs = ""
            image_url = list_args[-1]
            result["image_url"] = image_url
            if image_url != "":
                imagesr = None
                if service_id != "":
                    try:
                        imagesr = ImageServiceRelation.objects.get(
                            service_id=service_id)
                    except Exception:
                        pass

                if imagesr is None:
                    imagesr = ImageServiceRelation()
                    service_id = make_uuid(self.tenant.tenant_id)

                imagesr.tenant_id = self.tenant.tenant_id
                imagesr.service_id = service_id
                imagesr.image_url = image_url
                imagesr.service_cname = service_cname
                imagesr.save()
                logger.debug("setp2 image url is {}".format(image_url))
                # save service attach info
                # min_memory = int(request.POST.get("service_min_memory", 128))
                # # 将G转换为M
                # if min_memory < 128:
                #     min_memory *= 1024
                # min_node = int(request.POST.get("service_min_node", 1))

                # judge region tenant is init
                success = tenant_svc.init_for_region(self.response_region,
                                                     self.tenantName,
                                                     tenant_id, self.user)
                if not success:
                    result["status"] = "failure"
                    return JsonResponse(result, status=200)

                result["status"] = "success"
                result["id"] = service_id
                group_id = request.POST.get("select_group_id", "")
                # 创建关系
                if group_id != "":
                    group_id = int(group_id)
                    if group_id > 0:
                        ServiceGroupRelation.objects.create(
                            service_id=service_id,
                            group_id=group_id,
                            tenant_id=self.tenant.tenant_id,
                            region_name=self.response_region)
                if _is == "is_docker":
                    args = ""
                    for mm in list_args[:-1]:
                        if args:
                            args = "{0}^_^{1}=={2}".format(args, mm[0], mm[1])
                        else:
                            args = "{0}=={1}".format(mm[0], mm[1])
                    if args:
                        args += "^_^{0}=={1}^_^{2}=={3}".format(
                            "image", image_url, "run_exec", run_execs)
                    else:
                        args = "{0}=={1}^_^{2}=={3}".format(
                            "image", image_url, "run_exec", run_execs)
                    logger.debug(args)
                    result["params"] = base64.b64encode(args)
            else:
                result["status"] = "no_image_url"
                return JsonResponse(result, status=500)
        except Exception as e:
            logger.exception(e)
            if service_id != "" and service_id is not None:
                ImageServiceRelation.objects.filter(
                    service_id=service_id).delete()
                ServiceAttachInfo.objects.filter(
                    service_id=service_id).delete()
            result["status"] = "failure"
            result["fail_info"] = 900
            return JsonResponse(result, status=500)
        return JsonResponse(result, status=200)