Skip to content

Commit 79e2bb7

Browse files
Fix system id usage
1 parent fb08ded commit 79e2bb7

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

extensions/MaaS/maas.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,48 @@ def parse_json(self):
4444

4545
extension = json_data.get("externaldetails", {}).get("extension", {})
4646
host = json_data.get("externaldetails", {}).get("host", {})
47+
vm = json_data.get("externaldetails", {}).get("virtualmachine", {})
4748

4849
endpoint = host.get("endpoint") or extension.get("endpoint")
4950
apikey = host.get("apikey") or extension.get("apikey")
5051
distro_series = (
5152
json_data.get("cloudstack.vm.details", {})
5253
.get("details", {})
53-
.get("distro_series", "ubuntu")
54+
.get("distro_series", None)
5455
)
5556

5657
if not endpoint or not apikey:
5758
fail("Missing MAAS endpoint or apikey")
5859

59-
# normalize endpoint
6060
if not endpoint.startswith("http://") and not endpoint.startswith("https://"):
6161
endpoint = "http://" + endpoint
6262
endpoint = endpoint.rstrip("/")
6363

64-
# split api key
6564
parts = apikey.split(":")
6665
if len(parts) != 3:
6766
fail("Invalid apikey format. Expected consumer:token:secret")
6867

6968
consumer, token, secret = parts
69+
70+
system_id = (
71+
json_data.get("cloudstack.vm.details", {})
72+
.get("details", {})
73+
.get("maas_system_id")
74+
or vm.get("maas_system_id", "")
75+
)
76+
77+
vm_name = vm.get("vm_name") or json_data.get("cloudstack.vm.details", {}).get("name")
78+
if not vm_name:
79+
vm_name = f"cs-{system_id}" if system_id else "cs-unknown"
80+
7081
return {
7182
"endpoint": endpoint,
7283
"consumer": consumer,
7384
"token": token,
7485
"secret": secret,
75-
"distro_series": distro_series,
76-
"system_id": json_data.get("cloudstack.vm.details", {}).get("details", {}).get("maas_system_id", ""),
86+
"distro_series": distro_series or "ubuntu/focal",
87+
"system_id": system_id,
88+
"vm_name": vm_name,
7789
}
7890
except Exception as e:
7991
fail(f"Error parsing JSON: {str(e)}")
@@ -103,9 +115,19 @@ def prepare(self):
103115
if not ready:
104116
fail("No Ready machines available")
105117

106-
system = ready[0]
118+
sysid = self.data.get("system_id")
119+
120+
if sysid:
121+
match = next((m for m in ready if m["system_id"] == sysid), None)
122+
if not match:
123+
fail(f"Provided system_id '{sysid}' not found among Ready machines")
124+
system = match
125+
else:
126+
system = ready[0]
127+
107128
system_id = system["system_id"]
108129
mac = system.get("interface_set", [{}])[0].get("mac_address")
130+
hostname = system.get("hostname", "")
109131

110132
if not mac:
111133
fail("No MAC address found")
@@ -119,18 +141,38 @@ def prepare(self):
119141

120142
result = {
121143
"nics": json_data["cloudstack.vm.details"]["nics"],
122-
"details": {"External:mac_address": mac, "maas_system_id": system_id},
144+
"details": {
145+
"External:mac_address": mac,
146+
"External:maas_system_id": system_id,
147+
"External:hostname": hostname,
148+
},
123149
}
124150
succeed(result)
125151

126152
def create(self):
127153
sysid = self.data.get("system_id")
128154
if not sysid:
129155
fail("system_id missing for create")
156+
157+
ds = self.data.get("distro_series", "ubuntu/focal")
158+
vm_name = self.data.get("vm_name")
159+
160+
# Cloud-init userdata to disable netplan, flush IPs on ens35, and run dhclient
161+
userdata = """#cloud-config
162+
network:
163+
config: disabled
164+
runcmd:
165+
- [ sh, -c, "dhclient -v -4 ens35 || true" ]
166+
"""
167+
130168
self.call_maas(
131169
"POST",
132170
f"/machines/{sysid}/",
133-
{"op": "deploy", "distro_series": self.data["distro_series"]},
171+
{
172+
"op": "deploy",
173+
"distro_series": ds,
174+
"userdata": userdata,
175+
},
134176
)
135177
succeed({"status": "success", "message": f"Instance created with {self.data['distro_series']}"})
136178

0 commit comments

Comments
 (0)