From 9ee2b1b1f5d4d80a5cb3165ef12bfe37bfa5c077 Mon Sep 17 00:00:00 2001 From: Aaron3S Date: Tue, 7 Feb 2023 11:25:13 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20job=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ops/migrations/0027_auto_20230206_1927.py | 41 +++++++++++++++++++ apps/ops/models/job.py | 24 +++++------ 2 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 apps/ops/migrations/0027_auto_20230206_1927.py diff --git a/apps/ops/migrations/0027_auto_20230206_1927.py b/apps/ops/migrations/0027_auto_20230206_1927.py new file mode 100644 index 000000000..f292ab198 --- /dev/null +++ b/apps/ops/migrations/0027_auto_20230206_1927.py @@ -0,0 +1,41 @@ +# Generated by Django 3.2.16 on 2023-02-06 11:27 + +from django.db import migrations, models + + +def compatible(apps, schema_editor): + """ + 兼容旧版本的数据 + """ + model = apps.get_model("ops", "JobExecution") + for obj in model.objects.all(): + if obj.job: + if obj.job.type == 'adhoc': + obj.material = "{}:{}".format(obj.job.module, obj.job.args) + if obj.job.type == 'playbook': + obj.material = "{}:{}:{}".format(obj.job.org.name, obj.job.creator.name, obj.job.playbook.name) + obj.job_type = obj.job.type + obj.save() + else: + obj.delete() + + +class Migration(migrations.Migration): + dependencies = [ + ('ops', '0026_alter_jobexecution_job'), + ] + + operations = [ + migrations.AddField( + model_name='jobexecution', + name='job_type', + field=models.CharField(choices=[('adhoc', 'Adhoc'), ('playbook', 'Playbook')], default='adhoc', + max_length=128, verbose_name='Material Type'), + ), + migrations.AddField( + model_name='jobexecution', + name='material', + field=models.CharField(blank=True, default='', max_length=1024, null=True, verbose_name='Material'), + ), + migrations.RunPython(compatible), + ] diff --git a/apps/ops/models/job.py b/apps/ops/models/job.py index c8c16718b..0af043580 100644 --- a/apps/ops/models/job.py +++ b/apps/ops/models/job.py @@ -84,8 +84,15 @@ class Job(JMSOrgBaseModel, PeriodTaskModelMixin): def inventory(self): return JMSInventory(self.assets.all(), self.runas_policy, self.runas) + @property + def material(self): + if self.type == 'adhoc': + return "{}:{}".format(self.module, self.args) + if self.type == 'playbook': + return "{}:{}:{}".format(self.org.name, self.creator.name, self.playbook.name) + def create_execution(self): - return self.executions.create(job_version=self.version) + return self.executions.create(job_version=self.version, material=self.material, job_type=Types[self.type].label) class Meta: verbose_name = _("Job") @@ -106,19 +113,16 @@ class JobExecution(JMSOrgBaseModel): date_start = models.DateTimeField(null=True, verbose_name=_('Date start'), db_index=True) date_finished = models.DateTimeField(null=True, verbose_name=_("Date finished")) + material = models.CharField(max_length=1024, default='', verbose_name=_('Material'), null=True, blank=True) + job_type = models.CharField(max_length=128, choices=Types.choices, default=Types.adhoc, + verbose_name=_("Material Type")) + @property def current_job(self): if self.job.version != self.job_version: return self.job.get_history(self.job_version) return self.job - @property - def material(self): - if self.current_job.type == 'adhoc': - return "{}:{}".format(self.current_job.module, self.current_job.args) - if self.current_job.type == 'playbook': - return "{}:{}:{}".format(self.org.name, self.current_job.creator.name, self.current_job.playbook.name) - @property def assent_result_detail(self): if not self.is_finished or self.summary.get('error'): @@ -158,10 +162,6 @@ class JobExecution(JMSOrgBaseModel): result["detail"].append(asset_detail) return result - @property - def job_type(self): - return Types[self.job.type].label - def compile_shell(self): if self.current_job.type != 'adhoc': return