annotate PerfsDb/DbServer.py @ 156:f1a75985caa8

first Db test framework - work in progress
author am@osimis.io
date Thu, 16 Aug 2018 17:13:32 +0200
parents
children df1f9946571c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
1 import typing
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
2 import subprocess
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
3 import time
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
4
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
5 from DbType import DbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
6
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
7 class DbServer:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
8
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
9 class DockerDefinition:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
10
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
11 def __init__(self, image: str, internalPort: int, envVars: typing.Dict[str, str], storagePath: str, command: typing.List[str]=None):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
12 self.image = image
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
13 self.internalPort = internalPort
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
14 self.envVars = envVars
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
15 self.storagePath = storagePath
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
16 self.command = command
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
17
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
18 def __init__(self, dbType: DbType, port: int):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
19
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
20 self.port = port
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
21 self.dbType = dbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
22
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
23 self._containerId = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
24 self._label = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
25
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
26 def setLabel(self, label: str):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
27 self._label = label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
28
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
29 def launch(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
30 dockerDefinition = self.getDockerDefinition()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
31
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
32 # check if the container is already running
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
33 ret = subprocess.call([
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
34 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
35 "top",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
36 self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
37 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
38 if ret == 0:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
39 print("DbServer is already running")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
40 return
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
41
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
42 # create a volume (if it already exists, it wont be modified)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
43 subprocess.check_call([
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
44 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
45 "volume",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
46 "create",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
47 "--name=" + self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
48 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
49
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
50 dockerRunCommand = [
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
51 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
52 "run",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
53 "-d",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
54 "--name=" + self._label,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
55 "-p", str(self.port) + ":" + str(dockerDefinition.internalPort),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
56 "--volume=" + self._label + ":" + dockerDefinition.storagePath
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
57 ]
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
58
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
59 if len(dockerDefinition.envVars) > 0:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
60 for k,v in dockerDefinition.envVars.items():
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
61 dockerRunCommand.extend(["--env", k + "=" + v])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
62
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
63 dockerRunCommand.append(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
64 dockerDefinition.image
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
65 )
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
66
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
67 if dockerDefinition.command is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
68 dockerRunCommand.extend(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
69 dockerDefinition.command
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
70 )
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
71
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
72 print("Launching DbServer")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
73 subprocess.check_call(dockerRunCommand)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
74
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
75 print("Waiting for DbServer to be ready")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
76
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
77 # wait until its port is open
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
78 retryCounter = 0
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
79 connected = False
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
80 while not connected and retryCounter < 30:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
81 time.sleep(1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
82 connected = subprocess.call(["nc", "-z", "localhost", str(self.port)]) == 0
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
83 if retryCounter >= 30:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
84 print("DbServer still not ready after 30 sec")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
85 raise TimeoutError
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
86
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
87 def stop(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
88 subprocess.check_call([
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
89 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
90 "stop",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
91 self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
92 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
93 subprocess.check_call([
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
94 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
95 "rm",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
96 self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
97 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
98
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
99 def clear(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
100 # remove the volume
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
101 self.stop()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
102 subprocess.check_call([
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
103 "docker",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
104 "volume",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
105 "rm",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
106 self._label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
107 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
108
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
109
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
110 def getDockerDefinition(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
111 if self.dbType == DbType.MySQL:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
112 return DbServer.DockerDefinition(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
113 image="mysql:8.0",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
114 internalPort=3306,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
115 envVars={
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
116 "MYSQL_PASSWORD": "orthanc",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
117 "MYSQL_USER": "orthanc",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
118 "MYSQL_DATABASE": "orthanc",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
119 "MYSQL_ROOT_PASSWORD": "foo-root"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
120 },
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
121 storagePath="/var/lib/mysql",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
122 command=["mysqld", "--default-authentication-plugin=mysql_native_password", "--log-bin-trust-function-creators=1"]
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
123 )
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
124 elif self.dbType == DbType.MSSQL:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
125 return DbServer.DockerDefinition(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
126 image="microsoft/mssql-server-linux",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
127 internalPort=1433,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
128 envVars={
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
129 "ACCEPT_EULA": "Y",
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
130 "SA_PASSWORD": "MyStrOngPa55word!"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
131 },
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
132 storagePath="/var/opt/mssql/data"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
133 )
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
134 elif self.dbType == DbType.PG9 or self.dbType == DbType.PG10:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
135 if self.dbType == DbType.PG9:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
136 image = "postgres:9"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
137 elif self.dbType == DbType.PG10:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
138 image = "postgres:10"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
139 return DbServer.DockerDefinition(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
140 image=image,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
141 internalPort=5432,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
142 envVars={
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
143 },
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
144 storagePath="/var/lib/postgresql/data"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
145 )
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
146