-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirecracker.py
More file actions
444 lines (385 loc) · 15.3 KB
/
firecracker.py
File metadata and controls
444 lines (385 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""
CloudStack Firecracker extension (HTTP/REST client)
This module talks to a remote Firecracker Agent (FastAPI) over HTTP
using RESTful endpoints.
CLI compatibility is preserved:
firecracker.py <operation> <file.json> [timeout]
Operations: create, start, stop, reboot, delete, status, recover
Input JSON is the same CloudStack payload used previously. In addition, the
remote agent connection details are read from `externaldetails.host`.
- externaldetails.host.url (required, e.g. http://10.0.0.1)
- externaldetails.host.port (default: 8000)
The client supports optional HTTP Basic authentication (host_username/host_password)
and can also attach a bearer token (host_token) when provided.
Alternatively, the extension may receive pre-extracted (flattened) fields:
- host_url, host_port, skip_ssl_verification
- host_username, host_password
- image_file, kernel_file, boot_args
- vm_name, vm_cpus, vm_ram (bytes), vm_uuid
- vm_vlans (comma-separated), vm_macs (comma-separated), vm_nics (comma-separated)
These flattened keys are used for connectivity (host_url/host_port) and
name resolution (vm_name) but the full payload is forwarded to the agent as-is.
When HTTPS is used, certificate verification is enabled by default; setting
`skip_ssl_verification=true` bypasses it. If `host_username` and
`host_password` (or their equivalents inside `externaldetails.host`) are
provided, the client sends HTTP Basic credentials to the agent.
"""
import json
import os
import re
import sys
from pathlib import Path
from typing import Any, Dict, Optional, Union
from urllib.parse import urlparse
import requests
from requests.auth import HTTPBasicAuth
# -------------------------- small helpers --------------------------
def _ok(payload: Dict[str, Any]) -> None:
"""Print a JSON object and exit 0."""
print(json.dumps(payload, ensure_ascii=False))
sys.exit(0)
def _fail(message: str, code: int = 1) -> None:
"""Print an error JSON object and exit non-zero."""
print(json.dumps({"error": message}, ensure_ascii=False))
sys.exit(code)
def _read_json(path: str) -> Dict[str, Any]:
"""Load JSON file into a Python dict with helpful errors."""
try:
return json.loads(Path(path).read_text())
except FileNotFoundError:
_fail(f"JSON file not found: {path}")
except json.JSONDecodeError as e:
_fail(f"Invalid JSON in file: {e}")
def _validate_name(entity: str, name: str) -> None:
"""Validate names against [A-Za-z0-9-]+ (mirrors the shell script)."""
if not re.match(r"^[A-Za-z0-9-]+$", name or ""):
_fail(
(
f"Invalid {entity} name '{name}'. "
"Only alphanumeric characters and dashes (-) are allowed."
)
)
def _first_non_empty(*values: Any) -> Optional[Any]:
"""Return the first value that is not None/empty string."""
for value in values:
if isinstance(value, str):
stripped = value.strip()
if stripped:
return stripped
elif value is not None:
return value
return None
def _is_truthy(value: Any) -> bool:
"""Interpret common truthy representations."""
if isinstance(value, bool):
return value
if isinstance(value, (int, float)):
return value != 0
if isinstance(value, str):
return value.strip().lower() in {"1", "true", "yes", "on"}
return False
def _ensure_file(path: str, label: str) -> str:
"""Ensure a filesystem path exists and return its absolute form."""
resolved = Path(path).expanduser()
if not resolved.exists():
_fail(f"{label} not found: {resolved}")
return str(resolved)
# -------------------------- data models --------------------------
class Agent(object):
"""HTTP agent endpoint and auth context (Python 3.6 compatible)."""
def __init__(
self,
base_url: str,
token: Optional[str],
timeout: int,
verify: Union[bool, str] = True,
auth: Optional[HTTPBasicAuth] = None,
cert: Optional[Union[str, tuple]] = None,
console_host: Optional[str] = None,
) -> None:
self.base_url = base_url
self.token = token
self.timeout = int(timeout or 30)
self.verify = verify
self.auth = auth
self.cert = cert
self.console_host = console_host or ""
class Ctx(object):
"""Execution context parsed from JSON + CLI timeout (Python 3.6 compatible)."""
def __init__(self, raw, vm_name, agent):
self.raw = raw
self.vm_name = vm_name
self.agent = agent
# -------------------------- context parsing --------------------------
def _to_ctx(config_path: str, timeout: int):
"""Translate CloudStack input JSON to a context for HTTP calls."""
data = _read_json(config_path)
det = data.get("cloudstack.vm.details", {})
ext = data.get("externaldetails", {})
host = ext.get("host", {})
# Prefer flattened keys when present
vm_name = (data.get("vm_name") or det.get("name") or det.get("uuid") or "vm").strip()
_validate_name("VM", vm_name)
url = (data.get("host_url") or host.get("url") or "").strip()
if not url:
_fail("Agent host not provided (host_url or externaldetails.host.url)")
# host_port may be a string; accept both and default to 8000
port_val = data.get("host_port") or host.get("port") or host.get("agent_port") or 8000
try:
port = int(port_val)
except (TypeError, ValueError):
_fail(f"Invalid host_port value: {port_val}")
token = (data.get("host_token") or host.get("token") or host.get("agent_token") or None)
if isinstance(token, str):
token = token.strip() or None
# If the provided URL already includes an explicit port, don't append another
if "://" not in url:
url = f"http://{url}"
parsed = urlparse(url)
authority = parsed.netloc or ""
has_explicit_port = ":" in authority
base_url = f"{url}/v1" if has_explicit_port else f"{url}:{port}/v1"
username = _first_non_empty(
host.get("username"),
host.get("user"),
host.get("login"),
data.get("host_username"),
data.get("username"),
)
password = _first_non_empty(
host.get("password"),
host.get("pass"),
host.get("secret"),
data.get("host_password"),
data.get("password"),
)
if username and not isinstance(username, str):
username = str(username)
if password and not isinstance(password, str):
password = str(password)
if username and not password:
_fail("host_password is required when host_username is provided")
if password and not username:
_fail("host_username is required when host_password is provided")
auth = HTTPBasicAuth(username, password) if username and password else None
skip_candidates = (
data.get("skip_ssl_verification"),
data.get("host_skip_ssl_verification"),
host.get("skip_ssl_verification"),
host.get("skip_ssl_verify"),
)
skip_verify = False
for candidate in skip_candidates:
if candidate is None:
continue
skip_verify = _is_truthy(candidate)
break
ca_bundle = _first_non_empty(
data.get("ca_bundle"),
data.get("ca_cert"),
host.get("ca_bundle"),
host.get("ca_cert"),
host.get("ca_file"),
)
verify: Union[bool, str] = True
if skip_verify:
verify = False
try:
import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings(InsecureRequestWarning)
except Exception:
pass
elif isinstance(ca_bundle, str):
verify = _ensure_file(ca_bundle, "CA bundle")
client_cert = _first_non_empty(
host.get("client_cert"),
host.get("tls_client_cert"),
data.get("client_cert"),
data.get("tls_client_cert"),
)
client_key = _first_non_empty(
host.get("client_key"),
host.get("tls_client_key"),
data.get("client_key"),
data.get("tls_client_key"),
)
cert: Optional[Union[str, tuple]] = None
if isinstance(client_cert, str) and client_cert:
cert_path = _ensure_file(client_cert, "TLS client certificate")
if isinstance(client_key, str) and client_key:
key_path = _ensure_file(client_key, "TLS client key")
cert = (cert_path, key_path)
else:
cert = cert_path
elif isinstance(client_key, str) and client_key:
_fail("TLS client key provided but client certificate missing")
console_host = (host.get("console_host") or parsed.hostname or "").strip()
agent = Agent(base_url, token, int(timeout or 30), verify=verify, auth=auth, cert=cert, console_host=console_host)
return Ctx(data, vm_name, agent)
# -------------------------- HTTP helpers --------------------------
def _headers(agent):
"""Default headers with optional bearer token."""
headers = {"Accept": "application/json"}
if getattr(agent, "token", None):
headers["Authorization"] = f"Bearer {agent.token}"
return headers
def _req(method, url, agent, json_body=None):
"""Perform an HTTP request with proper timeouts and map errors."""
try:
return requests.request(
method,
url,
json=json_body,
headers=_headers(agent),
timeout=agent.timeout,
auth=getattr(agent, "auth", None),
verify=getattr(agent, "verify", True),
cert=getattr(agent, "cert", None),
)
except requests.exceptions.RequestException as e:
_fail(f"HTTP error contacting agent: {e}")
def _json_or_fail(resp):
"""Return parsed JSON or fail with a helpful message."""
try:
data = resp.json()
except Exception:
data = {"raw": resp.text[:500]}
if resp.status_code >= 400:
msg = data.get("error") if isinstance(data, dict) else str(data)
_fail(f"Agent error ({resp.status_code}): {msg}")
if not isinstance(data, dict):
_fail("Agent returned non-JSON or unexpected payload")
return data
# -------------------------- operations (HTTP) --------------------------
def op_create(ctx):
"""POST /v1/vms — create and start the microVM on the agent."""
url = f"{ctx.agent.base_url}/vms"
payload = {"spec": ctx.raw, "timeout": ctx.agent.timeout}
data = _json_or_fail(_req("POST", url, ctx.agent, json_body=payload))
_ok(data)
def op_start(ctx):
"""POST /v1/vms/start — start an existing VM using saved config."""
url = f"{ctx.agent.base_url}/vms/{ctx.vm_name}/start"
payload = {"spec": ctx.raw, "timeout": ctx.agent.timeout}
data = _json_or_fail(_req("POST", url, ctx.agent, json_body=payload))
_ok(data)
def op_stop(ctx):
"""POST /v1/vms/{name}/stop — graceful then force stop."""
url = f"{ctx.agent.base_url}/vms/{ctx.vm_name}/stop"
payload = {"timeout": ctx.agent.timeout}
data = _json_or_fail(_req("POST", url, ctx.agent, json_body=payload))
_ok(data)
def op_reboot(ctx):
"""POST /v1/vms/{name}/reboot — stop and start."""
url = f"{ctx.agent.base_url}/vms/{ctx.vm_name}/reboot"
payload = {"timeout": ctx.agent.timeout}
data = _json_or_fail(_req("POST", url, ctx.agent, json_body=payload))
_ok(data)
def op_delete(ctx):
"""DELETE /v1/vms/{name} — delete resources (net/config/socket/pid/volume)."""
url = f"{ctx.agent.base_url}/vms/{ctx.vm_name}"
data = _json_or_fail(_req("DELETE", url, ctx.agent))
_ok(data)
def op_status(ctx):
"""GET /v1/vms/{name}/status — return power_state."""
url = f"{ctx.agent.base_url}/vms/{ctx.vm_name}/status"
data = _json_or_fail(_req("GET", url, ctx.agent))
_ok(data)
def op_recover(ctx):
"""POST /v1/vms/{name}/recover — restore networking/process state."""
url = f"{ctx.agent.base_url}/vms/{ctx.vm_name}/recover"
payload = {"spec": ctx.raw, "timeout": ctx.agent.timeout}
data = _json_or_fail(_req("POST", url, ctx.agent, json_body=payload))
_ok(data)
def op_console(ctx):
"""POST /v1/vms/{name}/console — obtain VNC bridge connection info."""
url = f"{ctx.agent.base_url}/vms/{ctx.vm_name}/console"
data = _json_or_fail(_req("POST", url, ctx.agent))
console_obj = data.get("console")
if isinstance(console_obj, dict):
host = console_obj.get("host")
port = console_obj.get("port")
password = console_obj.get("password")
if host and port and password:
try:
console_obj["port"] = int(port)
except (TypeError, ValueError):
_fail(f"Invalid port value returned by agent: {port}")
_ok(data)
# fallthrough if malformed
console_host = getattr(ctx.agent, "console_host", "") or ""
resp_host = data.get("host")
if console_host and (not resp_host or resp_host in {"0.0.0.0", "127.0.0.1", "::"}):
data["host"] = console_host
host = data.get("host")
port = data.get("port")
password = data.get("password")
if not host or not port or not password:
_fail("Agent response missing host/port/password for console")
try:
port_int = int(port)
except (TypeError, ValueError):
_fail(f"Invalid port value returned by agent: {port}")
console_obj = {
"host": host,
"port": port_int,
"password": password,
"protocol": "vnc",
"passwordonetimeuseonly": False,
}
response = {
"status": data.get("status", "success"),
"message": data.get("message", "Console ready"),
"console": console_obj,
}
_ok(response)
# -------------------------- main --------------------------
def main():
"""Parse CLI, build context, dispatch operation over HTTP."""
if len(sys.argv) < 3:
_fail("Usage: firecracker.py <operation> <file.json> [timeout]")
operation = sys.argv[1].lower()
json_file = sys.argv[2]
timeout = int(sys.argv[3]) if len(sys.argv) > 3 else int(os.getenv("FC_AGENT_TIMEOUT", 30))
ctx = _to_ctx(json_file, timeout)
ops = {
"create": op_create,
"start": op_start,
"stop": op_stop,
"reboot": op_reboot,
"delete": op_delete,
"status": op_status,
"state": op_status,
"recover": op_recover,
"console": op_console,
"getconsole": op_console,
}
if operation not in ops:
_fail("Invalid action")
try:
ops[operation](ctx)
except SystemExit:
raise
except Exception as e:
_fail(str(e))
if __name__ == "__main__":
main()