diff --git a/netbox-event-driven-architectures/agents/config_backup/facts_backup.py b/netbox-event-driven-architectures/agents/config_backup/facts_backup.py index 92e138b..f32f44d 100644 --- a/netbox-event-driven-architectures/agents/config_backup/facts_backup.py +++ b/netbox-event-driven-architectures/agents/config_backup/facts_backup.py @@ -13,6 +13,7 @@ class FactsBackup(): github_token = "" github_org = "" github_repo = "" + commit_filename = "facts" # Class Functions def load_environment(self): @@ -29,8 +30,13 @@ def write_to_github(self, device_name, facts): gh = GitHubRepoHelper(self.github_token, self.github_org, self.github_repo) print(f"Created GitHub Helper object: {gh}") + commit_message = f"Device facts for {device_name} backed up by facts_backup agent 🚀" + # Write to GitHub - commit = gh.write_config_to_gh(device_name, str(facts), str(facts)) + commit = gh.write_config_to_gh(file_path=device_name, + file_name=self.commit_filename, + file_contents=str(facts), + commit_message=commit_message) print(f"Wrote device facts to GitHub. Commit: {commit}") async def message_handler(self, msg) -> None: diff --git a/netbox-event-driven-architectures/agents/config_backup/github_helper.py b/netbox-event-driven-architectures/agents/config_backup/github_helper.py index 53c79f5..67349cd 100644 --- a/netbox-event-driven-architectures/agents/config_backup/github_helper.py +++ b/netbox-event-driven-architectures/agents/config_backup/github_helper.py @@ -35,21 +35,12 @@ def __init__(self, auth_token, gh_org, gh_repo_name): return None # Functions - def write_config_to_gh(self, config_path, running_config, facts): + def write_config_to_gh(self, file_path, file_name, file_contents, commit_message): commit = None try: # Create a blob for each file - running_config_blob, facts_blob = [ - self.repo.create_git_blob( - content=running_config, - encoding='utf-8', - ), - self.repo.create_git_blob( - content=facts, - encoding='utf-8', - ), - ] + facts_blob = self.repo.create_git_blob(content=file_contents, encoding='utf-8') # Get the GH tree for the current HEAD head_sha = self.repo.get_branch('main').commit.sha @@ -59,13 +50,7 @@ def write_config_to_gh(self, config_path, running_config, facts): new_tree = self.repo.create_git_tree( tree=[ InputGitTreeElement( - path=f"{config_path}/running_config", - mode="100644", - type="blob", - sha=running_config_blob.sha, - ), - InputGitTreeElement( - path=f"{config_path}/facts", + path=f"{file_path}/{file_name}", mode="100644", type="blob", sha=facts_blob.sha, @@ -75,7 +60,7 @@ def write_config_to_gh(self, config_path, running_config, facts): ) parent = self.repo.get_git_commit(sha=head_sha) - commit = self.repo.create_git_commit("Scheduled backup courtesy of NetBox Labs 🚀", new_tree, [parent]) + commit = self.repo.create_git_commit(commit_message, new_tree, [parent]) master_ref = self.repo.get_git_ref('heads/main') master_ref.edit(sha=commit.sha) return commit