diff --git a/apps/ops/models.py b/apps/ops/models.py index b8c8054c7..105c55382 100644 --- a/apps/ops/models.py +++ b/apps/ops/models.py @@ -40,7 +40,7 @@ class TaskRecord(models.Model): from assets.models import Asset return [Asset.objects.get(hostname=hostname)._to_secret_json() for hostname in self.total_assets - if Asset.objects.exists(hostname=hostname)] + if Asset.objects.filter(hostname=hostname)] @property def module_args(self): diff --git a/apps/ops/tasks.py b/apps/ops/tasks.py index 5e333195e..992ef8e57 100644 --- a/apps/ops/tasks.py +++ b/apps/ops/tasks.py @@ -70,8 +70,20 @@ def run_AdHoc(self, task_tuple, assets, return summary -@shared_task(bind=True) -def push_users(self, assets, users): +def rerun_AdHoc(uuid): + from .models import TaskRecord + record = TaskRecord.objects.get(uuid=uuid) + assets = record.assets_json + task_tuple = record.module_args + pattern = record.pattern + task_name = record.name + task = run_AdHoc.apply_async((task_tuple, assets), + {'pattern': pattern, 'task_name': task_name}, + task_id=uuid) + return task + + +def push_users(assets, users): """ user: { name: 'web', @@ -104,25 +116,7 @@ def push_users(self, assets, users): )) ]) task_name = 'Push user {}'.format(','.join([user['name'] for user in users])) - record = TaskRecord(name=task_name, - uuid=self.request.id, - date_start=timezone.now(), - assets=','.join(asset['hostname'] for asset in assets)) - record.save() - logger.info('Runner {0} start {1}'.format(task_name, timezone.now())) - hoc = AdHocRunner(assets) - ts_start = time.time() - _ = hoc.run(task_tuple) - logger.info('Runner {0} complete {1}'.format(task_name, timezone.now())) - result_clean = hoc.clean_result() - record.time = int(time.time() - ts_start) - record.date_finished = timezone.now() - record.is_finished = True - - if len(result_clean['failed']) == 0: - record.is_success = True - else: - record.is_success = False - record.result = json.dumps(result_clean) - record.save() - return result_clean + task = run_AdHoc.delay(task_tuple, assets, pattern='all', task_name=task_name) + return task + + diff --git a/apps/perms/utils.py b/apps/perms/utils.py index a54c47f10..1e69d5af3 100644 --- a/apps/perms/utils.py +++ b/apps/perms/utils.py @@ -231,7 +231,7 @@ def push_system_user(assets, system_user): assets = [asset._to_secret_json() for asset in assets] system_user = system_user._to_secret_json() - task = push_users.delay(assets, system_user) + task = push_users(assets, system_user) return task.id diff --git a/apps/templates/_nav.html b/apps/templates/_nav.html index e9b55c48f..a19585727 100644 --- a/apps/templates/_nav.html +++ b/apps/templates/_nav.html @@ -49,7 +49,7 @@
diff --git a/run_server.py b/run_server.py index 9f11de4cf..77cda4139 100644 --- a/run_server.py +++ b/run_server.py @@ -29,7 +29,7 @@ def start_django(): def start_celery(): os.chdir(apps_dir) os.environ.setdefault('C_FORCE_ROOT', '1') - os.environ.setdefault('PYTHONOPTIMIZE', 1) + os.environ.setdefault('PYTHONOPTIMIZE', '1') print('start celery') subprocess.call('celery -A common worker -s /tmp/celerybeat-schedule -l debug', shell=True)