Add locking to git commands

Avoids conflicting attempts when multiple hosts check in at once.
This commit is contained in:
Joshua Boniface 2022-10-25 17:36:56 +00:00
parent fdae20c3c6
commit 00ac00ae2c
1 changed files with 53 additions and 42 deletions

View File

@ -22,6 +22,7 @@
import os.path
import git
import yaml
from filelock import FileLock
import pvcbootstrapd.lib.notifications as notifications
@ -60,25 +61,33 @@ def pull_repository(config):
"""
Pull (with rebase) the Ansible git repository
"""
lockfile = "/tmp/pvcbootstrapd-git.lock"
lock = FileLock(lockfile)
with lock:
logger.info(f"Updating local configuration repository {config['ansible_path']}")
try:
git_ssh_cmd = f"ssh -i {config['ansible_keyfile']} -o StrictHostKeyChecking=no"
g = git.cmd.Git(f"{config['ansible_path']}")
logger.debug("Performing git pull")
g.pull(rebase=True, env=dict(GIT_SSH_COMMAND=git_ssh_cmd))
logger.debug("Performing git submodule update")
g.submodule("update", "--init", env=dict(GIT_SSH_COMMAND=git_ssh_cmd))
except Exception as e:
logger.warn(e)
notifications.send_webhook(config, "failure", "Failed to update Git repository")
logger.info("Completed repository synchonization")
def commit_repository(config):
"""
Commit uncommitted changes to the Ansible git repository
"""
lockfile = "/tmp/pvcbootstrapd-git.lock"
lock = FileLock(lockfile)
with lock:
logger.info(
f"Committing changes to local configuration repository {config['ansible_path']}"
)
try:
g = git.cmd.Git(f"{config['ansible_path']}")
g.add("--all")
@ -102,10 +111,12 @@ def push_repository(config):
"""
Push changes to the default remote
"""
lockfile = "/tmp/pvcbootstrapd-git.lock"
lock = FileLock(lockfile)
with lock:
logger.info(
f"Pushing changes from local configuration repository {config['ansible_path']}"
)
try:
git_ssh_cmd = f"ssh -i {config['ansible_keyfile']} -o StrictHostKeyChecking=no"
g = git.Repo(f"{config['ansible_path']}")