jumpserver/apps/ops/api/playbook.py

43 lines
1.2 KiB
Python
Raw Normal View History

2022-11-11 11:20:17 +00:00
import os
import zipfile
from django.conf import settings
from orgs.mixins.api import OrgBulkModelViewSet
2022-12-02 04:21:56 +00:00
from ..exception import PlaybookNoValidEntry
2022-11-11 11:20:17 +00:00
from ..models import Playbook
from ..serializers.playbook import PlaybookSerializer
__all__ = ["PlaybookViewSet"]
def unzip_playbook(src, dist):
fz = zipfile.ZipFile(src, 'r')
for file in fz.namelist():
fz.extract(file, dist)
2022-12-15 09:25:21 +00:00
class PlaybookViewSet(OrgBulkModelViewSet):
2022-11-11 11:20:17 +00:00
serializer_class = PlaybookSerializer
permission_classes = ()
model = Playbook
2022-11-11 11:20:17 +00:00
2022-12-21 09:14:07 +00:00
def allow_bulk_destroy(self, qs, filtered):
return True
2022-12-15 09:25:21 +00:00
def get_queryset(self):
queryset = super().get_queryset()
queryset = queryset.filter(creator=self.request.user)
return queryset
2022-11-11 11:20:17 +00:00
def perform_create(self, serializer):
instance = serializer.save()
src_path = os.path.join(settings.MEDIA_ROOT, instance.path.name)
dest_path = os.path.join(settings.DATA_DIR, "ops", "playbook", instance.id.__str__())
unzip_playbook(src_path, dest_path)
2022-12-02 04:21:56 +00:00
valid_entry = ('main.yml', 'main.yaml', 'main')
for f in os.listdir(dest_path):
if f in valid_entry:
return
os.remove(dest_path)
raise PlaybookNoValidEntry