Handle calls to 'cloud-id' cli. Print the canonical cloud-id on which the instance is running. @return: 0 on success, 1 on error, 2 on disabled, 3 on cloud-init not run.
(name, args)
| 59 | |
| 60 | |
| 61 | def handle_args(name, args): |
| 62 | """Handle calls to 'cloud-id' cli. |
| 63 | |
| 64 | Print the canonical cloud-id on which the instance is running. |
| 65 | |
| 66 | @return: 0 on success, 1 on error, 2 on disabled, 3 on cloud-init not run. |
| 67 | """ |
| 68 | status_details = get_status_details() |
| 69 | if status_details.running_status == RunningStatus.DISABLED: |
| 70 | sys.stdout.write("{0}\n".format(status_details.running_status.value)) |
| 71 | return 2 |
| 72 | elif status_details.running_status == RunningStatus.NOT_STARTED: |
| 73 | sys.stdout.write("{0}\n".format(status_details.running_status.value)) |
| 74 | return 3 |
| 75 | |
| 76 | try: |
| 77 | with open(args.instance_data) as file: |
| 78 | instance_data = json.load(file) |
| 79 | except IOError: |
| 80 | return log_util.error( |
| 81 | "File not found '%s'. Provide a path to instance data json file" |
| 82 | " using --instance-data" % args.instance_data |
| 83 | ) |
| 84 | except ValueError as e: |
| 85 | return log_util.error( |
| 86 | "File '%s' is not valid json. %s" % (args.instance_data, e) |
| 87 | ) |
| 88 | v1 = instance_data.get("v1", {}) |
| 89 | cloud_id = canonical_cloud_id( |
| 90 | v1.get("cloud_name", METADATA_UNKNOWN), |
| 91 | v1.get("region", METADATA_UNKNOWN), |
| 92 | v1.get("platform", METADATA_UNKNOWN), |
| 93 | ) |
| 94 | if args.json: |
| 95 | sys.stderr.write("DEPRECATED: Use: cloud-init query v1\n") |
| 96 | v1["cloud_id"] = cloud_id |
| 97 | response = json.dumps( # Pretty, sorted json |
| 98 | v1, indent=1, sort_keys=True, separators=(",", ": ") |
| 99 | ) |
| 100 | elif args.long: |
| 101 | response = "%s\t%s" % (cloud_id, v1.get("region", METADATA_UNKNOWN)) |
| 102 | else: |
| 103 | response = cloud_id |
| 104 | sys.stdout.write("%s\n" % response) |
| 105 | return 0 |
| 106 | |
| 107 | |
| 108 | def main(): |
no test coverage detected