def ubuntu(self): """ install MongoDB in Linux system (Ubuntu) """ # check if openssl and curl is installed chk_script = "openssl version && curl --version" Script.run(chk_script) script = f""" mkdir -p {self.mongo_path} mkdir -p {self.mongo_home} mkdir -p {self.mongo_log} wget -q -O /tmp/mongodb.tgz {self.mongo_code} tar -zxvf /tmp/mongodb.tgz -C {self.local}/mongo --strip 1 echo \"export PATH={self.mongo_home}/bin:$PATH\" >> ~/.bashrc """ if self.dryrun: print(script) else: installer = Script.run(script) Console.info("MongoDB installation successful!") print() Console.info("Activate it with \n\n" f"export PATH={self.mongo_home}/bin:$PATH\n\n" "We also added this to ~/.bashrc\n") print()
def start(self, security=True): """ start the MongoDB server """ auth = "" if security: auth = "--auth" if platform.lower() == 'win32': try: MONGO_COMMAND = "-scriptblock { " + "mongod {auth} --bind_ip {MONGO_HOST} --dbpath {MONGO_PATH} --logpath {MONGO_LOG}/mongod.log".format( **self.data, auth=auth) + " }" script = """ powershell -noexit start-job {MONGO_COMMAND} """.format(**self.data, MONGO_COMMAND=MONGO_COMMAND) Script.run(script) result = "child process started successfully. Program existing now" except Exception as e: result = "Mongo in windows could not be started." + str(e) else: try: script = "mongod {auth} --bind_ip {MONGO_HOST} --dbpath {MONGO_PATH} --logpath {MONGO_LOG}/mongod.log --fork".format( **self.data, auth=auth) result = Script.run(script) except Exception as e: result = "Mongo could not be started." + str(e) if "child process started successfully" in result: print(Console.ok(result)) else: print(Console.error(result))
def stop(self): """ shutdown the MongoDB server linux and darwin have different way to shutdown the server, the common way is kill """ mode = self.data['MODE'] result = "Result unkown" if mode == 'docker': from cloudmesh.mongo.MongoDocker import MongoDocker mongo = MongoDocker() mongo.kill() result = "Container is down" elif platform.lower() == 'win32': ''' MONGO = f"\"{self.mongo_home}\\bin\mongo\"" script = f'{MONGO} --eval "db.getSiblingDB(\'admin\').shutdownServer()"' p1 = subprocess.Popen(script, shell=True, stdout=subprocess.PIPE, stderr=STDOUT) MONGO_USERNAME = self.data['MONGO_USERNAME'] MONGO_PASSWORD = self.data['MONGO_PASSWORD'] shutdown_with_auth1 = f"""{MONGO} -u {MONGO_USERNAME} -p {MONGO_PASSWORD} --eval "db.getSiblingDB(\'admin\').shutdownServer()" """ # print(shutdown_with_auth1) # print(script) p2 = subprocess.Popen(shutdown_with_auth1, shell=True, stdout=subprocess.PIPE, stderr=STDOUT) shutdown_with_auth = f"""{MONGO} --eval "db.getSiblingDB(\'admin\').shutdownServer()" """ # print(shutdown_with_auth) # print(script) p3 = subprocess.Popen(shutdown_with_auth, shell=True, stdout=subprocess.PIPE, stderr=STDOUT) r1 = p1.stdout.read().decode('utf-8') r2 = p2.stdout.read().decode('utf-8') if 'server should be down...' in r1 or 'connect failed' in r2: result = 'server should be down...' else: result = 'server is already down...' ''' try: result = Shell.run("taskkill /IM mongod.exe /F") except Exception as e: result = str(e) else: try: pid = Script.run('pgrep mongo') script = f'kill -2 {pid}' result = Script.run(script) result = 'server should be down...' except subprocess.CalledProcessError: result = 'server is already down...' print(result)
def stop(self): """ shutdown the MongoDB server linux and darwin have different way to shutdown the server, the common way is kill """ mode = self.data['MODE'] if mode == 'docker': Console.error("* Docker is not yet supported") raise NotImplementedError # TODO: there could be more mongos running, be more specific if platform.lower() == 'win32': MONGO = f"\"{self.mongo_home}\\bin\mongo\"" script = f'{MONGO} --eval "db.getSiblingDB(\'admin\').shutdownServer()"' p1 = subprocess.Popen(script, shell=True, stdout=subprocess.PIPE, stderr=STDOUT) MONGO_USERNAME = self.data['MONGO_USERNAME'] MONGO_PASSWORD = self.data['MONGO_PASSWORD'] shutdown_with_auth1 = f"""{MONGO} -u {MONGO_USERNAME} -p {MONGO_PASSWORD} --eval "db.getSiblingDB(\'admin\').shutdownServer()" """ # print(shutdown_with_auth1) # print(script) p2 = subprocess.Popen(shutdown_with_auth1, shell=True, stdout=subprocess.PIPE, stderr=STDOUT) shutdown_with_auth = f"""{MONGO} --eval "db.getSiblingDB(\'admin\').shutdownServer()" """ # print(shutdown_with_auth) # print(script) p3 = subprocess.Popen(shutdown_with_auth, shell=True, stdout=subprocess.PIPE, stderr=STDOUT) r1 = p1.stdout.read().decode('utf-8') r2 = p2.stdout.read().decode('utf-8') if 'server should be down...' in r1 or 'connect failed' in r2: result = 'server should be down...' else: result = 'server is already down...' else: try: pid = Script.run('pgrep mongo') script = f'kill -2 {pid}' result = Script.run(script) result = 'server should be down...' except subprocess.CalledProcessError: result = 'server is already down...' print(result)
def stats(self): mode = self.data['MODE'] if mode == 'docker': Console.error("Stats: Docker is not yet supported") raise NotImplementedError script = """mongo --eval 'db.stats()'""".format(**self.data) result = Script.run(script).split("\n") output = {} for line in result: line = line.replace("Implicit session: session {", "") line = line.replace(") }", "") line = line.replace("MongoDB shell version", "shell_version: ") line = line.replace("MongoDB server version:", "server_version: ") line = line.replace("connecting to", "endpoint") line = line.replace("UUID(", "") line = line.strip() line = line.replace("}", "") line = line.replace("{", "") if ":" in line: line = line.replace('"', "") line = line.replace(',', "") attribute, value = line.split(":", 1) output[attribute] = str(value).strip() return output
def stats(self): script = """mongo --eval 'db.stats()'""".format(**self.data) result = Script.run(script).split("\n") output = {} for line in result: line = line.replace("Implicit session: session {", "") line = line.replace(") }", "") line = line.replace("MongoDB shell version", "shell_version: ") line = line.replace("MongoDB server version:", "server_version: ") line = line.replace("connecting to", "endpoint") line = line.replace("UUID(", "") line = line.strip() line = line.replace("}", "") line = line.replace("{", "") if ":" in line: line = line.replace('"', "") line = line.replace(',', "") attribute, value = line.split(":", 1) output[attribute] = str(value).strip() return output
def debian(self, sudo=True, version=10): """ Install MongoDB in Linux Debian (9,10) """ if sudo: sudo_command = "sudo" else: sudo_command = "" apt_cmd = "error" if version == 9: apt_cmd = "apt-get --yes install openssl libcurl3" elif version == 10: # UNTESTED apt_cmd = "apt-get --yes install openssl libcurl4" else: Console.error("Unsupported Linux Version") raise Exception("unsupported operating system") script = f"{sudo_command} " + f"{apt_cmd} " + f""" mkdir -p {self.mongo_path} mkdir -p {self.mongo_home} mkdir -p {self.mongo_log} wget -q -O /tmp/mongodb.tgz {self.mongo_code} tar -zxvf /tmp/mongodb.tgz -C {self.local}/mongo --strip 1 echo \"export PATH={self.mongo_home}/bin:$PATH\" >> ~/.bashrc """ installer = Script.run(script)
def dist(self): script = \ f""" python setup.py sdist bdist_wheel twine check dist/* """ installer = Script.live(script)
def start(self, security=True): """ start the MongoDB server """ mode = self.data['MODE'] if mode == 'docker': Console.error("* Docker is not yet supported") raise NotImplementedError 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"\"{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))
def darwin(self, brew=False): """ install MongoDB in Darwin system (Mac) """ if brew: print("mongo installer via brew") if not self.dryrun: Brew.install("mongodb") path = Shell.which("mongod") SystemPath.add(f"{path}") else: script = f""" mkdir -p {self.mongo_path} mkdir -p {self.mongo_home} mkdir -p {self.mongo_log} curl -o /tmp/mongodb.tgz {self.mongo_code} tar -zxvf /tmp/mongodb.tgz -C {self.local}/mongo --strip 1 """ print(script) if self.dryrun: print(script) else: installer = Script.run(script) SystemPath.add(f"{self.mongo_home}/bin".format(**self.data)) # THIS IS BROKEN AS ITS A SUPBROCESS? '. ~/.bashrc' Console.info("MongoDB installation successful!")
def debian(self, sudo=True, version=10): """ Install MongoDB in Linux Debian (9,10) """ if sudo: sudo_command = "sudo" else: sudo_command = "" apt_cmd = "error" if version == 9: apt_cmd = "apt-get --yes install openssl libcurl3" elif version == 10: # UNTESTED apt_cmd = "apt-get --yes install openssl libcurl4" else: Console.error("Unsupported Linux Version") raise Exception("unsupported operating system") script = f"{sudo_command} " + f"{apt_cmd} " + """ mkdir -p {MONGO_PATH} mkdir -p {MONGO_HOME} mkdir -p {MONGO_LOG} wget -q -O /tmp/mongodb.tgz {MONGO_CODE} tar -zxvf /tmp/mongodb.tgz -C {LOCAL}/mongo --strip 1 echo \"export PATH={MONGO_HOME}/bin:$PATH\" >> ~/.bashrc """.format(**self.data) installer = Script.run(script)
def set_auth(self): """ add admin account into the MongoDB admin database """ if platform.lower( ) == 'win32': # don't remove this otherwise init won't work in windows, eval should start with double quote in windows self.data['MONGO'] = f"{self.mongo_home}\\bin\mongo" script = """ "{MONGO}" --eval "db.getSiblingDB('admin').createUser({{ user:'******',pwd:'{MONGO_PASSWORD}',roles:[{{role:'root',db:'admin'}}]}}) ; db.shutdownServer()" """.format( **self.data) # print(script) try: # result = Shell3.run2(script) p = subprocess.Popen(script, shell=True, stdout=subprocess.PIPE, stderr=STDOUT) result = p.stdout.read().decode('utf-8') except Exception as e: print(e) return else: script = """mongo --eval 'db.getSiblingDB("admin").createUser({{user:"******",pwd:"{MONGO_PASSWORD}",roles:[{{role:"root",db:"admin"}}]}})'""".format( **self.data) result = Script.run(script) if "Successfully added user" in result: Console.ok("Administrative user created.") elif "already exists" in result: Console.error('admin user already exists.') else: Console.error("Problem creating the administrative user. Check " "the yaml file and make sure the password and " "username are not TBD.")
def status(self): """ check the MongoDB status returns a json object with status: and pid: command """ if platform.lower() == 'win32': script = """ tasklist /FO LIST /FI "IMAGENAME eq mongod.exe" """ output = Script.run(script) result = {} for row in output.split('\n'): if ': ' in row: key, value = row.split(': ') result[key.strip()] = value.strip() if result is None: state = dotdict({ "status": "error", "message": "No mongod running", "output": None }) else: state = dotdict({ "status": "ok", "message": "running", "output": None }) process = { "pid": str(result['PID']), "command": result['Image Name'] } output = {} output[str()] = process state["output"] = output else: result = find_process("mongod") if result is None: state = dotdict({ "status": "error", "message": "No mongod running", "output": None }) output = None else: state = dotdict({ "status": "ok", "message": "running", "output": None }) output = {} for p in result: p = dotdict(p) process = {"pid": str(p.pid), "command": p.command} output[str(p.pid)] = process state["output"] = output return state
def stop(self): """ shutdown the MongoDB server linux and darwin have different way to shutdown the server, the common way is kill """ script = 'kill -2 `pgrep mongo`' result = Script.run(script) print(result)
def stop(self): """ shutdown the MongoDB server linux and darwin have different way to shutdown the server, the common way is kill """ # TODO: there could be more mongos running, be more specific script = 'kill -2 `pgrep mongo`' result = Script.run(script) print(result)
def patch(self, package): script = f""" bump2version --allow-dirty patch python setup.py sdist bdist_wheel twine check dist/* twine upload --repository testpypi dist/* sleep 10 pip install --index-url https://test.pypi.org/simple/ cloudmesh-{package} -U """ installer = Script.live(script)
def set_auth(self): """ add admin acount into the MongoDB admin database """ script = """mongo --eval 'db.getSiblingDB("admin").createUser({{user:"******",pwd:"{MONGO_PASSWORD}",roles:[{{role:"root",db:"admin"}}]}})'""".format( **self.data) result = Script.run(script) print(result)
def release(self): with open("VERSION") as f: version = f.read().strip() script = f'''' git tag "v{version}" git push origin main --tags python setup.py sdist bdist_wheel twine check dist/* twine upload --repository pypi dist/* sleep 10 pip install -U cloudmesh-common ''' installer = Script.live(script)
def docker(self): username = self.data["MONGO_USERNAME"] password = self.data["MONGO_PASSWORD"] port = self.data["MONGO_PORT"] host = self.data["MONGO_HOST"] script = \ f"docker run -d -p {host}:{port}:{port}" \ f" --name cloudmesh-mongo" \ f" -e MONGO_INITDB_ROOT_USERNAME={username}" \ f" -e MONGO_INITDB_ROOT_PASSWORD={password}" \ f" mongo" installer = Script.run(script) print(installer)
def restore(self, filename): """ :param filename: The filename restore the backup data generated by dump """ # # TODO: BUG: expand user # script = "mongorestore --authenticationDatabase admin -u {MONGO_USERNAME} -p " \ "{MONGO_PASSWORD} --gzip --archive={MONGO_HOME}/{filename}.gz".format(**self.data, filename=filename) result = Script.run(script) print(result)
def linux(self): # TODO UNTESTED """ install MongoDB in Linux system (Ubuntu) """ script = """ sudo apt-get --yes install libcurl4 openssl mkdir -p {MONGO_PATH} mkdir -p {MONGO_HOME} mkdir -p {MONGO_LOG} wget -P /tmp/mongodb.tgz {MONGO_CODE} tar -zxvf /tmp/mongodb.tgz -C {LOCAL}/mongo --strip 1 """.format(**self.data) installer = Script.run(script) SystemPath.add("{MONGO_HOME}/bin".format(**self.data))
def linux(self): # TODO UNTESTED """ install MongoDB in Linux system (Ubuntu) """ script = """ sudo apt-get --yes install libcurl4 openssl mkdir -p {MONGO_PATH} mkdir -p {MONGO_HOME} mkdir -p {MONGO_LOG} wget -O /tmp/mongodb.tgz {MONGO_CODE} tar -zxvf /tmp/mongodb.tgz -C {LOCAL}/mongo --strip 1 echo \"export PATH={MONGO_HOME}/bin:$PATH\" >> ~/.bashrc """.format(**self.data) installer = Script.run(script)
def windows(self): """ install MongoDB in windows """ # Added below code to change unix format to windows format for directory creation # self.data["MONGO_HOME"] = self.data["MONGO_HOME"].replace("/", "\\") # self.data["MONGO_PATH"] = self.data["MONGO_PATH"].replace("/", "\\") # self.data["MONGO_LOG"] = self.data["MONGO_LOG"].replace("/", "\\") # def is_admin(): # try: # if platform == 'win32': # return ctypes.windll.shell32.IsUserAnAdmin() # except: # return False # noinspection PyPep8 try: os.mkdir(self.mongo_home) except FileExistsError: Console.info(f"Folder {self.mongo_home} already exists") except FileNotFoundError: # means you don't have enough privilege Console.error("Permission denied, requesting admin access") import win32com.shell.shell as shell script = f'mkdir "{self.mongo_home}"' shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c ' + script) try: os.mkdir(self.mongo_path) except FileExistsError: Console.info(f"Folder {self.mongo_path} already exists") try: os.mkdir(self.mongo_log) except FileExistsError: Console.info(f"Folder {self.mongo_log} already exists") script = f"""msiexec.exe /l*v {self.mongo_log}\\mdbinstall.log /qb /i {self.mongo_code} INSTALLLOCATION="{self.mongo_home}" ADDLOCAL="all" """ print(script) if self.dryrun: print(script) else: print(script) installer = Script.run(script) Console.info("MongoDB installation successful!")
def set_auth(self): """ add admin acount into the MongoDB admin database """ if platform.lower() == 'win32': script = """ mongo --eval "db.getSiblingDB('admin').createUser({{user:'******',pwd:'{MONGO_PASSWORD}',roles:[{{role:'root',db:'admin'}}]}})" """.format(**self.data) print(script) else: script = """mongo --eval 'db.getSiblingDB("admin").createUser({{user:"******",pwd:"{MONGO_PASSWORD}",roles:[{{role:"root",db:"admin"}}]}})'""".format( **self.data) result = Script.run(script) print(result)
def clean(self): script = f""" rm -rf *.zip rm -rf *.egg-info rm -rf *.eggs rm -rf docs/build rm -rf build rm -rf dist find . -name '__pycache__' -delete find . -name '*.pyc' -delete rm -rf .tox rm -f *.whl """ installer = Script.run(script) print(installer)
def windows(self, brew=False): """ install MongoDB in windows """ #Added below code to change unix format to windows format for directory creation self.data["MONGO_HOME"] = self.data["MONGO_HOME"].replace("/", "\\") self.data["MONGO_PATH"] = self.data["MONGO_PATH"].replace("/", "\\") self.data["MONGO_LOG"] = self.data["MONGO_LOG"].replace("/", "\\") script = """ mkdir {MONGO_PATH} mkdir {MONGO_HOME} mkdir {MONGO_LOG} msiexec.exe /l*v {MONGO_LOG}\mdbinstall.log /qb /i {MONGO_CODE} INSTALLLOCATION={MONGO_PATH} ADDLOCAL="all" """.format(**self.data) installer = Script.run(script)
def dump(self, filename): """ :param filename: The filename dump the entire MongoDB database into output location """ # # TODO: BUG: expand user # script = "mongodump --authenticationDatabase admin --archive={MONGO_HOME}/{filename}.gz --gzip -u {MONGO_USERNAME} -p {MONGO_PASSWORD}".format( **self.data, filename=filename) result = Script.run(script) print(result)
def start(self, security=True): """ start the MongoDB server """ auth = "" if security: auth = "--auth" try: script = "mongod {auth} --bind_ip {MONGO_HOST} --dbpath {MONGO_PATH} --logpath {MONGO_LOG}/mongod.log --fork".format( **self.data, auth=auth) result = Script.run(script) except Exception as e: result = "Mongo could not be started." + str(e) if "child process started successfully" in result: print(Console.ok(result)) else: print(Console.error(result))
def run(self, script, verbose=True, terminate=False): if verbose: Console.msg(script) if self.dryrun: return "dryrun" else: try: installer = Script.run(script, debug=False) if verbose: print(installer) return installer except Exception as e: if verbose: Console.error("Script returned with error") print(e) if terminate: sys.exit() return "error"
def darwin(self, brew=False): """ install MongoDB in Darwin system (Mac) """ if brew: print("mongo installer via brew") Brew.install("mongodb") path = Shell.which("mongod") SystemPath.add("{path}".format(path=path)) else: script = """ mkdir -p {MONGO_PATH} mkdir -p {MONGO_HOME} mkdir -p {MONGO_LOG} curl -o /tmp/mongodb.tgz {MONGO_CODE} tar -zxvf /tmp/mongodb.tgz -C {LOCAL}/mongo --strip 1 """.format(**self.data) installer = Script.run(script) SystemPath.add("{MONGO_HOME}/bin".format(**self.data))