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): try: self._check_db() except Exception, e: err_msg = "database health check failed! database connection exception %s" % e logger.exception(err_msg) return FailJsonResponse(err_msg)
class AppReleaseViewSet(AppView): def post(self, request, app_code, mode): try: bk_app = models.BkApp.objects.get(app_code=app_code) except Exception, e: return FailJsonResponse(str(e)) # add saas fields in envs only for agent if request.json_data.get('is_saas', False): saas_settings = request.json_data.get('saas_settings', {}) saas_settings['is_saas'] = 'true' else: saas_settings = {'is_saas': 'false'} deploy_config = { "envs": request.json_data.get('envs', {}), "triggers": request.json_data.get('triggers', {}), "deploy_token": request.json_data.get("deploy_token", ''), "deploy_vars": request.json_data.get("deploy_vars", {}), "saas_settings": saas_settings } deploy_controller = DeployController(bk_app, mode, deploy_config) event_id, error_code, message = deploy_controller.online() data = {"event_id": event_id, "error_code": error_code} if error_code == 0: return OKJsonResponse(data=data) return FailJsonResponse(message, data=data)
class HealthCheckView(View): def _check_db(self): import django.db with django.db.connection.cursor() as c: c.execute("SELECT 0") def _check_agent(self): bk_servers = models.BkServer.objects.filter(is_active=True) if not bk_servers: raise Exception("have not registered and activated applicable app servers") for bk_server in bk_servers: check_agent_health(bk_server) def get(self, request): try: self._check_db() except Exception, e: err_msg = "database health check failed! database connection exception %s" % e logger.exception(err_msg) return FailJsonResponse(err_msg) try: self._check_agent() except Exception, e: err_msg = "paasagent health check failed, error: %s" % 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 get(self, request, server_id): try: bk_server = models.BkServer.objects.get(id=int(server_id)) check_agent_health(bk_server) return OKJsonResponse() except Exception, e: return FailJsonResponse("health check failed: %s" % e)
def post(self, request, app_code, event_id): try: bk_app = models.BkApp.objects.get(app_code=app_code) bk_app_event = models.BkAppEvent.objects.get(id=event_id, bk_app=bk_app) except Exception, e: return FailJsonResponse(str(e))
def put(self, request, server_id): try: bk_server = models.BkServer.objects.get(id=server_id) except models.BkServer.DoesNotExist: return FailJsonResponse("id %s server not registered") if not bk_server.is_active and has_active_server(bk_server.category): return FailJsonResponse( "%s environment has already one activated server, thus id %s cannot be activated again" % (bk_server.category, server_id)) try: resp = check_agent_health(bk_server) bk_server.is_active = True bk_server.mac = resp.get("mac", "") bk_server.save() return OKJsonResponse(data={"server_id": server_id}) except Exception, e: bk_server.is_active = False bk_server.save() return FailJsonResponse("active server id %s exception: %s" % (server_id, e))
def post(self, request): form_data = forms.AppInitForm(request.json_data) if not form_data.is_valid(): err_msg = form_data.errors return FailJsonResponse(err_msg) data = form_data.clean() bk_app, created = models.BkApp.objects.update_or_create( app_code=data["app_code"], defaults={"name": data["name"], "app_lang": data["app_lang"]}) if bk_app and created: models.BkAppToken.objects.create(bk_app=bk_app) return OKJsonResponse(data=bk_app.serializer_data())
class AgentViewSet(BaseView): def put(self, request, agent_ip): try: bk_server = models.BkServer.objects.get(ip_address=agent_ip) except Exception, e: return FailJsonResponse("active server ip %s exception: %s" % (agent_ip, e)) if not bk_server.is_active and has_active_server(bk_server.category): return FailJsonResponse( "%s environment has already one activated server, thus ip %s cannot be activated again" % (bk_server.category, agent_ip)) try: resp = check_agent_health(bk_server) bk_server.is_active = True bk_server.mac = resp.get("mac", "") bk_server.save() return OKJsonResponse(data={"agent_ip": bk_server.ip_address}) except Exception, e: bk_server.is_active = False bk_server.save() err_msg = "active server ip %s exception: %s" % (agent_ip, e) logger.exception(err_msg) return FailJsonResponse(err_msg)
def post(self, request): form_data = forms.AgentRegisterForm(request.json_data) if not form_data.is_valid(): err_msg = form_data.errors return FailJsonResponse(err_msg) data = form_data.clean() server_category = get_server_category(data["mode"]) server_params = { "ip_address": data["agent_ip"], "app_port": data["web_port"], "ip_port": data["agent_port"], "category": server_category, } try: bk_server = models.BkServer.objects.get(**server_params) except models.BkServer.DoesNotExist: server_params["is_active"] = False bk_server = models.BkServer.objects.create(**server_params) return OKJsonResponse(data={"sid": bk_server.s_id, "token": bk_server.token})
class AppEventLogsViewSet(AgentView): def post(self, request, app_code, event_id): try: bk_app = models.BkApp.objects.get(app_code=app_code) bk_app_event = models.BkAppEvent.objects.get(id=event_id, bk_app=bk_app) except Exception, e: return FailJsonResponse(str(e)) form_data = forms.AppEventLogsForm(request.json_data) if not form_data.is_valid(): err_msg = form_data.errors return FailJsonResponse(err_msg) data = form_data.clean() bk_app_event.status = data["status"] bk_app_event.save() models.BkAppEventLog.objects.create(bk_app_event=bk_app_event, log=data["log"]) return OKJsonResponse(data={"event_id": event_id})
def put(self, request, agent_ip): try: bk_server = models.BkServer.objects.get(ip_address=agent_ip) except Exception, e: return FailJsonResponse("active server ip %s exception: %s" % (agent_ip, e))
def delete(self, request, app_code, mode): try: bk_app = models.BkApp.objects.get(app_code=app_code) except Exception, e: return FailJsonResponse(str(e))
return OKJsonResponse(data=data) return FailJsonResponse(message, data=data) def delete(self, request, app_code, mode): try: bk_app = models.BkApp.objects.get(app_code=app_code) except Exception, e: return FailJsonResponse(str(e)) deploy_controller = DeployController(bk_app, mode, {}) event_id, error_code, message = deploy_controller.offline() data = {"event_id": event_id, "error_code": error_code} if error_code == 0: return OKJsonResponse(data=data) return FailJsonResponse(message, data=data) class AppLogsViewSet(AppView): def get(self, request, app_code, event_id): try: bk_app = models.BkApp.objects.get(app_code=app_code) bk_app_event = models.BkAppEvent.objects.get(id=event_id, bk_app=bk_app) except Exception, e: return FailJsonResponse(str(e)) return OKJsonResponse(data=bk_app_event.serializer_data()) class AppEventLogsViewSet(AgentView): def post(self, request, app_code, event_id):