Skip to content

Commit e0152b0

Browse files
committed
scripts: add script to load the osrd instance of a given PR and run it concurently to the dev instance
Signed-off-by: Ethan Perruzza <ethan.perruzza@gmail.com>
1 parent 99094af commit e0152b0

11 files changed

+346
-14
lines changed

docker/docker-compose.host.yml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ services:
4444
network_mode: host
4545
environment:
4646
OSRDYNE__WORKER_DRIVER__HOST_NETWORKING: "true"
47+
OSRDYNE__WORKER_DRIVER__NETWORK: ""
4748
OSRDYNE__WORKER_DRIVER__DEFAULT_ENV: >
4849
[
4950
"CORE_EDITOAST_URL=http://127.0.0.1:8090",

docker/docker-compose.pr-tests.yml

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
## This Docker Compose file is used by the pr-infra-compose.sh script.
2+
# More explanation on its purpose can be found in the script.
3+
#
4+
# This Docker Compose file is very similar to the one used for the development
5+
# environment, except that no services are built (all images are pulled from DockerHub),
6+
# and all ports have been adjusted (by adding +1 or +2 if the +1 port was already in use)
7+
# so that both instances can run concurrently on the same computer.
8+
9+
volumes:
10+
psql_data:
11+
redis_data:
12+
rabbitmq_data:
13+
14+
networks:
15+
pr-tests:
16+
name: osrd_pr_test
17+
18+
services:
19+
postgres:
20+
image: ${OSRD_POSTGIS_IMAGE:-postgis/postgis:16-3.4-alpine}
21+
container_name: osrd-postgres-pr-tests
22+
user: postgres
23+
restart: unless-stopped
24+
ports: ["5433:5433"]
25+
environment:
26+
POSTGRES_PASSWORD: "password"
27+
command: -c port=5433
28+
volumes:
29+
- "psql_data:/var/lib/postgresql/data"
30+
- "../docker/init_db.sql:/docker-entrypoint-initdb.d/init.sql"
31+
healthcheck:
32+
test: ["CMD", "pg_isready", "-d", "postgres://osrd:password@postgres:5433/osrd"]
33+
start_period: 4s
34+
interval: 5s
35+
networks:
36+
- pr-tests
37+
38+
redis:
39+
image: valkey/valkey:alpine
40+
container_name: osrd-redis-pr-tests
41+
restart: unless-stopped
42+
ports: ["6380:6380"]
43+
volumes:
44+
- "redis_data:/data"
45+
- "../docker/valkey.conf:/etc/valkey.conf"
46+
command: "valkey-server /etc/valkey.conf --port 6380 --save 30 1 --loglevel warning"
47+
healthcheck:
48+
test: ["CMD", "valkey-cli", "-p" , "6380", "ping"]
49+
start_period: 4s
50+
interval: 5s
51+
networks:
52+
- pr-tests
53+
54+
rabbitmq:
55+
image: rabbitmq:3-management
56+
container_name: osrd-rabbitmq-pr-tests
57+
ports:
58+
- "5673:5673"
59+
- "15673:15673"
60+
environment:
61+
RABBITMQ_DEFAULT_USER: osrd
62+
RABBITMQ_DEFAULT_PASS: password
63+
RABBITMQ_DIST_PORT: 25673
64+
RABBITMQ_CONFIG_FILE: "/etc/rabbitmq/rabbitmq.conf"
65+
volumes:
66+
- rabbitmq_data:/var/lib/rabbitmq
67+
- ../docker/rabbitmq-pr-tests.conf:/etc/rabbitmq/rabbitmq.conf
68+
healthcheck:
69+
test: ["CMD", "rabbitmqctl", "status"]
70+
interval: 15s
71+
timeout: 30s
72+
retries: 3
73+
networks:
74+
- pr-tests
75+
76+
core:
77+
image: ghcr.io/openrailassociation/osrd-edge/osrd-core:${PR_NB}
78+
container_name: osrd-core-dummy-pr-tests
79+
restart: "no"
80+
command: "true"
81+
networks:
82+
- pr-tests
83+
84+
editoast:
85+
image: ghcr.io/openrailassociation/osrd-edge/osrd-editoast:${PR_NB}
86+
container_name: osrd-editoast-pr-tests
87+
depends_on:
88+
postgres: {condition: service_healthy}
89+
redis: {condition: service_healthy}
90+
rabbitmq: {condition: service_healthy}
91+
restart: unless-stopped
92+
ports: ["8091:8091"]
93+
environment:
94+
EDITOAST_PORT: 8091
95+
ROOT_URL: "http://localhost:8091"
96+
REDIS_URL: "redis://redis:6380"
97+
DATABASE_URL: "postgres://osrd:password@postgres:5433/osrd"
98+
TELEMETRY_KIND: "opentelemetry"
99+
TELEMETRY_ENDPOINT: "http://jaeger:4319"
100+
OSRD_MQ_URL: "amqp://osrd:password@osrd-rabbitmq-pr-tests:5673/%2f"
101+
OSRDYNE_API_URL: "http://osrd-osrdyne-pr-tests:4243/"
102+
command:
103+
- /bin/sh
104+
- -c
105+
- "diesel migration run && exec editoast runserver"
106+
healthcheck:
107+
test: ["CMD", "curl", "-f", "http://localhost:8091/health"]
108+
start_period: 4s
109+
interval: 5s
110+
networks:
111+
- pr-tests
112+
113+
osrd-images:
114+
image: ghcr.io/openrailassociation/osrd-stable/osrd-images:${TAG_IMAGES-v1.0.0}
115+
container_name: osrd-images-pr-tests
116+
restart: unless-stopped
117+
environment:
118+
- NGINX_PORT=8081
119+
ports: ["8081:8081"]
120+
networks:
121+
- pr-tests
122+
123+
gateway:
124+
image: ghcr.io/openrailassociation/osrd-edge/osrd-gateway:${PR_NB}-front
125+
container_name: osrd-gateway-pr-tests
126+
volumes:
127+
- "../docker/gateway.pr-tests.simple.toml:/srv/gateway.toml"
128+
restart: unless-stopped
129+
ports: ["4001:4001"]
130+
networks:
131+
- pr-tests
132+
133+
osrdyne:
134+
image: ghcr.io/openrailassociation/osrd-edge/osrd-osrdyne:${PR_NB}
135+
container_name: osrd-osrdyne-pr-tests
136+
depends_on:
137+
rabbitmq: {condition: service_healthy}
138+
volumes:
139+
- ${DOCKER_SOCKET:-/var/run/docker.sock}:/var/run/docker.sock
140+
- ../docker/osrdyne-pr-tests.yml:/osrdyne.yml
141+
command: osrdyne
142+
restart: unless-stopped
143+
ports: ["4243:4243"]
144+
environment:
145+
OSRDYNE__WORKER_DRIVER__WORKER_IMAGE: "ghcr.io/openrailassociation/osrd-edge/osrd-core:${PR_NB}"
146+
RUST_LOG: "info"
147+
networks:
148+
- pr-tests
149+
150+
jaeger:
151+
image: jaegertracing/all-in-one:latest
152+
container_name: osrd-jaeger-pr-tests
153+
restart: unless-stopped
154+
command: --collector.otlp.grpc.host-port :4319 --collector.otlp.http.host-port :4320 --query.http-server.host-port :16687 --collector.grpc-server.host-port :14251
155+
ports:
156+
- "4319:4319"
157+
- "4320:4320"
158+
- "16687:16687"
159+
networks:
160+
- pr-tests
161+
162+
wait-healthy:
163+
depends_on:
164+
editoast: {condition: service_healthy}
165+
restart: "no"
166+
image: alpine:latest
167+
command: "true"
168+
networks:
169+
- pr-tests

docker/gateway.pr-tests.simple.toml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This config file is copied into the default dev container
2+
3+
# generate a production key with this command:
4+
# python3 -c "import secrets, base64; print(base64.standard_b64encode(secrets.token_bytes(64)).decode())"
5+
secret_key = "NOT+A+SECRET++NOT+A+SECRET++NOT+A+SECRET++NOT+A+SECRET++NOT+A+SECRET++NOT+A+SECRET++NOT+A+SECRET"
6+
7+
listen_addr = "0.0.0.0"
8+
port = 4001
9+
10+
[telemetry.tracing]
11+
enable = true
12+
type = "Otlp"
13+
service_name = "gateway"
14+
endpoint = "http://osrd-jaeger-pr-tests:4319"
15+
16+
17+
[static_files]
18+
root_folder = "/srv/front"
19+
redirect_404_to_index = true
20+
21+
22+
[[targets]]
23+
tracing_name = "editoast"
24+
prefix = "/api"
25+
upstream = "http://osrd-editoast-pr-tests:8091"
26+
require_auth = true
27+
28+
[[targets]]
29+
tracing_name = "images"
30+
prefix = "/images"
31+
upstream = "http://osrd-images:8080"
32+
require_auth = false
33+
34+
35+
[auth]
36+
default_provider = "mock"
37+
secure_cookies = false
38+
39+
[[auth.providers]]
40+
type = "Mocked"
41+
provider_id = "mock"
42+
username = "Example User"
43+
require_login = false

docker/init_db.sql

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
create user osrd with password 'password' createdb;
22

3-
-- Create template_osrd databse
3+
-- Create template_osrd database
44
create database template_osrd WITH IS_TEMPLATE true;
5+
6+
-- Connect to template_osrd
57
\c template_osrd
8+
9+
-- Enable required extensions
610
create extension postgis;
711
create extension pg_trgm;
812
create extension unaccent;
913

14+
-- Grant privileges to user
1015
grant all privileges on schema public to osrd;
1116

17+
-- Create osrd database using template_osrd
1218
create database osrd TEMPLATE template_osrd;

docker/osrdyne-pr-tests.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
worker_driver:
2+
type: "DockerDriver"
3+
network: "osrd_pr_test"
4+
container_prefix: "osrd"
5+
container_command: ["sh", "-c", "exec java $JAVA_OPTS -ea -jar /app/osrd_core.jar worker"]
6+
default_env:
7+
- "CORE_EDITOAST_URL=http://osrd-editoast-pr-tests:8091"
8+
- "JAVA_TOOL_OPTIONS=-javaagent:/app/opentelemetry-javaagent.jar"
9+
- "CORE_MONITOR_TYPE=opentelemetry"
10+
- "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc"
11+
- "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://osrd-jaeger-pr-tests:4319"
12+
- "OTEL_METRICS_EXPORTER=none"
13+
- "OTEL_LOGS_EXPORTER=none"
14+
amqp_uri: "amqp://osrd:password@osrd-rabbitmq-pr-tests:5673/%2f"
15+
management_port: 15673
16+
api_address: "0.0.0.0:4243"

docker/osrdyne.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
worker_driver:
22
type: "DockerDriver"
33
worker_image: "ghcr.io/openrailassociation/osrd-edge/osrd-core:dev"
4+
network: "osrd_default"
45
container_prefix: "osrd"
56
container_command: ["sh", "-c", "exec java $JAVA_OPTS -ea -jar /app/osrd_core.jar worker"]
67
default_env:

docker/rabbitmq-pr-tests.conf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
listeners.tcp.port = 5673
2+
management.tcp.port = 15673
3+
distribution.listener.port_range.min = 25673
4+
distribution.listener.port_range.max = 25673

osrdyne/src/drivers/docker.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct DockerDriverOptions {
3030
pub default_env: Vec<String>,
3131
// Should the containers be started in host networking mode
3232
pub host_networking: Option<bool>,
33+
/// The network to start the worker container in
34+
pub network: String,
3335
}
3436

3537
pub struct DockerDriver {
@@ -95,7 +97,7 @@ impl WorkerDriver for DockerDriver {
9597

9698
let mut networking_config = Some(NetworkingConfig {
9799
endpoints_config: HashMap::from([(
98-
"osrd_default".to_string(),
100+
self.options.network.clone(),
99101
bollard::models::EndpointSettings {
100102
..Default::default()
101103
},

scripts/cleanup-db.sh

+23-9
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,23 @@ if [ "$#" -ne 0 ]; then
1414
exit 1
1515
fi
1616

17+
# These variables are necessary to load the infra on the correct instance (the pr-infra or the dev one)
18+
OSRD_POSTGRES="osrd-postgres"
19+
OSRD_EDITOAST="osrd-editoast"
20+
OSRD_REDIS="osrd-redis"
21+
OSRD_POSTGRES_PORT=5432
22+
OSRD_REDIS_PORT=6379
23+
if [ "$PR_TEST" -eq 1 ]; then
24+
OSRD_POSTGRES="osrd-postgres-pr-tests"
25+
OSRD_EDITOAST="osrd-editoast-pr-tests"
26+
OSRD_REDIS="osrd-redis-pr-tests"
27+
OSRD_POSTGRES_PORT=5433
28+
OSRD_REDIS_PORT=6380
29+
fi
30+
1731
# Check if the database exists
1832
echo "Checking database exists..."
19-
DB_EXISTS="$(docker exec osrd-postgres psql -c "SELECT EXISTS (SELECT FROM pg_stat_database WHERE datname = 'osrd');")"
33+
DB_EXISTS="$(docker exec $OSRD_POSTGRES psql -p $OSRD_POSTGRES_PORT -c "SELECT EXISTS (SELECT FROM pg_stat_database WHERE datname = 'osrd');")"
2034
DB_EXISTS="$(echo "$DB_EXISTS" | grep -o -E '[tf]$')"
2135

2236
if [ $DB_EXISTS = 't' ]; then
@@ -30,12 +44,12 @@ if [ $DB_EXISTS = 't' ]; then
3044
echo "Checking database availability..."
3145

3246
# if editoast is running, shut it down.
33-
if [ "$(docker ps -q -f name=osrd-editoast)" ]; then
34-
echo "Stopping editoast..."
35-
docker stop osrd-editoast
47+
if [ "$(docker ps -q -f name=$OSRD_EDITOAST)" ]; then
48+
echo "Stopping $OSRD_EDITOAST..."
49+
docker stop "$OSRD_EDITOAST"
3650
fi
3751

38-
DB_CONN="$(docker exec osrd-postgres psql -c "SELECT numbackends FROM pg_stat_database WHERE datname = 'osrd';")"
52+
DB_CONN="$(docker exec "$OSRD_POSTGRES" psql -p "$OSRD_POSTGRES_PORT" -c "SELECT numbackends FROM pg_stat_database WHERE datname = 'osrd';")"
3953
DB_CONN="$(echo "$DB_CONN" | grep -o -E '[0-9]+$')"
4054

4155
if [ $DB_CONN -ne 0 ]; then
@@ -47,17 +61,17 @@ if [ $DB_EXISTS = 't' ]; then
4761

4862
# Drop database
4963
echo "Deleting osrd database..."
50-
docker exec osrd-postgres psql -c 'DROP DATABASE osrd;' > /dev/null
64+
docker exec "$OSRD_POSTGRES" psql -p "$OSRD_POSTGRES_PORT" -c 'DROP DATABASE osrd;' > /dev/null
5165
fi
5266

5367
echo "Initialize new database (no migration)..."
5468
# Here I remove the first line of the script cause the user already exists
55-
docker exec osrd-postgres sh -c 'cat /docker-entrypoint-initdb.d/init.sql | tail -n 1 > /tmp/init.sql'
56-
docker exec osrd-postgres psql -f //tmp/init.sql > /dev/null
69+
docker exec "$OSRD_POSTGRES" sh -c 'cat /docker-entrypoint-initdb.d/init.sql | tail -n 1 > /tmp/init.sql'
70+
docker exec "$OSRD_POSTGRES" psql -p "$OSRD_POSTGRES_PORT" -f //tmp/init.sql > /dev/null
5771

5872
# Clear Redis Cache
5973
echo "Deleting redis cache..."
60-
docker exec osrd-redis valkey-cli FLUSHALL > /dev/null 2>&1 || docker volume rm -f osrd_redis_data > /dev/null
74+
docker exec "$OSRD_REDIS" valkey-cli -p "$OSRD_REDIS_PORT" FLUSHALL > /dev/null 2>&1 || docker volume rm -f osrd_redis_data > /dev/null
6175

6276
echo "Cleanup done!\n"
6377
echo "You may want to apply migrations if you don't load a backup:"

scripts/load-backup.sh

+11-3
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,25 @@ else
3636
exit 2
3737
fi
3838

39+
# These variables are necessary to load the infra on the correct instance (the pr-infra or the dev one)
40+
OSRD_POSTGRES="osrd-postgres"
41+
OSRD_POSTGRES_PORT=5432
42+
if [ "$PR_TEST" -eq 1 ]; then
43+
OSRD_POSTGRES="osrd-postgres-pr-tests"
44+
OSRD_POSTGRES_PORT=5433
45+
fi
46+
3947
$(dirname "$0")/cleanup-db.sh # Cleanup and init db (no migration)
4048

4149
# Copy needed files to the container
42-
docker cp "$BACKUP_PATH" osrd-postgres:tmp/backup-osrd
50+
docker cp "$BACKUP_PATH" "$OSRD_POSTGRES:tmp/backup-osrd"
4351

4452
# restoring the backend can partialy fail, and that's sometimes ok
4553
echo "Restore backup..."
46-
docker exec osrd-postgres pg_restore --if-exists -c -d osrd -x //tmp/backup-osrd > /dev/null
54+
docker exec "$OSRD_POSTGRES" pg_restore --if-exists -p "$OSRD_POSTGRES_PORT" -c -d osrd -x //tmp/backup-osrd > /dev/null
4755

4856
# analyze db for performances
4957
echo "Analyze for performances..."
50-
docker exec osrd-postgres psql -d osrd -c 'ANALYZE;' > /dev/null
58+
docker exec "$OSRD_POSTGRES" psql -p "$OSRD_POSTGRES_PORT" -d osrd -c 'ANALYZE;' > /dev/null
5159

5260
echo "Load backup done!"

0 commit comments

Comments
 (0)