Add Dev Mode and fix test race conditions.
This commit is contained in:
parent
947453869e
commit
adc24ec91e
5 changed files with 67 additions and 14 deletions
17
Dockerfile
17
Dockerfile
|
@ -1,4 +1,6 @@
|
||||||
FROM python:3.11
|
###################################################################
|
||||||
|
# Base/release container
|
||||||
|
FROM python:3.11 AS release
|
||||||
|
|
||||||
ARG DEBIAN_FRONTEND=noninteractive
|
ARG DEBIAN_FRONTEND=noninteractive
|
||||||
|
|
||||||
|
@ -19,3 +21,16 @@ EXPOSE 80/tcp
|
||||||
|
|
||||||
CMD [ "/usr/bin/supervisord", "-c", "/etc/supervisord.conf" ]
|
CMD [ "/usr/bin/supervisord", "-c", "/etc/supervisord.conf" ]
|
||||||
|
|
||||||
|
|
||||||
|
###################################################################
|
||||||
|
# Dev container
|
||||||
|
FROM release AS dev
|
||||||
|
# we don't actually do anything different yet
|
||||||
|
|
||||||
|
|
||||||
|
###################################################################
|
||||||
|
# Utility container for running commands (tests, most notably)
|
||||||
|
FROM release AS operation
|
||||||
|
|
||||||
|
# don't run the server itself, just start up the environment and assume we'll exec things from the outside
|
||||||
|
CMD sleep infinity
|
||||||
|
|
5
docker-compose-operation.yml
Normal file
5
docker-compose-operation.yml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
services:
|
||||||
|
files:
|
||||||
|
container_name: "themotte"
|
||||||
|
build:
|
||||||
|
target: operation
|
|
@ -5,6 +5,7 @@ services:
|
||||||
container_name: "themotte"
|
container_name: "themotte"
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
|
target: dev
|
||||||
volumes:
|
volumes:
|
||||||
- "./:/service"
|
- "./:/service"
|
||||||
env_file: env
|
env_file: env
|
||||||
|
@ -21,11 +22,13 @@ services:
|
||||||
- postgres
|
- postgres
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
|
container_name: "themotte_redis"
|
||||||
image: redis
|
image: redis
|
||||||
ports:
|
ports:
|
||||||
- "6379:6379"
|
- "6379:6379"
|
||||||
|
|
||||||
postgres:
|
postgres:
|
||||||
|
container_name: "themotte_postgres"
|
||||||
image: postgres:12.3
|
image: postgres:12.3
|
||||||
# command: ["postgres", "-c", "log_statement=all"]
|
# command: ["postgres", "-c", "log_statement=all"]
|
||||||
# uncomment this if u wanna output all SQL queries to the console
|
# uncomment this if u wanna output all SQL queries to the console
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import sys, subprocess, time
|
import sys, subprocess, time
|
||||||
|
|
||||||
def _execute(command,**kwargs):
|
def _execute(command,**kwargs):
|
||||||
check = kwargs.get('check',False)
|
check = kwargs.get('check', True)
|
||||||
on_stdout_line = kwargs.get('on_stdout_line', None)
|
on_stdout_line = kwargs.get('on_stdout_line', None)
|
||||||
on_stderr_line = kwargs.get('on_stderr_line', None)
|
on_stderr_line = kwargs.get('on_stderr_line', None)
|
||||||
with subprocess.Popen(
|
with subprocess.Popen(
|
||||||
|
@ -51,29 +51,59 @@ def _docker(command, **kwargs):
|
||||||
' && '.join(command)
|
' && '.join(command)
|
||||||
], **kwargs)
|
], **kwargs)
|
||||||
|
|
||||||
def _running():
|
def _status_single(server):
|
||||||
command = ['docker','container','inspect','-f','{{.State.Status}}','themotte']
|
command = ['docker', 'container', 'inspect', '-f', '{{.State.Status}}', server]
|
||||||
result = _execute(command,check=False).stdout.strip()
|
result = _execute(command, check=False).stdout.strip()
|
||||||
return result == "running"
|
print(f"{server}: {result}")
|
||||||
|
return result
|
||||||
|
|
||||||
|
# this should really be yanked out of the docker-compose somehow
|
||||||
|
_containers = ["themotte", "themotte_postgres", "themotte_redis"]
|
||||||
|
|
||||||
|
def _all_running():
|
||||||
|
for container in _containers:
|
||||||
|
if _status_single(container) != "running":
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _any_exited():
|
||||||
|
for container in _containers:
|
||||||
|
if _status_single(container) == "exited":
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
def _start():
|
def _start():
|
||||||
command = ['docker-compose','up','--build','-d']
|
command = [
|
||||||
result = _execute(command,check=True)
|
'docker-compose',
|
||||||
time.sleep(1)
|
'-f', 'docker-compose.yml',
|
||||||
|
'-f', 'docker-compose-operation.yml',
|
||||||
|
'up',
|
||||||
|
'--build',
|
||||||
|
'-d',
|
||||||
|
]
|
||||||
|
result = _execute(command)
|
||||||
|
|
||||||
|
while not _all_running():
|
||||||
|
if _any_exited():
|
||||||
|
raise RuntimeError("Server exited prematurely")
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _stop():
|
def _stop():
|
||||||
command = ['docker-compose','down']
|
command = ['docker-compose','down']
|
||||||
result = _execute(command,check=True)
|
result = _execute(command)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _operation(name, command):
|
def _operation(name, command):
|
||||||
# check if running and start if not
|
# check if running and start if not
|
||||||
running = _running()
|
running = _all_running()
|
||||||
|
|
||||||
if not running:
|
if not running:
|
||||||
print("Starting containers...")
|
print("Starting containers . . .")
|
||||||
_start()
|
_start()
|
||||||
|
|
||||||
# run operation in docker container
|
# run operation in docker container
|
||||||
|
@ -85,7 +115,7 @@ def _operation(name, command):
|
||||||
)
|
)
|
||||||
|
|
||||||
if not running:
|
if not running:
|
||||||
print("Stopping containers...")
|
print("Stopping containers . . .")
|
||||||
_stop()
|
_stop()
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -4,7 +4,7 @@ import sys
|
||||||
from common import _operation
|
from common import _operation
|
||||||
|
|
||||||
def run_test(args):
|
def run_test(args):
|
||||||
result = _operation("tests",[
|
result = _operation("tests", [
|
||||||
"FLASK_APP=files/cli:app python3 -m flask db upgrade",
|
"FLASK_APP=files/cli:app python3 -m flask db upgrade",
|
||||||
"python3 -m pytest -s",
|
"python3 -m pytest -s",
|
||||||
])
|
])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue