comparison NewTests/helpers.py @ 619:79812e0df162

new way to clean storage for docker containers
author Alain Mazy <am@osimis.io>
date Mon, 05 Feb 2024 17:12:07 +0100
parents 4b3d13e498a5
children 8ba9b20ae95f
comparison
equal deleted inserted replaced
618:4b3d13e498a5 619:79812e0df162
162 return path 162 return path
163 163
164 @classmethod 164 @classmethod
165 def clear_storage(cls, storage_name: str): 165 def clear_storage(cls, storage_name: str):
166 storage_path = cls.get_storage_path(storage_name=storage_name) 166 storage_path = cls.get_storage_path(storage_name=storage_name)
167 167 if Helpers.is_exe():
168 # clear the directory but keep it ! 168
169 for root, dirs, files in os.walk(storage_path): 169 # clear the directory but keep it !
170 for f in files: 170 for root, dirs, files in os.walk(storage_path):
171 os.unlink(os.path.join(root, f)) 171 for f in files:
172 for d in dirs: 172 os.unlink(os.path.join(root, f))
173 shutil.rmtree(os.path.join(root, d)) 173 for d in dirs:
174 shutil.rmtree(storage_path, ignore_errors=True) 174 shutil.rmtree(os.path.join(root, d))
175 shutil.rmtree(storage_path, ignore_errors=True)
176 else:
177 cmd = [
178 "docker", "run", "--rm",
179 "-v", f"{storage_path}:/var/lib/orthanc/db/",
180 "--name", "storage-cleanup",
181 "debian:12-slim",
182 "rm", "-rf", "/var/lib/orthanc/db/*"
183 ]
184
185 subprocess.run(cmd, check=True)
175 186
176 @classmethod 187 @classmethod
177 def is_storage_empty(cls, storage_name: str): 188 def is_storage_empty(cls, storage_name: str):
178 storage_path = cls.get_storage_path(storage_name=storage_name) 189 storage_path = cls.get_storage_path(storage_name=storage_name)
179 return len(glob.glob(os.path.join(storage_path, '*'))) == 0 190 return len(glob.glob(os.path.join(storage_path, '*'))) == 0
277 "-e", "VERBOSE_STARTUP=true" if enable_verbose else "VERBOSE_STARTUP=false", 288 "-e", "VERBOSE_STARTUP=true" if enable_verbose else "VERBOSE_STARTUP=false",
278 "-v", f"{config_path}:/etc/orthanc/orthanc.json", 289 "-v", f"{config_path}:/etc/orthanc/orthanc.json",
279 "-v", f"{storage_path}:/var/lib/orthanc/db/", 290 "-v", f"{storage_path}:/var/lib/orthanc/db/",
280 "--name", config_name, 291 "--name", config_name,
281 "-p", f"{Helpers.orthanc_under_tests_http_port}:{Helpers.orthanc_under_tests_http_port}", 292 "-p", f"{Helpers.orthanc_under_tests_http_port}:{Helpers.orthanc_under_tests_http_port}",
282 "-p", f"{Helpers.orthanc_under_tests_dicom_port}:{Helpers.orthanc_under_tests_dicom_port}", 293 "-p", f"{Helpers.orthanc_under_tests_dicom_port}:{Helpers.orthanc_under_tests_dicom_port}"
283 "-u", f"999:999"
284 ] 294 ]
285 if network: 295 if network:
286 cmd.extend(["--network", network]) 296 cmd.extend(["--network", network])
287 cmd.append(docker_image) 297 cmd.append(docker_image)
288 298