Process and write INSTANCE_JSON_FILE with all instance metadata. Replace any hyphens with underscores in key names for use in template processing. :param write_cache: boolean set True to persist obj.pkl when instance_link exists. @return True on success
(self, write_cache=True)
| 515 | return return_value |
| 516 | |
| 517 | def persist_instance_data(self, write_cache=True): |
| 518 | """Process and write INSTANCE_JSON_FILE with all instance metadata. |
| 519 | |
| 520 | Replace any hyphens with underscores in key names for use in template |
| 521 | processing. |
| 522 | |
| 523 | :param write_cache: boolean set True to persist obj.pkl when |
| 524 | instance_link exists. |
| 525 | |
| 526 | @return True on successful write, False otherwise. |
| 527 | """ |
| 528 | if write_cache and os.path.lexists(self.paths.instance_link): |
| 529 | pkl_store(self, self.paths.get_ipath_cur("obj_pkl")) |
| 530 | if self._crawled_metadata is not None: |
| 531 | # Any datasource with _crawled_metadata will best represent |
| 532 | # most recent, 'raw' metadata |
| 533 | # |
| 534 | # TODO: This type is known internally, so it is possible |
| 535 | # to narrow the type (this cast shouldn't be necessary). |
| 536 | # However that would require rewriting code across various |
| 537 | # datasource modules, so for now just assume that the type is |
| 538 | # correct and let the code throw an exception when it isn't. |
| 539 | # This allows us to enable type checking on this module even |
| 540 | # if it doesn't benefit this piece of code. |
| 541 | crawled_metadata = cast( |
| 542 | dict, copy.deepcopy(self._crawled_metadata) |
| 543 | ) |
| 544 | crawled_metadata.pop("user-data", None) |
| 545 | crawled_metadata.pop("vendor-data", None) |
| 546 | instance_data = {"ds": crawled_metadata} |
| 547 | else: |
| 548 | instance_data = {"ds": {"meta_data": self.metadata}} |
| 549 | if self.network_json != UNSET: |
| 550 | instance_data["ds"]["network_json"] = self.network_json |
| 551 | if self.ec2_metadata != UNSET: |
| 552 | instance_data["ds"]["ec2_metadata"] = self.ec2_metadata |
| 553 | instance_data["ds"]["_doc"] = EXPERIMENTAL_TEXT |
| 554 | # Add merged cloud.cfg and sys info for jinja templates and cli query |
| 555 | instance_data["merged_cfg"] = copy.deepcopy(self.sys_cfg) |
| 556 | instance_data["merged_cfg"][ |
| 557 | "_doc" |
| 558 | ] = "DEPRECATED: Use merged_system_cfg. Will be dropped from 24.1" |
| 559 | # Deprecate merged_cfg to a more specific key name merged_system_cfg |
| 560 | instance_data["merged_system_cfg"] = copy.deepcopy( |
| 561 | instance_data["merged_cfg"] |
| 562 | ) |
| 563 | instance_data["merged_system_cfg"]["_doc"] = ( |
| 564 | "Merged cloud-init system config from /etc/cloud/cloud.cfg and" |
| 565 | " /etc/cloud/cloud.cfg.d/" |
| 566 | ) |
| 567 | instance_data["sys_info"] = util.system_info() |
| 568 | instance_data.update(self._get_standardized_metadata(instance_data)) |
| 569 | try: |
| 570 | # Process content base64encoding unserializable values |
| 571 | content = atomic_helper.json_dumps(instance_data) |
| 572 | # Strip base64: prefix and set base64_encoded_keys list. |
| 573 | processed_data = process_instance_metadata( |
| 574 | json.loads(content), |
no test coverage detected