Skip to content

Commit 15dda25

Browse files
authored
DRIVERS-3119 Enhanced cleanup and file permissions handling (#615)
1 parent ce4ed83 commit 15dda25

File tree

7 files changed

+113
-43
lines changed

7 files changed

+113
-43
lines changed

.evergreen/auth_aws/teardown.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pushd $SCRIPT_DIR
1010
# If we've gotten credentials, ensure the instance profile is set.
1111
if [ -f secrets-export.sh ]; then
1212
. ./activate-authawsvenv.sh
13+
source secrets-export.sh
1314
python ./lib/aws_assign_instance_profile.py
1415
fi
1516

.evergreen/clean.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
set -eu
3+
4+
SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
5+
. $SCRIPT_DIR/handle-paths.sh
6+
7+
python3 $SCRIPT_DIR/orchestration/drivers_orchestration.py clean
8+
9+
pushd $DRIVERS_TOOLS > /dev/null
10+
find . -type f -name '*.log' -exec rm {} \;
11+
rm -rf "$${TMPDIR:-$${TEMP:-$${TMP:-/tmp}}}"/mongo*
12+
find . -type f -name '*.env' -exec rm {} \;
13+
find . -type f -name '*results.json' -exec rm {} \;
14+
15+
popd > /dev/null

.evergreen/mongodl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ def _expand_tgz(
10011001
strip_components,
10021002
mem.isdir(),
10031003
lambda: cast("IO[bytes]", tf.extractfile(mem)), # noqa: B023
1004-
mem.mode,
1004+
mem.mode | 0o222, # make sure file is writable
10051005
test=test,
10061006
)
10071007
return n_extracted
@@ -1021,7 +1021,7 @@ def _expand_zip(
10211021
strip_components,
10221022
item.filename.endswith("/"), ## Equivalent to: item.is_dir(),
10231023
lambda: zf.open(item, "r"), # noqa: B023
1024-
0o655,
1024+
0o777,
10251025
test=test,
10261026
)
10271027
return n_extracted

.evergreen/orchestration/drivers_orchestration.py

Lines changed: 90 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,30 @@
2222
from datetime import datetime
2323
from pathlib import Path
2424

25-
from mongodl import main as mongodl
26-
from mongosh_dl import main as mongosh_dl
27-
2825
# Get global values.
2926
HERE = Path(__file__).absolute().parent
3027
EVG_PATH = HERE.parent
3128
DRIVERS_TOOLS = EVG_PATH.parent
3229
LOGGER = logging.getLogger(__name__)
3330
logging.basicConfig(level=logging.INFO, format="%(levelname)-8s %(message)s")
31+
PLATFORM = sys.platform.lower()
32+
CRYPT_NAME_MAP = {
33+
"win32": "mongo_crypt_v1.dll",
34+
"darwin": "mongo_crypt_v1.dylib",
35+
"linux": "mongo_crypt_v1.so",
36+
}
37+
38+
# Top level files
39+
URI_TXT = Path("uri.txt")
40+
MO_EXPANSION_SH = Path("mo-expansion.sh")
41+
MO_EXPANSION_YML = Path("mo-expansion.yml")
3442

3543

3644
def get_options():
3745
parser = argparse.ArgumentParser(
3846
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
3947
)
40-
parser.add_argument("command", choices=["run", "start", "stop"])
48+
parser.add_argument("command", choices=["run", "start", "stop", "clean"])
4149
parser.add_argument(
4250
"--verbose", "-v", action="store_true", help="Whether to log at the DEBUG level"
4351
)
@@ -188,25 +196,58 @@ def traverse(root):
188196

189197

190198
def normalize_path(path: Path | str) -> str:
191-
if os.name != "nt":
199+
if PLATFORM != "win32":
192200
return str(path)
193201
path = Path(path).as_posix()
194202
return re.sub("/cygdrive/(.*?)(/)", r"\1://", path, count=1)
195203

196204

205+
def run_command(cmd: str, **kwargs):
206+
LOGGER.debug(f"Running command {cmd}...")
207+
try:
208+
proc = subprocess.run(
209+
shlex.split(cmd),
210+
check=True,
211+
encoding="utf-8",
212+
stderr=subprocess.STDOUT,
213+
stdout=subprocess.PIPE,
214+
**kwargs,
215+
)
216+
LOGGER.info(proc.stdout)
217+
except subprocess.CalledProcessError as e:
218+
LOGGER.error(e.output)
219+
LOGGER.error(str(e))
220+
sys.exit(e.returncode)
221+
LOGGER.debug(f"Running command {cmd}... done.")
222+
223+
224+
def clean_run(opts):
225+
mdb_binaries = Path(opts.mongodb_binaries)
226+
mdb_binaries_str = normalize_path(mdb_binaries)
227+
shutil.rmtree(mdb_binaries_str, ignore_errors=True)
228+
229+
mongodb_dir = DRIVERS_TOOLS / "mongodb"
230+
if mongodb_dir.exists():
231+
shutil.rmtree(normalize_path(mongodb_dir), ignore_errors=True)
232+
233+
for path in [URI_TXT, MO_EXPANSION_SH, MO_EXPANSION_YML]:
234+
path.unlink(missing_ok=True)
235+
236+
crypt_path = DRIVERS_TOOLS / CRYPT_NAME_MAP[PLATFORM]
237+
crypt_path.unlink(missing_ok=True)
238+
239+
197240
def run(opts):
241+
# Deferred import so we can run as a script without the cli installed.
242+
from mongodl import main as mongodl
243+
from mongosh_dl import main as mongosh_dl
244+
198245
LOGGER.info("Running orchestration...")
246+
clean_run(opts)
199247

200-
# Clean up previous files.
201-
mdb_binaries = Path(opts.mongodb_binaries)
202248
# NOTE: in general, we need to normalize paths to account for cygwin/Windows.
249+
mdb_binaries = Path(opts.mongodb_binaries)
203250
mdb_binaries_str = normalize_path(mdb_binaries)
204-
shutil.rmtree(mdb_binaries, ignore_errors=True)
205-
expansion_yaml = Path("mo-expansion.yml")
206-
expansion_yaml.unlink(missing_ok=True)
207-
expansion_sh = Path("mo-expansion.sh")
208-
expansion_sh.unlink(missing_ok=True)
209-
uri_txt = DRIVERS_TOOLS / "uri.txt"
210251

211252
# The evergreen directory to path.
212253
os.environ["PATH"] = f"{EVG_PATH}:{os.environ['PATH']}"
@@ -231,7 +272,7 @@ def run(opts):
231272
LOGGER.info(f"Using existing mongod binaries dir: {opts.existing_binaries_dir}")
232273
shutil.copytree(opts.existing_binaries_dir, mdb_binaries)
233274

234-
subprocess.run([f"{mdb_binaries_str}/mongod", "--version"], check=True)
275+
run_command(f"{mdb_binaries_str}/mongod --version")
235276

236277
# Download legacy shell.
237278
if opts.install_legacy_shell:
@@ -247,22 +288,23 @@ def run(opts):
247288
# We download crypt_shared to DRIVERS_TOOLS so that it is on a different
248289
# path location than the other binaries, which is required for
249290
# https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.md#via-bypassautoencryption
250-
args = default_args.replace(mdb_binaries_str, normalize_path(DRIVERS_TOOLS))
251-
args += (
291+
args = default_args + (
252292
f" --version {version} --strip-path-components 1 --component crypt_shared"
253293
)
254294
LOGGER.info("Downloading crypt_shared...")
255295
mongodl(shlex.split(args))
256296
LOGGER.info("Downloading crypt_shared... done.")
257-
crypt_shared_path = None
258-
expected = [f"mongo_crypt_v1.{ext}" for ext in ["dll", "so", "dylib"]]
259-
for fname in os.listdir(DRIVERS_TOOLS):
260-
if fname in expected:
261-
crypt_shared_path = DRIVERS_TOOLS / fname
262-
assert crypt_shared_path is not None
297+
crypt_shared_path = mdb_binaries / CRYPT_NAME_MAP[PLATFORM]
298+
if crypt_shared_path.exists():
299+
shutil.move(crypt_shared_path, DRIVERS_TOOLS)
300+
crypt_shared_path = DRIVERS_TOOLS / crypt_shared_path.name
301+
else:
302+
raise RuntimeError(
303+
f"Could not find expected crypt_shared_path: {crypt_shared_path}"
304+
)
263305
crypt_text = f'CRYPT_SHARED_LIB_PATH: "{normalize_path(crypt_shared_path)}"'
264-
expansion_yaml.write_text(crypt_text)
265-
expansion_sh.write_text(crypt_text.replace(": ", "="))
306+
MO_EXPANSION_YML.write_text(crypt_text)
307+
MO_EXPANSION_SH.write_text(crypt_text.replace(": ", "="))
266308

267309
# Download mongosh
268310
args = f"--out {mdb_binaries_str} --strip-path-components 2 --retries 5"
@@ -350,9 +392,11 @@ def run(opts):
350392

351393
# Handle the cluster uri.
352394
uri = resp.get("mongodb_auth_uri", resp["mongodb_uri"])
353-
expansion_yaml.touch()
354-
expansion_yaml.write_text(expansion_yaml.read_text() + f'\nMONGODB_URI: "{uri}"')
355-
uri_txt.write_text(uri)
395+
MO_EXPANSION_YML.touch()
396+
MO_EXPANSION_YML.write_text(
397+
MO_EXPANSION_YML.read_text() + f'\nMONGODB_URI: "{uri}"'
398+
)
399+
URI_TXT.write_text(uri)
356400
LOGGER.info(f"Cluster URI: {uri}")
357401

358402
# Write the results file.
@@ -380,6 +424,19 @@ def run(opts):
380424
LOGGER.info("Running orchestration... done.")
381425

382426

427+
def clean_start(opts):
428+
mo_home = Path(opts.mongo_orchestration_home)
429+
for fname in [
430+
"out.log",
431+
"server.log",
432+
"orchestration.config",
433+
"config.json",
434+
"server.pid",
435+
]:
436+
if (mo_home / fname).exists():
437+
(mo_home / fname).unlink()
438+
439+
383440
def start(opts):
384441
# Start mongo-orchestration
385442

@@ -389,9 +446,7 @@ def start(opts):
389446
stop()
390447

391448
# Clean up previous files.
392-
for fname in ["out.log", "server.log", "orchestration.config", "config.json"]:
393-
if (mo_home / fname).exists():
394-
(mo_home / fname).unlink()
449+
clean_start(opts)
395450

396451
# Set up the mongo orchestration config.
397452
os.makedirs(mo_home / "lib", exist_ok=True)
@@ -404,7 +459,7 @@ def start(opts):
404459
command = f"{sys_executable} -m mongo_orchestration.server"
405460

406461
# Handle Windows-specific concerns.
407-
if os.name == "nt":
462+
if PLATFORM == "win32":
408463
# Copy default client certificate.
409464
src = DRIVERS_TOOLS / ".evergreen/x509gen/client.pem"
410465
dst = mo_home / "lib/client.pem"
@@ -474,11 +529,7 @@ def start(opts):
474529
def stop():
475530
LOGGER.info("Stopping mongo-orchestration...")
476531
py_exe = normalize_path(sys.executable)
477-
args = f"{py_exe} -m mongo_orchestration.server stop"
478-
proc = subprocess.run(
479-
shlex.split(args), check=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE
480-
)
481-
LOGGER.debug(proc.stdout.decode("utf-8"))
532+
run_command(f"{py_exe} -m mongo_orchestration.server stop")
482533
LOGGER.info("Stopping mongo-orchestration... done.")
483534

484535

@@ -490,6 +541,9 @@ def main():
490541
start(opts)
491542
elif opts.command == "stop":
492543
stop()
544+
elif opts.command == "clean":
545+
clean_run(opts)
546+
clean_start(opts)
493547

494548

495549
if __name__ == "__main__":

.evergreen/tests/test-cli.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
88

99
pushd $SCRIPT_DIR/..
1010

11+
# Ensure we can run clean before the cli is installed.
12+
make clean
13+
1114
bash install-cli.sh .
1215
DOWNLOAD_DIR=mongodl_test
1316

@@ -32,7 +35,6 @@ fi
3235
export PATH="${DOWNLOAD_DIR}/bin:$PATH"
3336
if [ "${OS:-}" != "Windows_NT" ]; then
3437
./mongosh-dl --version 2.1.1 --out ${DOWNLOAD_DIR} --strip-path-components 1 --retries 5
35-
chmod +x ./mongodl_test/bin/mongosh
3638
./mongodl_test/bin/mongosh --version
3739
else
3840
./mongosh-dl --version 2.1.1 --out ${DOWNLOAD_DIR} --strip-path-components 1 --retries 5

.github/workflows/tests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ jobs:
6060
- id: test-mongodb
6161
name: "Test GitHub Action"
6262
run: |
63-
chmod +x mongodb/bin/mongosh
6463
URI=$(cat uri.txt)
6564
ARGS=""
6665
if [ ${{ matrix.ssl }} == "ssl" ]; then

Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ all:
55

66
clean:
77
@echo "Cleaning files..."
8-
rm -rf ./mongodb .env results.json mo-expansion*
9-
rm -rf "$${TMPDIR:-$${TEMP:-$${TMP:-/tmp}}}"/mongo*
8+
.evergreen/clean.sh
109

11-
run-server: clean
10+
run-server:
1211
@echo "Running server..."
1312
.evergreen/run-orchestration.sh
1413

0 commit comments

Comments
 (0)