|
1 | 1 | import os |
2 | 2 | import shlex |
| 3 | +import shutil |
3 | 4 |
|
4 | 5 | from pytest_testconfig import config as py_config |
5 | 6 | from pytest import Item |
6 | 7 | from pyhelper_utils.shell import run_command |
7 | 8 | from simple_logger.logger import get_logger |
8 | 9 | from utilities.exceptions import InvalidArgumentsError |
9 | | -from utilities.infra import get_rhods_csv_version, get_oc_image_info, generate_openshift_pull_secret_file |
| 10 | +from utilities.infra import get_rhods_operator_installed_csv |
10 | 11 |
|
11 | 12 | BASE_DIRECTORY_NAME = "must-gather-collected" |
12 | 13 | BASE_RESULTS_DIR = "/home/odh/opendatahub-tests/" |
@@ -132,48 +133,56 @@ def run_must_gather( |
132 | 133 | return run_command(command=shlex.split(must_gather_command), check=False)[1] |
133 | 134 |
|
134 | 135 |
|
135 | | -def get_must_gather_image_info(architecture: str = "linux/amd64") -> str: |
136 | | - try: |
137 | | - csv_version = get_rhods_csv_version() |
138 | | - if csv_version: |
139 | | - must_gather_image_manifest = f"quay.io/modh/must-gather:rhoai-{csv_version.major}.{csv_version.minor}" |
140 | | - pull_secret = generate_openshift_pull_secret_file() |
141 | | - image_info = get_oc_image_info( |
142 | | - image=must_gather_image_manifest, architecture=architecture, pull_secret=pull_secret |
143 | | - ) |
144 | | - return f"quay.io/modh/must-gather@{image_info['digest']}" |
145 | | - else: |
146 | | - LOGGER.warning( |
147 | | - "No RHAOI CSV found. Potentially ODH cluster and must-gather collection is not " |
148 | | - "relevant for this cluster" |
149 | | - ) |
150 | | - return "" |
151 | | - except Exception as exec: |
152 | | - raise RuntimeError(f"Failed to retrieve must-gather image info: {str(exec)}") from exec |
| 136 | +def get_must_gather_image_info() -> str: |
| 137 | + csv_object = get_rhods_operator_installed_csv() |
| 138 | + if not csv_object: |
| 139 | + return "" |
| 140 | + must_gather_image = [ |
| 141 | + image["image"] for image in csv_object.instance.spec.relatedImages if "odh-must-gather" in image["image"] |
| 142 | + ] |
| 143 | + if not must_gather_image: |
| 144 | + LOGGER.warning( |
| 145 | + "No RHAOI CSV found. Potentially ODH cluster and must-gather collection is not relevant for this cluster" |
| 146 | + ) |
| 147 | + return "" |
| 148 | + return must_gather_image[0] |
153 | 149 |
|
154 | 150 |
|
155 | 151 | def collect_rhoai_must_gather( |
156 | | - target_dir: str, since: int, save_collection_output: bool = True, architecture: str = "linux/amd64" |
157 | | -) -> str: |
| 152 | + base_file_name: str, |
| 153 | + target_dir: str, |
| 154 | + since: int, |
| 155 | + save_collection_output: bool = True, |
| 156 | +) -> None: |
158 | 157 | """ |
159 | 158 | Collect must-gather data for RHOAI cluster. |
160 | 159 |
|
161 | 160 | Args: |
| 161 | + base_file_name: (str): Base file name for must-gather compressed file |
162 | 162 | target_dir (str): Directory to store the must-gather output |
163 | 163 | since (int): Time in seconds to collect logs from |
164 | 164 | save_collection_output (bool, optional): Whether to save must-gather command output. Defaults to True. |
165 | | - architecture (str, optional): Target architecture for must-gather image. Defaults to "linux/amd64". |
166 | 165 |
|
167 | 166 | Returns: |
168 | 167 | str: Path to the must-gather output directory, or empty string if collection is skipped |
169 | 168 | """ |
170 | | - must_gather_image = get_must_gather_image_info(architecture=architecture) |
| 169 | + must_gather_image = get_must_gather_image_info() |
171 | 170 | if must_gather_image: |
172 | 171 | output = run_must_gather(image_url=must_gather_image, target_dir=target_dir, since=f"{since}s") |
173 | 172 | if save_collection_output: |
174 | 173 | with open(os.path.join(target_dir, "output.log"), "w") as _file: |
175 | 174 | _file.write(output) |
176 | | - return get_must_gather_output_dir(must_gather_path=target_dir) |
| 175 | + # get must gather directory to archive |
| 176 | + path = get_must_gather_output_dir(must_gather_path=target_dir) |
| 177 | + if os.getenv("ARCHIVE_MUST_GATHER", "true") == "true": |
| 178 | + # archive the folder and get the zip file's name |
| 179 | + file_name = shutil.make_archive(base_name=base_file_name, format="zip", base_dir=path) |
| 180 | + # remove the folder that was archived |
| 181 | + shutil.rmtree(path=path, ignore_errors=True) |
| 182 | + # copy back the archived file to the same path |
| 183 | + dest_file = os.path.join(target_dir, file_name) |
| 184 | + shutil.copy(src=file_name, dst=dest_file) |
| 185 | + LOGGER.info(f"{dest_file} is collected successfully") |
| 186 | + os.unlink(file_name) |
177 | 187 | else: |
178 | | - LOGGER.warning("Must-gather collection would be skipped.") |
179 | | - return "" |
| 188 | + LOGGER.error("No must-gather image is found from the csv. Must-gather collection would be skipped.") |
0 commit comments