2222from datetime import datetime
2323from 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.
2926HERE = Path (__file__ ).absolute ().parent
3027EVG_PATH = HERE .parent
3128DRIVERS_TOOLS = EVG_PATH .parent
3229LOGGER = logging .getLogger (__name__ )
3330logging .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
3644def 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
190198def 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+
197240def 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'\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 )
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+
383440def 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):
474529def 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
495549if __name__ == "__main__" :
0 commit comments