perf: 持续优化作业创建

pull/9151/head
Aaron3S 2022-12-02 12:21:56 +08:00
parent 21f91358cf
commit 6d0545f04f
12 changed files with 820 additions and 630 deletions

View File

@ -0,0 +1,14 @@
# Generated by Django 3.2.14 on 2022-12-02 03:15
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('acls', '0004_connectacl'),
('acls', '0007_auto_20221202_1048'),
]
operations = [
]

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:4a5338177d87680e0030c77f187a06664136d5dea63c8dffc43fa686091f2da4
size 117102
oid sha256:a2d20ebe29a2ae521e5026f493313abbee6a7a6b103901164766e5d1ae4ab564
size 116377

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:30ae571e06eb7d2f0fee70013a812ea3bdb8e14715e1a1f4eb5e2c92311034f8
size 104086
oid sha256:eb680a5e6725fcd4459a8e712b0eda8df3e9990915e7f3b9602b16307ff36221
size 103614

File diff suppressed because it is too large Load Diff

View File

@ -28,10 +28,21 @@ class JobViewSet(OrgBulkModelViewSet):
def perform_create(self, serializer):
instance = serializer.save()
if instance.instant:
execution = instance.create_execution()
task = run_ops_job_execution.delay(execution.id)
set_task_to_serializer_data(serializer, task)
run_after_save = serializer.validated_data.get('run_after_save', False)
if instance.instant or run_after_save:
self.run_job(instance, serializer)
def perform_update(self, serializer):
instance = serializer.save()
run_after_save = serializer.validated_data.get('run_after_save', False)
if run_after_save:
self.run_job(instance, serializer)
@staticmethod
def run_job(job, serializer):
execution = job.create_execution()
task = run_ops_job_execution.delay(execution.id)
set_task_to_serializer_data(serializer, task)
class JobExecutionViewSet(OrgBulkModelViewSet):

View File

@ -4,6 +4,7 @@ import zipfile
from django.conf import settings
from orgs.mixins.api import OrgBulkModelViewSet
from ..exception import PlaybookNoValidEntry
from ..models import Playbook
from ..serializers.playbook import PlaybookSerializer
@ -25,6 +26,10 @@ class PlaybookViewSet(OrgBulkModelViewSet):
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__())
if os.path.exists(dest_path):
os.makedirs(dest_path)
unzip_playbook(src_path, dest_path)
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

6
apps/ops/exception.py Normal file
View File

@ -0,0 +1,6 @@
from common.exceptions import JMSException
from django.utils.translation import gettext_lazy as _
class PlaybookNoValidEntry(JMSException):
default_detail = _('no valid program entry found.')

View File

@ -136,7 +136,7 @@ class JobExecution(JMSOrgBaseModel):
)
elif self.job.type == 'playbook':
runner = PlaybookRunner(
self.inventory_path, self.job.playbook.work_path
self.inventory_path, self.job.playbook.entry
)
else:
raise Exception("unsupported job type")

View File

@ -5,6 +5,7 @@ from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _
from ops.exception import PlaybookNoValidEntry
from orgs.mixins.models import JMSOrgBaseModel
@ -16,5 +17,10 @@ class Playbook(JMSOrgBaseModel):
comment = models.CharField(max_length=1024, default='', verbose_name=_('Comment'), null=True, blank=True)
@property
def work_path(self):
return os.path.join(settings.DATA_DIR, "ops", "playbook", self.id.__str__(), "main.yaml")
def entry(self):
work_dir = os.path.join(settings.DATA_DIR, "ops", "playbook", self.id.__str__())
valid_entry = ('main.yml', 'main.yaml', 'main')
for f in os.listdir(work_dir):
if f in valid_entry:
return os.path.join(work_dir, f)
raise PlaybookNoValidEntry

View File

@ -1,14 +1,14 @@
from django.utils.translation import ugettext as _
from rest_framework import serializers
from common.drf.fields import ReadableHiddenField
from ops.mixin import PeriodTaskSerializerMixin
from ops.models import Job, JobExecution
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
_all_ = []
class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin):
owner = ReadableHiddenField(default=serializers.CurrentUserDefault())
run_after_save = serializers.BooleanField(label=_("Run after save"), default=False, required=False)
class Meta:
model = Job
@ -21,7 +21,7 @@ class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin):
"chdir",
"comment",
"summary",
"is_periodic", "interval", "crontab"
"is_periodic", "interval", "crontab", "run_after_save"
]

View File

@ -14,6 +14,7 @@ def parse_playbook_name(path):
class PlaybookSerializer(BulkOrgResourceModelSerializer, serializers.ModelSerializer):
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
path = serializers.FileField(required=False)
def create(self, validated_data):
name = validated_data.get('name')
@ -26,5 +27,5 @@ class PlaybookSerializer(BulkOrgResourceModelSerializer, serializers.ModelSerial
model = Playbook
read_only_fields = ["id", "date_created", "date_updated"]
fields = read_only_fields + [
"id", "name", "comment", "creator",
"id", 'path', "name", "comment", "creator",
]