Files
linux-cloud-router/bin/lm_dev.py
T

28 lines
1.3 KiB
Python

"""What the lm-dev scripts share: the repository's paths, lxc exec in an instance, and the .env of the repository."""
import pathlib
import subprocess
ROOT = pathlib.Path(__file__).resolve().parent.parent
# The test data: scenarios, configurations, cloud-init files, the repository's script and the files copied in.
TESTS = ROOT / 'tests' / 'remote'
# The project of the manual tests, which lm-dev-lxd-project.py makes.
PROJECT = 'lm-dev'
def lxc_exec(remote, project, instance, *command, input=None):
"""Run the command in the instance as root with the input, and return its CompletedProcess."""
return subprocess.run(['lxc', 'exec', f'{remote}:{instance}', '--project', project, '-T', '--', *command],
input=input, capture_output=True, text=True)
def set_dotenv(values):
"""Set the variables in the repository's .env, readable by its owner only, keeping its other lines."""
dotenv = ROOT / '.env'
lines = dotenv.read_text(encoding='utf-8').splitlines() if dotenv.is_file() else []
kept = [line for line in lines if line.split('=', 1)[0] not in values]
dotenv.touch(mode=0o600)
dotenv.chmod(0o600)
dotenv.write_text(''.join(f'{line}\n' for line in kept + [f'{key}={value}' for key, value in values.items()]),
encoding='utf-8')