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 | |
Aleksandra Maciaga | 13b7d05 | 2020-03-26 17:28:47 +0100 | [diff] [blame] | 13 | |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 14 | class CertClientManager: |
| 15 | |
Aleksandra Maciaga | 13b7d05 | 2020-03-26 17:28:47 +0100 | [diff] [blame] | 16 | def __init__(self, mount_path, truststore_path): |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 17 | self.mount_path = mount_path |
Aleksandra Maciaga | 13b7d05 | 2020-03-26 17:28:47 +0100 | [diff] [blame] | 18 | self.truststore_path = truststore_path |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 19 | |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 20 | 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] | 21 | self.create_mount_dir() |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 22 | client = docker.from_env() |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 23 | environment = EnvsReader().read_env_list_from_file(path_to_env) |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 24 | environment.append("REQUEST_URL=" + request_url) |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 25 | container = client.containers.run( |
| 26 | image=client_image, |
| 27 | name=container_name, |
| 28 | environment=environment, |
| 29 | network=network, |
Aleksandra Maciaga | 13b7d05 | 2020-03-26 17:28:47 +0100 | [diff] [blame] | 30 | user='root', # Run container as root to avoid permission issues with volume mount access |
| 31 | mounts=[Mount(target='/var/certs', source=self.mount_path, type='bind'), |
Pawel | b7a057e | 2020-08-07 14:53:48 +0200 | [diff] [blame] | 32 | Mount(target='/etc/onap/oom-platform-cert-service/certservice/certs/', source=self.truststore_path, type='bind')], |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 33 | detach=True |
| 34 | ) |
Tomasz Wrobel | 75573b0 | 2020-03-05 16:43:50 +0100 | [diff] [blame] | 35 | exitcode = container.wait() |
| 36 | return exitcode |
| 37 | |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 38 | def remove_client_container_and_save_logs(self, container_name, log_file_name): |
| 39 | client = docker.from_env() |
| 40 | container = client.containers.get(container_name) |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 41 | text_file = open(ARCHIVES_PATH + "client_container_" + log_file_name + ".log", "w") |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 42 | text_file.write(container.logs()) |
| 43 | text_file.close() |
| 44 | container.remove() |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 45 | self.remove_mount_dir() |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 46 | |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 47 | def create_mount_dir(self): |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 48 | if not os.path.exists(self.mount_path): |
| 49 | os.makedirs(self.mount_path) |
Remigiusz Janeczek | ed0f3be | 2020-03-19 14:20:22 +0100 | [diff] [blame] | 50 | |
| 51 | def remove_mount_dir(self): |
Joanna Jeremicz | 9c14f19 | 2020-03-19 14:51:51 +0100 | [diff] [blame] | 52 | shutil.rmtree(self.mount_path) |
Tomasz Wrobel | 6ed06d5 | 2020-03-11 15:31:10 +0100 | [diff] [blame] | 53 | |
| 54 | def can_find_api_response_in_logs(self, container_name): |
| 55 | logs = self.get_container_logs(container_name) |
| 56 | api_logs = re.findall(ERROR_API_REGEX, logs) |
| 57 | if api_logs: |
| 58 | return True |
| 59 | else: |
| 60 | return False |
| 61 | |
| 62 | def get_api_response_from_logs(self, container_name): |
| 63 | logs = self.get_container_logs(container_name) |
| 64 | error_api_message = re.findall(ERROR_API_REGEX, logs) |
| 65 | code = re.findall(RESPONSE_CODE_REGEX, error_api_message[0]) |
| 66 | return code[0] |
| 67 | |
| 68 | def get_container_logs(self, container_name): |
| 69 | client = docker.from_env() |
| 70 | container = client.containers.get(container_name) |
| 71 | logs = container.logs() |
| 72 | return logs |