Add scripts to run Postgres in docker
Run a Postgres docker image for (manual) testing
This commit is contained in:
parent
2bdbbbc1fb
commit
a08a7cac27
|
|
@ -20,3 +20,4 @@ Cargo.lock
|
||||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
*.pdb
|
*.pdb
|
||||||
|
|
||||||
|
/.python_venv
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
ROOT_DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
|
||||||
|
PYTHON_VENV="${ROOT_DIR}/.python_venv"
|
||||||
|
if [ ! -d "${PYTHON_VENV}" ]; then
|
||||||
|
python3 -m venv "${PYTHON_VENV}"
|
||||||
|
fi
|
||||||
|
"${PYTHON_VENV}/bin/python" "${ROOT_DIR}/dev.py" $@
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
certifi==2024.2.2
|
||||||
|
charset-normalizer==3.3.2
|
||||||
|
docker==7.0.0
|
||||||
|
idna==3.6
|
||||||
|
packaging==23.2
|
||||||
|
requests==2.31.0
|
||||||
|
urllib3==2.2.1
|
||||||
|
|
@ -0,0 +1,90 @@
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
|
root_dir = sys.path[0]
|
||||||
|
|
||||||
|
|
||||||
|
def devupdate(args):
|
||||||
|
subprocess.check_call(
|
||||||
|
[
|
||||||
|
sys.executable,
|
||||||
|
'-m',
|
||||||
|
'pip',
|
||||||
|
'install',
|
||||||
|
'-r',
|
||||||
|
os.path.join(root_dir, 'dev-python-requirements.txt'),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def run(args):
|
||||||
|
import docker
|
||||||
|
|
||||||
|
POSTGRES_USER = 'locality'
|
||||||
|
POSTGRES_PASSWORD = 'wkyhjofg2837f'
|
||||||
|
POSTGRES_DB = 'locality'
|
||||||
|
|
||||||
|
postgres_env = {
|
||||||
|
'POSTGRES_USER': POSTGRES_USER,
|
||||||
|
'POSTGRES_PASSWORD': POSTGRES_PASSWORD,
|
||||||
|
'POSTGRES_DB': POSTGRES_DB,
|
||||||
|
}
|
||||||
|
|
||||||
|
locality_env = {
|
||||||
|
'LOCALITY_DATABASE_URL': 'postgres://{}:{}@localhost/{}'.format(
|
||||||
|
POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
docker_client = docker.from_env()
|
||||||
|
postgres_container = docker_client.containers.run(
|
||||||
|
'postgres:14.11',
|
||||||
|
environment=postgres_env,
|
||||||
|
detach=True,
|
||||||
|
ports={'5432/tcp': ('127.0.0.1', 5432)},
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
docker_stream = postgres_container.attach(stream=True)
|
||||||
|
for line_binary in docker_stream:
|
||||||
|
line = line_binary.decode('utf-8')
|
||||||
|
print(line)
|
||||||
|
if 'listening on IPv4 address "0.0.0.0", port 5432' in line:
|
||||||
|
break
|
||||||
|
for line_binary in docker_stream:
|
||||||
|
line = line_binary.decode('utf-8')
|
||||||
|
print(line)
|
||||||
|
if 'database system is ready to accept connections' in line:
|
||||||
|
break
|
||||||
|
|
||||||
|
cargo_bin = shutil.which('cargo')
|
||||||
|
locality_process = subprocess.Popen(
|
||||||
|
[cargo_bin, 'run'], env=locality_env, cwd=root_dir
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
while locality_process.poll() is None:
|
||||||
|
time.sleep(0.5)
|
||||||
|
finally:
|
||||||
|
if locality_process.poll() is None:
|
||||||
|
locality_process.terminate()
|
||||||
|
finally:
|
||||||
|
postgres_container.stop()
|
||||||
|
postgres_container.remove()
|
||||||
|
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
subparsers = parser.add_subparsers(required=True)
|
||||||
|
run_parser = subparsers.add_parser(
|
||||||
|
'run', help='Run a test instance of locality'
|
||||||
|
)
|
||||||
|
run_parser.set_defaults(func=run)
|
||||||
|
devupdate_parser = subparsers.add_parser(
|
||||||
|
'devupdate', help='Install or update packages used by this script'
|
||||||
|
)
|
||||||
|
devupdate_parser.set_defaults(func=devupdate)
|
||||||
|
args = parser.parse_args()
|
||||||
|
args.func(args)
|
||||||
Loading…
Reference in New Issue