Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 4 | This module provides utilities for code updates. |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 5 | """ |
| 6 | |
| 7 | import os |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 8 | import re |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 9 | import sys |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 10 | import tarfile |
| 11 | import time |
| 12 | |
| 13 | robot_pgm_dir_path = os.path.dirname(__file__) + os.sep |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 14 | repo_data_path = re.sub('/lib', '/data', robot_pgm_dir_path) |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 15 | sys.path.append(repo_data_path) |
| 16 | |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 17 | import gen_robot_keyword as keyword |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 18 | import gen_print as gp |
| 19 | import variables as var |
| 20 | from robot.libraries.BuiltIn import BuiltIn |
| 21 | |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 22 | ############################################################################### |
Charles Paul Hofer | da24d0a | 2017-08-09 15:03:40 -0500 | [diff] [blame] | 23 | def get_non_running_bmc_software_object(): |
| 24 | |
| 25 | r""" |
| 26 | Get the URI to a BMC image from software that is not running on the BMC. |
| 27 | """ |
| 28 | |
| 29 | # Get the version of the image currently running on the BMC. |
| 30 | _, cur_img_version = keyword.run_key("Get BMC Version") |
| 31 | # Remove the surrounding double quotes from the version. |
| 32 | cur_img_version = cur_img_version.replace('"', '') |
| 33 | |
| 34 | _, images = keyword.run_key("Read Properties " |
| 35 | + var.SOFTWARE_VERSION_URI + "enumerate") |
| 36 | |
| 37 | for image_name in images: |
| 38 | _, image_properties = keyword.run_key( |
| 39 | "Get Host Software Property " + image_name) |
| 40 | if image_properties['Version'] != cur_img_version: |
| 41 | return image_name |
| 42 | BuiltIn().fail("Did not find any non-running BMC images.") |
| 43 | |
| 44 | ############################################################################### |
| 45 | |
| 46 | |
| 47 | ############################################################################### |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 48 | def delete_all_pnor_images(): |
| 49 | |
| 50 | r""" |
| 51 | Delete all PNOR images from the BMC. |
| 52 | """ |
| 53 | |
| 54 | status, images = keyword.run_key("Read Properties " |
| 55 | + var.SOFTWARE_VERSION_URI + "enumerate") |
| 56 | for image_name in images: |
| 57 | image_id = image_name.split('/')[-1] |
| 58 | image_purpose = images[image_name]["Purpose"] |
| 59 | if var.VERSION_PURPOSE_HOST == image_purpose: |
| 60 | # Delete twice, in case the image is in the /tmp/images directory |
| 61 | keyword.run_key("Call Method " + var.SOFTWARE_VERSION_URI |
| 62 | + image_id + " delete data={\"data\":[]}") |
| 63 | keyword.run_key("Call Method " + var.SOFTWARE_VERSION_URI |
| 64 | + image_id + " delete data={\"data\":[]}") |
| 65 | |
| 66 | ############################################################################### |
| 67 | |
| 68 | |
| 69 | ############################################################################### |
| 70 | def wait_for_activation_state_change(version_id, initial_state): |
| 71 | |
| 72 | r""" |
| 73 | Wait for the current activation state of ${version_id} to |
| 74 | change from the state provided by the calling function. |
| 75 | |
| 76 | Description of argument(s): |
| 77 | version_id The version ID whose state change we are waiting for. |
| 78 | initial_state The activation state we want to wait for. |
| 79 | """ |
| 80 | |
| 81 | keyword.run_key_u("Open Connection And Log In") |
| 82 | retry = 0 |
| 83 | while (retry < 20): |
| 84 | status, software_state = keyword.run_key("Read Properties " + |
| 85 | var.SOFTWARE_VERSION_URI + str(version_id)) |
| 86 | current_state = (software_state)["Activation"] |
| 87 | if (initial_state == current_state): |
| 88 | time.sleep(60) |
| 89 | retry += 1 |
| 90 | else: |
| 91 | return |
| 92 | return |
| 93 | |
| 94 | ############################################################################### |
| 95 | |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 96 | |
| 97 | ############################################################################### |
| 98 | def get_latest_file(dir_path): |
| 99 | |
| 100 | r""" |
| 101 | Get the path to the latest uploaded file. |
| 102 | |
| 103 | Description of argument(s): |
| 104 | dir_path Path to the dir from which the name of the last |
| 105 | updated file or folder will be returned to the |
| 106 | calling function. |
| 107 | """ |
| 108 | |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 109 | keyword.run_key_u("Open Connection And Log In") |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 110 | status, ret_values =\ |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 111 | keyword.run_key("Execute Command On BMC cd " + dir_path |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 112 | + "; stat -c '%Y %n' * | sort -k1,1nr | head -n 1", ignore=1) |
| 113 | return ret_values.split(" ")[-1] |
| 114 | |
| 115 | ############################################################################### |
| 116 | |
| 117 | |
| 118 | ############################################################################### |
| 119 | def get_version_tar(tar_file_path): |
| 120 | |
| 121 | r""" |
| 122 | Read the image version from the MANIFEST inside the tarball. |
| 123 | |
| 124 | Description of argument(s): |
| 125 | tar_file_path The path to a tar file that holds the image |
| 126 | version inside the MANIFEST. |
| 127 | """ |
| 128 | |
| 129 | tar = tarfile.open(tar_file_path) |
| 130 | for member in tar.getmembers(): |
| 131 | f=tar.extractfile(member) |
| 132 | content=f.read() |
| 133 | if "version=" in content: |
| 134 | content = content.split("\n") |
| 135 | content = [x for x in content if "version=" in x] |
| 136 | version = content[0].split("=")[-1] |
| 137 | break |
| 138 | tar.close() |
| 139 | return version |
| 140 | |
| 141 | ############################################################################### |
| 142 | |
| 143 | |
| 144 | ############################################################################### |
| 145 | def get_image_version(file_path): |
| 146 | |
| 147 | r""" |
| 148 | Read the file for a version object. |
| 149 | |
| 150 | Description of argument(s): |
| 151 | file_path The path to a file that holds the image version. |
| 152 | """ |
| 153 | |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 154 | keyword.run_key_u("Open Connection And Log In") |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 155 | status, ret_values =\ |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 156 | keyword.run_key("Execute Command On BMC cat " |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 157 | + file_path + " | grep \"version=\"", ignore=1) |
| 158 | return (ret_values.split("\n")[0]).split("=")[-1] |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 159 | |
| 160 | ############################################################################### |
| 161 | |
| 162 | |
| 163 | ############################################################################### |
| 164 | def get_image_purpose(file_path): |
| 165 | |
| 166 | r""" |
| 167 | Read the file for a purpose object. |
| 168 | |
| 169 | Description of argument(s): |
| 170 | file_path The path to a file that holds the image purpose. |
| 171 | """ |
| 172 | |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 173 | keyword.run_key_u("Open Connection And Log In") |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 174 | status, ret_values =\ |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 175 | keyword.run_key("Execute Command On BMC cat " |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 176 | + file_path + " | grep \"purpose=\"", ignore=1) |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 177 | return ret_values.split("=")[-1] |
| 178 | |
| 179 | ############################################################################### |
| 180 | |
| 181 | |
| 182 | ############################################################################### |
| 183 | def get_image_path(image_version): |
| 184 | |
| 185 | r""" |
| 186 | Query the upload image dir for the presence of image matching |
| 187 | the version that was read from the MANIFEST before uploading |
| 188 | the image. Based on the purpose verify the activation object |
| 189 | exists and is either READY or INVALID. |
| 190 | |
| 191 | Description of argument(s): |
| 192 | image_version The version of the image that should match one |
| 193 | of the images in the upload dir. |
| 194 | """ |
| 195 | |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 196 | upload_dir = BuiltIn().get_variable_value("${upload_dir_path}") |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 197 | keyword.run_key_u("Open Connection And Log In") |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 198 | status, image_list =\ |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 199 | keyword.run_key("Execute Command On BMC ls -d " + upload_dir |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 200 | + "*/") |
| 201 | |
| 202 | image_list = image_list.split("\n") |
| 203 | retry = 0 |
| 204 | while (retry < 10): |
| 205 | for i in range(0, len(image_list)): |
| 206 | version = get_image_version(image_list[i] + "MANIFEST") |
| 207 | if (version == image_version): |
| 208 | return image_list[i] |
| 209 | time.sleep(10) |
| 210 | retry += 1 |
| 211 | |
| 212 | ############################################################################### |
| 213 | |
| 214 | |
| 215 | ############################################################################### |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 216 | def verify_image_upload(timeout=3): |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 217 | |
| 218 | r""" |
| 219 | Verify the image was uploaded correctly and that it created |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 220 | a valid d-bus object. If the first check for the image |
| 221 | fails, try again until we reach the timeout. |
| 222 | |
| 223 | Description of argument(s): |
| 224 | timeout How long, in minutes, to keep trying to find the |
| 225 | image on the BMC. Default is 3 minutes. |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 226 | """ |
| 227 | |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 228 | image_version = BuiltIn().get_variable_value("${image_version}") |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 229 | image_path = get_image_path(image_version) |
| 230 | image_version_id = image_path.split("/")[-2] |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 231 | BuiltIn().set_global_variable("${version_id}", image_version_id) |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 232 | |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 233 | keyword.run_key_u("Open Connection And Log In") |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 234 | image_purpose = get_image_purpose(image_path + "MANIFEST") |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 235 | if (image_purpose == var.VERSION_PURPOSE_BMC or |
| 236 | image_purpose == var.VERSION_PURPOSE_HOST): |
George Keishing | ff1e3ec | 2017-07-20 01:58:21 -0500 | [diff] [blame] | 237 | uri = var.SOFTWARE_VERSION_URI + image_version_id |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 238 | ret_values = "" |
| 239 | for itr in range(timeout * 2): |
| 240 | status, ret_values = \ |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 241 | keyword.run_key("Read Attribute " + uri + " Activation") |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 242 | |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 243 | if ((ret_values == var.READY) or (ret_values == var.INVALID) |
| 244 | or (ret_values == var.ACTIVE)): |
| 245 | return True |
| 246 | else: |
| 247 | time.sleep(30) |
| 248 | |
| 249 | # If we exit the for loop, the timeout has been reached |
| 250 | gp.print_var(ret_values) |
| 251 | return False |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 252 | else: |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 253 | gp.print_var(image_purpose) |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 254 | return False |
| 255 | |
| 256 | ############################################################################### |
Charles P. Hofer | 1d20acd | 2017-07-05 15:24:40 -0500 | [diff] [blame] | 257 | |
| 258 | |
| 259 | ############################################################################### |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 260 | def verify_image_not_in_bmc_uploads_dir(image_version, timeout=3): |
Charles P. Hofer | 1d20acd | 2017-07-05 15:24:40 -0500 | [diff] [blame] | 261 | |
| 262 | r""" |
| 263 | Check that an image with the given version is not unpacked inside of the |
| 264 | BMCs image uploads directory. If no image is found, retry every 30 seconds |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 265 | until the given timeout is hit, in case the BMC takes time |
| 266 | unpacking the image. |
Charles P. Hofer | 1d20acd | 2017-07-05 15:24:40 -0500 | [diff] [blame] | 267 | |
| 268 | Description of argument(s): |
| 269 | image_version The version of the image to look for on the BMC. |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 270 | timeout How long, in minutes, to try to find an image on the BMC. |
| 271 | Default is 3 minutes. |
Charles P. Hofer | 1d20acd | 2017-07-05 15:24:40 -0500 | [diff] [blame] | 272 | """ |
| 273 | |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 274 | keyword.run_key('Open Connection And Log In') |
Charles P. Hofer | 346c045 | 2017-07-14 15:29:50 -0500 | [diff] [blame] | 275 | upload_dir_path = BuiltIn().get_variable_value("${upload_dir_path}") |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 276 | for i in range(timeout * 2): |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 277 | stat, grep_res = keyword.run_key('Execute Command On BMC ' |
Charles P. Hofer | 1d20acd | 2017-07-05 15:24:40 -0500 | [diff] [blame] | 278 | + 'ls ' + upload_dir_path + '*/MANIFEST 2>/dev/null ' |
| 279 | + '| xargs grep -rl "version=' + image_version + '"') |
| 280 | image_dir = os.path.dirname(grep_res.split('\n')[0]) |
| 281 | if '' != image_dir: |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 282 | keyword.run_key('Execute Command On BMC rm -rf ' + image_dir) |
Charles P. Hofer | 1d20acd | 2017-07-05 15:24:40 -0500 | [diff] [blame] | 283 | BuiltIn().fail('Found invalid BMC Image: ' + image_dir) |
| 284 | time.sleep(30) |
| 285 | |
Charles Paul Hofer | de7d408 | 2017-08-08 14:41:01 -0500 | [diff] [blame] | 286 | ############################################################################### |