Introduce lxc- prefix for services
This commit is contained in:
parent
2d3890fd51
commit
539a61662d
@ -1,23 +0,0 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import subprocess
|
|
||||||
import sys
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Print shifted UID / GID of an LXC user')
|
|
||||||
parser.add_argument('--group', '-g', action='store_true', help='Get primary GID')
|
|
||||||
parser.add_argument('container', help='LXC container in which to lookup')
|
|
||||||
parser.add_argument('user', help='User to lookup')
|
|
||||||
|
|
||||||
if len(sys.argv) < 2:
|
|
||||||
parser.print_usage()
|
|
||||||
sys.exit(1)
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
# Check if lxc-ls prints back the container name
|
|
||||||
is_running = subprocess.run(['lxc-ls', '--running', args.container], stdout=subprocess.PIPE, check=True)
|
|
||||||
lxc_command = 'lxc-attach' if is_running else 'lxc-execute'
|
|
||||||
# Run id command inside container
|
|
||||||
id = subprocess.run([lxc_command, args.container, '--', 'id', '-g' if args.group else '-u', args.user], stdout=subprocess.PIPE, check=True)
|
|
||||||
print(int(id.stdout) + 100000)
|
|
@ -6,12 +6,16 @@ import subprocess
|
|||||||
from .paths import AUTOSTART_SVC_DIR, SERVICE_DIR, STARTED_SVC_DIR
|
from .paths import AUTOSTART_SVC_DIR, SERVICE_DIR, STARTED_SVC_DIR
|
||||||
from .templates import SERVICE
|
from .templates import SERVICE
|
||||||
|
|
||||||
|
def lxcize(service):
|
||||||
|
# Prepend lxc- to service name, otherwise there's much greater risk of naming conflict with other host services
|
||||||
|
return 'lxc-{}'.format(service)
|
||||||
|
|
||||||
def create_service(container, image):
|
def create_service(container, image):
|
||||||
depends = ' '.join(image['depends']) if 'depends' in image else ''
|
depends = ' '.join([lxcize(service) for service in image['depends']]) if 'depends' in image else ''
|
||||||
# Add ready check to start_post
|
# Add ready check to start_post
|
||||||
# This could arguably be better done via some template engine, but introducing one for 2 template files seems like overkill
|
# This could arguably be done better via some template engine, but introducing one for a single template file seems like an overkill
|
||||||
start_post = '\nstart_post() {{\n timeout -t 60 lxc-attach {} -- sh -c \'until {}; do sleep 0.1; done\'\n}}\n'.format(container, image['ready']) if 'ready' in image else ''
|
start_post = '\nstart_post() {{\n timeout -t 60 lxc-attach {} -- sh -c \'until {}; do sleep 0.1; done\'\n}}\n'.format(container, image['ready']) if 'ready' in image else ''
|
||||||
service_file = os.path.join(SERVICE_DIR, container)
|
service_file = os.path.join(SERVICE_DIR, lxcize(container))
|
||||||
with open(service_file, 'w') as f:
|
with open(service_file, 'w') as f:
|
||||||
f.write(SERVICE.format(container=container, depends=depends, start_post=start_post))
|
f.write(SERVICE.format(container=container, depends=depends, start_post=start_post))
|
||||||
os.chmod(service_file, 0o755)
|
os.chmod(service_file, 0o755)
|
||||||
@ -23,7 +27,7 @@ def delete_service(service):
|
|||||||
if is_service_autostarted(service):
|
if is_service_autostarted(service):
|
||||||
update_service_autostart(service, False)
|
update_service_autostart(service, False)
|
||||||
try:
|
try:
|
||||||
os.unlink(os.path.join(SERVICE_DIR, service))
|
os.unlink(os.path.join(SERVICE_DIR, lxcize(service)))
|
||||||
update_services()
|
update_services()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
@ -33,20 +37,20 @@ def update_services():
|
|||||||
|
|
||||||
def start_service(service):
|
def start_service(service):
|
||||||
if not is_service_started(service):
|
if not is_service_started(service):
|
||||||
subprocess.run(['/sbin/service', service, 'stop'], check=True)
|
subprocess.run(['/sbin/service', lxcize(service), 'start'], check=True)
|
||||||
|
|
||||||
def stop_service(service):
|
def stop_service(service):
|
||||||
if is_service_started(service):
|
if is_service_started(service):
|
||||||
subprocess.run(['/sbin/service', service, 'stop'], check=True)
|
subprocess.run(['/sbin/service', lxcize(service), 'stop'], check=True)
|
||||||
|
|
||||||
def is_service_started(service):
|
def is_service_started(service):
|
||||||
# Check OpenRC service status without calling any binary
|
# Check OpenRC service status without calling any binary
|
||||||
return os.path.exists(os.path.join(STARTED_SVC_DIR, service))
|
return os.path.exists(os.path.join(STARTED_SVC_DIR, lxcize(service)))
|
||||||
|
|
||||||
def is_service_autostarted(service):
|
def is_service_autostarted(service):
|
||||||
# Check OpenRC service enablement
|
# Check OpenRC service enablement
|
||||||
return os.path.exists(os.path.join(AUTOSTART_SVC_DIR, service))
|
return os.path.exists(os.path.join(AUTOSTART_SVC_DIR, lxcize(service)))
|
||||||
|
|
||||||
def update_service_autostart(service, enabled):
|
def update_service_autostart(service, enabled):
|
||||||
# Add/remove the service to/from OpenRC default runlevel
|
# Add/remove the service to/from OpenRC default runlevel
|
||||||
subprocess.run(['/sbin/rc-update', 'add' if enabled else 'del', service])
|
subprocess.run(['/sbin/rc-update', 'add' if enabled else 'del', lxcize(service)])
|
||||||
|
Loading…
Reference in New Issue
Block a user