def full_test(params): admin_db.init_db() print('Starting Test; Container Name: %s' % params['dbname']) if container_util.container_exists(params['dbname']): print(' Duplicate container: KILLING') result = container_util.kill_con(params['dbname'], Config.accounts[dbtype]['admin'], Config.accounts[dbtype]['admin_pass'], params['username']) time.sleep(5) print(result) print(' removing old directories') volumes.cleanup_dirs(params['dbname']) print(' Create container') res = mongodb_util.create_mongodb(params) print(res) bck_path = Config.backup_voll + '/' + params['dbname'] print(' Test user account') if mongodb_util.auth_mongodb(params['dbuser'], params['dbuserpass'], params['port']): print(" - User exits!") else: print(" - User does not exits!") print(" - All Tests Complete! %s is up and running" % params['dbname'])
def mydb_setup(): """Create mydb_admin database if it does not exist. DB4SCI depends on mydb_admin database. """ if container_util.container_exists('mydb_admin'): print('Administrative DB is running.\nStarting DB4Sci') return print('Create Administrative DB') params = setup_data() dbtype = params['dbtype'] con_name = params['dbname'] result = postgres_util.create(params) # wait for container to startup print('Container Id: %s' % params['con']['Id']) print('Waiting for mydb_admin to start') time.sleep(20) badness = 0 status = False while (not status) and (badness < 6): badness += 1 status = postgres_util.auth_check(params['dbuser'], params['dbuserpass'], params['port']) print('mydb_admin setup status: %s count: %d' % (status, badness)) time.sleep(5) if not status: print('mydb_admin restart error. Could not setup db') return print('Setup mydb_admin tables') admin_db.init_db() inspect = container_util.inspect_con(params['con']['Id']) c_id = admin_db.add_container(inspect, params) state_info = admin_db.get_container_state(con_name) description = 'created %s by user %s' % (con_name, params['username']) admin_db.add_container_log(c_id, con_name, 'created', description)
def test_create(params): admin_db.init_db() dbtype = params['dbtype'] print('Starting Test; Container Name: %s' % params['dbname']) if container_util.container_exists(params['dbname']): print(' Duplicate container: KILLING') result = container_util.kill_con(params['dbname'], Config.accounts[dbtype]['admin'], Config.accounts[dbtype]['admin_pass'], params['username']) time.sleep(5) print(result) print(' removing old directories') volumes.cleanup_dirs(params['dbname']) print(' Create container') res = neo4j_util.create(params) print(res) params['port'] = params['port_bindings'][7687][1] print(' Bolt Port: %d' % params['port']) print(' Test user account') if neo4j_util.auth_check(params['dbuser'], params['dbuserpass'], params['port']): print(" - User exits!") else: print(" - User does not exits!") status = neo4j_util.create_account('keith', 'neoTester', params['port']) if status: print(" Created test account for Keith") else: print(" Badness: unable to create account for Keith") print(" - Create Neo4j Complete!")
def full_test(params): admin_db.init_db() con_name = params['dbname'] dbtype = params['dbtype'] print('Starting %s Test; Container Name: %s' % (dbtype, con_name)) if container_util.container_exists(con_name): print(' Duplicate container: KILLING') result = container_util.kill_con(con_name, Config.accounts[dbtype]['admin'], Config.accounts[dbtype]['admin_pass'], params['username']) time.sleep(5) print(result) print(' removing old directories') volumes.cleanup_dirs(con_name) print(' Create container') result = postgres_util.create(params) print(' Create result: %s' % result) port = params['port'] # # Admin DB checking # print(' Check Admin DB log for "create"') admin_db.display_container_log(limit=1) print(' Check Admin DB for State entry') info = admin_db.get_container_state(con_name) print(' Name: %s ' % info.name), print('State: %s ' % info.state), print('TS: %s ' % info.ts), print('CID: %d' % info.c_id) print(' Check Admin DB for Container Info') info = admin_db.display_container_info(con_name) print('Info: %s' % info) print(' Postgres Show All') postgres_util.showall(params) print("\n=========") print(" - Test Accounts\n") print("=========") admin_user = Config.accounts[dbtype]['admin'] admin_pass = Config.accounts[dbtype]['admin_pass'] test_user = Config.accounts['test_user']['admin'] test_pass = Config.accounts['test_user']['admin_pass'] for dbuser, dbuserpass in [[test_user, test_pass], ['svc_' + test_user, params['longpass']], [admin_user, admin_pass]]: auth = postgres_util.auth_check(dbuser, dbuserpass, port) if auth: print('User %s verified!' % dbuser) else: print('user account not valid: %s' % dbuser) print(" - Test Complete")
def create_mongodb(params): dbtype = params['dbtype'] con_name = params['dbname'] config_dat = Config.info[dbtype] volumes = config_dat['volumes'] for vol in volumes: if vol[0] == 'DBVOL': vol[0] = params['db_vol'] if vol[0] == 'BAKVOL': vol[0] = Config.backup_vol if container_util.container_exists(con_name): return "Container name %s already in use" % (con_name) port = container_util.get_max_port() params['port'] = port Ports = {config_dat['pub_ports'][0]: (Config.container_ip, port)} params['port_bindings'] = Ports env = {'DB_USER': params['dbuser']} # create container without Auth enabled container_util.make_dirs(con_name, volumes) (c_id, con) = container_util.create_con(params, env, config_dat['command']) print("DEBUG: noauth MongoDB created. Sleeping while it starts.\n") time.sleep(4) mongodb_create_account(params) print("DEBUG: stop and remove container") root = Config.accounts[params['dbtype']]['admin'] password = Config.accounts[params['dbtype']]['admin_pass'] result = container_util.kill_con(params['dbname'], root, password, params['username']) print("DEBUG: mongodb_util kill: %s" % result) admin_db.delete_container(c_id) # we don't duplicate container data print('Sleeping a bit before recreating mongo') time.sleep(4) print("DEBUG: Start with Auth") (c_id, con) = container_util.create_con(params, env, '--auth') print("DEBUG: MongoDB with --auth created: %s." % con['Id']) res = "Your MongoDB container has been created; %s\n\n" % con_name res += 'Mongo URI: "mongodb://%s:%s@%s:%s"' % ( params['dbuser'], params['dbuserpass'], Config.container_host, port) message = 'MongoDB created\n' message += res send_mail("DB4SCI: created MongoDB", message) return res
def create(params): """Create Postgres Container""" con_name = params['dbname'] config_dat = Config.info[params['dbtype']] volumes = config_dat['volumes'] for vol in volumes: if vol[0] == 'DBVOL': vol[0] = params['db_vol'] if container_util.container_exists(con_name): return "Container name %s already in use" % con_name # Ports is a dict of internal too external mappings if 'port' in params: port = params['port'] else: port = container_util.get_max_port() params['port'] = port Ports = {config_dat['pub_ports'][0]: (Config.container_host, port)} params['port_bindings'] = Ports params['longpass'] = generate_password() env = { 'POSTGRES_DB': params[ 'dbname'], # default database that is created when the image is first started 'POSTGRES_USER': params['dbuser'], 'POSTGRES_PASSWORD': params['dbuserpass'] } # create container container_util.make_dirs(con_name, volumes) (c_id, con) = container_util.create_con(params, env, config_dat['command']) params['con'] = con print("DEBUG: %s created. ID=%s\n" % (dbtype, con['Id'])) time.sleep(5) status = create_accounts(params) badness = 0 while status == 'connect error' and badness < 6: time.sleep(3) badness += 1 status = create_accounts(params) if status != 'ok': res = 'This is embarrassing. Your Postgres container; %s ' % params[ 'dbname'] res += 'has been created but I was unable to create your account.\n' res += 'This unfortunate incident will be reported to the DB4SCI admin staff.' send_mail("DB4SCI: Error creating account", res) return res # restart the container container_util.restart(con['Id']) res = "Your database server has been created. Use the following command " res += "to connect from the Linux command line.\n\n" res += "psql -h %s " % Config.container_host res += "-p %s -d %s " % (str(port), params['dbname']) res += "-U %s\n\n" % params['dbuser'] res += "If you would like to connect to the database without entering a " res += "password, create a .pgpass file in your home directory.\n" res += "Set permissions to 600. Format is hostname:port:database:" res += "username:password.\n" res += "Cut/paste this line and place in your /home/user/.pgpass file.\n\n" res += Config.container_host + ":" + str(port) + ":" + params['dbname'] res += ":" + params['dbuser'] + ":" + params['dbuserpass'] + "\n\n" res += "To use psql on the linux command line load the PostgreSQL" res += " module.\n" res += "module load PostgreSQL\n\n" message = 'DB4SCI created a new %s database called: %s\n' % ( dbtype, params['dbname']) message += 'Created by: %s <%s>\n' % (params['owner'], params['contact']) send_mail("DB4SCI: created %s" % dbtype, message) return res
def create_mariadb(params): """ Create MariaDB Docker instance :param params: dict :return: Help message for end user """ con_name = params['dbname'] if container_util.container_exists(con_name): return "Container name %s already in use" % con_name dbtype = params['dbtype'] config_dat = Config.info[params['dbtype']] volumes = config_dat['volumes'] print('DEBUG: DBVOL: %s' % params['db_vol']) for vol in volumes: if vol[0] == 'DBVOL': vol[0] = params['db_vol'] if vol[0] == 'BAKVOL': vol[0] = Config.backup_vol container_util.make_dirs(con_name, volumes) # Add key volume, encrypt.cnf file result = make_keys.create_mariadb_key(con_name, params) port = container_util.get_max_port() params['port'] = port pub_port = config_dat['pub_ports'][0] params['port_bindings'] = {pub_port: (Config.container_ip, str(port))} env = { 'MYSQL_ROOT_PASSWORD': Config.accounts[dbtype]['admin_pass'], 'MYSQL_USER': params['dbuser'], 'MYSQL_PASSWORD': params['dbuserpass'], } # create container container_util.make_dirs(con_name, volumes) cmd = config_dat['command'] + ' --ft_min_word_len=3' (c_id, con) = container_util.create_con(params, env, cmd) print("DEBUG: MariaDB created. ID=%s\n" % con['Id']) time.sleep(25) status = maria_create_account(params) badness = 0 while status == 'connect error' and badness < 6: time.sleep(10) badness += 1 status = maria_create_account(params) if status != 'ok': res = 'This is embarrassing. Your MariaDB container; %s ' % params[ 'dbname'] res += 'has been created but I was unable to create your account.\n' res += 'This unfortunate incident will be reported to the DB4SCI admin staff.' send_mail("DB4SCI: Error creating account", res) else: res = "Your MariaDB container has been created. " res += "Container name: %s\n\n" % con_name res += "Use mysql command line tools to access your new MariaDB.\n\n" res += "mysql --host %s --port %s --user %s --password\n\n" % ( Config.container_host, params['port'], params['dbuser']) res += 'Leave the password argument blank. You will be prompted to enter ' res += 'the password.\n\n' res += 'Encryption at Rest is enabled and TLS support is enabled.' res += 'Download TLS keys from the Documentation tab. Select "TLS ' res += 'for MariaDB."' msg = 'MariaDB created: %s\n' % params['dbname'] msg += 'Created by: %s <%s>\n' % (params['owner'], params['contact']) send_mail("DB4SCI: created MariaDB", msg) return res
def create(params): """ Create Neo4j Docker instance :param params: dict :return: Help message for end user """ con_name = params['dbname'] config_dat = Config.info[params['dbtype']] volumes = config_dat['volumes'] for vol in volumes: if vol[0] == 'DBVOL': vol[0] = params['db_vol'] if vol[0] == 'BAKVOL': vol[0] = Config.backup_vol if container_util.container_exists(con_name): return "Container name %s already in use" % con_name used_ports = container_util.get_ports() # find two consecutive ports that are not in usebolt_port for port in range(Config.base_port, max(used_ports) + 2): if port not in used_ports and ( (port + 1) not in used_ports): break Ports = {} for p in config_dat['pub_ports']: Ports[int(p)] = (Config.container_ip, port) port += 1 params['port_bindings'] = Ports bolt_port = params['port'] = Ports[7687][1] print("Ports: ", Ports) env = {'NEO4J_dbms_memory_pagecache_size': '4G', 'NEO4J_AUTH': 'neo4j/changeme', 'DB_USER': params['dbuser']} # create container container_util.make_dirs(con_name, volumes) command = config_dat['command'] (c_id, con) = container_util.create_con(params, env, args=command) print('waiting for container to startup...') time.sleep(5) status = reset_neo4j_password(params['port']) if status: stat2 = create_account(params['dbuser'], params['dbuserpass'], params['port']) badness = 0 while status is not True and badness < 6: print('create_account failed: %d' % badness) time.sleep(3) status = reset_neo4j_password(params['port']) if status: stat2 = create_account(params['dbuser'], params['dbuserpass'], params['port']) badness += 1 if status is not True: print('DEBUG very bad') return "Failed: neo4j unable to create accounts" https_port = Ports[7473][1] res = "Your neo4j container %s " % con_name res += "has been created.\n\n" res += "neo4j HTTPS interface: %d\n" % https_port res += "neo4j Bolt interface: %d\n" % bolt_port res += '\n' res += 'Web access: https://%s:%d' % (Config.container_host, https_port) res += '\n\n' res += 'Note: Web interface will display the default Bolt port of 7687. ' res += 'Change the Bolt port number from 7687 to %s ' % bolt_port res += 'before loginging in.\n\n' res += 'bolt://%s:%d' % (Config.FQDN_host, bolt_port) msg = 'Neo4j created: %s\n' % params['dbname'] msg += 'Created by: %s <%s>\n' % (params['owner'], params['contact']) send_mail("DB4SCI: created neo4j", msg) return res