From 7eac724a3a3d0d834091468cdb3e2c433b7c5a8f Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Sat, 14 Nov 2020 18:50:04 +0100 Subject: [PATCH] Initial commit --- .gitignore | 215 ++++++++++++++++++ .idea/.gitignore | 5 + .idea/inspectionProfiles/Project_Default.xml | 24 ++ .../inspectionProfiles/profiles_settings.xml | 7 + .idea/misc.xml | 4 + .idea/modules.xml | 8 + .idea/podlaunch.iml | 8 + LICENSE | 21 ++ main.py | 118 ++++++++++ pod@.service | 18 ++ podlaunch | 2 + 11 files changed, 430 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/podlaunch.iml create mode 100644 LICENSE create mode 100644 main.py create mode 100644 pod@.service create mode 100755 podlaunch diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a6fba18 --- /dev/null +++ b/.gitignore @@ -0,0 +1,215 @@ +# Created by .ignore support plugin (hsz.mobi) + +### Python template +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..ab30af0 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,24 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..dd4c951 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..479ee61 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..be5f652 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/podlaunch.iml b/.idea/podlaunch.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/podlaunch.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5536a40 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Benedikt Ziemons + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/main.py b/main.py new file mode 100644 index 0000000..f4293a2 --- /dev/null +++ b/main.py @@ -0,0 +1,118 @@ +import json +import os +import pathlib +import sys +import threading +import traceback +from datetime import datetime +from signal import signal, SIGCHLD, SIGHUP, SIGINT, SIGTERM, setitimer, SIGALRM, ITIMER_REAL + +import click +import sh +# noinspection PyUnresolvedReferences +from sh import podman + +SERVICES_BASE_PATH = "/docker/services/" + +sdnotify = sh.Command("systemd-notify") + + +class PodKeeper: + def __init__(self, network, identifier): + self.podnet_args = ("--network", network) if network else () + identifier_path = pathlib.PurePath(identifier) + if len(identifier_path.parts) != 1: + raise ValueError(f"identifier has too many parts: {identifier_path}") + self.podhome = pathlib.Path(SERVICES_BASE_PATH) / identifier_path + if not self.podhome.exists(): + raise NotADirectoryError(f"pod home does not exist: {self.podhome}") + self.podname = f"{identifier}_pod" + self.podyaml = f"pod-{identifier}.yaml" + podyaml_complete = (self.podhome / self.podyaml) + if not podyaml_complete.exists(): + raise FileNotFoundError(f"pod definition does not exist: {podyaml_complete}") + self.stopping = threading.Event() + self.reloading = threading.Event() + self.checking = threading.Event() + self.waiter = threading.Event() + + def destroy(self): + self.stopping.set() + self.waiter.set() + + def reload(self): + self.reloading.set() + self.waiter.set() + + def check(self): + self.checking.set() + self.waiter.set() + + def run(self): + os.chdir(self.podhome) + last_check = datetime.utcnow() + print(f"Starting pod {self.podname} at {last_check}", file=sys.stderr, flush=True) + podman.play.kube(self.podyaml, *self.podnet_args) + sdnotify("--ready") + while not self.stopping.is_set(): + self.waiter.wait() + self.waiter.clear() + if self.checking.is_set(): + self.checking.clear() + new_timestamp = datetime.utcnow() + pod_description = json.loads(podman.pod.inspect(self.podname)) + for container in pod_description["Containers"]: + if container["State"] != "running": + print(f"Container {container['name']} exited", file=sys.stderr, flush=True) + print(f"Log since last check:\n{podman.logs('--since', last_check.isoformat(), container['name'])}", file=sys.stderr, flush=True) + self.destroy() + last_check = new_timestamp + + if self.reloading.is_set(): + self.reloading.clear() + print("Reloading pod", self.podname, file=sys.stderr, flush=True) + try: + podman.pod.kill("--signal", "HUP", self.podname) + except sh.ErrorReturnCode: + print("Error reloading pod", file=sys.stderr, flush=True) + traceback.print_exc() + + self.stop_sequence() + + def stop_sequence(self): + print("Stopping pod", self.podname, file=sys.stderr, flush=True) + try: + podman.pod.stop("-t", "19", self.podname) + successful_stopped = True + except sh.ErrorReturnCode: + print(f"First stop of {self.podname} was not successful!", file=sys.stderr, flush=True) + successful_stopped = False + try: + podman.pod.stop("-t", "5", self.podname) + except sh.ErrorReturnCode: + if not successful_stopped: + print(f"Second stop of {self.podname} was not successful!", file=sys.stderr, flush=True) + try: + podman.pod.rm(self.podname) + except sh.ErrorReturnCode: + print(f"Removal of {self.podname} was not successful!", file=sys.stderr, flush=True) + + +@click.command() +@click.option("--network", default="brodge", help="Network for the created pod") +@click.argument("identifier") +def main(network, identifier): + keeper = PodKeeper(network, identifier) + + signal(SIGCHLD, keeper.destroy) + signal(SIGINT, keeper.destroy) + signal(SIGTERM, keeper.destroy) + signal(SIGHUP, keeper.reload) + signal(SIGALRM, keeper.check) + setitimer(ITIMER_REAL, 4.0, 10.0) + + keeper.run() + + +if __name__ == '__main__': + main() diff --git a/pod@.service b/pod@.service new file mode 100644 index 0000000..b400685 --- /dev/null +++ b/pod@.service @@ -0,0 +1,18 @@ +[Unit] +Description=Podman pod@%i +Documentation=man:podman-generate-systemd(1) +Wants=network.target +After=network-online.target + +[Service] +Type=simple +Environment=PODMAN_SYSTEMD_UNIT=%n +ExecStart=/usr/local/bin/podlaunch %i +Restart=on-failure +RestartSec=30s +TimeoutStartSec=2min +TimeoutStopSec=1min +KillMode=none + +[Install] +WantedBy=multi-user.target default.target diff --git a/podlaunch b/podlaunch new file mode 100755 index 0000000..8cc2bd2 --- /dev/null +++ b/podlaunch @@ -0,0 +1,2 @@ +#!/bin/sh +/usr/bin/env python3 $(dirname $0)/../lib/podlaunch/main.py $@