@microck/raft 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/raft.py CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  import argparse
5
5
  import fcntl
6
+ import getpass
7
+ import hashlib
6
8
  import importlib.util
7
9
  import json
8
10
  import os
@@ -10,26 +12,38 @@ from pathlib import Path
10
12
  import re
11
13
  import shlex
12
14
  import shutil
15
+ import socket
13
16
  import subprocess
14
17
  import sys
15
18
  import tempfile
16
19
  import tarfile
20
+ import threading
17
21
  import time
22
+ import urllib.error
23
+ import urllib.request
18
24
  import uuid
19
25
 
20
26
  import raft_files
21
27
 
22
- __version__ = "0.4.0"
28
+ __version__ = "0.5.0"
23
29
 
24
30
  CONFIG = Path.home() / ".config/raft/incus.json"
31
+ # Controller defaults for new live apart from the location inventory, so location
32
+ # names can never collide with setting names.
33
+ DEFAULTS_CONFIG = Path.home() / ".config/raft/config.json"
25
34
  PROJECT = "raft"
26
- SAVED_BOX_LIMIT = 4
27
35
  LOCATION_NAME = r"[a-zA-Z0-9][a-zA-Z0-9_-]*"
28
36
  GIB = 1024**3
29
- # Supported box sizes. Parser choices, capacity validation and recommendations
30
- # all derive from these lists, so adding a size here covers every command.
31
- CPU_SIZES = [1, 2]
32
- MEMORY_SIZES = ["1GiB", "2GiB", "4GiB", "8GiB"]
37
+ # Boxes use the shared Btrfs pool unless `new --disk` gives them their own disk:
38
+ # a fixed-size thin logical volume in this LVM pool. Deployment always creates the
39
+ # shared pool; the own-disk pool exists only on hosts deployed with --disk-pool.
40
+ SHARED_POOL = "raft-data"
41
+ BOX_DISK_POOL = "raft-disks"
42
+ # Size of a box created from the image without --cpu, --memory or a config.json
43
+ # default. Boxes created from a snapshot keep the snapshot's CPU, RAM and disk
44
+ # unless a flag overrides it; config.json defaults never do.
45
+ DEFAULT_CPU = 1
46
+ DEFAULT_MEMORY = "2GiB"
33
47
  # Box notes follow one template so any agent can tell why a box exists, who uses
34
48
  # it and what happens to it next. Each value is free text; purpose is required.
35
49
  BOX_NOTE_FIELDS = ["purpose", "owner", "next"]
@@ -45,7 +59,6 @@ print(json.dumps({
45
59
  'effectiveCpus': (group / 'cpuset.cpus.effective').read_text().strip(),
46
60
  'filesystemUsedBytes': disk.used,
47
61
  'filesystemAvailableBytes': disk.free,
48
- 'filesystemScope': 'shared Raft pool, not per-box usage',
49
62
  }))
50
63
  """
51
64
 
@@ -71,35 +84,43 @@ def settings(location):
71
84
  return inventory[location]
72
85
 
73
86
 
74
- def edit_configuration(change):
75
- """Apply change(inventory) to the controller configuration and save it."""
87
+ def edit_json(path, load, change):
88
+ """Apply change(document) to a controller JSON file and save it.
89
+
90
+ load() reads and validates the current file. An empty document removes the file.
91
+ """
76
92
  # main() sets umask 077, so a new configuration directory is private.
77
- CONFIG.parent.mkdir(parents=True, exist_ok=True)
93
+ path.parent.mkdir(parents=True, exist_ok=True)
78
94
  # Hold a controller lock across read, edit and replace so concurrent edits
79
95
  # cannot drop each other. Readers need no lock because replacement is atomic.
80
- with (CONFIG.parent / ".incus.json.lock").open("w") as lock:
96
+ with (path.parent / f".{path.name}.lock").open("w") as lock:
81
97
  fcntl.flock(lock, fcntl.LOCK_EX)
82
98
  # Onboarding can start on a controller that has no configuration yet.
83
- inventory = configuration() if CONFIG.exists() else {}
84
- change(inventory)
85
- if not inventory:
86
- # Removing the only location restores the unconfigured state.
87
- CONFIG.unlink(missing_ok=True)
99
+ document = load() if path.exists() else {}
100
+ change(document)
101
+ if not document:
102
+ # Removing the last entry restores the unconfigured state.
103
+ path.unlink(missing_ok=True)
88
104
  return
89
105
  # Key order is preserved because the first location is the default for new.
90
- # Replace atomically so an interrupted write cannot truncate the inventory;
106
+ # Replace atomically so an interrupted write cannot truncate the file;
91
107
  # NamedTemporaryFile creates the staged file with mode 0600.
92
- staged = tempfile.NamedTemporaryFile("w", dir=CONFIG.parent, prefix=".raft-", delete=False)
108
+ staged = tempfile.NamedTemporaryFile("w", dir=path.parent, prefix=".raft-", delete=False)
93
109
  try:
94
110
  with staged:
95
- json.dump(inventory, staged, indent=2)
111
+ json.dump(document, staged, indent=2)
96
112
  staged.write("\n")
97
- os.replace(staged.name, CONFIG)
113
+ os.replace(staged.name, path)
98
114
  finally:
99
115
  # A no-op after a successful replace; removes the file after a failure.
100
116
  Path(staged.name).unlink(missing_ok=True)
101
117
 
102
118
 
119
+ def edit_configuration(change):
120
+ """Apply change(inventory) to the location inventory and save it."""
121
+ edit_json(CONFIG, configuration, change)
122
+
123
+
103
124
  def set_note(location, text):
104
125
  """Record operator guidance for a location. Raft shows notes but never enforces them."""
105
126
  if text is not None and not text.strip():
@@ -136,6 +157,139 @@ def read_box_note(box):
136
157
  return json.loads(stored) if stored else None
137
158
 
138
159
 
160
+ def cpu_count(value):
161
+ """Parse --cpu: any positive whole number of CPUs."""
162
+ if not re.fullmatch(r"[1-9][0-9]*", value):
163
+ raise argparse.ArgumentTypeError("CPU count must be a positive whole number")
164
+ return int(value)
165
+
166
+
167
+ def gib_size(value):
168
+ """Parse --memory, --disk and --disk-pool. Whole GiB like 16GiB is the one accepted form."""
169
+ if not re.fullmatch(r"[1-9][0-9]*GiB", value):
170
+ raise argparse.ArgumentTypeError("Use a whole number of GiB, e.g. 16GiB")
171
+ return value
172
+
173
+
174
+ def disk_size(value):
175
+ """Parse new --disk: an own-disk size like 20GiB, or shared for the shared pool."""
176
+ return value if value == "shared" else gib_size(value)
177
+
178
+
179
+ def gib(size):
180
+ return int(size.removesuffix("GiB"))
181
+
182
+
183
+ def box_disk(box):
184
+ """Return a box's own disk size, e.g. 20GiB, or "shared" for the shared pool."""
185
+ root = box["expanded_devices"].get("root", {})
186
+ return root.get("size", "") if root.get("pool") == BOX_DISK_POOL else "shared"
187
+
188
+
189
+ # Box note policy. A host can require note fields for every box it holds; the
190
+ # policy lives on the host, in the Incus project key user.raft.require (for
191
+ # example "purpose,owner"), so every controller reads the same rule. Each
192
+ # command that creates a box or changes its note checks the policy twice: once
193
+ # from the controller for an actionable message before any work, and again in
194
+ # the host transaction that writes the note, under the host lock. This is a
195
+ # guardrail for agents: SSH and sudo on the host bypass it, and controllers
196
+ # older than this check never run it.
197
+ POLICY_KEY = "user.raft.require"
198
+ POLICY_CHANGED_KEY = "user.raft.require.changed"
199
+
200
+ # Host half of the check, run with the required fields and the note the box
201
+ # will carry. Kept short; the controller builds the detailed message.
202
+ CHECK_NOTE_CODE = """import json, sys
203
+ required = [field for field in sys.argv[1].split(",") if field]
204
+ note = json.loads(sys.argv[2]) if sys.argv[2] else {}
205
+ missing = [field for field in required if not note.get(field)]
206
+ if missing:
207
+ raise SystemExit(
208
+ "This host requires box note fields " + ", ".join(required)
209
+ + " (Incus project key user.raft.require); missing " + ", ".join(missing)
210
+ + ". Nothing was changed. Give the note with "
211
+ + " ".join("--" + field for field in required) + " and retry"
212
+ )
213
+ """
214
+ # Read the policy with errexit active: inside a command substitution used as an
215
+ # argument, a failed read would pass an empty policy and let every note through.
216
+ READ_NOTE_POLICY = f"require=$(incus project get raft {POLICY_KEY})\n"
217
+ # Callers set $note to the exact value they write to user.raft.note.
218
+ CHECK_NOTE = "python3 -c " + shlex.quote(CHECK_NOTE_CODE) + ' "$require" "$note"'
219
+
220
+
221
+ def note_policy(location):
222
+ """Return a location's required note fields and its last recorded policy change."""
223
+ # `incus query` rejects the --project flag that remote() always passes.
224
+ projects = json.loads(incus(location, "project", "list", "--format", "json"))
225
+ project = next(item for item in projects if item["name"] == PROJECT)
226
+ stored = project["config"].get(POLICY_KEY, "")
227
+ required = [field for field in stored.split(",") if field]
228
+ unknown = [field for field in required if field not in BOX_NOTE_FIELDS]
229
+ if unknown:
230
+ raise ValueError(
231
+ f"{location}: host policy {POLICY_KEY}={stored!r} names unknown note fields; "
232
+ f"fix it with raft policy {location} --require FIELDS or --clear"
233
+ )
234
+ return {"require": required, "changed": project["config"].get(POLICY_CHANGED_KEY, "")}
235
+
236
+
237
+ def note_problem(location, required, note, command, hint=""):
238
+ """Describe how note falls short of the required fields, or return None."""
239
+ missing = [field for field in required if not (note or {}).get(field)]
240
+ if not missing:
241
+ return None
242
+ flags = " and ".join("--" + field for field in missing)
243
+ usage = " ".join(
244
+ f'--{field} "..."' if field in required else f'[--{field} "..."]'
245
+ for field in BOX_NOTE_FIELDS
246
+ )
247
+ return (
248
+ f"location {location} requires {flags} (host policy).{hint} "
249
+ f"Add {'it' if len(missing) == 1 else 'them'}: raft {command} {usage}"
250
+ )
251
+
252
+
253
+ def require_note(location, note, command, hint=""):
254
+ """Fail before any host change when note lacks a field the location requires."""
255
+ problem = note_problem(location, note_policy(location)["require"], note, command, hint)
256
+ if problem:
257
+ raise ValueError(problem)
258
+
259
+
260
+ def note_fields(text):
261
+ """Parse --require FIELDS into BOX_NOTE_FIELDS order."""
262
+ fields = [field.strip() for field in text.split(",") if field.strip()]
263
+ unknown = [field for field in fields if field not in BOX_NOTE_FIELDS]
264
+ if unknown or not fields:
265
+ raise ValueError(
266
+ "Give note fields from " + ", ".join(BOX_NOTE_FIELDS) + ", e.g. --require purpose,owner"
267
+ )
268
+ if "purpose" not in fields:
269
+ # Every box note has a purpose, so a policy without it would be misleading.
270
+ raise ValueError("Box notes always need purpose; include it, e.g. --require purpose,owner")
271
+ return [field for field in BOX_NOTE_FIELDS if field in fields]
272
+
273
+
274
+ def set_policy(location, fields):
275
+ """Set (fields) or clear (None) a location's note policy and record the change on the host."""
276
+ settings(location)
277
+ value = ",".join(fields or [])
278
+ # Who and when is recorded on the host beside the policy and in its journal.
279
+ # The controller identity is self-reported; SUDO_USER is the SSH account.
280
+ before = transaction(
281
+ location,
282
+ f"before=$(incus project get raft {POLICY_KEY})\n"
283
+ 'changed="$(date -u +%Y-%m-%dT%H:%M:%SZ) by ${SUDO_USER:-root} from $2"\n'
284
+ f'incus project set raft {POLICY_KEY}="$1" {POLICY_CHANGED_KEY}="$changed"\n'
285
+ "logger -t raft \"Box note policy: required fields '$before' -> '$1' ($changed)\"\n"
286
+ "printf '%s\\n' \"$before\"",
287
+ value,
288
+ f"{getpass.getuser()}@{socket.gethostname()}",
289
+ ).strip()
290
+ print(f"{location}: required note fields {before or 'none'} -> {value or 'none'}")
291
+
292
+
139
293
  def receive(command, destination, *, stdin=None):
