From fbbd28b6a3e1228aea5a44cefc1f18d84ef2375d Mon Sep 17 00:00:00 2001 From: Hattori Keigo Date: Thu, 1 Nov 2018 14:39:23 +0900 Subject: [PATCH] Make dump method independent from update method --- app/apis/api_kubernetes.py | 44 ++++++++++++++++++++++++-------------- app/app.py | 2 +- app/auth/__init__.py | 2 +- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/app/apis/api_kubernetes.py b/app/apis/api_kubernetes.py index c90c842..86af3d2 100644 --- a/app/apis/api_kubernetes.py +++ b/app/apis/api_kubernetes.py @@ -342,7 +342,6 @@ def update_dbs_kubernetes(kubernetes_id:int, applist:set=None, description:str=N else: sobj.confirm_date = datetime.utcnow() db.session.flush() - dump_drucker_on_kubernetes(kobj.kubernetes_id, aobj.application_id, sobj.service_id) for application_name in applist: aobj = db.session.query(Application).filter( Application.application_name == application_name, @@ -816,27 +815,21 @@ def switch_drucker_service_model_assignment( ) return response_body + def dump_drucker_on_kubernetes( - kubernetes_id:int, application_id:int, service_id:int): - kobj = Kubernetes.query.filter_by( - kubernetes_id=kubernetes_id).one_or_none() + kobj:Kubernetes, aobj:Application, sobj:Service): if kobj is None: raise Exception("No such kubernetes_id.") - aobj = Application.query.filter_by( - application_id=application_id).one_or_none() if aobj is None: raise Exception("No such application_id.") - sobj = Service.query.filter_by( - service_id=service_id).one_or_none() if sobj is None: raise Exception("No such service_id.") config_path = kobj.config_path from kubernetes import client, config config.load_kube_config(config_path) - save_dir = pathlib.Path(DIR_KUBE_CONFIG, aobj.application_name) - if not os.path.isdir(save_dir): - os.mkdir(save_dir) + save_dir = pathlib.Path(DIR_KUBE_CONFIG, kobj.display_name, aobj.application_name) + save_dir.mkdir(parents=True, exist_ok=True) api_client = client.ApiClient() apps_v1 = client.AppsV1Api() @@ -849,9 +842,10 @@ def dump_drucker_on_kubernetes( json.dump(api_client.sanitize_for_serialization(v1_deployment), pathlib.Path( DIR_KUBE_CONFIG, + kobj.display_name, aobj.application_name, "{0}-deployment.json".format(sobj.service_name)).open("w", encoding='utf-8'), - ensure_ascii = False, indent = 2) + ensure_ascii=False, indent=2) core_vi = client.CoreV1Api() v1_service = core_vi.read_namespaced_service( name="{0}-service".format(sobj.service_name), @@ -862,9 +856,10 @@ def dump_drucker_on_kubernetes( json.dump(api_client.sanitize_for_serialization(v1_service), pathlib.Path( DIR_KUBE_CONFIG, + kobj.display_name, aobj.application_name, "{0}-service.json".format(sobj.service_name)).open("w", encoding='utf-8'), - ensure_ascii = False, indent = 2) + ensure_ascii=False, indent=2) extensions_v1_beta = client.ExtensionsV1beta1Api() v1_beta1_ingress = extensions_v1_beta.read_namespaced_ingress( name="{0}-ingress".format(sobj.service_name), @@ -875,9 +870,10 @@ def dump_drucker_on_kubernetes( json.dump(api_client.sanitize_for_serialization(v1_beta1_ingress), pathlib.Path( DIR_KUBE_CONFIG, + kobj.display_name, aobj.application_name, "{0}-ingress.json".format(sobj.service_name)).open("w", encoding='utf-8'), - ensure_ascii = False, indent = 2) + ensure_ascii=False, indent=2) autoscaling_v1 = client.AutoscalingV1Api() v1_horizontal_pod_autoscaler = autoscaling_v1.read_namespaced_horizontal_pod_autoscaler( name="{0}-autoscaling".format(sobj.service_name), @@ -888,9 +884,10 @@ def dump_drucker_on_kubernetes( json.dump(api_client.sanitize_for_serialization(v1_horizontal_pod_autoscaler), pathlib.Path( DIR_KUBE_CONFIG, + kobj.display_name, aobj.application_name, "{0}-autoscaling.json".format(sobj.service_name)).open("w", encoding='utf-8'), - ensure_ascii = False, indent = 2) + ensure_ascii=False, indent=2) """ autoscaling_v2_beta1 = client.AutoscalingV2beta1Api() v2_beta1_horizontal_pod_autoscaler = autoscaling_v2_beta1.read_namespaced_horizontal_pod_autoscaler( @@ -902,9 +899,10 @@ def dump_drucker_on_kubernetes( json.dump(api_client.sanitize_for_serialization(v2_beta1_horizontal_pod_autoscaler), pathlib.Path( DIR_KUBE_CONFIG, + kobj.display_name, aobj.application_name, "{0}-autoscaling.json".format(sobj.service_name)).open("w", encoding='utf-8'), - ensure_ascii = False, indent = 2) + ensure_ascii=False, indent=2) """ @@ -973,6 +971,20 @@ def post(self): raise error return response_body + +@kube_info_namespace.route('/dump') +class ApiKubernetesDump(Resource): + @kube_info_namespace.marshal_with(success_or_not) + def post(self): + """dump_kubernetes""" + for kobj in Kubernetes.query.all(): + for aobj in Application.query.filter_by(kubernetes_id=kobj.kubernetes_id).all(): + for sobj in Service.query.filter_by(application_id=aobj.application_id).all(): + dump_drucker_on_kubernetes(kobj, aobj, sobj) + response_body = {"status": True, "message": "Success."} + return response_body + + @kube_info_namespace.route('/') class ApiKubernetesId(Resource): @kube_info_namespace.marshal_with(kube_info) diff --git a/app/app.py b/app/app.py index f7dae30..85c8b30 100644 --- a/app/app.py +++ b/app/app.py @@ -36,7 +36,7 @@ def initialize_app(flask_app: Flask) -> None: from utils.env_loader import DIR_KUBE_CONFIG, config if not os.path.isdir(DIR_KUBE_CONFIG): - os.mkdir(DIR_KUBE_CONFIG) + os.makedirs(DIR_KUBE_CONFIG) configure_app(flask_app, db_url()) api.init_app(flask_app) diff --git a/app/auth/__init__.py b/app/auth/__init__.py index 99804b1..27391fc 100644 --- a/app/auth/__init__.py +++ b/app/auth/__init__.py @@ -20,7 +20,7 @@ def __init__(self, app=None): def auth_required(fn): @wraps(fn) def wrapper(*args, **kwargs): - if Auth.enabled and request.path.startswith('/api/') and not request.path.startswith('/api/settings'): + if Auth.enabled and request.path.startswith('/api/') and not request.path.startswith('/api/settings') and not request.path.startswith('/api/kubernetes/dump'): return jwt_required(fn(*args, **kwargs)) else: return fn(*args, **kwargs)