def _galaxy_db_exists(env): """ Check if galaxy database already exists. Return ``True`` if it does, ``False`` otherwise. Note that this method does a best-effort attempt at starting the DB server if one is not already running to do a thorough test. It shuts the server down upon completion, but only it if also started it. """ db_exists = False started = False c = _get_galaxy_db_configs(env) if exists(c['psql_data_dir']) and not _dir_is_empty(c['psql_data_dir']): sudo("chown --recursive {0}:{0} {1}".format(c['psql_user'], c['psql_data_dir'])) env.logger.debug("Galaxy database directory {0} already exists.".format(c['psql_data_dir'])) # Check if PostgreSQL is already running and try to start the DB if not if not _postgres_running(env): with settings(warn_only=True): env.logger.debug("Trying to start DB server in {0}".format(c['psql_data_dir'])) sudo("{0}".format(c['pg_start_cmd']), user=c['psql_user']) started = True # Check if galaxy DB already exists if 'galaxy' in sudo("{0} -P pager --list | grep {1} || true".format(c['psql_cmd'], c['galaxy_db_name']), user=c['psql_user']): env.logger.warning("Galaxy database {0} already exists in {1}! Not creating it."\ .format(c['galaxy_db_name'], c['psql_data_dir'])) db_exists = True if started: with settings(warn_only=True): sudo("{0}".format(c['pg_stop_cmd']), user=c['psql_user']) return db_exists
def _galaxy_db_exists(env): """ Check if galaxy database already exists. Return ``True`` if it does, ``False`` otherwise. Note that this method does a best-effort attempt at starting the DB server if one is not already running to do a thorough test. It shuts the server down upon completion, but only it if also started it. """ db_exists = False started = False c = _get_galaxy_db_configs(env) if exists(c['psql_data_dir']) and not _dir_is_empty(c['psql_data_dir']): sudo("chown --recursive {0}:{0} {1}".format(c['psql_user'], c['psql_data_dir'])) env.logger.debug("Galaxy database directory {0} already exists.".format(c['psql_data_dir'])) # Check if PostgreSQL is already running and try to start the DB if not if not _postgres_running(env): with settings(warn_only=True): env.logger.debug("Trying to start DB server in {0}".format(c['psql_data_dir'])) sudo("{0}".format(c['pg_start_cmd']), user=c['psql_user']) started = True # Check if galaxy DB already exists if 'galaxy' in sudo("{0} -P pager --list | grep {1} || true".format(c['psql_cmd'], c['galaxy_db_name']), user=c['psql_user']): env.logger.warning("Galaxy database {0} already exists in {1}! Not creating it." .format(c['galaxy_db_name'], c['psql_data_dir'])) db_exists = True if started: with settings(warn_only=True): sudo("{0}".format(c['pg_stop_cmd']), user=c['psql_user']) return db_exists
def _create_galaxy_db(env): """ Create a new PostgreSQL database for use by Galaxy """ c = _get_galaxy_db_configs(env) if not env.safe_exists(c['psql_data_dir']): env.safe_sudo("mkdir -p {0}".format(c['psql_data_dir'])) env.safe_sudo("chown --recursive {0}:{0} {1}".format( c['psql_user'], c['psql_data_dir'])) # Initialize a new database for Galaxy in ``psql_data_dir`` if _dir_is_empty(c['psql_data_dir']): env.safe_sudo("{0} -D {1}".format( os.path.join(c['psql_bin_dir'], 'initdb'), c['psql_data_dir']), user=c['psql_user']) # Set port for the database server env.safe_sed(c['psql_conf_file'], '#port = 5432', 'port = {0}'.format(c['psql_port']), use_sudo=True) env.safe_sudo("chown {0}:{0} {1}".format(c['psql_user'], c['psql_conf_file'])) # Start PostgreSQL server so a role for Galaxy user can be created if not _postgres_running(env): env.safe_sudo(c['pg_start_cmd'], user=c['psql_user']) started = True else: # Restart is required so port setting takes effect env.safe_sudo("{0} -D {1} -w -l {2} restart".format(c['pg_ctl_cmd']), c['psql_data_dir'], c['psql_log'], user=c['psql_user']) started = False # Create a role for env.galaxy_user env.safe_sudo('{0} -c"CREATE ROLE {1} LOGIN CREATEDB"'.format( c['psql_cmd'], env.galaxy_user), user=c['psql_user']) # Create a Galaxy database env.safe_sudo('{0} -p {1} {2}'.format( os.path.join(c['psql_bin_dir'], 'createdb'), c['psql_port'], c['galaxy_db_name']), user=env.galaxy_user) # Create a role for 'galaxyftp' user env.safe_sudo( '{0} -c"CREATE ROLE galaxyftp LOGIN PASSWORD \'{1}\'"'.format( c['psql_cmd'], c['galaxy_ftp_user_pwd']), user=c['psql_user']) if started: with settings(warn_only=True): env.safe_sudo("{0}".format(c['pg_stop_cmd']), user=c['psql_user']) exp = "export PATH={0}:$PATH".format(c['psql_bin_dir']) _add_to_profiles(exp)
def _create_galaxy_db(env): """ Create a new PostgreSQL database for use by Galaxy """ c = _get_galaxy_db_configs(env) if not exists(c["psql_data_dir"]): sudo("mkdir -p {0}".format(c["psql_data_dir"])) sudo("chown --recursive {0}:{0} {1}".format(c["psql_user"], c["psql_data_dir"])) # Initialize a new database for Galaxy in ``psql_data_dir`` if _dir_is_empty(c["psql_data_dir"]): sudo("{0} -D {1}".format(os.path.join(c["psql_bin_dir"], "initdb"), c["psql_data_dir"]), user=c["psql_user"]) # Set port for the database server sed(c["psql_conf_file"], "#port = 5432", "port = {0}".format(c["psql_port"]), use_sudo=True) sudo("chown {0}:{0} {1}".format(c["psql_user"], c["psql_conf_file"])) # Start PostgreSQL server so a role for Galaxy user can be created if not _postgres_running(env): sudo(c["pg_start_cmd"], user=c["psql_user"]) started = True else: # Restart is required so port setting takes effect sudo( "{0} -D {1} -w -l {2} restart".format(c["pg_ctl_cmd"]), c["psql_data_dir"], c["psql_log"], user=c["psql_user"], ) started = False # Create a role for env.galaxy_user sudo('{0} -c"CREATE ROLE {1} LOGIN CREATEDB"'.format(c["psql_cmd"], env.galaxy_user), user=c["psql_user"]) # Create a Galaxy database sudo( "{0} -p {1} {2}".format(os.path.join(c["psql_bin_dir"], "createdb"), c["psql_port"], c["galaxy_db_name"]), user=env.galaxy_user, ) # Create a role for 'galaxyftp' user sudo( "{0} -c\"CREATE ROLE galaxyftp LOGIN PASSWORD '{1}'\"".format(c["psql_cmd"], c["galaxy_ftp_user_pwd"]), user=c["psql_user"], ) if started: with settings(warn_only=True): sudo("{0}".format(c["pg_stop_cmd"]), user=c["psql_user"]) exp = "export PATH={0}:$PATH".format(c["psql_bin_dir"]) _add_to_profiles(exp)