mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2024-12-25 11:35:03 +01:00
Added python draft
This commit is contained in:
parent
10b04f8bd3
commit
407123492f
49
roles/update-docker/files/update-docker-compose-instances.py
Normal file
49
roles/update-docker/files/update-docker-compose-instances.py
Normal file
@ -0,0 +1,49 @@
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
def run_command(command):
|
||||
try:
|
||||
subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(e.output.decode())
|
||||
sys.exit(e.returncode)
|
||||
|
||||
def git_pull(directory):
|
||||
os.chdir(directory)
|
||||
print(f"Checking if the git repository in {directory} is up to date.")
|
||||
local = subprocess.check_output("git rev-parse @", shell=True).decode().strip()
|
||||
remote = subprocess.check_output("git rev-parse @{u}", shell=True).decode().strip()
|
||||
|
||||
if local != remote:
|
||||
print("Repository is not up to date. Performing git pull.")
|
||||
run_command("git pull")
|
||||
else:
|
||||
print("Repository is already up to date.")
|
||||
|
||||
def update_docker(directory):
|
||||
print("Pulling docker images and rebuilding containers.")
|
||||
run_command("docker-compose pull && docker-compose up -d --build --force-recreate")
|
||||
|
||||
def update_nextcloud(directory):
|
||||
print("Updating Nextcloud apps.")
|
||||
run_command("docker-compose exec -T -u www-data application /var/www/html/occ app:update --all")
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print("Please provide the path to the parent directory as a parameter.")
|
||||
sys.exit(1)
|
||||
|
||||
parent_directory = sys.argv[1]
|
||||
for dir_entry in os.scandir(parent_directory):
|
||||
if dir_entry.is_dir():
|
||||
dir_path = dir_entry.path
|
||||
print(f"Checking for updates in: {dir_path}")
|
||||
|
||||
if os.path.isdir(os.path.join(dir_path, ".git")):
|
||||
git_pull(dir_path)
|
||||
|
||||
update_docker(dir_path)
|
||||
|
||||
if os.path.basename(dir_path) == "nextcloud":
|
||||
update_nextcloud(dir_path)
|
@ -1,43 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Check if a path argument was provided
|
||||
if [ -z "$1" ]; then
|
||||
echo "Please provide the path to the parent directory as a parameter."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Iterate over all directories in the parent directory
|
||||
for dir in "$1"/*; do
|
||||
# Check if it is indeed a directory
|
||||
if [ -d "$dir" ]; then
|
||||
echo "Checking for updates in: $dir"
|
||||
cd "$dir"
|
||||
|
||||
# Check if directory is a Git repository
|
||||
if [ -d ".git" ]; then
|
||||
echo "Checking if the git repository is up to date."
|
||||
git fetch
|
||||
LOCAL=$(git rev-parse @)
|
||||
REMOTE=$(git rev-parse @{u})
|
||||
|
||||
# If local commit differs from remote, pull the changes
|
||||
if [ "$LOCAL" != "$REMOTE" ]; then
|
||||
echo "Repository is not up to date. Performing git pull."
|
||||
git pull || exit 3
|
||||
else
|
||||
echo "Repository is already up to date."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Pull the latest images and rebuild the containers
|
||||
echo "Pulling docker images and rebuilding containers."
|
||||
docker-compose pull && docker-compose up -d --build --force-recreate || exit 1
|
||||
|
||||
# Additional command for the 'nextcloud' directory
|
||||
if [ "$(basename "$dir")" == "nextcloud" ]; then
|
||||
echo "Updating Nextcloud apps."
|
||||
docker-compose exec -T -u www-data application /var/www/html/occ app:update --all || exit 2
|
||||
fi
|
||||
cd - > /dev/null
|
||||
fi
|
||||
done
|
Loading…
Reference in New Issue
Block a user