140
294
  """Write streamed bytes in Python: SSH can exit zero after a local write error."""
141
295
  with tempfile.TemporaryFile() as errors:
@@ -202,7 +356,7 @@ if sudo -n incus project list --format csv -c n 2>/dev/null | grep -qx raft; the
202
356
  """
203
357
 
204
358
 
205
- def host_problems(facts):
359
+ def host_problems(facts, disk_pool=None):
206
360
  """Return reasons a probed SSH target cannot become a Raft host; empty means supported."""
207
361
  kernel = facts.get("kernel") or "an unknown system"
208
362
  if kernel != "Linux":
@@ -228,10 +382,17 @@ def host_problems(facts):
228
382
  memory = int(facts.get("memory") or 0) * 1024
229
383
  if memory < 7.5 * GIB:
230
384
  problems.append(f"Raft hosts need at least 8 GiB RAM; found {memory / GIB:.1f} GiB")
231
- # Only a fresh deployment creates the 60 GiB pool; deploy.py enforces the same limit.
385
+ # Only a fresh deployment creates the 60 GiB shared pool, and the own-disk
386
+ # pool when --disk-pool asks for it; deploy.py enforces the same minimum.
232
387
  disk = int(facts.get("disk") or 0) * 1024
233
- if facts.get("deployed") != "yes" and disk < 80 * GIB:
234
- problems.append(f"Fresh hosts need at least 80 GiB free disk; found {disk / GIB:.1f} GiB")
388
+ needed = 80 + (gib(disk_pool) if disk_pool else 0)
389
+ if facts.get("deployed") != "yes" and disk < needed * GIB:
390
+ problems.append(
391
+ f"Fresh hosts need at least {needed} GiB free disk"
392
+ + (f" (80 GiB plus the {disk_pool} own-disk pool)" if disk_pool else "")
393
+ + f"; found {disk / GIB:.1f} GiB"
394
+ + (". A smaller --disk-pool needs less" if disk_pool else "")
395
+ )
235
396
  return problems
236
397
 
237
398
 
@@ -253,7 +414,18 @@ def smoke_test(location):
253
414
  """Create a disposable box, wait until it runs a command, then delete it."""
254
415
  handle = new(
255
416
  argparse.Namespace(
256
- location=location, ttl=600, cpu=1, memory="2GiB", disposable=True, template=None
417
+ location=location,
418
+ ttl=600,
419
+ cpu=1,
420
+ memory="2GiB",
421
+ # Explicit, so a configured disk default never moves the smoke box off the shared pool.
422
+ disk="shared",
423
+ disposable=True,
424
+ template=None,
425
+ # A full note satisfies any note policy left on a reused deployment.
426
+ purpose="raft host add smoke test",
427
+ owner="raft host add",
428
+ next="Deleted by the smoke test",
257
429
  )
258
430
  )
259
431
  print("Smoke test box: " + handle, flush=True)
@@ -301,7 +473,7 @@ def host_add(args):
301
473
  f"Verify the host key interactively, then check `ssh -o BatchMode=yes {args.ssh} true`"
302
474
  )
303
475
  facts = dict(line.split("=", 1) for line in probe.stdout.splitlines() if "=" in line)
304
- problems = host_problems(facts)
476
+ problems = host_problems(facts, args.disk_pool)
305
477
  if problems:
306
478
  raise ValueError(
307
479
  f"{args.ssh} cannot host Raft boxes; nothing was changed:\n- " + "\n- ".join(problems)
@@ -316,13 +488,20 @@ def host_add(args):
316
488
  print(
317
489
  f"Plan for {args.location} ({args.ssh}: Ubuntu {facts['version']} {architecture}, "
318
490
  f"{facts['cpus']} CPUs, {int(facts['memory']) * 1024 / GIB:.1f} GiB RAM)\n"
319
- "- install Incus 6.0 LTS from the Zabbly apt repository, btrfs-progs and nftables if missing\n"
491
+ "- install Incus 6.0 LTS from the Zabbly apt repository, btrfs-progs, lvm2 and nftables "
492
+ "if missing\n"
320
493
  + (
321
494
  "- reuse and verify the existing raft project, raft-data pool and rfbr0 bridge\n"
322
495
  if facts["deployed"] == "yes"
323
496
  else "- create Incus project raft, 60 GiB Btrfs pool raft-data and bridge rfbr0 "
324
497
  "(10.232.0.1/24 with NAT)\n"
325
498
  )
499
+ + (
500
+ f"- create the {args.disk_pool} thin LVM pool raft-disks for own box disks if it is "
501
+ "missing, and allocate its file on the host disk in full\n"
502
+ if args.disk_pool
503
+ else ""
504
+ )
326
505
  + "- install raft-network.service (rfbr0 isolation rules) and raft-expire.timer\n"
327
506
  f"- {image}; images are checked by SHA-256 and architecture\n"
328
507
  f"- add {args.location} with the image fingerprint to {CONFIG}\n"
@@ -338,7 +517,7 @@ def host_add(args):
338
517
  raise ValueError("Review the plan, then rerun with --yes to apply it")
339
518
  if input("Apply this plan? [y/N] ").strip().lower() not in {"y", "yes"}:
340
519
  raise ValueError("Onboarding cancelled; nothing was changed")
341
- deployer.deploy(args.location, args.ssh)
520
+ deployer.deploy(args.location, args.ssh, args.disk_pool)
342
521
  if args.build:
343
522
  # A fresh alias avoids refusing to build beside an earlier raft-dev image.
344
523
  alias = "raft-dev-" + uuid.uuid4().hex[:8]
@@ -392,11 +571,6 @@ def inventory(location):
392
571
  ]
393
572
 
394
573
 
395
- ADMISSION = f"""count=$(incus --project raft list --fast --format json | python3 -c 'import json,sys; print(sum(x["config"].get("user.raft")=="true" for x in json.load(sys.stdin)))')
396
- test "$count" -lt {SAVED_BOX_LIMIT} || {{ echo 'Location already holds four boxes' >&2; exit 1; }}
397
- """
398
-
399
-
400
574
  REQUIRE_STOPPED = """state=$(incus --project raft list "$1" --fast --format json | python3 -c 'import json,sys; print(next(x["status"] for x in json.load(sys.stdin) if x["name"] == sys.argv[1] and x["config"].get("user.raft") == "true"))' "$1")
401
575
  test "$state" = Stopped || { echo 'Stop the source box before resume, snapshot, restore, fork or backup' >&2; exit 1; }
