示例#1
0
    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 set_auth(self):
        """
        add admin account into the MongoDB admin database
        """

        mode = self.data['MODE']

        if mode == 'running':
            return

        if mode == 'docker':
            from cloudmesh.mongo.MongoDocker import MongoDocker
            mongo = MongoDocker()
            mongo.wait()
            mongo.create_admin()
            mongo.kill()
            return

        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.")
示例#3
0
    def install(self, sudo=True):
        """
        check where the MongoDB is installed in mongo location.
        if MongoDB is not installed, python help install it
        """
        if self.dryrun:
            print(self.mongo_path)
        # pprint(self.data)

        mode = self.data['MODE']

        Console.msg(f"Installing mongo in  mode: {mode}")

        if mode == 'docker':
            Console.ok(
                "Installing mongoDB in a docker container cloudmesh-mongo")

            from cloudmesh.mongo.MongoDocker import MongoDocker
            mongo = MongoDocker()
            mongo.kill()
            mongo.install(clean=True, pull=True)

            Console.ok("Shutting mongoDB down")
            Console.msg("")
            Console.ok("Start the mongodb service with")
            Console.msg("")
            Console.msg("   cms admin mongo create")
            Console.msg("   cms admin mongo start")
            Console.msg("")

            return ""

        if not self.data["MONGO_AUTOINSTALL"]:
            Console.error("Mongo auto install is off")
            print("You can set it with")
            print()
            Console.ok(
                "    cms config set cloudmesh.data.mongo.MONGO_AUTOINSTALL=True"
            )
            print()
            if self.machine == 'darwin':
                print("To install it with brew you need to set also")
                print()
                Console.ok(
                    "    cms config set cloudmesh.data.mongo.MONGO_BREWINSTALL=True"
                )
                print()

            return ""

        #
        # the path test may be wrong as we need to test for mongo and mongod
        #
        # print ('OOO', os.path.isdir(path), self.data["MONGO_AUTOINSTALL"] )
        if self.force or (not os.path.isdir(self.mongo_home)
                          and self.data["MONGO_AUTOINSTALL"]):
            print(f"MongoDB is not installed in {self.mongo_home}")
            #
            # ask if you like to install and give info where it is being installed
            #
            # use cloudmesh yes no question see cloudmesh 3
            #
            # print(f"Auto-install the MongoDB into {mongo_path}")

            self.local = self.data["LOCAL"]
            if self.machine == 'linux':
                self.linux()
            elif self.machine == 'darwin':
                self.darwin()
            elif self.machine == 'win32':  # Replaced windows with win32
                self.windows()
            else:
                print("platform not found", platform)
        elif os.path.isdir(self.mongo_home):
            Console.error(f"Folder {self.mongo_home} already exists")