Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 1 | import docker |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 2 | import os |
| 3 | import shutil |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 4 | import re |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 5 | from EnvsReader import EnvsReader |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 6 | from docker.types import Mount |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 7 | |
| 8 | ARCHIVES_PATH = os.getenv("WORKSPACE") + "/archives/" |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 9 | |
| 10 | ERROR_API_REGEX = 'Error on API response.*[0-9]{3}' |
| 11 | RESPONSE_CODE_REGEX = '[0-9]{3}' |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 12 | |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 13 | class CertClientManager: |
| 14 | |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 15 | def __init__(self, mount_path): |
| 16 | self.mount_path = mount_path |
| 17 | |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 18 | def run_client_container(self, client_image, container_name, path_to_env, request_url, network): |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 19 | self.create_mount_dir() |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 20 | client = docker.from_env() |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 21 | environment = EnvsReader().read_env_list_from_file(path_to_env) |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 22 | environment.append("REQUEST_URL=" + request_url) |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 23 | container = client.containers.run( |
| 24 | image=client_image, |
| 25 | name=container_name, |
| 26 | environment=environment, |
| 27 | network=network, |
| 28 | user='root', #Run container as root to avoid permission issues with volume mount access |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 29 | mounts=[Mount(target='/var/certs', source=self.mount_path, type='bind')], |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 30 | detach=True |
| 31 | ) |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 32 | exitcode = container.wait() |
| 33 | return exitcode |
| 34 | |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 35 | def remove_client_container_and_save_logs(self, container_name, log_file_name): |
| 36 | client = docker.from_env() |
| 37 | container = client.containers.get(container_name) |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 38 | text_file = open(ARCHIVES_PATH + "client_container_" + log_file_name + ".log", "w") |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 39 | text_file.write(container.logs()) |
| 40 | text_file.close() |
| 41 | container.remove() |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 42 | self.remove_mount_dir() |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 43 | |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 44 | def create_mount_dir(self): |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 45 | if not os.path.exists(self.mount_path): |
| 46 | os.makedirs(self.mount_path) |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 47 | |
| 48 | def remove_mount_dir(self): |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 49 | shutil.rmtree(self.mount_path) |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 50 | |
| 51 | def can_find_api_response_in_logs(self, container_name): |
| 52 | logs = self.get_container_logs(container_name) |
| 53 | api_logs = re.findall(ERROR_API_REGEX, logs) |
| 54 | if api_logs: |
| 55 | return True |
| 56 | else: |
| 57 | return False |
| 58 | |
| 59 | def get_api_response_from_logs(self, container_name): |
| 60 | logs = self.get_container_logs(container_name) |
| 61 | error_api_message = re.findall(ERROR_API_REGEX, logs) |
| 62 | code = re.findall(RESPONSE_CODE_REGEX, error_api_message[0]) |
| 63 | return code[0] |
| 64 | |
| 65 | def get_container_logs(self, container_name): |
| 66 | client = docker.from_env() |
| 67 | container = client.containers.get(container_name) |
| 68 | logs = container.logs() |
| 69 | return logs |