def list_users(db_path): info = serverinfo.ServerInfo(db_path) host = info['host'] port = info['port'] subprocess.run(f'''psql -d balsam -h {host} -p {port} -c \ "SELECT rolname FROM pg_roles;" ''', shell=True)
def list_connections(db_path): info = serverinfo.ServerInfo(db_path) host = info['host'] port = info['port'] subprocess.run(f'''psql -d balsam -h {host} -p {port} -c \ "SELECT pid,application_name,usename,state,substr(query, 1, 60)\ FROM pg_stat_activity WHERE datname = 'balsam';" \ ''', shell=True)
def drop_user(db_path, uname): info = serverinfo.ServerInfo(db_path) host = info['host'] port = info['port'] subprocess.run(f'psql -d balsam -h {host} -p {port} -c \ "REVOKE ALL privileges on all tables in schema public FROM {uname};\ REVOKE ALL privileges on all sequences in schema public FROM {uname};\ REVOKE ALL privileges on database balsam FROM {uname};\ DROP ROLE {uname};"', shell=True)
def add_user(db_path, uname): info = serverinfo.ServerInfo(db_path) host = info['host'] port = info['port'] subprocess.run(f'''psql -d balsam -h {host} -p {port} -c \ "CREATE user {uname}; \ grant all privileges on database balsam to {uname}; \ GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO {uname}; \ GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO {uname}; "\ ''', shell=True)
def _start_main(db_path): info = serverinfo.ServerInfo(db_path) if not (info.get('host') or info.get('port')): launch_server(info) test_connection(info, raises=True) elif test_connection(info): print("Connected to already running Balsam DB server!") elif info._is_owner: print( f"Server at {info['host']}:{info['port']} isn't responsive; will try to kill and restart" ) kill_server(info) launch_server(info) test_connection(info, raises=True) else: print( f"Server at {info['host']}:{info['port']} isn't responsive; please ask the owner to restart it" )
def reset_main(db_path): start_main(db_path) info = serverinfo.ServerInfo(db_path) kill_server(info)