feat(automate): make temporary working dir optional

This commit is contained in:
Iain Learmonth 2022-11-28 19:03:24 +00:00
parent 109851745b
commit d9158ac942
2 changed files with 13 additions and 8 deletions

View file

@ -1,5 +1,6 @@
import datetime
import logging
import os
import shutil
import tempfile
from traceback import TracebackException
@ -108,8 +109,12 @@ def run_job(job_cls: Type[BaseAutomation], *,
automation.state = AutomationState.RUNNING
db.session.commit()
try:
job: BaseAutomation = job_cls()
tempdir_path = tempfile.mkdtemp()
if 'TERRAFORM_DIRECTORY' in app.config:
working_dir = os.path.join(app.config['TERRAFORM_DIRECTORY'],
job_cls.short_name or job_cls.__class__.__name__.lower())
else:
working_dir = tempfile.mkdtemp()
job: BaseAutomation = job_cls(working_dir)
success, logs = job.automate()
# We want to catch any and all exceptions that would cause problems here, because
# the error handling process isn't really handling the error, but rather causing it
@ -123,7 +128,9 @@ def run_job(job_cls: Type[BaseAutomation], *,
automation.state = AutomationState.IDLE
automation.next_run = datetime.datetime.utcnow() + datetime.timedelta(
minutes=getattr(job, "frequency", 7))
shutil.rmtree(tempdir_path)
if not 'TERRAFORM_DIRECTORY' in app.config:
# We used a temporary working directory
shutil.rmtree(working_dir)
else:
automation.state = AutomationState.ERROR
automation.enabled = False