From 8ba6f8b0e1f0011588fb38e77f4c37636fb5f79f Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Wed, 7 Dec 2022 20:26:24 +0000 Subject: [PATCH 1/4] fix keyerror for old tasks --- api/tacticalrmm/autotasks/serializers.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/api/tacticalrmm/autotasks/serializers.py b/api/tacticalrmm/autotasks/serializers.py index edead8ca10..17432fc053 100644 --- a/api/tacticalrmm/autotasks/serializers.py +++ b/api/tacticalrmm/autotasks/serializers.py @@ -228,6 +228,10 @@ def get_task_actions(self, obj): # script doesn't exist so remove it actions_to_remove.append(action["script"]) continue + try: + env_vars = action["env_vars"] + except KeyError: + env_vars = [] tmp.append( { "type": "script", @@ -241,7 +245,7 @@ def get_task_actions(self, obj): "shell": script.shell, "timeout": action["timeout"], "run_as_user": script.run_as_user, - "env_vars": action["env_vars"], + "env_vars": env_vars, } ) if actions_to_remove: From c9deef6e7657b6b1a71c4c481e647d2baa562b5c Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Thu, 8 Dec 2022 05:34:41 +0000 Subject: [PATCH 2/4] add a migration for old tasks --- .../migrations/0038_add_missing_env_vars.py | 33 +++++++++++++++++++ api/tacticalrmm/autotasks/serializers.py | 2 ++ api/tacticalrmm/tacticalrmm/settings.py | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 api/tacticalrmm/autotasks/migrations/0038_add_missing_env_vars.py diff --git a/api/tacticalrmm/autotasks/migrations/0038_add_missing_env_vars.py b/api/tacticalrmm/autotasks/migrations/0038_add_missing_env_vars.py new file mode 100644 index 0000000000..999a700911 --- /dev/null +++ b/api/tacticalrmm/autotasks/migrations/0038_add_missing_env_vars.py @@ -0,0 +1,33 @@ +from django.db import migrations + + +def migrate_env_vars(apps, schema_editor): + AutomatedTask = apps.get_model("autotasks", "AutomatedTask") + for task in AutomatedTask.objects.all(): + try: + tmp = [] + if isinstance(task.actions, list) and task.actions: + for t in task.actions: + if isinstance(t, dict): + if t["type"] == "script": + try: + t["env_vars"] + except KeyError: + t["env_vars"] = [] + tmp.append(t) + if tmp: + task.actions = tmp + task.save(update_fields=["actions"]) + except Exception as e: + print(f"ERROR: {e}") + + +class Migration(migrations.Migration): + + dependencies = [ + ("autotasks", "0037_alter_taskresult_retcode"), + ] + + operations = [ + migrations.RunPython(migrate_env_vars), + ] diff --git a/api/tacticalrmm/autotasks/serializers.py b/api/tacticalrmm/autotasks/serializers.py index 17432fc053..2c9e8dbed1 100644 --- a/api/tacticalrmm/autotasks/serializers.py +++ b/api/tacticalrmm/autotasks/serializers.py @@ -228,6 +228,8 @@ def get_task_actions(self, obj): # script doesn't exist so remove it actions_to_remove.append(action["script"]) continue + # wrote a custom migration for env_vars but leaving this just in case. + # can be removed later try: env_vars = action["env_vars"] except KeyError: diff --git a/api/tacticalrmm/tacticalrmm/settings.py b/api/tacticalrmm/tacticalrmm/settings.py index a2d651f5a3..41dd9e5b03 100644 --- a/api/tacticalrmm/tacticalrmm/settings.py +++ b/api/tacticalrmm/tacticalrmm/settings.py @@ -20,7 +20,7 @@ AUTH_USER_MODEL = "accounts.User" # latest release -TRMM_VERSION = "0.15.4" +TRMM_VERSION = "0.15.5-dev" # https://github.com/amidaware/tacticalrmm-web WEB_VERSION = "0.101.9" From 9d62b4acdd7d0ff428bb674d98fd047f871945c0 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Thu, 8 Dec 2022 06:59:02 +0000 Subject: [PATCH 3/4] use iterator instead of all in case large queryset --- .../autotasks/migrations/0038_add_missing_env_vars.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/tacticalrmm/autotasks/migrations/0038_add_missing_env_vars.py b/api/tacticalrmm/autotasks/migrations/0038_add_missing_env_vars.py index 999a700911..1c168c9d96 100644 --- a/api/tacticalrmm/autotasks/migrations/0038_add_missing_env_vars.py +++ b/api/tacticalrmm/autotasks/migrations/0038_add_missing_env_vars.py @@ -3,7 +3,7 @@ def migrate_env_vars(apps, schema_editor): AutomatedTask = apps.get_model("autotasks", "AutomatedTask") - for task in AutomatedTask.objects.all(): + for task in AutomatedTask.objects.iterator(chunk_size=30): try: tmp = [] if isinstance(task.actions, list) and task.actions: From 483bf331fa1e7d8c943c1b51456cbedcfe172ff2 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Thu, 8 Dec 2022 06:59:40 +0000 Subject: [PATCH 4/4] bump version --- api/tacticalrmm/tacticalrmm/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/tacticalrmm/tacticalrmm/settings.py b/api/tacticalrmm/tacticalrmm/settings.py index 41dd9e5b03..bc3ac8cb9d 100644 --- a/api/tacticalrmm/tacticalrmm/settings.py +++ b/api/tacticalrmm/tacticalrmm/settings.py @@ -20,7 +20,7 @@ AUTH_USER_MODEL = "accounts.User" # latest release -TRMM_VERSION = "0.15.5-dev" +TRMM_VERSION = "0.15.5" # https://github.com/amidaware/tacticalrmm-web WEB_VERSION = "0.101.9"