2023-11-16 12:31:12 +01:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
def run_command(command):
|
2023-11-16 14:04:42 +01:00
|
|
|
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2023-11-16 14:24:13 +01:00
|
|
|
output = []
|
|
|
|
|
2023-11-16 14:04:42 +01:00
|
|
|
# Iterate over the output lines
|
|
|
|
for line in iter(process.stdout.readline, b''):
|
2023-11-16 14:24:13 +01:00
|
|
|
decoded_line = line.decode()
|
|
|
|
output.append(decoded_line)
|
|
|
|
sys.stdout.write(decoded_line)
|
2023-11-16 14:04:42 +01:00
|
|
|
|
|
|
|
process.stdout.close()
|
|
|
|
return_code = process.wait()
|
|
|
|
if return_code:
|
2023-11-16 14:24:13 +01:00
|
|
|
# Join the output list to create a single string
|
|
|
|
full_output = ''.join(output)
|
|
|
|
raise subprocess.CalledProcessError(return_code, command, output=full_output.encode())
|
|
|
|
|
2023-11-16 12:31:12 +01:00
|
|
|
|
|
|
|
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.")
|
2023-11-16 12:49:53 +01:00
|
|
|
|
2023-11-16 13:15:15 +01:00
|
|
|
def get_image_digests(directory):
|
|
|
|
compose_project = os.path.basename(directory)
|
|
|
|
images_output = subprocess.check_output(
|
|
|
|
f'docker images --format "{{{{.Repository}}}}:{{{{.Tag}}}}@{{{{.Digest}}}}" | grep {compose_project}',
|
|
|
|
shell=True
|
|
|
|
).decode().strip()
|
|
|
|
return dict(line.split('@') for line in images_output.splitlines() if line)
|
2023-11-16 12:31:12 +01:00
|
|
|
|
|
|
|
def update_docker(directory):
|
2023-11-16 12:49:53 +01:00
|
|
|
print(f"Checking for updates to Docker images in {directory}.")
|
|
|
|
os.chdir(directory)
|
2023-11-16 13:15:15 +01:00
|
|
|
before_digests = get_image_digests(directory)
|
2023-11-16 12:49:53 +01:00
|
|
|
print("Pulling docker images.")
|
2023-11-16 14:04:42 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
run_command("docker-compose pull")
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
if "pull access denied" in e.output.decode() or "must be built from source" in e.output.decode():
|
|
|
|
print("Need to build the image from source.")
|
|
|
|
else:
|
|
|
|
print("Failed to pull images with unexpected error.")
|
|
|
|
raise
|
2023-11-16 12:49:53 +01:00
|
|
|
|
2023-11-16 14:04:42 +01:00
|
|
|
after_digests = get_image_digests(directory)
|
2023-11-16 12:49:53 +01:00
|
|
|
if before_digests != after_digests:
|
|
|
|
print("Changes detected in image digests. Rebuilding containers.")
|
|
|
|
run_command("docker-compose up -d --build --force-recreate")
|
|
|
|
else:
|
|
|
|
print("Docker images are up to date. No rebuild necessary.")
|
2023-11-16 12:31:12 +01:00
|
|
|
|
|
|
|
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)
|