3
3
import os
4
4
import subprocess
5
5
import time
6
+ from tempfile import mkdtemp
6
7
7
- from galaxy .tool_util .deps import (
8
- singularity_util ,
9
- )
10
8
from galaxy .util .commands import execute
11
9
10
+ from planemo .io import info
12
11
from .interface import DatabaseSource
13
- from tempfile import mkdtemp
14
- from planemo .io import (
15
- info ,
16
- )
17
- from .postgres import (
18
- ExecutesPostgresSqlMixin ,
19
- )
20
-
12
+ from .postgres import ExecutesPostgresSqlMixin
21
13
22
14
DEFAULT_CONTAINER_NAME = "planemopostgres"
23
15
DEFAULT_POSTGRES_DATABASE_NAME = "galaxy"
27
19
DEFAULT_DOCKERIMAGE = "postgres:14.2-alpine3.15"
28
20
DEFAULT_SIF_NAME = "postgres_14_2-alpine3_15.sif"
29
21
30
- DEFAULT_CONNECTION_STRING = f"postgresql://{ DEFAULT_POSTGRES_USER } :{ DEFAULT_POSTGRES_PASSWORD } @localhost:{ DEFAULT_POSTGRES_PORT_EXPOSE } /{ DEFAULT_POSTGRES_DATABASE_NAME } "
22
+ DEFAULT_CONNECTION_STRING = f"postgresql://{ DEFAULT_POSTGRES_USER } :{ DEFAULT_POSTGRES_PASSWORD } @localhost:{ DEFAULT_POSTGRES_PORT_EXPOSE } /{ DEFAULT_POSTGRES_DATABASE_NAME } "
31
23
32
24
33
25
def start_postgres_singularity (
34
- singularity_path , container_instance_name , database_location , databasename = DEFAULT_POSTGRES_DATABASE_NAME , user = DEFAULT_POSTGRES_USER , password = DEFAULT_POSTGRES_PASSWORD , ** kwds
26
+ singularity_path ,
27
+ container_instance_name ,
28
+ database_location ,
29
+ databasename = DEFAULT_POSTGRES_DATABASE_NAME ,
30
+ user = DEFAULT_POSTGRES_USER ,
31
+ password = DEFAULT_POSTGRES_PASSWORD ,
32
+ ** kwds ,
35
33
):
36
34
info (f"Postgres database stored at: { database_location } " )
37
- pgdata_path = os .path .join (database_location , ' pgdata' )
38
- pgrun_path = os .path .join (database_location , ' pgrun' )
35
+ pgdata_path = os .path .join (database_location , " pgdata" )
36
+ pgrun_path = os .path .join (database_location , " pgrun" )
39
37
40
38
if not os .path .exists (pgdata_path ):
41
39
os .makedirs (pgdata_path )
42
40
if not os .path .exists (pgrun_path ):
43
41
os .makedirs (pgrun_path )
44
42
45
- version_file = os .path .join (pgdata_path , ' PG_VERSION' )
43
+ version_file = os .path .join (pgdata_path , " PG_VERSION" )
46
44
if not os .path .exists (version_file ):
47
45
# Run container for a short while to initialize the database
48
46
# The database will not be initilizaed during a
49
- # ' singularity instance start' command
47
+ # " singularity instance start" command
50
48
init_database_command = [
51
49
singularity_path ,
52
- 'run' ,
53
- '-B' , f'{ pgdata_path } :/var/lib/postgresql/data' ,
54
- '-B' , f'{ pgrun_path } :/var/run/postgresql' ,
55
- '-e' ,
56
- '-C' ,
57
- '--env' , f'POSTGRES_DB={ databasename } ' ,
58
- '--env' , f'POSTGRES_USER={ user } ' ,
59
- '--env' , f'POSTGRES_PASSWORD={ password } ' ,
60
- '--env' , 'POSTGRES_INITDB_ARGS=\' --encoding=UTF-8\' ' ,
50
+ "run" ,
51
+ "-B" ,
52
+ f"{ pgdata_path } :/var/lib/postgresql/data" ,
53
+ "-B" ,
54
+ f"{ pgrun_path } :/var/run/postgresql" ,
55
+ "-e" ,
56
+ "-C" ,
57
+ "--env" ,
58
+ f"POSTGRES_DB={ databasename } " ,
59
+ "--env" ,
60
+ f"POSTGRES_USER={ user } " ,
61
+ "--env" ,
62
+ f"POSTGRES_PASSWORD={ password } " ,
63
+ "--env" ,
64
+ "POSTGRES_INITDB_ARGS='--encoding=UTF-8'" ,
61
65
f"docker://{ DEFAULT_DOCKERIMAGE } " ,
62
66
]
63
67
info (f"Initilizing postgres database in folder: { pgdata_path } " )
64
- process = subprocess .Popen (init_database_command ,
65
- stdout = subprocess .PIPE ,
66
- stderr = subprocess .PIPE )
68
+ process = subprocess .Popen (init_database_command , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
67
69
# Give the container time to initialize the database
68
70
for _ in range (10 ):
69
71
if os .path .exists (version_file ):
@@ -80,12 +82,14 @@ def start_postgres_singularity(
80
82
# when starting a instance of the container.
81
83
run_command = [
82
84
singularity_path ,
83
- 'instance' ,
84
- 'start' ,
85
- '-B' , f'{ pgdata_path } :/var/lib/postgresql/data' ,
86
- '-B' , f'{ pgrun_path } :/var/run/postgresql' ,
87
- '-e' ,
88
- '-C' ,
85
+ "instance" ,
86
+ "start" ,
87
+ "-B" ,
88
+ f"{ pgdata_path } :/var/lib/postgresql/data" ,
89
+ "-B" ,
90
+ f"{ pgrun_path } :/var/run/postgresql" ,
91
+ "-e" ,
92
+ "-C" ,
89
93
f"docker://{ DEFAULT_DOCKERIMAGE } " ,
90
94
container_instance_name ,
91
95
]
@@ -112,20 +116,22 @@ def __init__(self, **kwds):
112
116
self .database_password = DEFAULT_POSTGRES_PASSWORD
113
117
self .database_host = "localhost" # TODO: Make docker host
114
118
self .database_port = DEFAULT_POSTGRES_PORT_EXPOSE
115
- if ' postgres_storage_location' in kwds and kwds [' postgres_storage_location' ] is not None :
116
- self .database_location = kwds [' postgres_storage_location' ]
119
+ if " postgres_storage_location" in kwds and kwds [" postgres_storage_location" ] is not None :
120
+ self .database_location = kwds [" postgres_storage_location" ]
117
121
else :
118
- self .database_location = os .path .join (mkdtemp (suffix = ' _planemo_postgres_db' ))
119
- self .container_instance_name = f' { DEFAULT_CONTAINER_NAME } - { int (time .time ()* 1000000 )} '
122
+ self .database_location = os .path .join (mkdtemp (suffix = " _planemo_postgres_db" ))
123
+ self .container_instance_name = f" { DEFAULT_CONTAINER_NAME } - { int (time .time ()* 1000000 )} "
120
124
self ._kwds = kwds
121
125
122
126
def __enter__ (self ):
123
- start_postgres_singularity (singularity_path = self .singularity_path ,
124
- database_location = self .database_location ,
125
- user = self .database_user ,
126
- password = self .database_password ,
127
- container_instance_name = self .container_instance_name ,
128
- ** self ._kwds )
127
+ start_postgres_singularity (
128
+ singularity_path = self .singularity_path ,
129
+ database_location = self .database_location ,
130
+ user = self .database_user ,
131
+ password = self .database_password ,
132
+ container_instance_name = self .container_instance_name ,
133
+ ** self ._kwds ,
134
+ )
129
135
130
136
def __exit__ (self , exc_type , exc_value , traceback ):
131
137
stop_postgress_singularity (self .container_instance_name )
0 commit comments