diff --git a/tljh/conda.py b/tljh/conda.py index f1053b1..5b96249 100644 --- a/tljh/conda.py +++ b/tljh/conda.py @@ -4,6 +4,10 @@ Wrap conda commandline program import os import subprocess import json +import sys + +# Use sys.executable to call conda to avoid needing to fudge PATH +CONDA_EXECUTABLE = [sys.executable, '-m', 'conda'] def ensure_conda_env(prefix): @@ -13,7 +17,7 @@ def ensure_conda_env(prefix): abspath = os.path.abspath(prefix) try: output = json.loads( - subprocess.check_output(['conda', 'create', '--json', '--prefix', abspath]).decode() + subprocess.check_output(CONDA_EXECUTABLE + ['create', '--json', '--prefix', abspath]).decode() ) except subprocess.CalledProcessError as e: output = json.loads(e.output.decode()) @@ -31,8 +35,8 @@ def ensure_conda_packages(prefix, packages): abspath = os.path.abspath(prefix) # Let subprocess errors propagate # FIXME: raise different exception when using - raw_output = subprocess.check_output([ - 'conda', 'install', + raw_output = subprocess.check_output(CONDA_EXECUTABLE + [ + 'install', '--json', '--prefix', abspath ] + packages).decode() diff --git a/tljh/systemd.py b/tljh/systemd.py new file mode 100644 index 0000000..53cc42b --- /dev/null +++ b/tljh/systemd.py @@ -0,0 +1,59 @@ +""" +Wraps systemctl to install, uninstall, start & stop systemd services. + +We use sudo + subprocess calls for everything. This works when we +are running as root & as normal user (with arbitrary sudo privileges). +Arbitrary sudo privileges suck, but are better than running the whole +process as root. + +If we use a debian package instead, we can get rid of all this code. +""" +import subprocess +import os + + +def reload_daemon(): + """ + Equivalent to systemctl daemon-reload. + + Makes systemd discover new units. + """ + subprocess.run([ + 'sudo', + 'systemctl', + 'daemon-reload' + ], check=True) + + +def install_unit(name, unit, path='/etc/systemd/system'): + """ + Install unit wih given name + """ + subprocess.run([ + 'sudo', + 'tee', + os.path.join(path, name) + ], input=unit.encode('utf-8'), check=True) + + +def uninstall_unit(name, path='/etc/systemd/system'): + """ + Uninstall unit with given name + """ + subprocess.run([ + 'sudo', + 'rm', + os.path.join(path, name) + ], check=True) + + +def start_service(name): + """ + Start service with given name. + """ + subprocess.run([ + 'sudo', + 'systemctl', + 'start', + name + ], check=True)