def post(self, request): form_data = forms.ServiceServerRegisterForm({"server_ip": request.POST.get("mq_ip", '')}) if not form_data.is_valid(): err_msg = form_data.errors return JsonResponse({"msg": err_msg}, status=400) data = form_data.clean() server_ip = data["server_ip"] if models.ThirdServer.objects.filter(category=models.THIRD_SERVER_CATEGORY_MQ, is_active=True): err_msg = "the %s cluster has been already activated, thus ip %s cannot be activated again" \ % (models.THIRD_SERVER_CATEGORY_MQ, server_ip) return JsonResponse({"msg": err_msg}) server, _ = servicemanager.update_or_create_rabbitmq_server( server_ip=server_ip, username=request.POST.get('username', 'admin'), password=request.POST.get('password', 'admin') ) service_manager = servicemanager.ServiceManagerFactory(models.THIRD_SERVER_CATEGORY_MQ) try: service_manager.health_check(server.id) server.is_active = True server.save() return JsonResponse({"mq_ip": server_ip}) except Exception, e: err_msg = "%s registered, but active failed: %s" % (server_ip, e) logger.exception(err_msg) return JsonResponse({"msg": err_msg}, status=400)
def put(self, request, service_name, server_id): if service_name != models.THIRD_SERVER_CATEGORY_MQ: return FailJsonResponse("not support %s server register" % service_name) try: service_server = models.ThirdServer.objects.get(id=server_id, category=service_name) except models.ThirdServer.DoesNotExist: return FailJsonResponse("id %s server not registered" % server_id) if not service_server.is_active and has_active_thirdserver(service_server.category): return FailJsonResponse( "the %s cluster has been already activated, thus id %s cannot be activated again" % (service_server.category, server_id)) service_manager = servicemanager.ServiceManagerFactory(service_server.category) try: service_manager.health_check(server_id) service_server.is_active = True service_server.save() return OKJsonResponse() except Exception, e: service_server.is_active = False service_server.save() err_msg = "%s registered, but active failed: %s" % (server_id, e) logger.exception(err_msg) return FailJsonResponse(err_msg)
def post(self, request, service_name): if service_name != models.THIRD_SERVER_CATEGORY_MQ: return FailJsonResponse("not support %s server register" % service_name) category = service_name form_data = forms.ServiceServerRegisterForm({"server_ip": request.json_data.get("server_ip")}) if not form_data.is_valid(): err_msg = form_data.errors return FailJsonResponse(err_msg) data = form_data.clean() server_ip = data["server_ip"] if models.ThirdServer.objects.filter(category=category, is_active=True): return FailJsonResponse( "the %s cluster has been already activated, thus ip %s cannot be activated again" % (models.THIRD_SERVER_CATEGORY_MQ, server_ip)) server, _ = servicemanager.update_or_create_rabbitmq_server( server_ip=server_ip, username=request.json_data.get('username', 'admin'), password=request.json_data.get('password', 'admin') ) service_manager = servicemanager.ServiceManagerFactory(service_name) try: service_manager.health_check(server.id) server.is_active = True server.save() return OKJsonResponse() except Exception, e: err_msg = "%s registered, but active failed: %s" % (server_ip, e) logger.exception(err_msg) return FailJsonResponse(err_msg)
def get(self, request, server_name, server_id): try: server_manager = servicemanager.ServiceManagerFactory(server_name) server_manager.health_check(server_id) return OKJsonResponse() except Exception, e: return FailJsonResponse(str(e))
def _apply_third_service(self, service_name): triggers = self.deploy_config.get("triggers", {}) service_manager = servicemanager.ServiceManagerFactory(service_name) return service_manager.apply(self.bk_app, self.mode, triggers)