From 03114463849a830bd87ec00abb16a14fb6330236 Mon Sep 17 00:00:00 2001 From: wangruidong <940853815@qq.com> Date: Fri, 20 Sep 2024 15:07:44 +0800 Subject: [PATCH] perf: playbook clone with file --- apps/ops/api/playbook.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/ops/api/playbook.py b/apps/ops/api/playbook.py index 6c3ed5a7c..c214269c4 100644 --- a/apps/ops/api/playbook.py +++ b/apps/ops/api/playbook.py @@ -65,9 +65,19 @@ class PlaybookViewSet(JMSBulkModelViewSet): def perform_create(self, serializer): instance = serializer.save() + base_path = safe_join(settings.DATA_DIR, "ops", "playbook") + clone_id = self.request.query_params.get('clone_from') + if clone_id: + src_path = safe_join(base_path, clone_id) + dest_path = safe_join(base_path, str(instance.id)) + if not os.path.exists(src_path): + raise JMSException(code='invalid_playbook_id', detail={"msg": "clone playbook file not found"}) + shutil.copytree(src_path, dest_path) + return + if 'multipart/form-data' in self.request.headers['Content-Type']: src_path = safe_join(settings.MEDIA_ROOT, instance.path.name) - dest_path = safe_join(settings.DATA_DIR, "ops", "playbook", instance.id.__str__()) + dest_path = safe_join(base_path, str(instance.id)) try: unzip_playbook(src_path, dest_path) @@ -78,7 +88,7 @@ class PlaybookViewSet(JMSBulkModelViewSet): raise PlaybookNoValidEntry elif instance.create_method == 'blank': - dest_path = safe_join(settings.DATA_DIR, "ops", "playbook", instance.id.__str__()) + dest_path = safe_join(base_path, str(instance.id)) os.makedirs(dest_path) with open(safe_join(dest_path, 'main.yml'), 'w') as f: f.write('## write your playbook here')