From 130b26ba5fbcd1cb15c6c11f3350fc0d61fe7a1e Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Thu, 21 Dec 2023 18:38:03 +0100 Subject: [PATCH] feat: add self-update functionality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add new configuration option `self-update` to enable automatic updates - implement `do_selfupdate` function in `lib/helper.py` to perform the update - call `do_selfupdate` in `dc-ops` if `self-update` is enabled 🤖 --- README.md | 1 + dc-ops | 7 ++++++- lib/helper.py | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c0c121f..a9c3677 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ See `requirements.txt` See `dc-ops --help` ## Configuration Options +- `self-update`: (optional) update this script on startup. Defaults to `false`. - `loglevel`: (optional) define loglevel. Defaults to "INFO" if not set. - `compose-opts`: (optional) define additional parameters to `docker compose up -d`. - `stacks`: an array containing details for each repository. Each element is a dictionary containing the following keys: diff --git a/dc-ops b/dc-ops index aa350dc..965bda1 100755 --- a/dc-ops +++ b/dc-ops @@ -11,7 +11,7 @@ from pathlib import Path import yaml -from lib.helper import run_subprocess, update_git_repo +from lib.helper import run_subprocess, update_git_repo, do_selfupdate # read config file configfile = Path(__file__).with_name("config.yml") @@ -34,6 +34,11 @@ log.basicConfig(format="%(message)s", level=loglevel) # define docker compose parameters composeopts = cfg.get("compose-opts", "") +# check for updates of myself +allow_self_update = cfg.get("self-update", False) +if allow_self_update: + do_selfupdate() + # iterate all stacks for stack in cfg["stacks"]: # skip disabled stacks diff --git a/lib/helper.py b/lib/helper.py index 2efa89e..80caf03 100644 --- a/lib/helper.py +++ b/lib/helper.py @@ -1,9 +1,26 @@ import logging as log import subprocess +import sys +from pathlib import Path import git +def do_selfupdate(): + """perform git pull on script's base directory""" + repo_path = Path(sys.argv[0]).parent.absolute() + repo = git.Repo(repo_path) + try: + pull_res = repo.remotes.origin.pull()[0] + except git.exc.GitCommandError as e: + log.error(str(e)) + return + if pull_res.old_commit: + log.info("selfupdate: successfully updated myself - exiting") + sys.exit(0) + log.info("selfupdate: no updates found for myself") + + def run_subprocess(command: str, workdir: str) -> bool: """execute subprocess and print output