-
|
Hi, I want to add some extra functionality to quadlets, and I figured systemd generators combined with unit dropin files should come in handy. First I thought of defining entirely new directives, but I think that might not be doable without breaking the existing generator. One thing I want to do is use a couple of shorthands for the rather verbose Traefik labelling system. Writing your own generators seems simple enough. Ditto for some basic parsing of quadlets, but I'm curious as to whether there are any existing utilities I can use to this end? I was unable to find the source for |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
See the file Here is how I found it
|
Beta Was this translation helpful? Give feedback.
-
|
Ok, I guess I should have read up more on generators. They only have write access to the final unit directory, and are only intended to be used that way, which I suppose makes sense. I was hoping to write |
Beta Was this translation helpful? Give feedback.
-
|
I've ended up with this. It's a little hacky, but it works well enough. It goes in
#!/bin/python3
import sys
import os
import glob
import re
from pathlib import Path
CONTAINER_DIR = '/etc/containers/systemd'
TRAEFIK_DIR = '/etc/traefik/dynamic/containers.d'
normal_dir = sys.argv[1]
def get_container_info(path):
container = {
'name': Path(path).stem,
'labels': {},
}
with open(path) as file:
for line in file:
line = line.strip()
match = re.match(r'\[(.+)\]', line)
if match:
section_name = match.group(1)
elif not (line.startswith('#') or line.startswith(';')):
match = re.match(r'(.+?)=(.+)', line)
if match:
key = match.group(1)
value = match.group(2)
if key == 'Label':
key, value = value.split('=', maxsplit=1)
container['labels'][key] = value
return container
for file in glob.glob(f'{CONTAINER_DIR}/*.container'):
container = get_container_info(file)
proxy_host = container['labels'].get('proxy.host')
if proxy_host is not None:
unit_file = Path(os.path.join(normal_dir, container['name'] + '.service.d', 'proxy.conf'))
unit_file.parent.mkdir(exist_ok=True)
with open(unit_file, 'w') as o:
config_path = f'{TRAEFIK_DIR}/%N.yaml'
proxy_args = proxy_host.replace(',', ' ')
proxy_port = container['labels'].get('proxy.port')
if proxy_port is not None:
proxy_args = f'-p {proxy_port} {proxy_args}'
o.write('[Service]\n')
o.write(f'ExecStartPost=/usr/local/bin/create_proxy_config %N -o {config_path} {proxy_args}\n')
o.write(f'ExecStopPost=rm {config_path}\n') |
Beta Was this translation helpful? Give feedback.
I've ended up with this. It's a little hacky, but it works well enough. It goes in
/usr/lib/systemd/system-generators(though/etc/systemd/system-generatorsmight be more appropriate) and parses the quadlet files (not all possible directories) and generates unit dropins to override the generated podman units. Using labels is the best way I could think of for defining arbitrary properties. Other possible features include:SYSTEMD_SCOPEto determine whether system or user quadlets should be processed, but user processing might require placing the generator in a different directory, such as/etc/systemd/user-generators?ExecStartdirec…