402
576
  """
@@ -414,92 +588,250 @@ CHECK_WORKER = (
414
588
  )
415
589
 
416
590
 
417
- HOST_RESOURCES = """import json, os, shutil, subprocess
591
+ # Host-side accounting for the own-disk pool, shared by capacity reports and the
592
+ # reservation check. It runs as root on the host, where python3 and lvs exist.
593
+ #
594
+ # raft-disks is a thin LVM pool, so forks, templates and snapshots of own-disk
595
+ # boxes are thin snapshots that share blocks instead of copies. Thin volumes can
596
+ # promise more space than the pool holds, and a full thin pool fails every volume
597
+ # in it at once. Raft never lets that happen: a thin volume can never hold more
598
+ # data than its virtual size, so while the virtual sizes of all thin volumes in
599
+ # the pool (boxes, their snapshots, the cached image and anything else) add up to
600
+ # no more than the pool's data space, the pool cannot fill. Every Raft operation
601
+ # that adds a thin volume to raft-disks checks that sum first, under the host lock.
602
+ BOX_DISK_ACCOUNTING = (
603
+ "POOL = "
604
+ + repr(BOX_DISK_POOL)
605
+ + """
606
+ import json, subprocess, sys
607
+ GIB = 1024**3
608
+
609
+ def query(path):
610
+ return json.loads(subprocess.run(['incus', 'query', path], check=True, capture_output=True).stdout)
611
+
612
+ def size_bytes(value):
613
+ # Raft sizes own disks in whole GiB; anything else came from a native Incus edit.
614
+ if not value.endswith('GiB') or not value[:-3].isdigit():
615
+ sys.exit(f'Unexpected own-disk size {value!r}; inspect it with incus')
616
+ return int(value[:-3]) * GIB
617
+
618
+ def box_disk_pool():
619
+ # Own box disks are opt-in per host, so a missing pool is a normal state.
620
+ if '/1.0/storage-pools/' + POOL not in query('/1.0/storage-pools'):
621
+ return None
622
+ config = query('/1.0/storage-pools/' + POOL)['config']
623
+ thinpool = config['lvm.thinpool_name']
624
+ columns = 'lv_name,lv_size,pool_lv,data_percent,metadata_percent'
625
+ report = subprocess.run(['lvs', '--reportformat', 'json', '--units', 'b', '--nosuffix', '-o', columns, config['lvm.vg_name']], check=True, capture_output=True)
626
+ volumes = json.loads(report.stdout)['report'][0]['lv']
627
+ pool = next(volume for volume in volumes if volume['lv_name'] == thinpool)
628
+ # LVM reports usage only while the thin pool is active; reservations never need it.
629
+ active = pool['data_percent'] != ''
630
+ total = int(pool['lv_size'])
631
+ return {
632
+ 'totalBytes': total,
633
+ 'reservedBytes': sum(int(volume['lv_size']) for volume in volumes if volume['pool_lv'] == thinpool),
634
+ 'usedBytes': int(total * float(pool['data_percent']) / 100) if active else None,
635
+ 'metadataUsedPercent': float(pool['metadata_percent']) if active else None,
636
+ # Incus caches the image as a thin volume of the pool's volume.size, or
637
+ # its 10 GiB default, and starts each own-disk box as a snapshot of it.
638
+ 'imageVolumeBytes': size_bytes(config.get('volume.size', '10GiB')),
639
+ }
640
+
641
+ def reserve(location, pool, added, origin=''):
642
+ # Exit with the reason unless thin volumes of the added sizes fit in full.
643
+ # origin, when given, is a sentence saying where the requested size came from.
644
+ suffix = '. ' + origin if origin else ''
645
+ if added and pool is None:
646
+ sys.exit(
647
+ f'{location} has no {POOL} pool, so own box disks are not enabled there; nothing was '
648
+ f'created. Enable them with python3 deploy/deploy.py --location {location} '
649
+ '--disk-pool SIZE' + suffix
650
+ )
651
+ if added and pool['reservedBytes'] + sum(added) > pool['totalBytes']:
652
+ sys.exit(
653
+ f'{location}: own box disks would need {sum(added) / GIB:.1f} GiB more of {POOL}, '
654
+ f'which has {(pool["totalBytes"] - pool["reservedBytes"]) / GIB:.1f} of '
655
+ f'{pool["totalBytes"] / GIB:.1f} GiB unreserved; nothing was created. Destroy '
656
+ 'own-disk boxes or snapshots you no longer need, or use a smaller disk or the '
657
+ 'shared pool' + suffix
658
+ )
659
+ """
660
+ )
661
+
662
+ HOST_RESOURCES = (
663
+ BOX_DISK_ACCOUNTING
664
+ + """
665
+ import os, shutil
418
666
  from pathlib import Path
419
667
  memory = dict(line.split()[:2] for line in Path('/proc/meminfo').read_text().splitlines())
420
668
  disk = shutil.disk_usage('/var/lib/incus')
421
- pool = json.loads(subprocess.check_output(['incus', 'query', '/1.0/storage-pools/raft-data/resources']))
669
+ space = query('/1.0/storage-pools/raft-data/resources')['space']
670
+ own = box_disk_pool()
671
+ if own is not None:
672
+ del own['imageVolumeBytes']
422
673
  print(json.dumps({
423
674
  'cpus': len(os.sched_getaffinity(0)),
424
675
  'memoryTotalBytes': int(memory['MemTotal:']) * 1024,
425
676
  'memoryAvailableBytes': int(memory['MemAvailable:']) * 1024,
426
677
  'hostDiskFreeBytes': disk.free,
427
- 'poolTotalBytes': pool['space']['total'],
428
- 'poolFreeBytes': pool['space']['total'] - pool['space']['used'],
678
+ 'sharedPool': {'totalBytes': space['total'], 'freeBytes': space['total'] - space['used']},
679
+ 'boxDiskPool': own,
429
680
  }))
430
681
  """
682
+ )
683
+
684
+ # Reservation check run inside lifecycle transactions before Incus adds a thin
685
+ # volume to raft-disks. Arguments: location (for messages), the requested own-disk
686
+ # size in GiB (0 for none), the source: image:FINGERPRINT, BOX or BOX/SNAPSHOT,
687
+ # and the origin sentence for refusals (empty when the size is a flag).
688
+ # A new volume starts as a thin snapshot of its source and is then resized to the
689
+ # requested size, so it needs the larger of the two. Copies of shared-pool boxes
690
+ # without --disk never touch raft-disks and need nothing.
691
+ RESERVE_BOX_DISK = (
692
+ BOX_DISK_ACCOUNTING
693
+ + """
694
+ location, requested, source, origin = sys.argv[1], int(sys.argv[2]) * GIB, sys.argv[3], sys.argv[4]
695
+ pool = box_disk_pool()
696
+ added = []
697
+ if source.startswith('image:'):
698
+ if requested:
699
+ image = pool['imageVolumeBytes'] if pool else 0
700
+ added.append(max(requested, image))
701
+ # Incus caches the image in the pool the first time an own-disk box needs it.
702
+ volume = f'/1.0/storage-pools/{POOL}/volumes/image/{source[6:]}'
703
+ if pool and subprocess.run(['incus', 'query', volume], capture_output=True).returncode:
704
+ added.append(image)
705
+ else:
706
+ path = '/1.0/instances/' + source.replace('/', '/snapshots/') + '?project=raft'
707
+ root = query(path)['expanded_devices']['root']
708
+ if root.get('pool') == POOL:
709
+ added.append(max(requested, size_bytes(root['size'])))
710
+ elif requested:
711
+ added.append(requested)
712
+ reserve(location, pool, added, origin)
713
+ """
714
+ )
715
+
716
+ # Prints the pool `raft recover` imports into, from backup/index.yaml, the first
717
+ # entry of every Incus export. Own-disk backups return to raft-disks with their
718
+ # recorded size after the same reservation check: Incus restores the box and each
719
+ # of its snapshots as a thin volume of that size. Other backups use the shared pool.
720
+ RECOVERY_POOL = (
721
+ BOX_DISK_ACCOUNTING
722
+ + "SHARED_POOL = "
723
+ + repr(SHARED_POOL)
724
+ + """
725
+ import tarfile, yaml
726
+ archive, location = sys.argv[1], sys.argv[2]
727
+ with tarfile.open(archive, 'r|gz') as backup:
728
+ entry = backup.next()
729
+ if entry is None or entry.name != 'backup/index.yaml':
730
+ sys.exit('Not an Incus instance backup: backup/index.yaml is not its first entry')
731
+ index = yaml.safe_load(backup.extractfile(entry))
732
+ if index.get('pool') != POOL:
733
+ print(SHARED_POOL)
734
+ sys.exit()
735
+ size = size_bytes(index['config']['container']['expanded_devices']['root']['size'])
736
+ reserve(location, box_disk_pool(), [size] * (1 + len(index.get('snapshots') or [])))
737
+ print(POOL)
738
+ """
739
+ )
740
+
741
+
742
+ def reserve_box_disk(location, disk, source, origin=""):
743
+ """Shell line that refuses an own-disk volume raft-disks could not hold in full."""
744
+ size = str(gib(disk) if disk else 0)
745
+ return shlex.join(["python3", "-c", RESERVE_BOX_DISK, location, size, source, origin]) + "\n"
746
+
747
+
748
+ # Agents read capacity output to plan work. Earlier wording that looked like a cap
749
+ # made them skip boxes they needed, so every report leads with this sentence.
750
+ ADVISORY = (
751
+ "Advisory estimate only. Raft enforces no box count or size budget; "
752
+ "create the boxes your task needs."
753
+ )
431
754
 
432
755
 
433
756
  def capacity(host, boxes):
434
- """Conservative allocation advice, not an exclusive CPU or memory reservation."""
435
- gib = 1024**3
757
+ """Advisory planning numbers, not a reservation, admission rule or limit."""
436
758
  active = [box for box in boxes if box["status"] != "Stopped"]
437
759
  cpu_allocated = 0
438
760
  memory_allocated = 0
439
761
  for box in active:
440
762
  limits = box["expanded_config"]
