def start_if_not_running(self): """ checks if mongo service is running :return: """ mode = self.data['MODE'] if mode == 'docker': from cloudmesh.mongo.MongoDocker import MongoDocker mongo = MongoDocker() result = mongo.ps() if 'cloudmesh-mongo' not in result: mongo.start() return if platform.lower() == 'linux': if not self.linux_process_is_running(): self.start() elif platform.lower() == 'darwin': if not self.mac_process_is_running(): self.start() elif platform.lower() == 'win32': # Replaced windows with win32 if not self.win_service_is_running(): self.start() else: Console.error(f"platform {platform} not found")
def start(self, security=True): """ start the MongoDB server """ mode = self.data['MODE'] if mode == "running": return if mode == 'docker': from cloudmesh.mongo.MongoDocker import MongoDocker mongo = MongoDocker() mongo.start(auth=security) # mongo.wait() # mongo.ps() return auth = "" if security: auth = "--auth" mongo_host = self.data['MONGO_HOST'] if platform.lower() == 'win32': try: # command = 'where mongo' # proc = subprocess.Popen(command, shell=True, # stdin=subprocess.PIPE, # stdout=subprocess.PIPE) # out, err = proc.communicate() # print ("MMM", command) # print ("O", out) # print ("E", err) # if out == b'': # Console.error("mongo command not found") # sys.exit() mongo_runner = f"mongod {auth} " \ f"--bind_ip {mongo_host}" \ f" --dbpath \"{self.mongo_path}\" --logpath \"{self.mongo_log}\mongod.log\"" #mongo_runner = f"\"{self.mongo_home}\\bin\mongod\" {auth} " \ # f"--bind_ip {mongo_host}" \ # f" --dbpath \"{self.mongo_path}\" --logpath \"{self.mongo_log}\mongod.log\"" print(mongo_runner) if not os.path.isfile(f'{self.mongo_path}\\invisible.vbs'): with open(f'{self.mongo_path}\\invisible.vbs', 'w') as f: f.write( 'CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False' ) if not os.path.isfile(f'{self.mongo_path}\\mongo_starter.bat'): with open(f'{self.mongo_path}\\mongo_starter.bat', 'w') as f: f.write(mongo_runner) script = f'wscript.exe "{self.mongo_path}\\invisible.vbs" "{self.mongo_path}\\mongo_starter.bat"' print(script) p = subprocess.Popen(script, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = "mongod child process should be started successfully." except Exception as e: result = "Mongo in windows could not be started: \n\n" + str(e) else: try: script = f"mongod {auth} --bind_ip {mongo_host}" \ f" --dbpath {self.mongo_path} --logpath {self.mongo_log}/mongod.log --fork" result = Script.run(script) except Exception as e: result = "Mongo could not be started." + str(e) if "successfully" in result: print(Console.ok(result)) else: print(Console.error(result))