65 lines
3.4 KiB
Python
65 lines
3.4 KiB
Python
"""Dedicated disposable PostgreSQL / Redis; never modifies the preview database."""
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import secrets
|
|
import subprocess
|
|
import time
|
|
|
|
if os.environ.get('WSL_DISTRO_NAME') != 'WebUp-Local' or os.geteuid() != 0:
|
|
raise SystemExit('Dedicated local environment required.')
|
|
root = pathlib.Path('/etc/webup-local/test-services')
|
|
root.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
credentials = root / 'services.env'
|
|
if not credentials.exists():
|
|
password, redis_password = secrets.token_hex(32), secrets.token_hex(32)
|
|
values = {
|
|
'POSTGRES_DB': 'webup_integration', 'POSTGRES_USER': 'webup_test', 'POSTGRES_PASSWORD': password,
|
|
'TEST_DATABASE_URL': 'jdbc:postgresql://127.0.0.1:15432/webup_integration',
|
|
'TEST_DATABASE_USER': 'webup_test', 'TEST_DATABASE_PASSWORD': password,
|
|
'TEST_REDIS_HOST': '127.0.0.1', 'TEST_REDIS_PORT': '16379',
|
|
'TEST_REDIS_PASSWORD': redis_password, 'RUN_REDIS_TESTS': 'true',
|
|
}
|
|
credentials.write_text(''.join(k + '=' + v + '\n' for k, v in values.items()))
|
|
credentials.chmod(0o600)
|
|
values = dict(line.split('=', 1) for line in credentials.read_text().splitlines())
|
|
postgres_env = root / 'postgres.env'
|
|
postgres_env.write_text(''.join(k + '=' + values[k] + '\n' for k in ('POSTGRES_DB', 'POSTGRES_USER', 'POSTGRES_PASSWORD')))
|
|
postgres_env.chmod(0o600)
|
|
redis_config = root / 'redis.conf'
|
|
redis_config.write_text('bind 0.0.0.0\nprotected-mode yes\nrequirepass ' + values['TEST_REDIS_PASSWORD'] + '\nsave ""\nappendonly no\n')
|
|
# Readable by the container's unprivileged Redis user; parent directory stays root-only.
|
|
redis_config.chmod(0o444)
|
|
|
|
|
|
def docker(*args, **kwargs):
|
|
return subprocess.run(['docker', *args], text=True, timeout=120, **kwargs)
|
|
|
|
|
|
for name, args in (
|
|
('webup-test-postgres', ['--publish', '127.0.0.1:15432:5432', '--env-file', str(postgres_env),
|
|
'--volume', 'webup-test-postgres-data:/var/lib/postgresql/data',
|
|
'public.ecr.aws/docker/library/postgres:17']),
|
|
('webup-test-redis', ['--publish', '127.0.0.1:16379:6379', '--mount', 'type=bind,source=' + str(redis_config) + ',target=/etc/redis/redis.conf,readonly',
|
|
'public.ecr.aws/docker/library/redis:7.4', 'redis-server', '/etc/redis/redis.conf']),
|
|
):
|
|
existing = docker('container', 'inspect', name, capture_output=True)
|
|
if existing.returncode == 0:
|
|
metadata = json.loads(existing.stdout)[0]
|
|
if metadata['Config'].get('Labels', {}).get('webup.local-test') != 'true':
|
|
raise SystemExit('Refusing to modify an unowned container: ' + name)
|
|
docker('start', name, check=True, stdout=subprocess.DEVNULL)
|
|
else:
|
|
docker('run', '--detach', '--name', name, '--restart', 'unless-stopped', '--label', 'webup.local-test=true', *args,
|
|
check=True, stdout=subprocess.DEVNULL)
|
|
for attempt in range(30):
|
|
ready = docker('exec', 'webup-test-postgres', 'pg_isready', '-U', 'webup_test', '-d', 'webup_integration', capture_output=True)
|
|
if ready.returncode == 0:
|
|
break
|
|
time.sleep(1)
|
|
else:
|
|
raise SystemExit('PostgreSQL did not become ready')
|
|
host_credentials = pathlib.Path(__file__).resolve().parents[2] / '.runtime/local-cluster/services.env'
|
|
host_credentials.write_text(credentials.read_text())
|
|
print('Disposable PostgreSQL 17 and password-protected Redis 7.4 started on loopback ports 15432 / 16379.')
|