It uses the GCE VM metadata server `update_task_enabled` flag. This flag will be used to rollout the update_task deprecation by disabling it progressively for each instance group through the instance template metadata
()
| 255 | |
| 256 | |
| 257 | def update_task_enabled() -> bool: |
| 258 | """ It uses the GCE VM metadata server `update_task_enabled` flag. |
| 259 | |
| 260 | This flag will be used to rollout the update_task deprecation |
| 261 | by disabling it progressively for each instance group through |
| 262 | the instance template metadata |
| 263 | """ |
| 264 | metadata_url = ("http://metadata.google.internal/computeMetadata/v1/" + |
| 265 | "instance/attributes/") |
| 266 | metadata_header = {"Metadata-Flavor": "Google"} |
| 267 | metadata_key = "update_task_enabled" |
| 268 | |
| 269 | running_on_batch = bool(environment.is_uworker()) |
| 270 | |
| 271 | try: |
| 272 | # Construct the full URL for your specific metadata key |
| 273 | response = requests.get( |
| 274 | f"{metadata_url}{metadata_key}", headers=metadata_header, timeout=10) |
| 275 | |
| 276 | # Raise an exception for bad status codes (4xx or 5xx) |
| 277 | response.raise_for_status() |
| 278 | |
| 279 | # The metadata value is in the response text |
| 280 | metadata_value = response.text |
| 281 | logs.info(f"The value for '{metadata_key}' is: {metadata_value}") |
| 282 | is_update_task_enabled = metadata_value.lower() != 'false' |
| 283 | |
| 284 | # The flag is_uworker is true for Batch environment |
| 285 | # The update task should run if it's not a Batch environment |
| 286 | # and the flag is enabled on the VM template metadata |
| 287 | return not running_on_batch and is_update_task_enabled |
| 288 | |
| 289 | except requests.exceptions.HTTPError as http_error: |
| 290 | logs.warning(f"Http error fetching metadata: {http_error}") |
| 291 | |
| 292 | except Exception as ex: |
| 293 | logs.error(f"Unknown exception fetching metadata: {ex}") |
| 294 | |
| 295 | return not running_on_batch |
| 296 | |
| 297 | |
| 298 | def main(): |
no test coverage detected