Initial commit

This commit is contained in:
Benedikt Ziemons 2020-11-14 18:50:04 +01:00
commit 7eac724a3a
No known key found for this signature in database
GPG Key ID: 0F54A7ED232D3319
11 changed files with 430 additions and 0 deletions

215
.gitignore vendored Normal file
View File

@ -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

5
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/

View File

@ -0,0 +1,24 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyCompatibilityInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ourVersions">
<value>
<list size="4">
<item index="0" class="java.lang.String" itemvalue="3.6" />
<item index="1" class="java.lang.String" itemvalue="3.7" />
<item index="2" class="java.lang.String" itemvalue="3.8" />
<item index="3" class="java.lang.String" itemvalue="3.9" />
</list>
</value>
</option>
</inspection_tool>
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="INFORMATION" enabled_by_default="true">
<option name="ignoredPackages">
<value>
<list size="0" />
</value>
</option>
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1,7 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="PROJECT_PROFILE" value="Default" />
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (.venv)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/podlaunch.iml" filepath="$PROJECT_DIR$/.idea/podlaunch.iml" />
</modules>
</component>
</project>

8
.idea/podlaunch.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

21
LICENSE Normal file
View File

@ -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.

118
main.py Normal file
View File

@ -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()

18
pod@.service Normal file
View File

@ -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

2
podlaunch Executable file
View File

@ -0,0 +1,2 @@
#!/bin/sh
/usr/bin/env python3 $(dirname $0)/../lib/podlaunch/main.py $@