441
- if (
442
- limits["limits.cpu"] not in {str(cpu) for cpu in CPU_SIZES}
443
- or limits["limits.memory"] not in MEMORY_SIZES
763
+ # Raft writes whole CPU counts and whole GiB; other forms come from native Incus edits.
764
+ if not re.fullmatch(r"[1-9][0-9]*", limits["limits.cpu"]) or not re.fullmatch(
765
+ r"[1-9][0-9]*GiB", limits["limits.memory"]
444
766
  ):
445
- raise ValueError("Active box has unsupported sizing; inspect its Incus configuration")
767
+ raise ValueError(
768
+ f"Active box {box['name']} has sizing Raft did not set; inspect its Incus configuration"
769
+ )
446
770
  cpu_allocated += int(limits["limits.cpu"])
447
- memory_allocated += int(limits["limits.memory"].removesuffix("GiB")) * gib
448
- memory_reserve = max(2 * gib, host["memoryTotalBytes"] // 10)
771
+ memory_allocated += gib(limits["limits.memory"]) * GIB
772
+ memory_reserve = max(2 * GIB, host["memoryTotalBytes"] // 10)
449
773
  cpu_budget = max(0, host["cpus"] - 1)
450
774
  memory_budget = max(0, host["memoryTotalBytes"] - memory_reserve)
451
- cpu_remaining = max(0, cpu_budget - cpu_allocated)
452
- memory_remaining = max(
775
+ cpu_headroom = max(0, cpu_budget - cpu_allocated)
776
+ memory_headroom = max(
453
777
  0,
454
778
  min(
455
779
  memory_budget - memory_allocated,
456
780
  host["memoryAvailableBytes"] - memory_reserve,
457
781
  ),
458
782
  )
459
- slots = max(0, SAVED_BOX_LIMIT - len(boxes))
460
- recommendations = []
461
- for cpu in CPU_SIZES:
462
- for memory in [int(size.removesuffix("GiB")) for size in MEMORY_SIZES]:
463
- total = min(SAVED_BOX_LIMIT, cpu_budget // cpu, memory_budget // (memory * gib))
464
- additional = min(
465
- max(0, SAVED_BOX_LIMIT - len(active)),
466
- cpu_remaining // cpu,
467
- memory_remaining // (memory * gib),
468
- )
469
- recommendations.append(
470
- {
471
- "cpu": cpu,
472
- "memoryGiB": memory,
473
- "totalRunning": total,
474
- "additionalRunning": additional,
475
- "newBoxes": min(slots, additional),
476
- }
477
- )
783
+ # Example shapes double from the 1 CPU / 2 GiB default at 2 GiB per CPU and
784
+ # stop at the largest that fits an empty host. Boxes may use any other size;
785
+ # headroom lets callers work out counts for those.
786
+ estimates = []
787
+ cpu = 1
788
+ while cpu == 1 or (cpu <= cpu_budget and 2 * cpu * GIB <= memory_budget):
789
+ memory = 2 * cpu * GIB
790
+ estimates.append(
791
+ {
792
+ "cpu": cpu,
793
+ "memoryGiB": 2 * cpu,
794
+ "fitEmptyHost": min(cpu_budget // cpu, memory_budget // memory),
795
+ "fitNow": min(cpu_headroom // cpu, memory_headroom // memory),
796
+ }
797
+ )
798
+ cpu *= 2
478
799
  warnings = []
479
800
  if cpu_allocated > cpu_budget or memory_allocated > memory_budget:
480
- warnings.append("Active configured limits exceed the recommended host budget")
801
+ warnings.append(
802
+ "Active boxes may use more CPU or RAM than the host budget; under load they share it"
803
+ )
481
804
  if host["memoryAvailableBytes"] < memory_reserve:
482
805
  warnings.append(
483
- "Available host RAM is below the reserve; stop boxes or reduce other workloads"
806
+ "Available host RAM is below the reserve; stop idle boxes or reduce other workloads"
484
807
  )
485
- if host["hostDiskFreeBytes"] < 5 * gib or host["poolFreeBytes"] < 5 * gib:
808
+ if host["hostDiskFreeBytes"] < 5 * GIB or host["sharedPool"]["freeBytes"] < 5 * GIB:
486
809
  warnings.append(
487
810
  "Host or shared-pool free disk is below 5 GiB; check image and workspace growth"
488
811
  )
812
+ # LVM sizes thin pool metadata for every data block mapped, and reservations
813
+ # keep mappings within that, so this should never fire. It stays because full
814
+ # metadata stops every own-disk box and needs offline repair.
815
+ own = host["boxDiskPool"]
816
+ if own is not None and (own["metadataUsedPercent"] or 0) >= 80:
817
+ warnings.append(
818
+ f"{BOX_DISK_POOL} thin pool metadata is {own['metadataUsedPercent']:.0f}% full; "
819
+ "inspect it with lvs before creating more own-disk boxes"
820
+ )
489
821
  return {
822
+ "notice": ADVISORY,
490
823
  "host": host,
491
- "savedBoxLimit": SAVED_BOX_LIMIT,
492
- "savedBoxes": len(boxes),
824
+ "boxes": len(boxes),
493
825
  "activeBoxes": len(active),
494
- "savedSlots": slots,
495
826
  "allocated": {"cpus": cpu_allocated, "memoryBytes": memory_allocated},
496
827
  "reserve": {"cpus": 1, "memoryBytes": memory_reserve},
497
- "recommendations": recommendations,
828
+ "headroom": {"cpus": cpu_headroom, "memoryBytes": memory_headroom},
829
+ "estimates": estimates,
498
830
  "warnings": warnings,
499
831
  }
500
832
 
501
833
 
502
- def host_limits(location):
834
+ def host_capacity(location):
503
835
  response = subprocess.run(
504
836
  [
505
837
  "ssh",
@@ -522,36 +854,47 @@ def host_limits(location):
522
854
  }
523
855
 
524
856
 
525
- def print_limits(report):
526
- gib = 1024**3
857
+ def print_capacity(report):
527
858
  host = report["host"]
859
+ print(report["notice"])
528
860
  print(
529
861
  f"{report['location']}: {host['cpus']} CPUs, "
530
- f"{host['memoryTotalBytes'] / gib:.1f} GiB RAM "
531
- f"({host['memoryAvailableBytes'] / gib:.1f} GiB available)"
862
+ f"{host['memoryTotalBytes'] / GIB:.1f} GiB RAM "
863
+ f"({host['memoryAvailableBytes'] / GIB:.1f} GiB available)"
532
864
  )
533
865
  if report["note"]:
534
866
  print("Note: " + report["note"])
867
+ print(f"Boxes: {report['boxes']} saved, {report['activeBoxes']} active")
535
868
  print(
536
- f"Saved boxes: {report['savedBoxes']}/{report['savedBoxLimit']} enforced; "
537
- f"active: {report['activeBoxes']}; free saved slots: {report['savedSlots']}"
538
- )
539
- print(
540
- f"Active limits: {report['allocated']['cpus']} CPUs, "
541
- f"{report['allocated']['memoryBytes'] / gib:.1f} GiB; "
542
- f"host reserve: 1 CPU, {report['reserve']['memoryBytes'] / gib:.1f} GiB"
869
+ f"Allocated to active boxes: {report['allocated']['cpus']} CPUs, "
870
+ f"{report['allocated']['memoryBytes'] / GIB:.1f} GiB; "
871
+ f"host reserve: 1 CPU, {report['reserve']['memoryBytes'] / GIB:.1f} GiB; "
872
+ f"headroom: {report['headroom']['cpus']} CPUs, "
873
+ f"{report['headroom']['memoryBytes'] / GIB:.1f} GiB"
543
874
  )
544
875
  print(
545
- f"Disk free: {host['poolFreeBytes'] / gib:.1f} GiB shared pool, "
546
- f"{host['hostDiskFreeBytes'] / gib:.1f} GiB host"
876
+ f"Disk free: {host['sharedPool']['freeBytes'] / GIB:.1f} of "
877
+ f"{host['sharedPool']['totalBytes'] / GIB:.1f} GiB shared pool, "
878
+ f"{host['hostDiskFreeBytes'] / GIB:.1f} GiB host"
547
879
  )
548
- print("CPU RAM GiB Total running More running New boxes")
549
- for size in report["recommendations"]:
880
+ own = host["boxDiskPool"]
881
+ if own is None:
882
+ print("Own box disks: not enabled (redeploy with --disk-pool SIZE)")
883
+ else:
884
+ print(
885
+ f"Own box disks: {(own['totalBytes'] - own['reservedBytes']) / GIB:.1f} of "
886
+ f"{own['totalBytes'] / GIB:.1f} GiB unreserved"
887
+ )
888
+ print("CPU RAM GiB Fit on empty host Fit now")
889
+ for size in report["estimates"]:
550
890
  print(
551
- f"{size['cpu']:>3} {size['memoryGiB']:>7} {size['totalRunning']:>13} "
552
- f"{size['additionalRunning']:>12} {size['newBoxes']:>9}"
891
+ f"{size['cpu']:>3} {size['memoryGiB']:>7} {size['fitEmptyHost']:>17} "
892
+ f"{size['fitNow']:>7}"
553
893
  )
554
- print("Advisory CPU/RAM counts; total assumes an empty host. Disk growth is not included.")
894
+ print(
895
+ "Counts assume every box uses its full CPU and RAM at once. Boxes share host CPUs, "
896
+ "so more can run when they are not all busy. Any whole CPU count and GiB size works."
897
+ )
555
898
  for warning in report["warnings"]:
556
899
  print("Warning: " + warning)
557
900
 
@@ -578,7 +921,12 @@ def doctor(location):
578
921
  check=True,
579
922
  )
580
923
  print(response.stdout.strip())
581
- print(incus(location, "storage", "info", "raft-data").strip())
924
+ print(incus(location, "storage", "info", SHARED_POOL).strip())
925
+ # Own box disks are opt-in, so a host without their pool is healthy.
926
+ if BOX_DISK_POOL in incus(location, "storage", "list", "--format", "csv", "-c", "n").split():
927
+ print(incus(location, "storage", "info", BOX_DISK_POOL).strip())
928
+ else:
929
+ print("own box disks: not enabled (redeploy with --disk-pool SIZE)")
582
930
  print(f"{len(inventory(location))} saved boxes; system containers (shared host kernel)")
583
931
 
584
932
 
@@ -629,8 +977,52 @@ def sizing(args):
629
977
  return values
630
978
 
631
979
 
980
+ def fits_host(args):
981
+ """Shell checks that refuse a CPU or RAM limit larger than the whole host.
982
+
983
+ Incus accepts such limits and quietly gives the box less, so an agent would
984
+ plan around resources it never gets. MemTotal is rounded up to whole GiB:
985
+ an 8 GiB machine reports about 7.7 GiB and must still accept 8GiB. The
986
+ parsers already restrict both values to whole numbers, so they embed safely.
987
+ """
988
+ checks = ""
989
+ if args.cpu is not None:
990
+ checks += (
991
+ f'test {args.cpu} -le "$(nproc)" || '
992
+ f'{{ echo "Requested {args.cpu} CPUs, but this host has $(nproc)" >&2; exit 1; }}\n'
993
+ )
994
+ if args.memory is not None:
995
+ checks += (
996
+ "total=$(awk '/^MemTotal:/ {print int(($2 + 1048575) / 1048576)}' /proc/meminfo)\n"
997
+ f'test {gib(args.memory)} -le "$total" || '
998
+ f'{{ echo "Requested {args.memory} RAM, but this host has $total GiB" >&2; exit 1; }}\n'
999
+ )
1000
+ return checks
1001
+
1002
+
1003
+ def own_disk(size):
1004
+ """Incus device overrides that give a box its own fixed-size disk in raft-disks.
1005
+
1006
+ The full root device is spelled out because `incus copy` of a snapshot whose
1007
+ root comes from the profile adds an override as a whole new device.
1008
+ """
1009
+ return [
1010
+ "-d",
1011
+ "root,type=disk",
1012
+ "-d",
1013
+ "root,path=/",
1014
+ "-d",
1015
+ f"root,pool={BOX_DISK_POOL}",
1016
+ "-d",
1017
+ f"root,size={size}",
1018
+ ]
1019
+
1020
+
632
1021
  def new(args):
633
1022
  box_fields = box_note(args)
1023
+ # Sizes are filled here only for boxes from the image; a box from a snapshot
1024
+ # keeps the snapshot's CPU, RAM and disk unless a flag overrides them.
1025
+ from_config = apply_new_defaults(args)
634
1026
  source = None
635
1027
  if args.template:
636
1028
  handle, separator, snapshot = args.template.partition("/")
@@ -643,8 +1035,6 @@ def new(args):
643
1035
  args.location = location
644
1036
  instance(location, original)
645
1037
  source = original + "/" + snapshot
646
- if args.location is None:
647
- args.location = next(iter(configuration()))
648
1038
  if source is None:
649
1039
  source = settings(args.location).get("image", "")
650
1040
  if not isinstance(source, str) or not re.fullmatch(r"[a-f0-9]{64}", source):
@@ -655,18 +1045,37 @@ def new(args):
655
1045
  if note:
656
1046
  # Stderr keeps stdout limited to the handle for scripts.
657
1047
  print(f"{args.location} note: {note}", file=sys.stderr)
1048
+ require_note(args.location, box_fields, "new")
658
1049
  name = "rf-" + uuid.uuid4().hex[:16]
659
1050
  # Initialize before starting, so a failed start still has its intended policy.
660
1051
  transaction(
661
1052
  args.location,
662
1053
  (CHECK_WORKER if args.disposable else "")
663
- + ADMISSION
664
- + 'lifetime="$1"; name="$2"; shift 2; "$@"; '
1054
+ + fits_host(args)
1055
+ # Plain boxes from the image stay off raft-disks and skip the check.
1056
+ + (
1057
+ reserve_box_disk(
1058
+ args.location,
1059
+ args.disk,
1060
+ source if args.template else "image:" + source,
1061
+ # A refusal of a configured disk says where the size came from.
1062
+ disk_origin(args.disk) if "disk" in from_config else "",
1063
+ )
1064
+ if args.disk or args.template
1065
+ else ""
1066
+ )
1067
+ + 'lifetime="$1"; name="$2"; note="$3"; shift 3\n'
1068
+ + READ_NOTE_POLICY
1069
+ + CHECK_NOTE
1070
+ # Always set the note, so a box created from a snapshot never inherits the
1071
+ # source note. The host checks the same value it writes.
1072
+ + '\n"$@" -c user.raft.note="$note"; '
665
1073
  'incus --project raft start "$name"; '
666
1074
  'incus --project raft config set "$name" '
667
1075
  'user.raft.expires="$(( $(date +%s) + lifetime ))"',
668
1076
  str(args.ttl),
669
1077
  name,
1078
+ json.dumps(box_fields) if box_fields else "",
670
1079
  "incus",
671
1080
  "--project",
672
1081
  PROJECT,
@@ -682,11 +1091,12 @@ def new(args):
682
1091
  "-c",
683
1092
  f"user.raft.disposable={'true' if args.disposable else 'false'}",
684
1093
  "-c",
685
- # Always set, so a box created from a snapshot never inherits the source note.
686
- "user.raft.note=" + (json.dumps(box_fields) if box_fields else ""),
687
- "-c",
688
1094
  "volatile.eth0.hwaddr=",
689
1095
  *sizing(args),
1096
+ # Without --disk, init uses the profile's shared pool and copy keeps the
1097
+ # snapshot's disk: the shared pool or an own disk of the same size. Copies
1098
+ # within either pool are snapshots that share the source's blocks.
1099
+ *(own_disk(args.disk) if args.disk else []),
690
1100
  )
691
1101
  return f"{args.location}:{name}"
692
1102
 
@@ -786,7 +1196,11 @@ def backup(args, location, name):
786
1196
 
787
1197
 
788
1198
  def recover(args):
1199
+ box_fields = box_note(args)
789
1200
  host = settings(args.location)["ssh"]
1201
+ if box_fields:
1202
+ # Without a new note the archived one applies; the host checks it after import.
1203
+ require_note(args.location, box_fields, f"recover {args.source} --location {args.location}")
790
1204
  name = "rf-" + uuid.uuid4().hex[:16]
791
1205
  response = subprocess.run(
792
1206
  ["ssh", "-T", "-o", "BatchMode=yes", host, "mktemp /tmp/raft-recovery.XXXXXXXX"],
@@ -812,12 +1226,24 @@ def recover(args):
812
1226
  stdin=archive,
813
1227
  check=True,
814
1228
  )
1229
+ # The archive carries the source note, known only after import. A rejected
1230
+ # note deletes the imported box inside the same locked transaction.
815
1231
  transaction(
816
1232
  args.location,
817
- ADMISSION + 'incus --project raft import "$1" "$2" --storage raft-data '
818
- "-c user.raft=true -c user.raft.expires= -c user.raft.disposable=false -c user.raft.stopped-at= -c volatile.eth0.hwaddr=",
1233
+ # The reservation check runs first and creates nothing when it refuses.
1234
+ 'pool=$(python3 -c "$3" "$1" "$4")\n'
1235
+ + READ_NOTE_POLICY
1236
+ + 'incus --project raft import "$1" "$2" --storage "$pool" '
1237
+ "-c user.raft=true -c user.raft.expires= -c user.raft.disposable=false -c user.raft.stopped-at= -c volatile.eth0.hwaddr=\n"
1238
+ 'if [ -n "$5" ]; then incus --project raft config set "$2" user.raft.note="$5"; fi\n'
1239
+ 'note=$(incus --project raft config get "$2" user.raft.note)\n'
1240
+ + CHECK_NOTE
1241
+ + ' || { incus --project raft delete "$2" --force; exit 1; }',
819
1242
  staged,
820
1243
  name,
1244
+ RECOVERY_POOL,
1245
+ args.location,
1246
+ json.dumps(box_fields) if box_fields else "",
821
1247
  )
822
1248
  if instance(args.location, name)["status"] != "Stopped":
823
1249
  raise RuntimeError("Recovered box unexpectedly running: " + args.location + ":" + name)
@@ -876,8 +1302,382 @@ def ttl(value):
876
1302
  return seconds
877
1303
 
878
1304
 
1305
+ def location_name(value):
1306
+ if not re.fullmatch(LOCATION_NAME, value):
1307
+ raise ValueError("Location names must use letters, digits, underscores or hyphens")
1308
+ return value
1309
+
1310
+
1311
+ # Options of new that ~/.config/raft/config.json can default, with their built-in
1312
+ # defaults and value checks. Values go through str() first, so a configured value
1313
+ # is accepted exactly when the same text passed as the flag would be. Adding a
1314
+ # defaultable option means adding one entry here and keeping its flag default None.
1315
+ NEW_DEFAULTS = {
1316
+ # None means the first configured location.
1317
+ "location": (None, location_name),
1318
+ "ttl": (600, ttl),
1319
+ "cpu": (DEFAULT_CPU, cpu_count),
1320
+ "memory": (DEFAULT_MEMORY, gib_size),
1321
+ "disk": ("shared", disk_size),
1322
+ }
1323
+ # A box from a snapshot keeps the snapshot's size, so these defaults, configured
1324
+ # or built-in, apply only to boxes created from the image.
1325
+ SIZE_DEFAULTS = {"cpu", "memory", "disk"}
1326
+
1327
+
1328
+ def default_value(key, value):
1329
+ """Validate one configured default; errors name the file and the key."""
1330
+ if key not in NEW_DEFAULTS:
1331
+ raise ValueError(
1332
+ f"{DEFAULTS_CONFIG}: unknown key defaults.{key}; "
1333
+ "supported keys: " + ", ".join(NEW_DEFAULTS)
1334
+ )
1335
+ try:
1336
+ return NEW_DEFAULTS[key][1](str(value))
1337
+ except (ValueError, argparse.ArgumentTypeError) as error:
1338
+ raise ValueError(
1339
+ f"{DEFAULTS_CONFIG}: defaults.{key}={value!r} is invalid: {error}"
1340
+ ) from None
1341
+
1342
+
1343
+ def configured_defaults(unchecked=None):
1344
+ """Return the validated defaults from config.json; a missing file means none.
1345
+
1346
+ The unchecked key is returned as stored, so `raft config set/unset KEY` can
1347
+ repair a file whose only problem is that key.
1348
+ """
1349
+ if not DEFAULTS_CONFIG.exists():
1350
+ return {}
1351
+ try:
1352
+ document = json.loads(DEFAULTS_CONFIG.read_text())
1353
+ except json.JSONDecodeError as error:
1354
+ raise ValueError(f"{DEFAULTS_CONFIG}: invalid JSON: {error}") from None
1355
+ if (
1356
+ not isinstance(document, dict)
1357
+ or set(document) - {"defaults"}
1358
+ or not isinstance(document.get("defaults", {}), dict)
1359
+ ):
1360
+ raise ValueError(f'{DEFAULTS_CONFIG}: expected {{"defaults": {{"KEY": VALUE, ...}}}}')
1361
+ return {
1362
+ key: value if key == unchecked else default_value(key, value)
1363
+ for key, value in document.get("defaults", {}).items()
1364
+ }
1365
+
1366
+
1367
+ def apply_new_defaults(args):
1368
+ """Fill omitted new options: explicit flag, then config.json, then the built-in default.
1369
+
1370
+ Returns the keys whose value came from config.json. With --from only explicit
1371
+ flags change the size: the snapshot's CPU, RAM and disk beat any default.
1372
+ """
1373
+ configured = configured_defaults()
1374
+ used = set()
1375
+ for key, (builtin, _) in NEW_DEFAULTS.items():
1376
+ # getattr's default lets internal callers such as smoke_test omit options.
1377
+ if key == "location" or getattr(args, key, None) is not None:
1378
+ continue
1379
+ if args.template and key in SIZE_DEFAULTS:
1380
+ continue
1381
+ if key in configured:
1382
+ used.add(key)
1383
+ setattr(args, key, configured.get(key, builtin))
1384
+ if args.disk == "shared":
1385
+ if args.template:
1386
+ raise ValueError(
1387
+ "--from keeps the snapshot's disk; leave out --disk, or give --disk SIZE "
1388
+ "for an own disk of that size"
1389
+ )
1390
+ # The rest of new() spells the shared pool as no own disk.
1391
+ args.disk = None
1392
+ # A --from template fixes the location, so only plain creation takes a default.
1393
+ if args.location is None and not args.template:
1394
+ if "location" not in configured:
1395
+ args.location = next(iter(configuration()))
1396
+ elif configured["location"] in configuration():
1397
+ args.location = configured["location"]
1398
+ used.add("location")
1399
+ else:
1400
+ raise ValueError(
1401
+ f"{DEFAULTS_CONFIG}: defaults.location names unknown location "
1402
+ f"{configured['location']}; run raft config set location NAME or "
1403
+ "raft config unset location"
1404
+ )
1405
+ return used
1406
+
1407
+
1408
+ def effective_defaults():
1409
+ """Return each defaultable option's effective value and where it comes from."""
1410
+ configured = configured_defaults()
1411
+ report = {}
1412
+ for key, (builtin, _) in NEW_DEFAULTS.items():
1413
+ if key in configured:
1414
+ report[key] = {"value": configured[key], "source": "config"}
1415
+ else:
1416
+ value = next(iter(configuration())) if key == "location" else builtin
1417
+ report[key] = {"value": value, "source": "built-in"}
1418
+ return report
1419
+
1420
+
1421
+ def disk_origin(size):
1422
+ """Explain a reservation refusal for an own disk that config.json chose, not a flag."""
1423
+ return (
1424
+ f"The {size} own disk comes from defaults.disk in {DEFAULTS_CONFIG}; "
1425
+ "give --disk shared or --disk SIZE, or run raft config unset disk"
1426
+ )
1427
+
1428
+
1429
+ def set_default(key, value):
1430
+ """Set (value) or remove (None) one default in config.json and print the change."""
1431
+ if value is not None:
1432
+ value = default_value(key, value)
1433
+ if key == "location":
1434
+ settings(value)
1435
+ previous = {}
1436
+
1437
+ def change(document):
1438
+ defaults = document.setdefault("defaults", {})
1439
+ previous.update(defaults)
1440
+ if value is None:
1441
+ defaults.pop(key, None)
1442
+ else:
1443
+ defaults[key] = value
1444
+ if not defaults:
1445
+ document.pop("defaults")
1446
+
1447
+ edit_json(DEFAULTS_CONFIG, lambda: {"defaults": configured_defaults(key)}, change)
1448
+ before = previous.get(key, "built-in default")
1449
+ print(f"defaults.{key}: {before} -> {'built-in default' if value is None else value}")
1450
+
1451
+
1452
+ # GitHub releases are the one source of version truth. The release workflow
1453
+ # publishes npm before it creates the GitHub release, so every version this API
1454
+ # reports is already installable from both supported channels.
1455
+ RELEASES_API = "https://api.github.com/repos/Microck/raft/releases"
1456
+ # Matches the user agent deploy/deploy.py sends for release downloads.
1457
+ USER_AGENT = "OpenAI File Downloader, XaiImageApiFetch/1.0"
1458
+ # self-update --check exit status when a newer release exists, as dnf check-update uses.
1459
+ UPDATE_AVAILABLE = 100
1460
+ UPDATE_CHECK_INTERVAL = 24 * 3600
1461
+ UPDATE_CHECK_TIMEOUT = 2
1462
+
1463
+
1464
+ def parse_version(text):
1465
+ """Return an X.Y.Z release version, optionally v-prefixed, as a comparable tuple."""
1466
+ match = re.fullmatch(r"v?(\d+)\.(\d+)\.(\d+)", text)
1467
+ if match is None:
1468
+ raise ValueError(f"Not a release version: {text}")
1469
+ return tuple(int(part) for part in match.groups())
1470
+
1471
+
1472
+ def release_version(value):
1473
+ """Parser type for self-update --version; returns the version without its v."""
1474
+ parse_version(value)
1475
+ return value.removeprefix("v")
1476
+
1477
+
1478
+ def fetch(url, timeout):
1479
+ request = urllib.request.Request(
1480
+ url, headers={"User-Agent": USER_AGENT, "Accept": "application/vnd.github+json"}
1481
+ )
1482
+ return urllib.request.urlopen(request, timeout=timeout)
1483
+
1484
+
1485
+ def release(api, version=None, timeout=30):
1486
+ """Return GitHub metadata for the latest release, or for release vVERSION."""
1487
+ try:
1488
+ with fetch(f"{api}/tags/v{version}" if version else api + "/latest", timeout) as response:
1489
+ metadata = json.load(response)
1490
+ except urllib.error.HTTPError as error:
1491
+ if version and error.code == 404:
1492
+ raise ValueError(f"No published Raft release v{version}") from error
1493
+ raise
1494
+ metadata["version"] = metadata["tag_name"].removeprefix("v")
1495
+ parse_version(metadata["version"])
1496
+ return metadata
1497
+
1498
+
1499
+ def update_message(latest):
1500
+ return f"raft {latest} is available (you have {__version__}). Run: raft self-update"
1501
+
1502
+
1503
+ def update_cache():
1504
+ return Path(os.environ.get("XDG_CACHE_HOME") or Path.home() / ".cache") / "raft/update.json"
1505
+
1506
+
1507
+ def save_update_check():
1508
+ """Record a release check. A torn write only makes the next command check again."""
1509
+ path = update_cache()
1510
+ try:
1511
+ path.parent.mkdir(parents=True, exist_ok=True)
1512
+ path.write_text(json.dumps({"checkedAt": time.time()}))
1513
+ except OSError:
1514
+ pass
1515
+
1516
+
1517
+ def update_check(action, api=RELEASES_API):
1518
+ """Start the passive release check for a command; the returned function prints its notice.
1519
+
1520
+ At most once per UPDATE_CHECK_INTERVAL, a background thread asks GitHub for
1521
+ the latest release while the command runs, and only that command can print
1522
+ the notice, so a controller sees it at most once per interval. The notice is
1523
+ one stderr line, so stdout stays parseable. It never installs anything, and
1524
+ network, thread or cache failures stay silent. RAFT_NO_UPDATE_CHECK=1
1525
+ disables it.
1526
+ """
1527
+ if action == "self-update" or os.environ.get("RAFT_NO_UPDATE_CHECK"):
1528
+ return lambda: None
1529
+ try:
1530
+ checked = float(json.loads(update_cache().read_text())["checkedAt"])
1531
+ except (OSError, ValueError, KeyError, TypeError):
1532
+ checked = 0
1533
+ # A timestamp in the future, e.g. after a clock change, counts as stale.
1534
+ if 0 <= time.time() - checked < UPDATE_CHECK_INTERVAL:
1535
+ return lambda: None
1536
+ # Record the attempt first: an offline controller then waits for at most one
1537
+ # timeout per interval instead of one per command.
1538
+ save_update_check()
1539
+ found = {}
1540
+
1541
+ def check():
1542
+ try:
1543
+ found["latest"] = release(api, timeout=UPDATE_CHECK_TIMEOUT)["version"]
1544
+ except Exception:
1545
+ # A best-effort notice must not print tracebacks or fail commands.
1546
+ pass
1547
+
1548
+ thread = threading.Thread(target=check, daemon=True)
1549
+ try:
1550
+ thread.start()
1551
+ except RuntimeError:
1552
+ # The runtime could not create the thread; skip this interval's notice.
1553
+ return lambda: None
1554
+
1555
+ def notice():
1556
+ # Commands usually outlast the request; otherwise wait a bounded time.
1557
+ thread.join(UPDATE_CHECK_TIMEOUT)
1558
+ latest = found.get("latest")
1559
+ if latest is not None and parse_version(latest) > parse_version(__version__):
1560
+ print(update_message(latest), file=sys.stderr)
1561
+
1562
+ return notice
1563
+
1564
+
1565
+ def install_channel(source, prefix):
1566
+ """Return (channel, root) for the installation holding source, the resolved raft.py.
1567
+
1568
+ self-update manages npm global installs and uv tools. Any other layout raises
1569
+ ValueError with instructions, so an update never moves to another channel.
1570
+ """
1571
+ package = source.parent
1572
+ if (package / ".git").exists():
1573
+ raise ValueError(
1574
+ f"raft runs from the source checkout {package}; update it with "
1575
+ "`git fetch --tags && git checkout vX.Y.Z` and `uv sync --locked`"
1576
+ )
1577
+ # A global install puts the package at PREFIX/lib/node_modules/@microck/raft and
1578
+ # links PREFIX/bin/raft to raft.py. Project-local node_modules have no such link.
1579
+ if package.parts[-4:] == ("lib", "node_modules", "@microck", "raft"):
1580
+ npm_prefix = package.parents[3]
1581
+ if (npm_prefix / "bin/raft").resolve() == source:
1582
+ return "npm", npm_prefix
1583
+ # uv names each tool environment after its package and leaves a receipt in it.
1584
+ # Editable installs keep raft.py outside the environment and are excluded.
1585
+ if prefix.name == "raft" and (prefix / "uv-receipt.toml").exists():
1586
+ if source.is_relative_to(prefix):
1587
+ return "uv", prefix
1588
+ raise ValueError(
1589
+ f"self-update manages npm global installs and uv tools; {source} is neither. "
1590
+ "Reinstall with the tool you used, from a release verified against its SHA256SUMS: "
1591
+ "https://github.com/Microck/raft/releases"
1592
+ )
1593
+
1594
+
1595
+ def verified_wheel(metadata, directory):
1596
+ """Download the release wheel and check it against the release SHA256SUMS."""
1597
+ name = f"raft-{metadata['version']}-py3-none-any.whl"
1598
+ assets = {asset["name"]: asset["browser_download_url"] for asset in metadata["assets"]}
1599
+ for asset in [name, "SHA256SUMS"]:
1600
+ if asset not in assets:
1601
+ raise ValueError(
1602
+ f"Release {metadata['tag_name']} has no {asset}; nothing was installed"
1603
+ )
1604
+ with fetch(assets[asset], 60) as response, (directory / asset).open("wb") as file:
1605
+ shutil.copyfileobj(response, file)
1606
+ expected = [
1607
+ line.split()[0]
1608
+ for line in (directory / "SHA256SUMS").read_text().splitlines()
1609
+ if line.split()[1:] == [name]
1610
+ ]
1611
+ with (directory / name).open("rb") as file:
1612
+ digest = hashlib.file_digest(file, "sha256").hexdigest()
1613
+ if expected != [digest]:
1614
+ raise ValueError(f"{name} does not match the release SHA256SUMS; nothing was installed")
1615
+ return directory / name
1616
+
1617
+
1618
+ def self_update(args, api=RELEASES_API):
1619
+ """Install the latest or the requested release through the channel that installed raft."""
1620
+ if args.check:
1621
+ latest = release(api)["version"]
1622
+ save_update_check()
1623
+ if parse_version(latest) > parse_version(__version__):
1624
+ print(update_message(latest))
1625
+ return UPDATE_AVAILABLE
1626
+ print(f"raft {__version__} is up to date (latest release: {latest})")
1627
+ return 0
1628
+ if args.version is not None and parse_version(args.version) == parse_version(__version__):
1629
+ print(f"raft {__version__} is already installed")
1630
+ return 0
1631
+ # Refuse unmanaged layouts before any download.
1632
+ channel, root = install_channel(Path(__file__).resolve(), Path(sys.prefix).resolve())
1633
+ tool = shutil.which(channel)
1634
+ if tool is None:
1635
+ raise ValueError(f"{channel} installed raft but is not on PATH")
1636
+ metadata = release(api, args.version)
1637
+ target = metadata["version"]
1638
+ # Without --version, never downgrade a build that is newer than the latest release.
1639
+ if args.version is None and parse_version(target) <= parse_version(__version__):
1640
+ print(f"raft {__version__} is up to date (latest release: {target})")
1641
+ return 0
1642
+ # Installer output goes to stderr; stdout carries only the result below.
1643
+ if channel == "npm":
1644
+ # An explicit prefix keeps the update in this install even when another
1645
+ # Node.js installation provides npm. The registry checks package integrity.
1646
+ subprocess.run(
1647
+ [tool, "install", "--global", "--prefix", str(root), f"@microck/raft@{target}"],
1648
+ stdout=sys.stderr,
1649
+ check=True,
1650
+ )
1651
+ else:
1652
+ tools = subprocess.run([tool, "tool", "dir"], capture_output=True, text=True, check=True)
1653
+ if Path(tools.stdout.strip()).resolve() != root.parent:
1654
+ raise ValueError(
1655
+ f"uv now installs tools in {tools.stdout.strip()}, not {root.parent}; "
1656
+ "set UV_TOOL_DIR to the original directory, then retry"
1657
+ )
1658
+ with tempfile.TemporaryDirectory(prefix="raft-update-") as work:
1659
+ wheel = verified_wheel(metadata, Path(work))
1660
+ print(f"Verified {wheel.name} against the release SHA256SUMS", file=sys.stderr)
1661
+ subprocess.run(
1662
+ [tool, "tool", "install", "--force", str(wheel)], stdout=sys.stderr, check=True
1663
+ )
1664
+ installed = subprocess.run(
1665
+ [str(root / "bin/raft"), "--version"], capture_output=True, text=True, check=True
1666
+ )
1667
+ if installed.stdout.strip() != target:
1668
+ raise RuntimeError(f"{channel} installed {installed.stdout.strip()}, not {target}")
1669
+ print(f"raft {__version__} -> {target}")
1670
+ print("Release notes: " + metadata["html_url"])
1671
+ print(
1672
+ "Hosts were not changed. Run `raft doctor`: if a host's worker differs from this CLI, "
1673
+ f"redeploy that host from release {metadata['tag_name']}. "
1674
+ f"`raft host add` now imports {metadata['tag_name']} images by default."
1675
+ )
1676
+ return 0
1677
+
1678
+
879
1679
  def note_options(command):
880
- """Add the box note template flags shared by new and note."""
1680
+ """Add the box note template flags shared by new, note, fork and recover."""
881
1681
  command.add_argument(
882
1682
  "--purpose", help="Box note: what the box is for, e.g. 'Integration tests for PR 42'"
883
1683
  )
@@ -895,13 +1695,48 @@ def parser():
895
1695
  commands = cli.add_subparsers(dest="action", required=True)
896
1696
  create = commands.add_parser("new")
897
1697
  create.add_argument("--location", default=None)
898
- create.add_argument("--ttl", type=ttl, default=600)
899
- create.add_argument("--cpu", type=int, choices=CPU_SIZES, default=1)
900
- create.add_argument("--memory", choices=MEMORY_SIZES, default="2GiB")
1698
+ # Omitted options take ~/.config/raft/config.json defaults, then NEW_DEFAULTS.
1699
+ # With --from, every omitted size comes from the snapshot instead of a default.
1700
+ create.add_argument("--ttl", type=ttl)
1701
+ create.add_argument(
1702
+ "--cpu",
1703
+ type=cpu_count,
1704
+ help=f"CPU limit (default: config, else {DEFAULT_CPU}; with --from, the snapshot's)",
1705
+ )
1706
+ create.add_argument(
1707
+ "--memory",
1708
+ type=gib_size,
1709
+ help="RAM limit, e.g. 16GiB "
1710
+ f"(default: config, else {DEFAULT_MEMORY}; with --from, the snapshot's)",
1711
+ )
1712
+ create.add_argument(
1713
+ "--disk",
1714
+ type=disk_size,
1715
+ metavar="SIZE",
1716
+ help="Give the box its own disk of SIZE, e.g. 20GiB, that it cannot grow past, "
1717
+ "or shared for the shared pool "
1718
+ "(default: config, else shared; with --from, the snapshot's disk)",
1719
+ )
901
1720
  create.add_argument("--disposable", action="store_true", help="Delete on stop or expiry")
902
1721
  create.add_argument("--from", dest="template", metavar="LOCATION:BOX/SNAPSHOT")
903
1722
  note_options(create)
904
1723
  commands.add_parser("locations", help="List configured locations and their notes")
1724
+ defaults = commands.add_parser(
1725
+ "config", help="Show defaults for new and each location's note policy, or change defaults"
1726
+ )
1727
+ default_commands = defaults.add_subparsers(dest="config_action")
1728
+ default_set = default_commands.add_parser("set", help="Set a default for new")
1729
+ default_set.add_argument("key", choices=list(NEW_DEFAULTS))
1730
+ default_set.add_argument("value")
1731
+ default_unset = default_commands.add_parser("unset", help="Restore a built-in default")
1732
+ default_unset.add_argument("key", choices=list(NEW_DEFAULTS))
1733
+ policy = commands.add_parser(
1734
+ "policy", help="Set or clear the box note fields a location's host requires"
1735
+ )
1736
+ policy.add_argument("location")
1737
+ rule = policy.add_mutually_exclusive_group(required=True)
1738
+ rule.add_argument("--require", metavar="FIELDS", help="Note fields, e.g. purpose,owner")
1739
+ rule.add_argument("--clear", action="store_true", help="Stop requiring note fields")
905
1740
  note = commands.add_parser("note", help="Set or clear the note of a location or a box")
906
1741
  note.add_argument("target", metavar="LOCATION|BOX", help="A location name or a box handle")
907
1742
  note.add_argument("text", nargs="?", help="Location guidance, e.g. 'Preferred for builds'")
@@ -929,11 +1764,18 @@ def parser():
929
1764
  "--dry-run", action="store_true", help="Check the host and print the plan; change nothing"
930
1765
  )
931
1766
  onboard.add_argument("--yes", action="store_true", help="Apply the plan without prompting")
1767
+ onboard.add_argument(
1768
+ "--disk-pool",
1769
+ type=gib_size,
1770
+ metavar="SIZE",
1771
+ help="Enable own box disks: set aside SIZE of host disk for them, e.g. 40GiB "
1772
+ "(default: not enabled)",
1773
+ )
932
1774
  listing = commands.add_parser("list")
933
1775
  listing.add_argument("--location")
934
- limits = commands.add_parser("limits", help="Inspect host capacity and recommended box counts")
935
- limits.add_argument("--location")
936
- limits.add_argument("--json", action="store_true")
1776
+ report = commands.add_parser("capacity", help=ADVISORY, description=ADVISORY)
1777
+ report.add_argument("--location")
1778
+ report.add_argument("--json", action="store_true")
937
1779
  commands.add_parser("doctor")
938
1780
  gc = commands.add_parser("gc", help="Enforce deadlines, including disposable deletion")
939
1781
  gc.add_argument("--location")
@@ -944,6 +1786,22 @@ def parser():
944
1786
  recovery = commands.add_parser("recover", help="Import a portable backup as a new stopped box")
945
1787
  recovery.add_argument("source")
946
1788
  recovery.add_argument("--location", required=True)
1789
+ note_options(recovery)
1790
+ update = commands.add_parser(
1791
+ "self-update", help="Upgrade this CLI to the latest or a chosen release"
1792
+ )
1793
+ target = update.add_mutually_exclusive_group()
1794
+ target.add_argument(
1795
+ "--check",
1796
+ action="store_true",
1797
+ help=f"Only report whether a newer release exists; exit {UPDATE_AVAILABLE} if one does",
1798
+ )
1799
+ target.add_argument(
1800
+ "--version",
1801
+ type=release_version,
1802
+ metavar="X.Y.Z",
1803
+ help="Install this release instead of the latest, e.g. to roll back",
1804
+ )
947
1805
  for action in [
948
1806
  "info",
949
1807
  "stop",
@@ -972,10 +1830,11 @@ def parser():
972
1830
  if action in ["resume", "fork", "extend"]:
973
1831
  command.add_argument("--ttl", type=ttl, required=True)
974
1832
  if action in ["resume", "fork"]:
975
- command.add_argument("--cpu", type=int, choices=CPU_SIZES)
976
- command.add_argument("--memory", choices=MEMORY_SIZES)
1833
+ command.add_argument("--cpu", type=cpu_count)
1834
+ command.add_argument("--memory", type=gib_size)
977
1835
  if action == "fork":
978
1836
  command.add_argument("--disposable", action="store_true")
1837
+ note_options(command)
979
1838
  if action == "exec":
980
1839
  command.add_argument("--detach", action="store_true")
981
1840
  command.add_argument("command", nargs="+")
@@ -998,6 +1857,8 @@ def parser():
998
1857
 
999
1858
 
1000
1859
  def dispatch(args):
1860
+ if args.action == "self-update":
1861
+ return self_update(args)
1001
1862
  if args.action == "recover":
1002
1863
  recover(args)
1003
1864
  return 0
@@ -1007,6 +1868,21 @@ def dispatch(args):
1007
1868
  if args.action == "host":
1008
1869
  host_add(args)
1009
1870
  return 0
1871
+ if args.action == "config":
1872
+ if args.config_action is None:
1873
+ report = {"file": str(DEFAULTS_CONFIG), "defaults": effective_defaults(), "policy": {}}
1874
+ for location in configuration():
1875
+ try:
1876
+ report["policy"][location] = note_policy(location)
1877
+ except RuntimeError as error:
1878
+ raise RuntimeError(f"{location}: {error}") from error
1879
+ print(json.dumps(report, indent=2))
1880
+ else:
1881
+ set_default(args.key, args.value if args.config_action == "set" else None)
1882
+ return 0
1883
+ if args.action == "policy":
1884
+ set_policy(args.location, None if args.clear else note_fields(args.require))
1885
+ return 0
1010
1886
  if args.action == "note":
1011
1887
  fields = box_note(args)
1012
1888
  # Location names cannot contain ":", so a colon always means a box handle.
@@ -1023,28 +1899,41 @@ def dispatch(args):
1023
1899
  raise ValueError("Give --purpose (with optional --owner and --next) or --clear")
1024
1900
  location, name = parse_handle(args.target)
1025
1901
  instance(location, name)
1902
+ require_note(
1903
+ location,
1904
+ fields,
1905
+ f"note {args.target}",
1906
+ " Clearing would leave the box without them." if args.clear else "",
1907
+ )
1026
1908
  # Each call replaces the whole note; an empty value removes the key.
1027
- value = "" if args.clear else json.dumps(fields)
1028
- incus(location, "config", "set", name, "user.raft.note=" + value, locked=True)
1909
+ transaction(
1910
+ location,
1911
+ 'note="$2"\n'
1912
+ + READ_NOTE_POLICY
1913
+ + CHECK_NOTE
1914
+ + '\nincus --project raft config set "$1" user.raft.note="$note"',
1915
+ name,
1916
+ "" if args.clear else json.dumps(fields),
1917
+ )
1029
1918
  return 0
1030
1919
  if args.action == "locations":
1031
1920
  for index, (location, entry) in enumerate(configuration().items()):
1032
1921
  record = {"location": location, "default": index == 0, "note": entry.get("note", "")}
1033
1922
  print(json.dumps(record))
1034
1923
  return 0
1035
- if args.action in ["list", "limits", "doctor", "gc", "prune"]:
1924
+ if args.action in ["list", "capacity", "doctor", "gc", "prune"]:
1036
1925
  if args.action == "prune" and args.older_than < 1:
1037
1926
  raise ValueError("Retention must be at least one day")
1038
1927
  locations = configuration()
1039
1928
  if getattr(args, "location", None):
1040
1929
  locations = [args.location]
1041
1930
  for location in locations:
1042
- if args.action == "limits":
1043
- report = host_limits(location)
1931
+ if args.action == "capacity":
1932
+ report = host_capacity(location)
1044
1933
  if args.json:
1045
1934
  print(json.dumps(report))
1046
1935
  else:
1047
- print_limits(report)
1936
+ print_capacity(report)
1048
1937
  elif args.action in ["gc", "prune"]:
1049
1938
  worker = ["sudo", "-n", "/usr/local/lib/raft/expire"]
1050
1939
  if args.action == "prune":
@@ -1090,11 +1979,14 @@ def dispatch(args):
1090
1979
  "box": location + ":" + box["name"],
1091
1980
  "state": box["status"],
1092
1981
  "stopAt": box["config"].get("user.raft.expires", ""),
1982
+ "disk": box_disk(box),
1093
1983
  "note": read_box_note(box),
1094
1984
  }
1095
1985
  )
1096
1986
  )
1097
1987
  return 0
1988
+ # Only fork accepts note flags here; check them before contacting the host.
1989
+ fields = box_note(args)
1098
1990
  location, name = parse_handle(args.box)
1099
1991
  box = instance(location, name)
1100
1992
  if args.action == "info":
@@ -1106,7 +1998,12 @@ def dispatch(args):
1106
1998
  response = execute(location, name, ["python3", "-c", USAGE], capture=True)
1107
1999
  if response.returncode:
1108
2000
  raise RuntimeError(response.stderr.decode().strip())
1109
- print(response.stdout.decode().strip())
2001
+ usage = json.loads(response.stdout)
2002
+ disk = box_disk(box)
2003
+ usage["filesystemScope"] = (
2004
+ "shared Raft pool, not per-box usage" if disk == "shared" else f"own {disk} box disk"
2005
+ )
2006
+ print(json.dumps(usage))
1110
2007
  elif args.action == "destroy":
1111
2008
  incus(location, "delete", name, "--force", locked=True)
1112
2009
  if any(item["name"] == name for item in inventory(location)):
@@ -1130,7 +2027,7 @@ def dispatch(args):
1130
2027
  overrides.append(f"limits.{field}={value}")
1131
2028
  transaction(
1132
2029
  location,
1133
- REQUIRE_STOPPED + 'name="$1"; lifetime="$2"; shift 2; '
2030
+ REQUIRE_STOPPED + fits_host(args) + 'name="$1"; lifetime="$2"; shift 2; '
1134
2031
  'if [ "$#" -gt 0 ]; then incus --project raft config set "$name" "$@"; fi; '
1135
2032
  'incus --project raft start "$name"; '
1136
2033
  'incus --project raft config set "$name" user.raft.stopped-at=; '
@@ -1193,7 +2090,9 @@ def dispatch(args):
1193
2090
  print(
1194
2091
  transaction(
1195
2092
  location,
1196
- REQUIRE_STOPPED + 'incus --project raft snapshot create "$1" "$2"',
2093
+ REQUIRE_STOPPED
2094
+ + reserve_box_disk(location, None, name)
2095
+ + 'incus --project raft snapshot create "$1" "$2"',
1197
2096
  name,
1198
2097
  args.name,
1199
2098
  ).strip()
@@ -1207,6 +2106,9 @@ def dispatch(args):
1207
2106
  snapshot_name(args.name)
1208
2107
  # Imported/forked snapshots carry source configuration. Restore files,
1209
2108
  # keeping the destination's network identity and explicitly chosen limits.
2109
+ # On raft-disks Incus swaps in a thin snapshot of the snapshot for the box's
2110
+ # volume, so a restore needs no reservation: it writes no data, and the
2111
+ # reserved total is unchanged once the old volume is removed.
1210
2112
  transaction(
1211
2113
  location,
1212
2114
  REQUIRE_STOPPED + 'incus --project raft snapshot restore "$1" "$2" --diskonly',
@@ -1214,19 +2116,33 @@ def dispatch(args):
1214
2116
  args.name,
1215
2117
  )
1216
2118
  elif args.action == "fork":
2119
+ # Note flags replace the whole note, as with raft note; without them the
2120
+ # fork keeps the source note, which must then satisfy the host policy.
2121
+ require_note(
2122
+ location,
2123
+ fields or read_box_note(box),
2124
+ f"fork {args.box} --ttl {args.ttl}",
2125
+ "" if fields else " Forks keep the source note unless you give a new one.",
2126
+ )
1217
2127
  child = "rf-" + uuid.uuid4().hex[:16]
1218
2128
  transaction(
1219
2129
  location,
1220
2130
  (CHECK_WORKER if args.disposable else "")
1221
2131
  + REQUIRE_STOPPED
1222
- + ADMISSION
1223
- + 'name="$2"; lifetime="$3"; '
1224
- 'source="$1"; shift 3; incus --project raft copy "$source" "$name" --instance-only "$@"; '
2132
+ + fits_host(args)
2133
+ + reserve_box_disk(location, None, name)
2134
+ + 'name="$2"; lifetime="$3"; source="$1"; note="$4"; shift 4\n'
2135
+ # Read the source note under the lock, so the check sees what the copy keeps.
2136
+ 'if [ -z "$note" ]; then note=$(incus --project raft config get "$source" user.raft.note); fi\n'
2137
+ + READ_NOTE_POLICY
2138
+ + CHECK_NOTE
2139
+ + '\nincus --project raft copy "$source" "$name" --instance-only "$@" -c user.raft.note="$note"; '
1225
2140
  'incus --project raft start "$name"; '
1226
2141
  'incus --project raft config set "$name" user.raft.expires="$(( $(date +%s) + lifetime ))"',
1227
2142
  name,
1228
2143
  child,
1229
2144
  str(args.ttl),
2145
+ json.dumps(fields) if fields else "",
1230
2146
  "-c",
1231
2147
  "user.raft.expires=",
1232
2148
  "-c",
@@ -1293,8 +2209,11 @@ def main():
1293
2209
  print("raft: Python 3.11.8 or newer is required", file=sys.stderr)
1294
2210
  return 1
1295
2211
  os.umask(0o077)
2212
+ # Parsing exits for --version, --help and usage errors, before any release check.
2213
+ args = parser().parse_args()
2214
+ notice = update_check(args.action, RELEASES_API)
1296
2215
  try:
1297
- return dispatch(parser().parse_args())
2216
+ status = dispatch(args)
1298
2217
  except (
1299
2218
  OSError,
1300
2219
  ValueError,
@@ -1303,9 +2222,11 @@ def main():
1303
2222
  subprocess.CalledProcessError,
1304
2223
  ) as error:
1305
2224
  print(f"raft: {error}", file=sys.stderr)
1306
- return 1
2225
+ status = 1
1307
2226
  except KeyboardInterrupt:
1308
2227
  return 130
2228
+ notice()
2229
+ return status
1309
2230
 
1310
2231
 
1311
2232
  if __name__ == "__main__":