Mercurial > hg > orthanc-tests
annotate PerfsDb/DbServer.py @ 735:be8f174d3c9d find-refactoring tip
tools/find: Limit and Since are now forbidden when filtering on DICOM tags that are not stored in DB
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Thu, 24 Oct 2024 15:08:59 +0200 |
parents | dbc42a03ee75 |
children |
rev | line source |
---|---|
156 | 1 import typing |
2 import subprocess | |
3 import time | |
4 | |
5 from DbType import DbType | |
6 | |
7 class DbServer: | |
8 | |
9 class DockerDefinition: | |
10 | |
162 | 11 def __init__(self, image: str, internalPort: int, envVars: typing.Dict[str, str], storagePath: str, command: typing.List[str]=None, extraInitTime: int=0): |
156 | 12 self.image = image |
13 self.internalPort = internalPort | |
14 self.envVars = envVars | |
15 self.storagePath = storagePath | |
16 self.command = command | |
162 | 17 self.extraInitTime = extraInitTime |
156 | 18 |
19 def __init__(self, dbType: DbType, port: int): | |
20 | |
21 self.port = port | |
22 self.dbType = dbType | |
23 | |
24 self._containerId = None | |
25 self._label = None | |
26 | |
27 def setLabel(self, label: str): | |
28 self._label = label | |
29 | |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
30 def isRunning(self) -> bool: |
156 | 31 ret = subprocess.call([ |
32 "docker", | |
33 "top", | |
34 self._label | |
35 ]) | |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
36 return ret == 0 |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
37 |
162 | 38 def volumeExists(self) -> bool: |
39 ret = subprocess.call([ | |
40 "docker", | |
41 "volume", | |
42 "inspect", | |
43 self._label | |
44 ]) | |
45 return ret == 0 | |
46 | |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
47 def launch(self): |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
48 dockerDefinition = self.getDockerDefinition() |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
49 |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
50 # check if the container is already running |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
51 if self.isRunning(): |
156 | 52 print("DbServer is already running") |
53 return | |
54 | |
162 | 55 volumeCreatedThisTime = False |
56 if not self.volumeExists(): | |
57 subprocess.check_call([ | |
58 "docker", | |
59 "volume", | |
60 "create", | |
61 "--name=" + self._label | |
62 ]) | |
63 volumeCreatedThisTime = True | |
156 | 64 |
65 dockerRunCommand = [ | |
66 "docker", | |
67 "run", | |
68 "-d", | |
69 "--name=" + self._label, | |
70 "-p", str(self.port) + ":" + str(dockerDefinition.internalPort), | |
71 "--volume=" + self._label + ":" + dockerDefinition.storagePath | |
72 ] | |
73 | |
74 if len(dockerDefinition.envVars) > 0: | |
75 for k,v in dockerDefinition.envVars.items(): | |
76 dockerRunCommand.extend(["--env", k + "=" + v]) | |
77 | |
78 dockerRunCommand.append( | |
79 dockerDefinition.image | |
80 ) | |
81 | |
82 if dockerDefinition.command is not None: | |
83 dockerRunCommand.extend( | |
84 dockerDefinition.command | |
85 ) | |
86 | |
87 print("Launching DbServer") | |
88 subprocess.check_call(dockerRunCommand) | |
89 | |
90 print("Waiting for DbServer to be ready") | |
91 | |
92 # wait until its port is open | |
93 retryCounter = 0 | |
94 connected = False | |
95 while not connected and retryCounter < 30: | |
96 time.sleep(1) | |
97 connected = subprocess.call(["nc", "-z", "localhost", str(self.port)]) == 0 | |
98 if retryCounter >= 30: | |
99 print("DbServer still not ready after 30 sec") | |
100 raise TimeoutError | |
101 | |
162 | 102 if dockerDefinition.extraInitTime > 0 and volumeCreatedThisTime: |
103 time.sleep(dockerDefinition.extraInitTime) | |
104 | |
156 | 105 def stop(self): |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
106 if self.isRunning(): |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
107 subprocess.check_call([ |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
108 "docker", |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
109 "stop", |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
110 self._label |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
111 ]) |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
112 |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
113 subprocess.call([ |
156 | 114 "docker", |
115 "rm", | |
116 self._label | |
117 ]) | |
118 | |
119 def clear(self): | |
120 # remove the volume | |
121 self.stop() | |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
122 subprocess.call([ |
156 | 123 "docker", |
124 "volume", | |
125 "rm", | |
126 self._label | |
127 ]) | |
128 | |
129 | |
130 def getDockerDefinition(self): | |
131 if self.dbType == DbType.MySQL: | |
132 return DbServer.DockerDefinition( | |
133 image="mysql:8.0", | |
134 internalPort=3306, | |
135 envVars={ | |
136 "MYSQL_PASSWORD": "orthanc", | |
137 "MYSQL_USER": "orthanc", | |
138 "MYSQL_DATABASE": "orthanc", | |
139 "MYSQL_ROOT_PASSWORD": "foo-root" | |
140 }, | |
141 storagePath="/var/lib/mysql", | |
162 | 142 command=["mysqld", "--default-authentication-plugin=mysql_native_password", "--log-bin-trust-function-creators=1"], |
143 extraInitTime=30 | |
156 | 144 ) |
145 elif self.dbType == DbType.MSSQL: | |
146 return DbServer.DockerDefinition( | |
147 image="microsoft/mssql-server-linux", | |
148 internalPort=1433, | |
149 envVars={ | |
150 "ACCEPT_EULA": "Y", | |
151 "SA_PASSWORD": "MyStrOngPa55word!" | |
152 }, | |
162 | 153 storagePath="/var/opt/mssql/data", |
154 extraInitTime=10 | |
156 | 155 ) |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
156 elif self.dbType == DbType.PG9 or self.dbType == DbType.PG10 or self.dbType == DbType.PG11: |
156 | 157 if self.dbType == DbType.PG9: |
158 image = "postgres:9" | |
159 elif self.dbType == DbType.PG10: | |
160 image = "postgres:10" | |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
161 elif self.dbType == DbType.PG11: |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
162 image = "postgres:11" |
156 | 163 return DbServer.DockerDefinition( |
164 image=image, | |
165 internalPort=5432, | |
166 envVars={ | |
167 }, | |
168 storagePath="/var/lib/postgresql/data" | |
169 ) | |
170 |