class TestConfig: def setup(self): self.config = Config() def test_00_config(self): HEADING(myself()) pprint(self.config.dict()) print(self.config) print(type(self.config.data)) #pprint(config.credentials('local')) assert self.config is not None #assert 'cloud' in config.cloud def test_10_config_print(self): HEADING(myself()) print(self.config) assert True is True def test_20_config_subscriptable(self): HEADING(myself()) data = self.config["cloudmesh"]["data"]["mongo"] assert data is not None
class MongoDBController(object): def __init__(self): self.config = Config() pprint(self.config.dict()) # TODO: self.initial_mongo_config(False) def __str__(self): return yaml.dump(self.data, default_flow_style=False, indent=2) def update_auth(self): """ create admin acount in MongoDB """ # # TODO: BUG: should that not be done differently, e.g. from commandline or via ENV variables # # run mongodb self.run_mongodb() # set up auth information self.set_auth() # shut down mongodb self.shutdown_mongodb() # enable secutiry self.initial_mongo_config(True) print( "Enable the Secutiry. You will use your username and password to login the MongoDB" ) def initial_mongo_config(self, security=False): """ create the MongoDB config file :param security: enable the security """ # # TODO: BUG: we do not use mongoconfig, but pass everything from commandline, # everything shoudl be specified in cloudmesh4.yaml # default_config_file = dict( net=dict(bindIp=self.host, port=self.port), storage=dict(dbPath=os.path.join(self.mongo_db_path, 'database'), journal=dict(enabled=True)), systemLog=dict(destination='file', path=os.path.join(self.mongo_db_path, 'log', 'mongod.log'), logAppend=True)) if security: default_config_file.update( dict(security=dict(authorization='enabled'))) with open(os.path.join(self.mongo_db_path, 'mongod.conf'), "w") as output: try: yaml.dump(default_config_file, output, default_flow_style=False) except yaml.YAMLError as exc: print(exc) def run_mongodb(self): """ start the MongoDB server """ cmd = 'mongod --dbpath %s --config %s' % (os.path.join( self.mongo_db_path, 'database'), os.path.join(self.mongo_db_path, 'mongod.conf')) subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) print('MonogDB is running') # noinspection PyMethodMayBeStatic def shutdown_mongodb(self): """ shutdown the MongoDB server linux and darwin have different way to shutdown the server, the common way is kill """ cmd = 'pgrep mongo' pid = int( str(subprocess.check_output( cmd, shell=True).decode("utf-8")).split('\n')[0]) cmd = 'kill %s' % pid subprocess.check_output(cmd, shell=True) print('MonogDB is stopped') def set_auth(self): """ add admin acount into the MongoDB admin database """ client = MongoClient(self.host, self.port) client.admin.add_user(self.username, self.password, roles=[{ 'role': "userAdminAnyDatabase", 'db': "admin" }, "readWriteAnyDatabase"]) client.close() def dump(self, output_location): """ dump the entire MongoDB database into output location :param output_location: the location to save the backup """ # # TODO: BUG: expand user # cmd = 'mongodump --host %s --port %s --username %s --password %s --out %s' % ( self.host, self.port, self.username, self.password, output_location) subprocess.check_output(cmd, shell=True) def restore(self, data): """ restore the backup data generated by dump :param data: the backup data folder """ # # TODO: BUG: expand user # cmd = 'mongorestore --host %s --port %s --username %s --password %s %s' % ( self.host, self.port, self.username, self.password, data) subprocess.check_output(cmd, shell=True) def status(self): """ check the MongoDB status """ script = "ps -ax | grep mongo | fgrep -v grep" ps_output = Script(script) print(ps_output)
class MongoDBController(object): def __init__(self): self.config = Config() self.data = self.config.data["cloudmesh"]["data"]["mongo"] self.expanduser() pprint(self.config.dict()) def __str__(self): return yaml.dump(self.data, default_flow_style=False, indent=2) def expanduser(self): for key in self.data: if type(self.data[key]) == str: self.data[key] = os.path.expanduser(self.data[key]) pprint(self.data) def update_auth(self): """ create admin acount in MongoDB """ # # TODO: BUG: should that not be done differently, e.g. from commandline or via ENV variables # # run mongodb self.run_mongodb(True) # set up auth information self.set_auth() # shut down mongodb self.shutdown_mongodb() print( "Enable the Secutiry. You will use your username and password to login the MongoDB" ) def run_mongodb(self, security=False): """ start the MongoDB server """ if security: script = "mongod --dbpath {MONGO_PATH} --logpath {MONGO_LOG}/mongod.log --fork".format( **self.data) else: script = "mongod --auth --dbpath {MONGO_PATH} --logpath {MONGO_LOG}/mongod.log --fork".format( **self.data) print(script) run = Script(script) # noinspection PyMethodMayBeStatic def shutdown_mongodb(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`' run = Script(script) def set_auth(self): """ add admin acount into the MongoDB admin database """ script = """mongo --eval 'db.getSiblingDB("admin").createUser({user:"******",pwd:"%s",roles:[{role:"root",db:"admin"}]})'""" % ( self.data['MONGO_USERNAME'], self.data['MONGO_PASSWORD']) run = Script(script) def dump(self, filename): """ dump the entire MongoDB database into output location :param output_location: the location to save the backup """ # # TODO: BUG: expand user # script = "mongodump --authenticationDatabase admin --archive={MONGO_HOME}/".format( **self.data ) + filename + ".gz --gzip -u {MONGO_USERNAME} -p {MONGO_PASSWORD}".format( **self.data) run = Script(script) def restore(self, filename): """ restore the backup data generated by dump :param data: the backup data folder """ # # TODO: BUG: expand user # script = "mongorestore --authenticationDatabase admin -u {MONGO_USERNAME} -p " \ "{MONGO_PASSWORD} --gzip --archive={MONGO_HOME}/".format(**self.data)+filename+".gz" run = Script(script) def status(self): """ check the MongoDB status """ script = "ps -ax | grep mongo | fgrep -v grep" ps_output = Script(script) print(ps_output)