nautobot / nautobot-app-golden-config

Golden Configuration App for Nautobot.
https://docs.nautobot.com/projects/golden-config/en/latest/
Other
100 stars 57 forks source link

:bug: fix repo sync not executing on any task failure #684

Closed jeffkala closed 11 months ago

jeffkala commented 11 months ago

fixes issues seen in NB2.0 installs where if any backup/intended fails out of all devices selected that the repo commit, push never executes. More details in : https://github.com/nautobot/nautobot-plugin-golden-config/discussions/657#discussioncomment-7316239

itdependsnetworks commented 11 months ago

I was thinking something like


class GoldenConfigJobMixin(Job):  # pylint: disable=abstract-method
    """Reused mixin to be able to reuse common celery primitives in all GC jobs."""

    def before_start(self, repo_types, *args, **data):
        """Ensure repos before tasks runs."""
        self.logger.debug(f"Repository types to sync: {', '.join(repo_types)}")
        for repo in self.Meta.repos:
            self.logger.debug(f"Refreshing repositories of type {repo}.")
            # repo_obj = some_magic_to_get_repo  <---- update
            current_repos = get_refreshed_repos(job_obj=self, repo_type=repo, data=data)

    def after_return(self, job_type, *args):
        """Commit and Push each repo after job is completed."""
        now = make_aware(datetime.now())
        for repo in self.Meta.repos:
            self.logger.debug(f"Pushing {job_type} results to repo %s.", repo.base_url)
            repo.commit_with_added(f"{job_type} JOB {now}")
            repo.push()

class BackupJob(GoldenConfigJobMixin, FormEntry):
    """Job to to run the backup job."""

    class Meta:
        """Meta object boilerplate for backup configurations."""

        name = "Backup Configurations"
        description = "Backup the configurations of your network devices."
        has_sensitive_variables = False
        repos = ["backup"]  <---- key piece

    def run(self, *args, **data):
        """Run config backup process."""
        self.logger.debug("Starting config backup nornir play.")
        config_backup(self.job_result, self.logger.getEffectiveLevel(), data)