| 39 | self.session = self.init_session() |
| 40 | |
| 41 | def parse_json(self): |
| 42 | try: |
| 43 | with open(self.config_path, "r") as f: |
| 44 | json_data = json.load(f) |
| 45 | |
| 46 | extension = json_data.get("externaldetails", {}).get("extension", {}) |
| 47 | host = json_data.get("externaldetails", {}).get("host", {}) |
| 48 | vm = json_data.get("externaldetails", {}).get("virtualmachine", {}) |
| 49 | |
| 50 | endpoint = host.get("endpoint") or extension.get("endpoint") |
| 51 | apikey = host.get("apikey") or extension.get("apikey") |
| 52 | |
| 53 | details = json_data.get("cloudstack.vm.details", {}).get("details", {}) |
| 54 | |
| 55 | os_name = details.get("os") or vm.get("os") |
| 56 | architecture = details.get("architecture") or vm.get("architecture") |
| 57 | distro_series = details.get("distro_series") or vm.get("distro_series") |
| 58 | |
| 59 | if not endpoint or not apikey: |
| 60 | fail("Missing MAAS endpoint or apikey") |
| 61 | |
| 62 | if not endpoint.startswith("http://") and not endpoint.startswith("https://"): |
| 63 | endpoint = "http://" + endpoint |
| 64 | endpoint = endpoint.rstrip("/") |
| 65 | |
| 66 | parts = apikey.split(":") |
| 67 | if len(parts) != 3: |
| 68 | fail("Invalid apikey format. Expected consumer:token:secret") |
| 69 | |
| 70 | consumer, token, secret = parts |
| 71 | |
| 72 | system_id = details.get("maas_system_id") or vm.get("maas_system_id", "") |
| 73 | |
| 74 | vm_name = vm.get("vm_name") or json_data.get("cloudstack.vm.details", {}).get("name") |
| 75 | if not vm_name: |
| 76 | vm_name = f"cs-{system_id}" if system_id else "cs-unknown" |
| 77 | |
| 78 | return { |
| 79 | "endpoint": endpoint, |
| 80 | "consumer": consumer, |
| 81 | "token": token, |
| 82 | "secret": secret, |
| 83 | "distro_series": distro_series or "ubuntu/focal", |
| 84 | "os": os_name, |
| 85 | "architecture": architecture, |
| 86 | "system_id": system_id, |
| 87 | "vm_name": vm_name, |
| 88 | } |
| 89 | except Exception as e: |
| 90 | fail(f"Error parsing JSON: {str(e)}") |
| 91 | |
| 92 | def init_session(self): |
| 93 | return OAuth1Session( |