Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Smeds committed Feb 12, 2025
1 parent a8a5e7d commit 9160232
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
32 changes: 18 additions & 14 deletions planemo/database/postgres_singularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@
DEFAULT_POSTGRES_DATABASE_NAME = "galaxy"
DEFAULT_POSTGRES_USER = "galaxy"
DEFAULT_POSTGRES_PASSWORD = "mysecretpassword"
DEFAULT_POSTGRES_PORT_EXPOSE = 15432
DEFAULT_POSTGRES_PORT_EXPOSE = 5432
DEFAULT_DOCKERIMAGE = "postgres:14.2-alpine3.15"
DEFAULT_SIF_NAME = "postgres_14_2-alpine3_15.sif"


def singularity_fetch_image():
fetch_command = singularity_util.pull_singularity_command(DEFAULT_DOCKERIMAGE, "./SINGULARITY_CACHE")
execute(fetch_command)
DEFAULT_CONNECTION_STRING = f"postgresql://{DEFAULT_POSTGRES_USER}:{DEFAULT_POSTGRES_PASSWORD}@localhost:{DEFAULT_POSTGRES_PORT_EXPOSE}/{DEFAULT_POSTGRES_DATABASE_NAME}"


def start_postgres_singularity(
singularity_path, container_instance_name, database_location, databasename=DEFAULT_POSTGRES_DATABASE_NAME, user=DEFAULT_POSTGRES_USER, password=DEFAULT_POSTGRES_PASSWORD, port=DEFAULT_POSTGRES_PORT_EXPOSE, **kwds
singularity_path, container_instance_name, database_location, databasename=DEFAULT_POSTGRES_DATABASE_NAME, user=DEFAULT_POSTGRES_USER, password=DEFAULT_POSTGRES_PASSWORD, **kwds
):
info(f"Postgres database stored at: {database_location}")
pgdata_path = os.path.join(database_location, 'pgdata')
Expand All @@ -44,8 +42,11 @@ def start_postgres_singularity(
if not os.path.exists(pgrun_path):
os.makedirs(pgrun_path)

if not os.path.exists(os.path.join(pgdata_path, 'PG_VERSION')):
version_file = os.path.join(pgdata_path, 'PG_VERSION')
if not os.path.exists(version_file):
# Run container for a short while to initialize the database
# The database will not be initilizaed during a
# 'singularity instance start' command
init_database_command = [
singularity_path,
'run',
Expand All @@ -56,18 +57,21 @@ def start_postgres_singularity(
'--env', f'POSTGRES_DB={databasename}',
'--env', f'POSTGRES_USER={user}',
'--env', f'POSTGRES_PASSWORD={password}',
'--env', f'POSTGRES_PORT_EXPOSE={port}',
'--env', 'POSTGRES_INITDB_ARGS=\'--encoding=UTF-8\'',
f'docker://{DEFAULT_DOCKERIMAGE}',
f"docker://{DEFAULT_DOCKERIMAGE}",
]

info(f"Initilizing postgres database in folder: {pgdata_path}")
process = subprocess.Popen(init_database_command,
shell=False,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# Give the container time to initialize the database
for _ in range(10):
if os.path.exists(version_file):
break
time.sleep(5)
info("Waiting for the postgres database to initialize.")
else:
raise Exception("Failed to initialize the postgres database.")
time.sleep(10)
process.terminate()

Expand All @@ -82,8 +86,8 @@ def start_postgres_singularity(
'-B', f'{pgrun_path}:/var/run/postgresql',
'-e',
'-C',
f'docker://{DEFAULT_DOCKERIMAGE}',
f'{container_instance_name}',
f"docker://{DEFAULT_DOCKERIMAGE}",
container_instance_name,
]
info(f"Starting singularity instance named: {container_instance_name}")
execute(run_command)
Expand Down
6 changes: 5 additions & 1 deletion planemo/galaxy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from planemo import git
from planemo.config import OptionSource
from planemo.deps import ensure_dependency_resolvers_conf_configured
from planemo.database import postgres_singularity
from planemo.docker import docker_host_args
from planemo.galaxy.workflows import (
get_toolshed_url_for_tool_id,
Expand Down Expand Up @@ -1135,7 +1136,10 @@ def default_use_path_paste(self):


def _database_connection(database_location, **kwds):
default_connection = DATABASE_LOCATION_TEMPLATE % database_location
if 'database_type' in kwds and kwds['database_type'] == 'postgres_singularity':
default_connection = postgres_singularity.DEFAULT_CONNECTION_STRING
else:
default_connection = DATABASE_LOCATION_TEMPLATE % database_location
database_connection = kwds.get("database_connection") or default_connection
return database_connection

Expand Down

0 comments on commit 9160232

Please sign in to comment.