Skip to content

Commit

Permalink
Merge pull request #42 from drucker/fix/make-dump-independent
Browse files Browse the repository at this point in the history
Merged
  • Loading branch information
keigohtr authored Nov 19, 2018
2 parents c00645d + fbbd28b commit 5f3697c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
44 changes: 28 additions & 16 deletions app/apis/api_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -821,27 +820,21 @@ def switch_drucker_service_model_assignment(
response_body["status"] = True
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()
Expand All @@ -854,9 +847,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),
Expand All @@ -867,9 +861,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),
Expand All @@ -880,9 +875,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),
Expand All @@ -893,9 +889,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(
Expand All @@ -907,9 +904,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)
"""


Expand Down Expand Up @@ -978,6 +976,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('/<int:kubernetes_id>')
class ApiKubernetesId(Resource):
@kube_info_namespace.marshal_with(kube_info)
Expand Down
2 changes: 1 addition & 1 deletion app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5f3697c

Please sign in to comment.