22
22
from datetime import datetime
23
23
from pathlib import Path
24
24
25
- from mongodl import main as mongodl
26
- from mongosh_dl import main as mongosh_dl
27
-
28
25
# Get global values.
29
26
HERE = Path (__file__ ).absolute ().parent
30
27
EVG_PATH = HERE .parent
31
28
DRIVERS_TOOLS = EVG_PATH .parent
32
29
LOGGER = logging .getLogger (__name__ )
33
30
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" )
34
42
35
43
36
44
def get_options ():
37
45
parser = argparse .ArgumentParser (
38
46
description = __doc__ , formatter_class = argparse .RawDescriptionHelpFormatter
39
47
)
40
- parser .add_argument ("command" , choices = ["run" , "start" , "stop" ])
48
+ parser .add_argument ("command" , choices = ["run" , "start" , "stop" , "clean" ])
41
49
parser .add_argument (
42
50
"--verbose" , "-v" , action = "store_true" , help = "Whether to log at the DEBUG level"
43
51
)
@@ -188,25 +196,58 @@ def traverse(root):
188
196
189
197
190
198
def normalize_path (path : Path | str ) -> str :
191
- if os . name != "nt " :
199
+ if PLATFORM != "win32 " :
192
200
return str (path )
193
201
path = Path (path ).as_posix ()
194
202
return re .sub ("/cygdrive/(.*?)(/)" , r"\1://" , path , count = 1 )
195
203
196
204
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
+
197
240
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
+
198
245
LOGGER .info ("Running orchestration..." )
246
+ clean_run (opts )
199
247
200
- # Clean up previous files.
201
- mdb_binaries = Path (opts .mongodb_binaries )
202
248
# NOTE: in general, we need to normalize paths to account for cygwin/Windows.
249
+ mdb_binaries = Path (opts .mongodb_binaries )
203
250
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"
210
251
211
252
# The evergreen directory to path.
212
253
os .environ ["PATH" ] = f"{ EVG_PATH } :{ os .environ ['PATH' ]} "
@@ -231,7 +272,7 @@ def run(opts):
231
272
LOGGER .info (f"Using existing mongod binaries dir: { opts .existing_binaries_dir } " )
232
273
shutil .copytree (opts .existing_binaries_dir , mdb_binaries )
233
274
234
- subprocess . run ([ f"{ mdb_binaries_str } /mongod" , " --version"], check = True )
275
+ run_command ( f"{ mdb_binaries_str } /mongod --version" )
235
276
236
277
# Download legacy shell.
237
278
if opts .install_legacy_shell :
@@ -247,22 +288,23 @@ def run(opts):
247
288
# We download crypt_shared to DRIVERS_TOOLS so that it is on a different
248
289
# path location than the other binaries, which is required for
249
290
# 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 + (
252
292
f" --version { version } --strip-path-components 1 --component crypt_shared"
253
293
)
254
294
LOGGER .info ("Downloading crypt_shared..." )
255
295
mongodl (shlex .split (args ))
256
296
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
+ )
263
305
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 (": " , "=" ))
266
308
267
309
# Download mongosh
268
310
args = f"--out { mdb_binaries_str } --strip-path-components 2 --retries 5"
@@ -350,9 +392,11 @@ def run(opts):
350
392
351
393
# Handle the cluster uri.
352
394
uri = resp .get ("mongodb_auth_uri" , resp ["mongodb_uri" ])
353
- expansion_yaml .touch ()
354
- expansion_yaml .write_text (expansion_yaml .read_text () + f'\n MONGODB_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'\n MONGODB_URI: "{ uri } "'
398
+ )
399
+ URI_TXT .write_text (uri )
356
400
LOGGER .info (f"Cluster URI: { uri } " )
357
401
358
402
# Write the results file.
@@ -380,6 +424,19 @@ def run(opts):
380
424
LOGGER .info ("Running orchestration... done." )
381
425
382
426
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
+
383
440
def start (opts ):
384
441
# Start mongo-orchestration
385
442
@@ -389,9 +446,7 @@ def start(opts):
389
446
stop ()
390
447
391
448
# 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 )
395
450
396
451
# Set up the mongo orchestration config.
397
452
os .makedirs (mo_home / "lib" , exist_ok = True )
@@ -404,7 +459,7 @@ def start(opts):
404
459
command = f"{ sys_executable } -m mongo_orchestration.server"
405
460
406
461
# Handle Windows-specific concerns.
407
- if os . name == "nt " :
462
+ if PLATFORM == "win32 " :
408
463
# Copy default client certificate.
409
464
src = DRIVERS_TOOLS / ".evergreen/x509gen/client.pem"
410
465
dst = mo_home / "lib/client.pem"
@@ -474,11 +529,7 @@ def start(opts):
474
529
def stop ():
475
530
LOGGER .info ("Stopping mongo-orchestration..." )
476
531
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" )
482
533
LOGGER .info ("Stopping mongo-orchestration... done." )
483
534
484
535
@@ -490,6 +541,9 @@ def main():
490
541
start (opts )
491
542
elif opts .command == "stop" :
492
543
stop ()
544
+ elif opts .command == "clean" :
545
+ clean_run (opts )
546
+ clean_start (opts )
493
547
494
548
495
549
if __name__ == "__main__" :
0 commit comments