From 1cfe8d9cc88108c9d5bf2b93c5d8ffa66767f2c1 Mon Sep 17 00:00:00 2001 From: Aaron3S Date: Tue, 27 Dec 2022 16:06:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/ansible/callback.py | 1 + apps/ops/ansible/runner.py | 4 ++- apps/ops/api/job.py | 3 +- apps/ops/const.py | 2 ++ .../ops/migrations/0035_auto_20221227_1520.py | 33 +++++++++++++++++++ apps/ops/models/job.py | 3 +- 6 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 apps/ops/migrations/0035_auto_20221227_1520.py diff --git a/apps/ops/ansible/callback.py b/apps/ops/ansible/callback.py index 7d2b1f39d..4bcb9be60 100644 --- a/apps/ops/ansible/callback.py +++ b/apps/ops/ansible/callback.py @@ -8,6 +8,7 @@ class DefaultCallback: 'failed': 'failed', 'running': 'running', 'pending': 'pending', + 'timeout': 'timeout', 'unknown': 'unknown' } diff --git a/apps/ops/ansible/runner.py b/apps/ops/ansible/runner.py index 8c7517ade..280f8f05c 100644 --- a/apps/ops/ansible/runner.py +++ b/apps/ops/ansible/runner.py @@ -14,7 +14,7 @@ class AdHocRunner: ] def __init__(self, inventory, module, module_args='', pattern='*', project_dir='/tmp/', extra_vars={}, - dry_run=False): + dry_run=False, timeout=-1): self.id = uuid.uuid4() self.inventory = inventory self.pattern = pattern @@ -25,6 +25,7 @@ class AdHocRunner: self.runner = None self.extra_vars = extra_vars self.dry_run = dry_run + self.timeout = timeout def check_module(self): if self.module not in self.cmd_modules_choices: @@ -41,6 +42,7 @@ class AdHocRunner: os.mkdir(self.project_dir, 0o755) ansible_runner.run( + timeout=self.timeout if self.timeout > 0 else None, extravars=self.extra_vars, host_pattern=self.pattern, private_data_dir=self.project_dir, diff --git a/apps/ops/api/job.py b/apps/ops/api/job.py index 1c7cac33d..817e5040e 100644 --- a/apps/ops/api/job.py +++ b/apps/ops/api/job.py @@ -7,7 +7,7 @@ from ops.models import Job, JobExecution from ops.serializers.job import JobSerializer, JobExecutionSerializer __all__ = ['JobViewSet', 'JobExecutionViewSet', 'JobRunVariableHelpAPIView', - 'JobAssetDetail', 'JobExecutionTaskDetail','FrequentUsernames'] + 'JobAssetDetail', 'JobExecutionTaskDetail', 'FrequentUsernames'] from ops.tasks import run_ops_job_execution from ops.variables import JMS_JOB_VARIABLE_HELP @@ -110,6 +110,7 @@ class JobExecutionTaskDetail(APIView): with tmp_to_org(org): execution = get_object_or_404(JobExecution, task_id=task_id) return Response(data={ + 'status': execution.status, 'is_finished': execution.is_finished, 'is_success': execution.is_success, 'time_cost': execution.time_cost, diff --git a/apps/ops/const.py b/apps/ops/const.py index 8288a663e..c383ef3c7 100644 --- a/apps/ops/const.py +++ b/apps/ops/const.py @@ -43,9 +43,11 @@ class RunasPolicies(models.TextChoices): class Modules(models.TextChoices): shell = 'shell', _('Shell') winshell = 'win_shell', _('Powershell') + python = 'python', _('Python') class JobStatus(models.TextChoices): running = 'running', _('Running') success = 'success', _('Success') + timeout = 'timeout', _('Timeout') failed = 'failed', _('Failed') diff --git a/apps/ops/migrations/0035_auto_20221227_1520.py b/apps/ops/migrations/0035_auto_20221227_1520.py new file mode 100644 index 000000000..36752a9bb --- /dev/null +++ b/apps/ops/migrations/0035_auto_20221227_1520.py @@ -0,0 +1,33 @@ +# Generated by Django 3.2.14 on 2022-12-27 07:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('ops', '0034_alter_celerytask_options'), + ] + + operations = [ + migrations.AlterField( + model_name='historicaljob', + name='module', + field=models.CharField(choices=[('shell', 'Shell'), ('win_shell', 'Powershell'), ('python', 'Python')], default='shell', max_length=128, null=True, verbose_name='Module'), + ), + migrations.AlterField( + model_name='historicaljob', + name='timeout', + field=models.IntegerField(default=-1, verbose_name='Timeout (Seconds)'), + ), + migrations.AlterField( + model_name='job', + name='module', + field=models.CharField(choices=[('shell', 'Shell'), ('win_shell', 'Powershell'), ('python', 'Python')], default='shell', max_length=128, null=True, verbose_name='Module'), + ), + migrations.AlterField( + model_name='job', + name='timeout', + field=models.IntegerField(default=-1, verbose_name='Timeout (Seconds)'), + ), + ] diff --git a/apps/ops/models/job.py b/apps/ops/models/job.py index 98ec9797f..85f960df1 100644 --- a/apps/ops/models/job.py +++ b/apps/ops/models/job.py @@ -27,7 +27,7 @@ class Job(JMSOrgBaseModel, PeriodTaskModelMixin): module = models.CharField(max_length=128, choices=Modules.choices, default=Modules.shell, verbose_name=_('Module'), null=True) chdir = models.CharField(default="", max_length=1024, verbose_name=_('Chdir'), null=True, blank=True) - timeout = models.IntegerField(default=60, verbose_name=_('Timeout (Seconds)')) + timeout = models.IntegerField(default=-1, verbose_name=_('Timeout (Seconds)')) playbook = models.ForeignKey('ops.Playbook', verbose_name=_("Playbook"), null=True, on_delete=models.SET_NULL) type = models.CharField(max_length=128, choices=Types.choices, default=Types.adhoc, verbose_name=_("Type")) creator = models.ForeignKey('users.User', verbose_name=_("Creator"), on_delete=models.SET_NULL, null=True) @@ -197,6 +197,7 @@ class JobExecution(JMSOrgBaseModel): runner = AdHocRunner( self.inventory_path, module, + timeout=self.current_job.timeout, module_args=args, pattern="all", project_dir=self.private_dir,