Skip to content

Commit 486e95e

Browse files
committed
Add script
1 parent 9608022 commit 486e95e

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

.evergreen/scripts/generate_config.py

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# /// script
2+
# requires-python = ">=3.9"
3+
# dependencies = [
4+
# "shrub.py>=3.2.0",
5+
# "pyyaml>=6.0.2"
6+
# ]
7+
# ///
8+
9+
# Note: Run this file with `hatch run`, `pipx run`, or `uv run`.
10+
from __future__ import annotations
11+
12+
import os
13+
import sys
14+
from itertools import product
15+
from pathlib import Path
16+
17+
from shrub.v3.evg_build_variant import BuildVariant
18+
from shrub.v3.evg_command import FunctionCall
19+
from shrub.v3.evg_project import EvgProject
20+
from shrub.v3.evg_task import EvgTask, EvgTaskRef
21+
from shrub.v3.shrub_service import ShrubService
22+
23+
# Top level variables.
24+
ALL_VERSIONS = ["4.0", "4.4", "5.0", "6.0", "7.0", "8.0", "rapid", "latest"]
25+
CPYTHONS = ["py3.9", "py3.10", "py3.11", "py3.12", "py3.13"]
26+
PYPYS = ["pypy3.9", "pypy3.10"]
27+
ALL_PYTHONS = CPYTHONS + PYPYS
28+
ALL_WIN_PYTHONS = CPYTHONS.copy()
29+
ALL_WIN_PYTHONS = ALL_WIN_PYTHONS + [f"32-bit {p}" for p in ALL_WIN_PYTHONS]
30+
AUTHS = ["noauth", "auth"]
31+
SSLS = ["nossl", "ssl"]
32+
AUTH_SSLS = list(product(AUTHS, SSLS))
33+
TOPOLOGIES = ["standalone", "replica_set", "sharded_cluster"]
34+
C_EXTS = ["without c extensions", "with c extensions"]
35+
BATCHTIME_WEEK = 10080
36+
HOSTS = dict(rhel8="rhel87-small", Win64="windows-64-vsMulti-small", macOS="macos-14")
37+
38+
39+
# Helper functions.
40+
def create_variant(task_names, display_name, *, python=None, host=None, **kwargs):
41+
task_refs = [EvgTaskRef(name=n) for n in task_names]
42+
kwargs.setdefault("expansions", dict())
43+
expansions = kwargs.pop("expansions")
44+
host = host or "rhel8"
45+
run_on = [HOSTS[host]]
46+
name = display_name.replace(" ", "-").lower()
47+
if python:
48+
expansions["PYTHON_BINARY"] = get_python_binary(python, host)
49+
expansions = expansions or None
50+
return BuildVariant(
51+
name=name,
52+
display_name=display_name,
53+
tasks=task_refs,
54+
expansions=expansions,
55+
run_on=run_on,
56+
**kwargs,
57+
)
58+
59+
60+
def get_python_binary(python, host):
61+
if host.lower() == "win64":
62+
is_32 = python.startswith("32-bit")
63+
if is_32:
64+
_, python = python.split()
65+
base = "C:/python/32/"
66+
else:
67+
base = "C:/python/"
68+
middle = python.replace("py", "Python").replace(".", "")
69+
return base + middle + "/python.exe"
70+
71+
if host.lower() == "rhel8":
72+
if python.startswith("pypy"):
73+
return f"/opt/python/{python}/bin/python3"
74+
return f"/opt/python/{python[2:]}/bin/python3"
75+
76+
if host.lower() == "macos":
77+
ver = python.replace("py", "")
78+
return f"/Library/Frameworks/Python.Framework/Versions/{ver}/bin/python3"
79+
80+
raise ValueError(f"no match found for {python} on {host}")
81+
82+
83+
def write_output(project: EvgProject, target: str) -> str:
84+
HERE = Path(__file__).resolve().parent
85+
with open(HERE.parent / "generated_configs" / target, "w") as fid:
86+
fid.write(ShrubService.generate_yaml(project))
87+
88+
89+
##############
90+
# OCSP
91+
##############
92+
93+
94+
def create_ocsp_task(file_name, server_type):
95+
algo = file_name.split("-")[0]
96+
97+
# Create ocsp server call.
98+
vars = dict(OCSP_ALGORITHM=algo, SERVER_TYPE=server_type)
99+
server_func = FunctionCall(func="run-ocsp-server", vars=vars)
100+
101+
# Create bootstrap function call.
102+
vars = dict(ORCHESTRATION_FILE=file_name)
103+
bootstrap_func = FunctionCall(func="bootstrap mongo-orchestration", vars=vars)
104+
105+
# Create test function call.
106+
should_succeed = "true" if "valid" in server_type else "false"
107+
vars = dict(OCSP_ALGORITHM=algo, OCSP_TLS_SHOULD_SUCCEED=should_succeed)
108+
test_func = FunctionCall(func="run tests", vars=vars)
109+
110+
# Handle tags.
111+
tags = ["ocsp", f"ocsp-{algo}"]
112+
if "mustStaple" in file_name:
113+
tags.append("ocsp-staple")
114+
115+
# Create task.
116+
name = file_name.replace(".json", "")
117+
task_name = f"test-ocsp-{server_type}-{name}"
118+
commands = [server_func, bootstrap_func, test_func]
119+
return EvgTask(name=task_name, tags=tags, commands=commands)
120+
121+
122+
tasks = []
123+
124+
# Create OCSP tasks.
125+
if not os.environ.get("DRIVERS_TOOLS"):
126+
print("Set DRIVERS_TOOLS environment variable!") # noqa: T201
127+
sys.exit(1)
128+
config = Path(os.environ["DRIVERS_TOOLS"]) / ".evergreen/orchestration/configs/servers"
129+
for path in config.glob("*ocsp*"):
130+
for server_type in ["valid", "revoked", "valid-delegate", "revoked-delegate"]:
131+
task = create_ocsp_task(path.name, server_type)
132+
tasks.append(task)
133+
134+
# Create build variants.
135+
variants = []
136+
137+
# OCSP tests on rhel8 with rotating CPython versions.
138+
for version in ALL_VERSIONS:
139+
task_refs = [EvgTaskRef(name=".ocsp")]
140+
expansions = dict(VERSION=version, AUTH="noauth", SSL="ssl", TOPOLOGY="server")
141+
batchtime = BATCHTIME_WEEK * 2
142+
python = ALL_PYTHONS[len(variants) % len(ALL_PYTHONS)]
143+
host = "rhel8"
144+
if version in ["rapid", "latest"]:
145+
display_name = f"OCSP test RHEL8 {version}"
146+
else:
147+
display_name = f"OCSP test RHEL8 v{version}"
148+
variant = create_variant(
149+
[".ocsp"],
150+
display_name,
151+
python=python,
152+
batchtime=batchtime,
153+
host=host,
154+
expansions=expansions,
155+
)
156+
variants.append(variant)
157+
158+
# OCSP tests on Windows and MacOS with lowest CPython version.
159+
for host, version in product(["Win64", "macOS"], ["4.4", "8.0"]):
160+
# MongoDB servers do not staple OCSP responses and only support RSA.
161+
task_names = [".ocsp-rsa !.ocsp-staple"]
162+
expansions = dict(VERSION=version, AUTH="noauth", SSL="ssl", TOPOLOGY="server")
163+
batchtime = BATCHTIME_WEEK * 2
164+
display_name = f"OCSP test {host} v{version}"
165+
variant = create_variant(
166+
task_names,
167+
display_name,
168+
python=CPYTHONS[0],
169+
host=host,
170+
expansions=expansions,
171+
batchtime=batchtime,
172+
)
173+
variants.append(variant)
174+
175+
# Generate OCSP config.
176+
# project = EvgProject(tasks=None, buildvariants=variants)
177+
# write_output(project, "ocsp.yaml")
178+
# print(ShrubService.generate_yaml(project))

0 commit comments

Comments
 (0)