loft-cli 0.6.3__py3-none-any.whl
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.
- loft_cli/__init__.py +3 -0
- loft_cli/_builtins.py +269 -0
- loft_cli/addons/__init__.py +12 -0
- loft_cli/addons/goss/__init__.py +1 -0
- loft_cli/addons/goss/generator.py +202 -0
- loft_cli/addons/goss/renderer.py +207 -0
- loft_cli/addons/goss/shipper.py +174 -0
- loft_cli/agent_installer.py +65 -0
- loft_cli/checks/__init__.py +0 -0
- loft_cli/checks/compose.py +147 -0
- loft_cli/checks/container.py +24 -0
- loft_cli/checks/http.py +30 -0
- loft_cli/checks/nginx.py +21 -0
- loft_cli/checks/ports.py +24 -0
- loft_cli/checks/postgres.py +21 -0
- loft_cli/checks/ssh.py +66 -0
- loft_cli/checks/wireguard.py +21 -0
- loft_cli/cli.py +1161 -0
- loft_cli/compiler/__init__.py +0 -0
- loft_cli/compiler/normalizer.py +420 -0
- loft_cli/compiler/parser.py +42 -0
- loft_cli/compiler/planner.py +2543 -0
- loft_cli/local/__init__.py +0 -0
- loft_cli/local/ddl/__init__.py +0 -0
- loft_cli/local/ddl/bootstrap_tables.py +75 -0
- loft_cli/local/ddl/versionize_system.py +374 -0
- loft_cli/local/inventory.py +449 -0
- loft_cli/local/inventory_db.py +255 -0
- loft_cli/local/keys.py +45 -0
- loft_cli/local/remove.py +135 -0
- loft_cli/local/ssh_config.py +118 -0
- loft_cli/local/tunnel.py +228 -0
- loft_cli/local/wireguard_store.py +138 -0
- loft_cli/logs/__init__.py +0 -0
- loft_cli/logs/reader.py +55 -0
- loft_cli/logs/writer.py +58 -0
- loft_cli/runtime/__init__.py +0 -0
- loft_cli/runtime/agent_transport.py +115 -0
- loft_cli/runtime/executor.py +798 -0
- loft_cli/runtime/fabric_transport.py +68 -0
- loft_cli/runtime/secret_rotation.py +99 -0
- loft_cli/runtime/ssh.py +147 -0
- loft_cli/runtime/steps/__init__.py +0 -0
- loft_cli/runtime/steps/backup.py +72 -0
- loft_cli/runtime/steps/bootstrap.py +227 -0
- loft_cli/runtime/steps/compose.py +43 -0
- loft_cli/runtime/steps/container.py +40 -0
- loft_cli/runtime/steps/docker.py +15 -0
- loft_cli/runtime/steps/file_template.py +25 -0
- loft_cli/runtime/steps/nginx.py +126 -0
- loft_cli/runtime/steps/postgres.py +101 -0
- loft_cli/runtime/steps/postgres_ensure.py +141 -0
- loft_cli/runtime/steps/systemd.py +131 -0
- loft_cli/runtime/steps/wireguard.py +76 -0
- loft_cli/runtime/transport.py +43 -0
- loft_cli/updater.py +208 -0
- loft_cli-0.6.3.dist-info/METADATA +19 -0
- loft_cli-0.6.3.dist-info/RECORD +61 -0
- loft_cli-0.6.3.dist-info/WHEEL +5 -0
- loft_cli-0.6.3.dist-info/entry_points.txt +5 -0
- loft_cli-0.6.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
"""High-level inventory operations built on InventoryDB."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from datetime import UTC, datetime
|
|
7
|
+
from typing import TYPE_CHECKING
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from loft_cli.local.inventory_db import InventoryDB
|
|
11
|
+
from loft_cli.runtime.executor import ApplyResult
|
|
12
|
+
from loft_cli_core.specs.backup_job_schema import BackupJobSpec
|
|
13
|
+
from loft_cli_core.specs.bootstrap_schema import BootstrapSpec
|
|
14
|
+
from loft_cli_core.specs.compose_project_schema import ComposeProjectSpec
|
|
15
|
+
from loft_cli_core.specs.file_template_schema import FileTemplateSpec
|
|
16
|
+
from loft_cli_core.specs.http_check_schema import HttpCheckSpec
|
|
17
|
+
from loft_cli_core.specs.postgres_ensure_schema import PostgresEnsureSpec
|
|
18
|
+
from loft_cli_core.specs.service_schema import ServiceSpec
|
|
19
|
+
from loft_cli_core.specs.stack_schema import StackSpec
|
|
20
|
+
from loft_cli_core.specs.systemd_timer_schema import SystemdTimerSpec
|
|
21
|
+
from loft_cli_core.specs.systemd_unit_schema import SystemdUnitSpec
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _now_iso() -> str:
|
|
25
|
+
return datetime.now(UTC).isoformat()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def record_bootstrap(
|
|
29
|
+
db: InventoryDB,
|
|
30
|
+
spec: BootstrapSpec,
|
|
31
|
+
apply_result: ApplyResult,
|
|
32
|
+
) -> None:
|
|
33
|
+
"""After a successful bootstrap apply, record server + run in inventory."""
|
|
34
|
+
|
|
35
|
+
status = apply_result.status
|
|
36
|
+
finished = apply_result.finished_at
|
|
37
|
+
|
|
38
|
+
db.upsert_server(
|
|
39
|
+
id=spec.host.name,
|
|
40
|
+
name=spec.host.name,
|
|
41
|
+
address=spec.host.address,
|
|
42
|
+
bootstrap_status="bootstrapped" if "success" in status else "failed",
|
|
43
|
+
os_family=spec.host.os_family,
|
|
44
|
+
ssh_alias=spec.local.ssh_config.host_alias or spec.host.name,
|
|
45
|
+
ssh_host=spec.host.address,
|
|
46
|
+
ssh_user=spec.admin_user.name,
|
|
47
|
+
ssh_port=spec.ssh.port,
|
|
48
|
+
ssh_identity_file=spec.login.private_key,
|
|
49
|
+
wireguard_enabled=spec.wireguard.enabled,
|
|
50
|
+
wireguard_interface=spec.wireguard.interface if spec.wireguard.enabled else "",
|
|
51
|
+
wireguard_address=spec.wireguard.address if spec.wireguard.enabled else "",
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
metadata = {
|
|
55
|
+
"spec_name": spec.meta.name,
|
|
56
|
+
"step_count": len(apply_result.step_results),
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
db.record_run(
|
|
60
|
+
id=apply_result.started_at.replace(":", "-").replace("+", "Z"),
|
|
61
|
+
kind="bootstrap",
|
|
62
|
+
spec_hash=apply_result.plan.spec_hash,
|
|
63
|
+
plan_hash=apply_result.plan.plan_hash,
|
|
64
|
+
status=status,
|
|
65
|
+
started_at=apply_result.started_at,
|
|
66
|
+
finished_at=finished,
|
|
67
|
+
server_id=spec.host.name,
|
|
68
|
+
metadata_json=json.dumps(metadata),
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def record_service_apply(
|
|
73
|
+
db: InventoryDB,
|
|
74
|
+
spec: ServiceSpec,
|
|
75
|
+
apply_result: ApplyResult,
|
|
76
|
+
) -> None:
|
|
77
|
+
"""After a service apply, record services + run in inventory."""
|
|
78
|
+
import json
|
|
79
|
+
|
|
80
|
+
server_id = spec.host.name
|
|
81
|
+
status = apply_result.status
|
|
82
|
+
|
|
83
|
+
if spec.postgres and spec.postgres.enabled:
|
|
84
|
+
db.upsert_service(
|
|
85
|
+
server_id=server_id,
|
|
86
|
+
service_type="postgres",
|
|
87
|
+
service_name=f"postgresql-{spec.postgres.version}",
|
|
88
|
+
status="active" if "success" in status else "failed",
|
|
89
|
+
metadata_json=json.dumps({"version": spec.postgres.version}),
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
if spec.nginx and spec.nginx.enabled:
|
|
93
|
+
sites = [s.domain for s in spec.nginx.sites]
|
|
94
|
+
db.upsert_service(
|
|
95
|
+
server_id=server_id,
|
|
96
|
+
service_type="nginx",
|
|
97
|
+
service_name="nginx",
|
|
98
|
+
status="active" if "success" in status else "failed",
|
|
99
|
+
metadata_json=json.dumps({"sites": sites}),
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
if spec.docker and spec.docker.enabled:
|
|
103
|
+
db.upsert_service(
|
|
104
|
+
server_id=server_id,
|
|
105
|
+
service_type="docker",
|
|
106
|
+
service_name="docker",
|
|
107
|
+
status="active" if "success" in status else "failed",
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
for c in spec.containers:
|
|
111
|
+
db.upsert_service(
|
|
112
|
+
server_id=server_id,
|
|
113
|
+
service_type="container",
|
|
114
|
+
service_name=c.name,
|
|
115
|
+
status="running" if "success" in status else "failed",
|
|
116
|
+
metadata_json=json.dumps({"image": c.image}),
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
db.record_run(
|
|
120
|
+
id=apply_result.started_at.replace(":", "-").replace("+", "Z"),
|
|
121
|
+
kind="service",
|
|
122
|
+
spec_hash=apply_result.plan.spec_hash,
|
|
123
|
+
plan_hash=apply_result.plan.plan_hash,
|
|
124
|
+
status=status,
|
|
125
|
+
started_at=apply_result.started_at,
|
|
126
|
+
finished_at=apply_result.finished_at,
|
|
127
|
+
server_id=server_id,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def record_file_template_apply(
|
|
132
|
+
db: InventoryDB,
|
|
133
|
+
spec: FileTemplateSpec,
|
|
134
|
+
apply_result: ApplyResult,
|
|
135
|
+
) -> None:
|
|
136
|
+
"""After a file_template apply, record template metadata + run in inventory."""
|
|
137
|
+
import json
|
|
138
|
+
|
|
139
|
+
server_id = spec.host.name
|
|
140
|
+
status = apply_result.status
|
|
141
|
+
|
|
142
|
+
template_dests = [t.dest for t in spec.templates]
|
|
143
|
+
db.upsert_service(
|
|
144
|
+
server_id=server_id,
|
|
145
|
+
service_type="file_template",
|
|
146
|
+
service_name=f"file_template:{spec.meta.name}",
|
|
147
|
+
status="active" if "success" in status else "failed",
|
|
148
|
+
metadata_json=json.dumps(
|
|
149
|
+
{
|
|
150
|
+
"templates": template_dests,
|
|
151
|
+
"template_count": len(template_dests),
|
|
152
|
+
}
|
|
153
|
+
),
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
db.record_run(
|
|
157
|
+
id=apply_result.started_at.replace(":", "-").replace("+", "Z"),
|
|
158
|
+
kind="file_template",
|
|
159
|
+
spec_hash=apply_result.plan.spec_hash,
|
|
160
|
+
plan_hash=apply_result.plan.plan_hash,
|
|
161
|
+
status=status,
|
|
162
|
+
started_at=apply_result.started_at,
|
|
163
|
+
finished_at=apply_result.finished_at,
|
|
164
|
+
server_id=server_id,
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def record_compose_project_apply(
|
|
169
|
+
db: InventoryDB,
|
|
170
|
+
spec: ComposeProjectSpec,
|
|
171
|
+
apply_result: ApplyResult,
|
|
172
|
+
) -> None:
|
|
173
|
+
"""After a compose_project apply, record project metadata + run in inventory."""
|
|
174
|
+
import json
|
|
175
|
+
|
|
176
|
+
server_id = spec.host.name
|
|
177
|
+
status = apply_result.status
|
|
178
|
+
p = spec.project
|
|
179
|
+
|
|
180
|
+
db.upsert_service(
|
|
181
|
+
server_id=server_id,
|
|
182
|
+
service_type="compose_project",
|
|
183
|
+
service_name=p.name,
|
|
184
|
+
status="active" if "success" in status else "failed",
|
|
185
|
+
metadata_json=json.dumps(
|
|
186
|
+
{
|
|
187
|
+
"directory": p.directory,
|
|
188
|
+
"compose_file": p.compose_file,
|
|
189
|
+
"template_count": len(p.templates),
|
|
190
|
+
}
|
|
191
|
+
),
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
db.record_run(
|
|
195
|
+
id=apply_result.started_at.replace(":", "-").replace("+", "Z"),
|
|
196
|
+
kind="compose_project",
|
|
197
|
+
spec_hash=apply_result.plan.spec_hash,
|
|
198
|
+
plan_hash=apply_result.plan.plan_hash,
|
|
199
|
+
status=status,
|
|
200
|
+
started_at=apply_result.started_at,
|
|
201
|
+
finished_at=apply_result.finished_at,
|
|
202
|
+
server_id=server_id,
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def record_postgres_ensure_apply(
|
|
207
|
+
db: InventoryDB,
|
|
208
|
+
spec: PostgresEnsureSpec,
|
|
209
|
+
apply_result: ApplyResult,
|
|
210
|
+
) -> None:
|
|
211
|
+
"""After a postgres_ensure apply, record PG resource metadata + run."""
|
|
212
|
+
import json
|
|
213
|
+
|
|
214
|
+
server_id = spec.host.name
|
|
215
|
+
status = apply_result.status
|
|
216
|
+
|
|
217
|
+
db.upsert_service(
|
|
218
|
+
server_id=server_id,
|
|
219
|
+
service_type="postgres_ensure",
|
|
220
|
+
service_name=f"pg_ensure:{spec.meta.name}",
|
|
221
|
+
status="active" if "success" in status else "failed",
|
|
222
|
+
metadata_json=json.dumps(
|
|
223
|
+
{
|
|
224
|
+
"users": [u.name for u in spec.users],
|
|
225
|
+
"databases": [d.name for d in spec.databases],
|
|
226
|
+
"extensions": [e.name for e in spec.extensions],
|
|
227
|
+
"grants": len(spec.grants),
|
|
228
|
+
}
|
|
229
|
+
),
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
db.record_run(
|
|
233
|
+
id=apply_result.started_at.replace(":", "-").replace("+", "Z"),
|
|
234
|
+
kind="postgres_ensure",
|
|
235
|
+
spec_hash=apply_result.plan.spec_hash,
|
|
236
|
+
plan_hash=apply_result.plan.plan_hash,
|
|
237
|
+
status=status,
|
|
238
|
+
started_at=apply_result.started_at,
|
|
239
|
+
finished_at=apply_result.finished_at,
|
|
240
|
+
server_id=server_id,
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def record_backup_job_apply(
|
|
245
|
+
db: InventoryDB,
|
|
246
|
+
spec: BackupJobSpec,
|
|
247
|
+
apply_result: ApplyResult,
|
|
248
|
+
) -> None:
|
|
249
|
+
"""After a backup_job apply, record backup metadata + run in inventory."""
|
|
250
|
+
import json
|
|
251
|
+
|
|
252
|
+
server_id = spec.host.name
|
|
253
|
+
status = apply_result.status
|
|
254
|
+
|
|
255
|
+
db.upsert_service(
|
|
256
|
+
server_id=server_id,
|
|
257
|
+
service_type="backup_job",
|
|
258
|
+
service_name=f"backup:{spec.backup.name}",
|
|
259
|
+
status="active" if "success" in status else "failed",
|
|
260
|
+
metadata_json=json.dumps(
|
|
261
|
+
{
|
|
262
|
+
"name": spec.backup.name,
|
|
263
|
+
"source_type": spec.backup.source.type,
|
|
264
|
+
"schedule": spec.backup.schedule,
|
|
265
|
+
"retention_count": spec.backup.retention.count,
|
|
266
|
+
}
|
|
267
|
+
),
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
db.record_run(
|
|
271
|
+
id=apply_result.started_at.replace(":", "-").replace("+", "Z"),
|
|
272
|
+
kind="backup_job",
|
|
273
|
+
spec_hash=apply_result.plan.spec_hash,
|
|
274
|
+
plan_hash=apply_result.plan.plan_hash,
|
|
275
|
+
status=status,
|
|
276
|
+
started_at=apply_result.started_at,
|
|
277
|
+
finished_at=apply_result.finished_at,
|
|
278
|
+
server_id=server_id,
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def record_systemd_timer_apply(
|
|
283
|
+
db: InventoryDB,
|
|
284
|
+
spec: SystemdTimerSpec,
|
|
285
|
+
apply_result: ApplyResult,
|
|
286
|
+
) -> None:
|
|
287
|
+
"""After a systemd_timer apply, record timer metadata + run in inventory."""
|
|
288
|
+
import json
|
|
289
|
+
|
|
290
|
+
server_id = spec.host.name
|
|
291
|
+
status = apply_result.status
|
|
292
|
+
|
|
293
|
+
db.upsert_service(
|
|
294
|
+
server_id=server_id,
|
|
295
|
+
service_type="systemd_timer",
|
|
296
|
+
service_name=spec.timer.timer_name,
|
|
297
|
+
status="active" if "success" in status else "failed",
|
|
298
|
+
metadata_json=json.dumps(
|
|
299
|
+
{
|
|
300
|
+
"timer_name": spec.timer.timer_name,
|
|
301
|
+
"on_calendar": spec.timer.on_calendar,
|
|
302
|
+
"exec_start": spec.service.exec_start,
|
|
303
|
+
}
|
|
304
|
+
),
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
db.record_run(
|
|
308
|
+
id=apply_result.started_at.replace(":", "-").replace("+", "Z"),
|
|
309
|
+
kind="systemd_timer",
|
|
310
|
+
spec_hash=apply_result.plan.spec_hash,
|
|
311
|
+
plan_hash=apply_result.plan.plan_hash,
|
|
312
|
+
status=status,
|
|
313
|
+
started_at=apply_result.started_at,
|
|
314
|
+
finished_at=apply_result.finished_at,
|
|
315
|
+
server_id=server_id,
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def record_systemd_unit_apply(
|
|
320
|
+
db: InventoryDB,
|
|
321
|
+
spec: SystemdUnitSpec,
|
|
322
|
+
apply_result: ApplyResult,
|
|
323
|
+
) -> None:
|
|
324
|
+
"""After a systemd_unit apply, record unit metadata + run in inventory."""
|
|
325
|
+
import json
|
|
326
|
+
|
|
327
|
+
server_id = spec.host.name
|
|
328
|
+
status = apply_result.status
|
|
329
|
+
|
|
330
|
+
db.upsert_service(
|
|
331
|
+
server_id=server_id,
|
|
332
|
+
service_type="systemd_unit",
|
|
333
|
+
service_name=spec.unit.unit_name,
|
|
334
|
+
status="active" if "success" in status else "failed",
|
|
335
|
+
metadata_json=json.dumps(
|
|
336
|
+
{
|
|
337
|
+
"unit_name": spec.unit.unit_name,
|
|
338
|
+
"exec_start": spec.unit.exec_start,
|
|
339
|
+
"user": spec.unit.user,
|
|
340
|
+
}
|
|
341
|
+
),
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
db.record_run(
|
|
345
|
+
id=apply_result.started_at.replace(":", "-").replace("+", "Z"),
|
|
346
|
+
kind="systemd_unit",
|
|
347
|
+
spec_hash=apply_result.plan.spec_hash,
|
|
348
|
+
plan_hash=apply_result.plan.plan_hash,
|
|
349
|
+
status=status,
|
|
350
|
+
started_at=apply_result.started_at,
|
|
351
|
+
finished_at=apply_result.finished_at,
|
|
352
|
+
server_id=server_id,
|
|
353
|
+
)
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def record_http_check_apply(
|
|
357
|
+
db: InventoryDB,
|
|
358
|
+
spec: HttpCheckSpec,
|
|
359
|
+
apply_result: ApplyResult,
|
|
360
|
+
) -> None:
|
|
361
|
+
"""After an http_check apply, record check metadata + run in inventory."""
|
|
362
|
+
import json
|
|
363
|
+
|
|
364
|
+
server_id = spec.host.name
|
|
365
|
+
status = apply_result.status
|
|
366
|
+
|
|
367
|
+
db.upsert_service(
|
|
368
|
+
server_id=server_id,
|
|
369
|
+
service_type="http_check",
|
|
370
|
+
service_name=f"http_check:{spec.meta.name}",
|
|
371
|
+
status="active" if "success" in status else "failed",
|
|
372
|
+
metadata_json=json.dumps(
|
|
373
|
+
{
|
|
374
|
+
"url": spec.check.url,
|
|
375
|
+
"expected_status": spec.check.expected_status,
|
|
376
|
+
}
|
|
377
|
+
),
|
|
378
|
+
)
|
|
379
|
+
|
|
380
|
+
db.record_run(
|
|
381
|
+
id=apply_result.started_at.replace(":", "-").replace("+", "Z"),
|
|
382
|
+
kind="http_check",
|
|
383
|
+
spec_hash=apply_result.plan.spec_hash,
|
|
384
|
+
plan_hash=apply_result.plan.plan_hash,
|
|
385
|
+
status=status,
|
|
386
|
+
started_at=apply_result.started_at,
|
|
387
|
+
finished_at=apply_result.finished_at,
|
|
388
|
+
server_id=server_id,
|
|
389
|
+
)
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
def record_stack_apply(
|
|
393
|
+
db: InventoryDB,
|
|
394
|
+
spec: StackSpec,
|
|
395
|
+
apply_result: ApplyResult,
|
|
396
|
+
) -> None:
|
|
397
|
+
"""After a stack apply, record stack metadata + per-resource services + run."""
|
|
398
|
+
import json
|
|
399
|
+
|
|
400
|
+
server_id = spec.host.name
|
|
401
|
+
status = apply_result.status
|
|
402
|
+
|
|
403
|
+
# Record each resource as a service entry
|
|
404
|
+
for res in spec.resources:
|
|
405
|
+
db.upsert_service(
|
|
406
|
+
server_id=server_id,
|
|
407
|
+
service_type="stack_resource",
|
|
408
|
+
service_name=f"{spec.meta.name}/{res.name}",
|
|
409
|
+
status="active" if "success" in status else "failed",
|
|
410
|
+
metadata_json=json.dumps(
|
|
411
|
+
{
|
|
412
|
+
"stack": spec.meta.name,
|
|
413
|
+
"resource_kind": res.kind,
|
|
414
|
+
"depends_on": res.depends_on,
|
|
415
|
+
}
|
|
416
|
+
),
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
db.record_run(
|
|
420
|
+
id=apply_result.started_at.replace(":", "-").replace("+", "Z"),
|
|
421
|
+
kind="stack",
|
|
422
|
+
spec_hash=apply_result.plan.spec_hash,
|
|
423
|
+
plan_hash=apply_result.plan.plan_hash,
|
|
424
|
+
status=status,
|
|
425
|
+
started_at=apply_result.started_at,
|
|
426
|
+
finished_at=apply_result.finished_at,
|
|
427
|
+
server_id=server_id,
|
|
428
|
+
metadata_json=json.dumps(
|
|
429
|
+
{
|
|
430
|
+
"stack_name": spec.meta.name,
|
|
431
|
+
"resource_count": len(spec.resources),
|
|
432
|
+
"resources": [r.name for r in spec.resources],
|
|
433
|
+
}
|
|
434
|
+
),
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
def show_server(db: InventoryDB, server_id: str) -> dict | None:
|
|
439
|
+
"""Return server record with its services."""
|
|
440
|
+
server = db.get_server(server_id)
|
|
441
|
+
if server is None:
|
|
442
|
+
return None
|
|
443
|
+
server["services"] = db.get_services(server_id)
|
|
444
|
+
return server
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def list_inventory(db: InventoryDB) -> list[dict]:
|
|
448
|
+
"""Return all active server records."""
|
|
449
|
+
return db.list_servers()
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"""Local SQLite inventory database with versionize historization.
|
|
2
|
+
|
|
3
|
+
Uses Python's built-in sqlite3 — no native dependencies, works on all platforms.
|
|
4
|
+
The schema, temporal versioning pattern, and all queries are identical to the
|
|
5
|
+
former SQLCipher implementation; the commercial edition can swap in encryption
|
|
6
|
+
by replacing `import sqlite3` with `from sqlcipher3 import dbapi2 as sqlite3`
|
|
7
|
+
and adding the two PRAGMA key/cipher_compatibility calls in open().
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import contextlib
|
|
13
|
+
import sqlite3
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
from loft_cli.local.ddl.bootstrap_tables import DOMAIN_TABLES
|
|
17
|
+
from loft_cli.local.ddl.versionize_system import VERSIONIZE_SYSTEM_DDLS
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class InventoryDB:
|
|
21
|
+
"""Local SQLite inventory database with versionize historization."""
|
|
22
|
+
|
|
23
|
+
DEFAULT_DB_PATH = "~/.loft-cli/inventory.db"
|
|
24
|
+
|
|
25
|
+
def __init__(self, db_path: str | None = None) -> None:
|
|
26
|
+
self._path = Path(db_path or self.DEFAULT_DB_PATH).expanduser()
|
|
27
|
+
self._conn = None
|
|
28
|
+
self._cursor = None
|
|
29
|
+
|
|
30
|
+
# ------------------------------------------------------------------ #
|
|
31
|
+
# Connection lifecycle
|
|
32
|
+
# ------------------------------------------------------------------ #
|
|
33
|
+
|
|
34
|
+
def open(self) -> None:
|
|
35
|
+
"""Open (or create) the SQLite database."""
|
|
36
|
+
self._path.parent.mkdir(parents=True, exist_ok=True)
|
|
37
|
+
self._conn = sqlite3.connect(str(self._path))
|
|
38
|
+
self._cursor = self._conn.cursor()
|
|
39
|
+
|
|
40
|
+
def close(self) -> None:
|
|
41
|
+
"""Close the database connection."""
|
|
42
|
+
if self._conn:
|
|
43
|
+
with contextlib.suppress(Exception):
|
|
44
|
+
self._conn.close()
|
|
45
|
+
self._conn = None
|
|
46
|
+
self._cursor = None
|
|
47
|
+
|
|
48
|
+
def __enter__(self) -> InventoryDB:
|
|
49
|
+
self.open()
|
|
50
|
+
return self
|
|
51
|
+
|
|
52
|
+
def __exit__(self, *_) -> None:
|
|
53
|
+
self.close()
|
|
54
|
+
|
|
55
|
+
# ------------------------------------------------------------------ #
|
|
56
|
+
# Initialization
|
|
57
|
+
# ------------------------------------------------------------------ #
|
|
58
|
+
|
|
59
|
+
def initialize(self) -> None:
|
|
60
|
+
"""Create the versionize system + domain tables + activate historization.
|
|
61
|
+
|
|
62
|
+
Idempotent — safe to call on an already-initialized database.
|
|
63
|
+
"""
|
|
64
|
+
for ddl in VERSIONIZE_SYSTEM_DDLS:
|
|
65
|
+
self._cursor.executescript(ddl)
|
|
66
|
+
|
|
67
|
+
for table_name, table_ddl in DOMAIN_TABLES:
|
|
68
|
+
self._cursor.executescript(table_ddl)
|
|
69
|
+
self._versionize_table(table_name)
|
|
70
|
+
|
|
71
|
+
def _versionize_table(self, table_name: str) -> None:
|
|
72
|
+
"""Activate versionize historization on a tv_* table."""
|
|
73
|
+
result = self._conn.execute(
|
|
74
|
+
"SELECT COUNT(*) FROM sqlite_master WHERE type='view' AND name='vv' || SUBSTR(?, 3);",
|
|
75
|
+
(table_name,),
|
|
76
|
+
)
|
|
77
|
+
if result.fetchone()[0] > 0:
|
|
78
|
+
return
|
|
79
|
+
|
|
80
|
+
self._cursor.executescript(
|
|
81
|
+
f"INSERT INTO t_versionize_jobs (table_name) VALUES ('{table_name}');"
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
result = self._conn.execute(
|
|
85
|
+
"SELECT view_ddl, trigger_insert_ddl, trigger_update_ddl, trigger_delete_ddl "
|
|
86
|
+
"FROM t_versionize_ddl WHERE table_name = ?;",
|
|
87
|
+
(table_name,),
|
|
88
|
+
)
|
|
89
|
+
ddl_row = result.fetchone()
|
|
90
|
+
if ddl_row is None:
|
|
91
|
+
raise RuntimeError(f"versionize trigger did not generate DDL for {table_name}")
|
|
92
|
+
|
|
93
|
+
view_ddl, trigger_insert_ddl, trigger_update_ddl, trigger_delete_ddl = ddl_row
|
|
94
|
+
|
|
95
|
+
self._cursor.executescript(view_ddl)
|
|
96
|
+
self._cursor.executescript(trigger_insert_ddl)
|
|
97
|
+
self._cursor.executescript(trigger_update_ddl)
|
|
98
|
+
self._cursor.executescript(trigger_delete_ddl)
|
|
99
|
+
|
|
100
|
+
self._cursor.executescript(
|
|
101
|
+
f"DELETE FROM t_versionize_jobs WHERE table_name = '{table_name}';"
|
|
102
|
+
f"DELETE FROM t_versionize_ddl WHERE table_name = '{table_name}';"
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
# ------------------------------------------------------------------ #
|
|
106
|
+
# Server CRUD (via vv_server view)
|
|
107
|
+
# ------------------------------------------------------------------ #
|
|
108
|
+
|
|
109
|
+
def upsert_server(
|
|
110
|
+
self,
|
|
111
|
+
id: str,
|
|
112
|
+
name: str,
|
|
113
|
+
address: str,
|
|
114
|
+
bootstrap_status: str,
|
|
115
|
+
os_family: str = "",
|
|
116
|
+
ssh_alias: str = "",
|
|
117
|
+
ssh_host: str = "",
|
|
118
|
+
ssh_user: str = "",
|
|
119
|
+
ssh_port: int | None = None,
|
|
120
|
+
ssh_identity_file: str = "",
|
|
121
|
+
wireguard_enabled: bool = False,
|
|
122
|
+
wireguard_interface: str = "",
|
|
123
|
+
wireguard_address: str = "",
|
|
124
|
+
changed_by: str = "loft-cli",
|
|
125
|
+
) -> None:
|
|
126
|
+
"""INSERT INTO vv_server — versionize trigger handles history."""
|
|
127
|
+
self._conn.execute(
|
|
128
|
+
"""
|
|
129
|
+
INSERT INTO vv_server (
|
|
130
|
+
id, name, address, os_family, bootstrap_status,
|
|
131
|
+
ssh_alias, ssh_host, ssh_user, ssh_port, ssh_identity_file,
|
|
132
|
+
wireguard_enabled, wireguard_interface, wireguard_address,
|
|
133
|
+
version_changed_by
|
|
134
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
135
|
+
""",
|
|
136
|
+
(
|
|
137
|
+
id,
|
|
138
|
+
name,
|
|
139
|
+
address,
|
|
140
|
+
os_family,
|
|
141
|
+
bootstrap_status,
|
|
142
|
+
ssh_alias,
|
|
143
|
+
ssh_host,
|
|
144
|
+
ssh_user,
|
|
145
|
+
ssh_port,
|
|
146
|
+
ssh_identity_file,
|
|
147
|
+
1 if wireguard_enabled else 0,
|
|
148
|
+
wireguard_interface,
|
|
149
|
+
wireguard_address,
|
|
150
|
+
changed_by,
|
|
151
|
+
),
|
|
152
|
+
)
|
|
153
|
+
self._conn.commit()
|
|
154
|
+
|
|
155
|
+
def get_server(self, server_id: str) -> dict | None:
|
|
156
|
+
result = self._conn.execute("SELECT * FROM vv_server WHERE id = ?", (server_id,))
|
|
157
|
+
row = result.fetchone()
|
|
158
|
+
if row is None:
|
|
159
|
+
return None
|
|
160
|
+
cols = [d[0] for d in result.description]
|
|
161
|
+
return dict(zip(cols, row, strict=False))
|
|
162
|
+
|
|
163
|
+
def list_servers(self) -> list[dict]:
|
|
164
|
+
result = self._conn.execute("SELECT * FROM vv_server ORDER BY name")
|
|
165
|
+
cols = [d[0] for d in result.description]
|
|
166
|
+
return [dict(zip(cols, row, strict=False)) for row in result.fetchall()]
|
|
167
|
+
|
|
168
|
+
# ------------------------------------------------------------------ #
|
|
169
|
+
# Service CRUD (via vv_server_service view)
|
|
170
|
+
# ------------------------------------------------------------------ #
|
|
171
|
+
|
|
172
|
+
def upsert_service(
|
|
173
|
+
self,
|
|
174
|
+
server_id: str,
|
|
175
|
+
service_type: str,
|
|
176
|
+
service_name: str,
|
|
177
|
+
status: str,
|
|
178
|
+
metadata_json: str = "",
|
|
179
|
+
changed_by: str = "loft-cli",
|
|
180
|
+
) -> None:
|
|
181
|
+
self._conn.execute(
|
|
182
|
+
"""
|
|
183
|
+
INSERT INTO vv_server_service (
|
|
184
|
+
server_id, service_type, service_name, status, metadata_json,
|
|
185
|
+
version_changed_by
|
|
186
|
+
) VALUES (?, ?, ?, ?, ?, ?)
|
|
187
|
+
""",
|
|
188
|
+
(server_id, service_type, service_name, status, metadata_json, changed_by),
|
|
189
|
+
)
|
|
190
|
+
self._conn.commit()
|
|
191
|
+
|
|
192
|
+
def get_services(self, server_id: str) -> list[dict]:
|
|
193
|
+
result = self._conn.execute(
|
|
194
|
+
"SELECT * FROM vv_server_service WHERE server_id = ?", (server_id,)
|
|
195
|
+
)
|
|
196
|
+
cols = [d[0] for d in result.description]
|
|
197
|
+
return [dict(zip(cols, row, strict=False)) for row in result.fetchall()]
|
|
198
|
+
|
|
199
|
+
# ------------------------------------------------------------------ #
|
|
200
|
+
# Run metadata (via vv_run view)
|
|
201
|
+
# ------------------------------------------------------------------ #
|
|
202
|
+
|
|
203
|
+
def record_run(
|
|
204
|
+
self,
|
|
205
|
+
id: str,
|
|
206
|
+
kind: str,
|
|
207
|
+
spec_hash: str,
|
|
208
|
+
plan_hash: str,
|
|
209
|
+
status: str,
|
|
210
|
+
started_at: str,
|
|
211
|
+
finished_at: str = "",
|
|
212
|
+
server_id: str = "",
|
|
213
|
+
metadata_json: str = "",
|
|
214
|
+
changed_by: str = "loft-cli",
|
|
215
|
+
) -> None:
|
|
216
|
+
self._conn.execute(
|
|
217
|
+
"""
|
|
218
|
+
INSERT INTO vv_run (
|
|
219
|
+
id, server_id, spec_hash, plan_hash, kind, status,
|
|
220
|
+
started_at, finished_at, metadata_json, version_changed_by
|
|
221
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
222
|
+
""",
|
|
223
|
+
(
|
|
224
|
+
id,
|
|
225
|
+
server_id,
|
|
226
|
+
spec_hash,
|
|
227
|
+
plan_hash,
|
|
228
|
+
kind,
|
|
229
|
+
status,
|
|
230
|
+
started_at,
|
|
231
|
+
finished_at,
|
|
232
|
+
metadata_json,
|
|
233
|
+
changed_by,
|
|
234
|
+
),
|
|
235
|
+
)
|
|
236
|
+
self._conn.commit()
|
|
237
|
+
|
|
238
|
+
def get_run(self, run_id: str) -> dict | None:
|
|
239
|
+
result = self._conn.execute("SELECT * FROM vv_run WHERE id = ?", (run_id,))
|
|
240
|
+
row = result.fetchone()
|
|
241
|
+
if row is None:
|
|
242
|
+
return None
|
|
243
|
+
cols = [d[0] for d in result.description]
|
|
244
|
+
return dict(zip(cols, row, strict=False))
|
|
245
|
+
|
|
246
|
+
def list_runs(self, server_id: str | None = None) -> list[dict]:
|
|
247
|
+
if server_id:
|
|
248
|
+
result = self._conn.execute(
|
|
249
|
+
"SELECT * FROM vv_run WHERE server_id = ? ORDER BY started_at DESC",
|
|
250
|
+
(server_id,),
|
|
251
|
+
)
|
|
252
|
+
else:
|
|
253
|
+
result = self._conn.execute("SELECT * FROM vv_run ORDER BY started_at DESC")
|
|
254
|
+
cols = [d[0] for d in result.description]
|
|
255
|
+
return [dict(zip(cols, row, strict=False)) for row in result.fetchall()]
|