feat: add self-update functionality

- 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

🤖
This commit is contained in:
Sebastian Mark 2023-12-21 18:38:03 +01:00
parent a2e5852159
commit 130b26ba5f
3 changed files with 24 additions and 1 deletions

View file

@ -23,6 +23,7 @@ See `requirements.txt`
See `dc-ops --help` See `dc-ops --help`
## Configuration Options ## Configuration Options
- `self-update`: (optional) update this script on startup. Defaults to `false`.
- `loglevel`: (optional) define loglevel. Defaults to "INFO" if not set. - `loglevel`: (optional) define loglevel. Defaults to "INFO" if not set.
- `compose-opts`: (optional) define additional parameters to `docker compose up -d`. - `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: - `stacks`: an array containing details for each repository. Each element is a dictionary containing the following keys:

7
dc-ops
View file

@ -11,7 +11,7 @@ from pathlib import Path
import yaml 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 # read config file
configfile = Path(__file__).with_name("config.yml") configfile = Path(__file__).with_name("config.yml")
@ -34,6 +34,11 @@ log.basicConfig(format="%(message)s", level=loglevel)
# define docker compose parameters # define docker compose parameters
composeopts = cfg.get("compose-opts", "") 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 # iterate all stacks
for stack in cfg["stacks"]: for stack in cfg["stacks"]:
# skip disabled stacks # skip disabled stacks

View file

@ -1,9 +1,26 @@
import logging as log import logging as log
import subprocess import subprocess
import sys
from pathlib import Path
import git 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: def run_subprocess(command: str, workdir: str) -> bool:
"""execute subprocess and print output """execute subprocess and print output