maproom 0.2.0__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.
- maproom/__init__.py +14 -0
- maproom/__main__.py +10 -0
- maproom/analyze/__init__.py +1 -0
- maproom/analyze/accel_families.py +45 -0
- maproom/analyze/account_azure_activity.py +260 -0
- maproom/analyze/account_azure_quota_requests.py +177 -0
- maproom/analyze/account_cloudtrail.py +316 -0
- maproom/analyze/account_quota_requests.py +180 -0
- maproom/analyze/analyze_v1_orchestration.py +1194 -0
- maproom/analyze/catalog.py +353 -0
- maproom/analyze/change_detection.py +652 -0
- maproom/analyze/context_client.py +99 -0
- maproom/analyze/context_lookup.py +142 -0
- maproom/analyze/cover_analyze.py +344 -0
- maproom/analyze/cover_client.py +65 -0
- maproom/analyze/cover_lookup.py +242 -0
- maproom/analyze/engine.py +644 -0
- maproom/analyze/fleetfit_endpoints.py +217 -0
- maproom/analyze/fleetfit_policy.py +66 -0
- maproom/analyze/instance_price_analyze.py +221 -0
- maproom/analyze/instance_price_client.py +108 -0
- maproom/analyze/instance_price_lookup.py +60 -0
- maproom/analyze/local_reads.py +484 -0
- maproom/analyze/lookup_http.py +280 -0
- maproom/analyze/metric_format.py +20 -0
- maproom/analyze/migration_method.py +130 -0
- maproom/analyze/move_list.py +93 -0
- maproom/analyze/operator_context.py +156 -0
- maproom/analyze/performance_rank.py +97 -0
- maproom/analyze/primary_recommendation.py +422 -0
- maproom/analyze/provider_comparison.py +401 -0
- maproom/analyze/quota_analyze.py +481 -0
- maproom/analyze/quota_client.py +75 -0
- maproom/analyze/quota_lookup.py +110 -0
- maproom/analyze/quota_outcome_steer.py +105 -0
- maproom/analyze/recommend_batch.py +199 -0
- maproom/analyze/recommend_client.py +362 -0
- maproom/analyze/recommend_share.py +118 -0
- maproom/analyze/recommendation.py +591 -0
- maproom/analyze/reservation_pack.py +883 -0
- maproom/analyze/reservation_read.py +461 -0
- maproom/analyze/safe_analyze.py +1134 -0
- maproom/analyze/safe_move.py +195 -0
- maproom/analyze/safe_moves.py +571 -0
- maproom/analyze/safe_moves_catalog.py +127 -0
- maproom/analyze/safe_moves_client.py +70 -0
- maproom/analyze/series_analyze.py +491 -0
- maproom/analyze/series_client.py +106 -0
- maproom/analyze/series_lookup.py +663 -0
- maproom/analyze/shape.py +254 -0
- maproom/analyze/size_analyze.py +883 -0
- maproom/analyze/size_client.py +81 -0
- maproom/analyze/size_keep.py +409 -0
- maproom/analyze/size_lookup.py +368 -0
- maproom/analyze/specs.py +325 -0
- maproom/analyze/spot_analyze.py +392 -0
- maproom/analyze/spot_client.py +74 -0
- maproom/analyze/spot_lookup.py +213 -0
- maproom/analyze/token_client.py +97 -0
- maproom/analyze/utilization.py +190 -0
- maproom/analyze/workload.py +104 -0
- maproom/analyze/workset.py +762 -0
- maproom/aws_region.py +78 -0
- maproom/cache/__init__.py +1 -0
- maproom/cache/catalog.py +372 -0
- maproom/cache/commands.py +215 -0
- maproom/cache/instances.py +460 -0
- maproom/cli.py +2084 -0
- maproom/cli_argv.py +329 -0
- maproom/cli_floors.py +89 -0
- maproom/cliio/__init__.py +1 -0
- maproom/cliio/registry.py +377 -0
- maproom/cliio/runner.py +489 -0
- maproom/consent/__init__.py +33 -0
- maproom/consent/gate.py +238 -0
- maproom/consent/policy.py +20 -0
- maproom/consent/prompt.py +61 -0
- maproom/consent/service_upload_gate.py +318 -0
- maproom/consent/service_upload_prompt.py +62 -0
- maproom/consent/stdin.py +45 -0
- maproom/consent/storage.py +24 -0
- maproom/consent/write_token.py +29 -0
- maproom/contracts/__init__.py +6 -0
- maproom/contracts/schema_diff.py +119 -0
- maproom/contracts/validate.py +113 -0
- maproom/data/__init__.py +1 -0
- maproom/data/clear.py +76 -0
- maproom/data/commands.py +14 -0
- maproom/doctor/__init__.py +1 -0
- maproom/doctor/aws_session.py +226 -0
- maproom/doctor/checks.py +701 -0
- maproom/doctor/context.py +32 -0
- maproom/doctor/display.py +96 -0
- maproom/doctor/inventory_display.py +377 -0
- maproom/doctor/runner.py +1467 -0
- maproom/errors.py +43 -0
- maproom/exitcodes.py +70 -0
- maproom/inventory/__init__.py +1 -0
- maproom/inventory/account_identity.py +79 -0
- maproom/inventory/accounts.py +155 -0
- maproom/inventory/command.py +961 -0
- maproom/inventory/messages.py +264 -0
- maproom/inventory/reservations_refresh.py +37 -0
- maproom/mock_environment/__init__.py +28 -0
- maproom/mock_environment/load.py +241 -0
- maproom/mock_environment/runner.py +387 -0
- maproom/output/__init__.py +1 -0
- maproom/output/json_envelope.py +241 -0
- maproom/output/redact.py +11 -0
- maproom/output/terminal.py +129 -0
- maproom/parsers.py +284 -0
- maproom/paths.py +105 -0
- maproom/plan/__init__.py +1 -0
- maproom/plan/accept.py +59 -0
- maproom/plan/analysis_context.py +78 -0
- maproom/plan/analyze_selection.py +279 -0
- maproom/plan/commands.py +586 -0
- maproom/plan/current.py +59 -0
- maproom/plan/errors.py +10 -0
- maproom/plan/export.py +36 -0
- maproom/plan/formatting.py +91 -0
- maproom/plan/ids.py +13 -0
- maproom/plan/migration_options.py +75 -0
- maproom/plan/model.py +265 -0
- maproom/plan/names.py +24 -0
- maproom/plan/store.py +193 -0
- maproom/plan/tty.py +34 -0
- maproom/pricing/__init__.py +5 -0
- maproom/pricing/prices.py +119 -0
- maproom/privacy/__init__.py +6 -0
- maproom/privacy/commands.py +159 -0
- maproom/privacy/delete_uploads.py +204 -0
- maproom/privacy/fleetfit_policy.py +125 -0
- maproom/privacy/fleetfit_statement.py +104 -0
- maproom/privacy/format.py +76 -0
- maproom/privacy/manifests.py +342 -0
- maproom/privacy/statement.py +58 -0
- maproom/quotas/__init__.py +21 -0
- maproom/quotas/aws.py +204 -0
- maproom/quotas/azure.py +75 -0
- maproom/quotas/commands.py +240 -0
- maproom/quotas/family_coverage.py +122 -0
- maproom/quotas/fetch.py +124 -0
- maproom/quotas/load.py +41 -0
- maproom/quotas/model.py +178 -0
- maproom/quotas/outcome_ledger.py +245 -0
- maproom/quotas/quota_request_sync.py +221 -0
- maproom/quotas/show.py +73 -0
- maproom/quotas/status.py +206 -0
- maproom/quotas/store.py +161 -0
- maproom/redaction.py +157 -0
- maproom/reference_data/__init__.py +1 -0
- maproom/reference_data/aws/aws_prices.csv +25562 -0
- maproom/reference_data/aws/capabilities.csv +1346 -0
- maproom/reference_data/aws/instance_types.csv +1346 -0
- maproom/reference_data/azure/azure_prices.csv +1 -0
- maproom/reference_data/azure/vm_types.csv +1 -0
- maproom/reference_data/benchmarks/cpu-lscpu-v2-instances.json +27 -0
- maproom/reference_data/benchmarks/safe-moves-v1.json +328 -0
- maproom/reference_data/manifest.json +174 -0
- maproom/reference_data/manifest.py +69 -0
- maproom/reservations/__init__.py +17 -0
- maproom/reservations/aws.py +76 -0
- maproom/reservations/azure.py +36 -0
- maproom/reservations/commands.py +149 -0
- maproom/reservations/load.py +41 -0
- maproom/reservations/model.py +316 -0
- maproom/reservations/show.py +64 -0
- maproom/reservations/status.py +181 -0
- maproom/reservations/store.py +391 -0
- maproom/resources.py +54 -0
- maproom/root_help.py +251 -0
- maproom/saving_plans/__init__.py +15 -0
- maproom/saving_plans/aws.py +73 -0
- maproom/saving_plans/commands.py +122 -0
- maproom/saving_plans/load.py +41 -0
- maproom/saving_plans/model.py +101 -0
- maproom/saving_plans/show.py +64 -0
- maproom/saving_plans/status.py +118 -0
- maproom/saving_plans/store.py +173 -0
- maproom/secret_egress_gate.py +108 -0
- maproom/service/install_id.py +60 -0
- maproom/settings/__init__.py +1 -0
- maproom/settings/defaults.py +73 -0
- maproom/settings/init.py +143 -0
- maproom/settings/model.py +105 -0
- maproom/settings/product_version_change.py +343 -0
- maproom/settings/product_versions.py +68 -0
- maproom/settings/set.py +347 -0
- maproom/settings/show.py +227 -0
- maproom/settings/write.py +273 -0
- maproom/snapshot_time.py +27 -0
- maproom-0.2.0.dist-info/METADATA +263 -0
- maproom-0.2.0.dist-info/RECORD +199 -0
- maproom-0.2.0.dist-info/WHEEL +5 -0
- maproom-0.2.0.dist-info/entry_points.txt +2 -0
- maproom-0.2.0.dist-info/licenses/LICENSE +201 -0
- maproom-0.2.0.dist-info/licenses/NOTICE +72 -0
- maproom-0.2.0.dist-info/top_level.txt +1 -0
maproom/plan/commands.py
ADDED
|
@@ -0,0 +1,586 @@
|
|
|
1
|
+
"""Plan subcommand implementations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
from typing import Any, Callable
|
|
8
|
+
|
|
9
|
+
from maproom.exitcodes import SUCCESS, USAGE, exit_code_for_error
|
|
10
|
+
from maproom.output.json_envelope import (
|
|
11
|
+
build_document,
|
|
12
|
+
build_error_document,
|
|
13
|
+
command_metadata,
|
|
14
|
+
print_error_envelope,
|
|
15
|
+
print_json,
|
|
16
|
+
)
|
|
17
|
+
from maproom.plan import current as current_plan
|
|
18
|
+
from maproom.plan import store as plan_store
|
|
19
|
+
from maproom.plan.errors import PlanError
|
|
20
|
+
from maproom.plan.model import PLAN_ADD_ITEMS_HINT
|
|
21
|
+
from maproom.plan.export import export_plan_markdown
|
|
22
|
+
from maproom.plan.formatting import format_item_tty_lines
|
|
23
|
+
from maproom.plan.names import validate_plan_name
|
|
24
|
+
from maproom.plan.tty import PromptClosed, confirm_yes_no
|
|
25
|
+
|
|
26
|
+
_plan_command_started_at: datetime | None = None
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def begin_plan_command() -> datetime:
|
|
30
|
+
"""Record command start for plan JSON metadata.duration_ms."""
|
|
31
|
+
global _plan_command_started_at
|
|
32
|
+
started = datetime.now(timezone.utc)
|
|
33
|
+
_plan_command_started_at = started
|
|
34
|
+
return started
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _plan_started_at() -> datetime:
|
|
38
|
+
if _plan_command_started_at is not None:
|
|
39
|
+
return _plan_command_started_at
|
|
40
|
+
return datetime.now(timezone.utc)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def plan_summary_line(name: str | None, item_count: int | None = None) -> str:
|
|
44
|
+
if not name:
|
|
45
|
+
return "plan: none"
|
|
46
|
+
if item_count is None:
|
|
47
|
+
loaded = plan_store.load_plan(name)
|
|
48
|
+
if isinstance(loaded, PlanError):
|
|
49
|
+
return "plan: none"
|
|
50
|
+
item_count = len(loaded.items)
|
|
51
|
+
return f'plan "{name}" ({item_count} items)'
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _usage(message: str, *, hint: str | None = None) -> int:
|
|
55
|
+
print(f"error: {message}", file=sys.stderr)
|
|
56
|
+
if hint:
|
|
57
|
+
print(f"hint: {hint}", file=sys.stderr)
|
|
58
|
+
return USAGE
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _plan_error_code(message: str) -> str:
|
|
62
|
+
if "corrupt plan file" in message:
|
|
63
|
+
return "runtime"
|
|
64
|
+
if message.startswith("could not read plan"):
|
|
65
|
+
return "runtime"
|
|
66
|
+
if message.startswith("could not write plan"):
|
|
67
|
+
return "runtime"
|
|
68
|
+
return "usage"
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _fail_plan(message: str, *, as_json: bool, invoked: str) -> int:
|
|
72
|
+
code = _plan_error_code(message)
|
|
73
|
+
if as_json:
|
|
74
|
+
return print_error_envelope(
|
|
75
|
+
code,
|
|
76
|
+
message,
|
|
77
|
+
invoked=invoked,
|
|
78
|
+
metadata=command_metadata(invoked, started_at=_plan_started_at()),
|
|
79
|
+
)
|
|
80
|
+
print(message, file=sys.stderr)
|
|
81
|
+
return exit_code_for_error(code)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _print_current_change(previous: str | None, new: str) -> None:
|
|
85
|
+
if previous == new:
|
|
86
|
+
return
|
|
87
|
+
if previous:
|
|
88
|
+
print(
|
|
89
|
+
f'Current plan is now "{new}" (was "{previous}"). '
|
|
90
|
+
"New analyze selections target this plan."
|
|
91
|
+
)
|
|
92
|
+
else:
|
|
93
|
+
print(
|
|
94
|
+
f'Current plan is now "{new}". '
|
|
95
|
+
"New analyze selections target this plan."
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def cmd_plan_show_current(*, as_json: bool = False) -> int:
|
|
100
|
+
name = current_plan.get_current_plan_name()
|
|
101
|
+
if not name:
|
|
102
|
+
if as_json:
|
|
103
|
+
print_json(
|
|
104
|
+
build_error_document(
|
|
105
|
+
"no_current_plan",
|
|
106
|
+
"No current plan.",
|
|
107
|
+
invoked="plan",
|
|
108
|
+
hint="Create one with: maproom plan create {name}",
|
|
109
|
+
)
|
|
110
|
+
)
|
|
111
|
+
return SUCCESS
|
|
112
|
+
print("No current plan.")
|
|
113
|
+
print("Create one with: maproom plan create {name}")
|
|
114
|
+
return SUCCESS
|
|
115
|
+
record = plan_store.load_plan(name)
|
|
116
|
+
if isinstance(record, PlanError):
|
|
117
|
+
return _fail_plan(record.message, as_json=as_json, invoked="plan")
|
|
118
|
+
if as_json:
|
|
119
|
+
_print_plan_document(
|
|
120
|
+
"plan_show",
|
|
121
|
+
"maproom.plan_show",
|
|
122
|
+
"plan",
|
|
123
|
+
_plan_record_payload(record),
|
|
124
|
+
)
|
|
125
|
+
return SUCCESS
|
|
126
|
+
_print_plan_tty(record)
|
|
127
|
+
return SUCCESS
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _print_plan_document(
|
|
131
|
+
schema_type: str,
|
|
132
|
+
schema_id: str,
|
|
133
|
+
invoked: str,
|
|
134
|
+
payload: dict[str, Any],
|
|
135
|
+
*,
|
|
136
|
+
warnings: list[dict[str, Any]] | None = None,
|
|
137
|
+
) -> None:
|
|
138
|
+
print_json(
|
|
139
|
+
build_document(
|
|
140
|
+
schema_type=schema_type,
|
|
141
|
+
schema_id=schema_id,
|
|
142
|
+
metadata=command_metadata(invoked, started_at=_plan_started_at()),
|
|
143
|
+
errors=[],
|
|
144
|
+
warnings=list(warnings or []),
|
|
145
|
+
payload=payload,
|
|
146
|
+
)
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _plan_record_payload(record: Any) -> dict[str, Any]:
|
|
151
|
+
return {
|
|
152
|
+
"name": record.name,
|
|
153
|
+
"revision": record.revision,
|
|
154
|
+
"updated_at": record.updated_at,
|
|
155
|
+
"items": [item.to_dict() for item in record.items],
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _plan_name_label(name: str) -> str:
|
|
160
|
+
current = current_plan.get_current_plan_name()
|
|
161
|
+
suffix = " (current)" if name == current else ""
|
|
162
|
+
return f'Plan "{name}"{suffix}'
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _print_plan_tty(record: Any) -> None:
|
|
166
|
+
current = current_plan.get_current_plan_name()
|
|
167
|
+
title = f"Plan: {record.name}"
|
|
168
|
+
if record.name == current:
|
|
169
|
+
title += " (current)"
|
|
170
|
+
print(title)
|
|
171
|
+
print(f"Revision: {record.revision}")
|
|
172
|
+
print(f"Updated: {record.updated_at}")
|
|
173
|
+
print(f"Items: {len(record.items)}")
|
|
174
|
+
if len(record.items) == 0:
|
|
175
|
+
print(PLAN_ADD_ITEMS_HINT)
|
|
176
|
+
return
|
|
177
|
+
for item in record.items:
|
|
178
|
+
print(
|
|
179
|
+
f" {item.item_id}: {item.resource} -> {item.instance_type} "
|
|
180
|
+
f"({item.candidate_id})"
|
|
181
|
+
)
|
|
182
|
+
for line in format_item_tty_lines(item):
|
|
183
|
+
print(line)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def cmd_plan_list(*, as_json: bool = False) -> int:
|
|
187
|
+
current_name = current_plan.get_current_plan_name()
|
|
188
|
+
summaries = plan_store.list_plans(current_name)
|
|
189
|
+
if isinstance(summaries, PlanError):
|
|
190
|
+
return _fail_plan(summaries.message, as_json=as_json, invoked="plan list")
|
|
191
|
+
if as_json:
|
|
192
|
+
_print_plan_document(
|
|
193
|
+
"plan_list",
|
|
194
|
+
"maproom.plan_list",
|
|
195
|
+
"plan list",
|
|
196
|
+
{
|
|
197
|
+
"current": current_name,
|
|
198
|
+
"plans": [
|
|
199
|
+
{
|
|
200
|
+
"name": summary.name,
|
|
201
|
+
"item_count": summary.item_count,
|
|
202
|
+
"updated_at": summary.updated_at,
|
|
203
|
+
"is_current": summary.is_current,
|
|
204
|
+
}
|
|
205
|
+
for summary in summaries
|
|
206
|
+
],
|
|
207
|
+
},
|
|
208
|
+
)
|
|
209
|
+
return SUCCESS
|
|
210
|
+
if not summaries:
|
|
211
|
+
print("No plans.")
|
|
212
|
+
print("Create one with: maproom plan create {name}")
|
|
213
|
+
return SUCCESS
|
|
214
|
+
for summary in summaries:
|
|
215
|
+
marker = " (current)" if summary.is_current else ""
|
|
216
|
+
print(
|
|
217
|
+
f"{summary.name}: {summary.item_count} items, "
|
|
218
|
+
f"updated {summary.updated_at}{marker}"
|
|
219
|
+
)
|
|
220
|
+
return SUCCESS
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def cmd_plan_show(name: str, *, as_json: bool = False) -> int:
|
|
224
|
+
record = plan_store.load_plan(name)
|
|
225
|
+
if isinstance(record, PlanError):
|
|
226
|
+
return _fail_plan(record.message, as_json=as_json, invoked="plan show")
|
|
227
|
+
if as_json:
|
|
228
|
+
_print_plan_document(
|
|
229
|
+
"plan_show",
|
|
230
|
+
"maproom.plan_show",
|
|
231
|
+
"plan show",
|
|
232
|
+
_plan_record_payload(record),
|
|
233
|
+
)
|
|
234
|
+
return SUCCESS
|
|
235
|
+
_print_plan_tty(record)
|
|
236
|
+
return SUCCESS
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def cmd_plan_use(
|
|
240
|
+
name: str,
|
|
241
|
+
*,
|
|
242
|
+
stdin_is_tty: bool,
|
|
243
|
+
prompt_reader: Callable[[], str] | None = None,
|
|
244
|
+
as_json: bool = False,
|
|
245
|
+
) -> int:
|
|
246
|
+
invoked = "plan use"
|
|
247
|
+
_MISSING_HINT = (
|
|
248
|
+
"See existing plans with: maproom plan list, or create it with: "
|
|
249
|
+
"maproom plan create NAME"
|
|
250
|
+
)
|
|
251
|
+
validated = validate_plan_name(name)
|
|
252
|
+
if isinstance(validated, PlanError):
|
|
253
|
+
if as_json:
|
|
254
|
+
return _fail_plan(validated.message, as_json=True, invoked=invoked)
|
|
255
|
+
return _usage(validated.message, hint="maproom plan -h")
|
|
256
|
+
previous = current_plan.get_current_plan_name()
|
|
257
|
+
path = plan_store._plan_path(validated)
|
|
258
|
+
if not path.is_file():
|
|
259
|
+
message = f'plan "{validated}" does not exist.'
|
|
260
|
+
if as_json:
|
|
261
|
+
return _fail_plan(message, as_json=True, invoked=invoked)
|
|
262
|
+
return _usage(message, hint=_MISSING_HINT)
|
|
263
|
+
if previous == validated:
|
|
264
|
+
if as_json:
|
|
265
|
+
_print_plan_document(
|
|
266
|
+
"plan_use",
|
|
267
|
+
"maproom.plan_use",
|
|
268
|
+
invoked,
|
|
269
|
+
{
|
|
270
|
+
"name": validated,
|
|
271
|
+
"previous": previous,
|
|
272
|
+
"changed": False,
|
|
273
|
+
},
|
|
274
|
+
)
|
|
275
|
+
return SUCCESS
|
|
276
|
+
print(
|
|
277
|
+
f'Current plan is already "{validated}". '
|
|
278
|
+
"New analyze selections target this plan."
|
|
279
|
+
)
|
|
280
|
+
return SUCCESS
|
|
281
|
+
err = current_plan.set_current_plan_name(validated)
|
|
282
|
+
if err is not None:
|
|
283
|
+
if as_json:
|
|
284
|
+
return _fail_plan(err.message, as_json=True, invoked=invoked)
|
|
285
|
+
return _usage(err.message, hint="maproom plan -h")
|
|
286
|
+
if as_json:
|
|
287
|
+
_print_plan_document(
|
|
288
|
+
"plan_use",
|
|
289
|
+
"maproom.plan_use",
|
|
290
|
+
invoked,
|
|
291
|
+
{
|
|
292
|
+
"name": validated,
|
|
293
|
+
"previous": previous,
|
|
294
|
+
"changed": True,
|
|
295
|
+
},
|
|
296
|
+
)
|
|
297
|
+
return SUCCESS
|
|
298
|
+
_print_current_change(previous, validated)
|
|
299
|
+
return SUCCESS
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def cmd_plan_create(name: str, *, as_json: bool = False) -> int:
|
|
303
|
+
validated = validate_plan_name(name)
|
|
304
|
+
if isinstance(validated, PlanError):
|
|
305
|
+
return _fail_plan(validated.message, as_json=as_json, invoked="plan create")
|
|
306
|
+
previous = current_plan.get_current_plan_name()
|
|
307
|
+
created = plan_store.create_plan(validated)
|
|
308
|
+
if isinstance(created, PlanError):
|
|
309
|
+
return _fail_plan(created.message, as_json=as_json, invoked="plan create")
|
|
310
|
+
record = created.record
|
|
311
|
+
err = current_plan.set_current_plan_name(validated)
|
|
312
|
+
if err is not None:
|
|
313
|
+
return _fail_plan(err.message, as_json=as_json, invoked="plan create")
|
|
314
|
+
if as_json:
|
|
315
|
+
warnings: list[dict[str, Any]] = []
|
|
316
|
+
if not created.created:
|
|
317
|
+
count = len(record.items)
|
|
318
|
+
noun = "item" if count == 1 else "items"
|
|
319
|
+
warnings.append(
|
|
320
|
+
{
|
|
321
|
+
"code": "plan_exists",
|
|
322
|
+
"message": f'Plan "{validated}" already exists ({count} {noun}).',
|
|
323
|
+
}
|
|
324
|
+
)
|
|
325
|
+
_print_plan_document(
|
|
326
|
+
"plan_show",
|
|
327
|
+
"maproom.plan_show",
|
|
328
|
+
"plan create",
|
|
329
|
+
_plan_record_payload(record),
|
|
330
|
+
warnings=warnings,
|
|
331
|
+
)
|
|
332
|
+
return SUCCESS
|
|
333
|
+
if created.created:
|
|
334
|
+
print(f'Created plan "{validated}".')
|
|
335
|
+
_print_current_change(previous, validated)
|
|
336
|
+
print(PLAN_ADD_ITEMS_HINT)
|
|
337
|
+
return SUCCESS
|
|
338
|
+
count = len(record.items)
|
|
339
|
+
noun = "item" if count == 1 else "items"
|
|
340
|
+
print(f'Plan "{validated}" already exists ({count} {noun}).')
|
|
341
|
+
if previous == validated:
|
|
342
|
+
print("It is the current plan. Nothing was changed.")
|
|
343
|
+
return SUCCESS
|
|
344
|
+
_print_current_change(previous, validated)
|
|
345
|
+
return SUCCESS
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def cmd_plan_export(fmt: str) -> int:
|
|
349
|
+
name = current_plan.get_current_plan_name()
|
|
350
|
+
if not name:
|
|
351
|
+
return _usage("no current plan. Create one with: maproom plan create {name}")
|
|
352
|
+
record = plan_store.load_plan(name)
|
|
353
|
+
if isinstance(record, PlanError):
|
|
354
|
+
return _usage(record.message)
|
|
355
|
+
if fmt == "json":
|
|
356
|
+
_print_plan_document(
|
|
357
|
+
"plan_export",
|
|
358
|
+
"maproom.plan_export",
|
|
359
|
+
"plan export",
|
|
360
|
+
_plan_record_payload(record),
|
|
361
|
+
)
|
|
362
|
+
else:
|
|
363
|
+
print(export_plan_markdown(record), end="")
|
|
364
|
+
return SUCCESS
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def cmd_plan_remove(
|
|
368
|
+
item_id: str | None,
|
|
369
|
+
*,
|
|
370
|
+
force: bool,
|
|
371
|
+
stdin_is_tty: bool,
|
|
372
|
+
prompt_reader: Callable[[], str] | None = None,
|
|
373
|
+
as_json: bool = False,
|
|
374
|
+
) -> int:
|
|
375
|
+
name = current_plan.get_current_plan_name()
|
|
376
|
+
if not name:
|
|
377
|
+
return _fail_plan(
|
|
378
|
+
"no current plan. Create one with: maproom plan create {name}",
|
|
379
|
+
as_json=as_json,
|
|
380
|
+
invoked="plan remove",
|
|
381
|
+
)
|
|
382
|
+
record = plan_store.load_plan(name)
|
|
383
|
+
if isinstance(record, PlanError):
|
|
384
|
+
return _fail_plan(record.message, as_json=as_json, invoked="plan remove")
|
|
385
|
+
if not item_id:
|
|
386
|
+
if as_json:
|
|
387
|
+
return _fail_plan(
|
|
388
|
+
"item id is required with --json. "
|
|
389
|
+
"Run maproom plan show --json to list item ids.",
|
|
390
|
+
as_json=True,
|
|
391
|
+
invoked="plan remove",
|
|
392
|
+
)
|
|
393
|
+
if not stdin_is_tty:
|
|
394
|
+
return _fail_plan(
|
|
395
|
+
"item id is required outside a TTY. Run maproom plan to list item IDs.",
|
|
396
|
+
as_json=as_json,
|
|
397
|
+
invoked="plan remove",
|
|
398
|
+
)
|
|
399
|
+
if not record.items:
|
|
400
|
+
print(f"{_plan_name_label(name)} has no items. Nothing to remove.")
|
|
401
|
+
print(PLAN_ADD_ITEMS_HINT)
|
|
402
|
+
return SUCCESS
|
|
403
|
+
for index, item in enumerate(record.items, start=1):
|
|
404
|
+
print(
|
|
405
|
+
f" {index}. {item.item_id}: {item.resource} -> {item.instance_type}"
|
|
406
|
+
)
|
|
407
|
+
read_prompt = prompt_reader or input
|
|
408
|
+
try:
|
|
409
|
+
choice = read_prompt("Remove which item? [number]: ").strip()
|
|
410
|
+
except EOFError:
|
|
411
|
+
return _fail_plan(
|
|
412
|
+
"confirmation closed before an answer. Pass --force to skip the prompt.",
|
|
413
|
+
as_json=as_json,
|
|
414
|
+
invoked="plan remove",
|
|
415
|
+
)
|
|
416
|
+
except KeyboardInterrupt:
|
|
417
|
+
return _fail_plan(
|
|
418
|
+
"confirmation cancelled.",
|
|
419
|
+
as_json=as_json,
|
|
420
|
+
invoked="plan remove",
|
|
421
|
+
)
|
|
422
|
+
if not choice.isdigit():
|
|
423
|
+
return SUCCESS
|
|
424
|
+
pick = int(choice)
|
|
425
|
+
if pick < 1 or pick > len(record.items):
|
|
426
|
+
return SUCCESS
|
|
427
|
+
item_id = record.items[pick - 1].item_id
|
|
428
|
+
target = next((item for item in record.items if item.item_id == item_id), None)
|
|
429
|
+
if target is None:
|
|
430
|
+
return _fail_plan(
|
|
431
|
+
f'item "{item_id}" not found in plan "{name}"',
|
|
432
|
+
as_json=as_json,
|
|
433
|
+
invoked="plan remove",
|
|
434
|
+
)
|
|
435
|
+
if not as_json:
|
|
436
|
+
print(
|
|
437
|
+
f'Remove {target.item_id}: {target.resource} -> {target.instance_type} '
|
|
438
|
+
f'from plan "{name}"?'
|
|
439
|
+
)
|
|
440
|
+
if not force:
|
|
441
|
+
try:
|
|
442
|
+
confirmed = confirm_yes_no("Confirm removal", default_no=True)
|
|
443
|
+
except PromptClosed as exc:
|
|
444
|
+
return _fail_plan(exc.message, as_json=as_json, invoked="plan remove")
|
|
445
|
+
if not confirmed:
|
|
446
|
+
return SUCCESS
|
|
447
|
+
updated = plan_store.remove_item(name, item_id)
|
|
448
|
+
if isinstance(updated, PlanError):
|
|
449
|
+
return _fail_plan(updated.message, as_json=as_json, invoked="plan remove")
|
|
450
|
+
if as_json:
|
|
451
|
+
_print_plan_document(
|
|
452
|
+
"plan_remove",
|
|
453
|
+
"maproom.plan_remove",
|
|
454
|
+
"plan remove",
|
|
455
|
+
{
|
|
456
|
+
"name": name,
|
|
457
|
+
"item_id": item_id,
|
|
458
|
+
"revision": updated.revision,
|
|
459
|
+
"updated_at": updated.updated_at,
|
|
460
|
+
},
|
|
461
|
+
)
|
|
462
|
+
return SUCCESS
|
|
463
|
+
print(f'Removed {item_id} from plan "{name}".')
|
|
464
|
+
return SUCCESS
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
def cmd_plan_clear(
|
|
468
|
+
name: str,
|
|
469
|
+
*,
|
|
470
|
+
force: bool,
|
|
471
|
+
stdin_is_tty: bool,
|
|
472
|
+
) -> int:
|
|
473
|
+
record = plan_store.load_plan(name)
|
|
474
|
+
if isinstance(record, PlanError):
|
|
475
|
+
return _usage(record.message)
|
|
476
|
+
if not record.items:
|
|
477
|
+
print(f"{_plan_name_label(name)} is already empty. Nothing to clear.")
|
|
478
|
+
return SUCCESS
|
|
479
|
+
count = len(record.items)
|
|
480
|
+
noun = "item" if count == 1 else "items"
|
|
481
|
+
print(f'Plan "{record.name}" has {count} {noun}.')
|
|
482
|
+
if not force:
|
|
483
|
+
if not stdin_is_tty:
|
|
484
|
+
return _usage(
|
|
485
|
+
f'confirm clear for plan "{name}" in a TTY or pass --force'
|
|
486
|
+
)
|
|
487
|
+
try:
|
|
488
|
+
confirmed = confirm_yes_no(
|
|
489
|
+
f'Clear all items from plan "{name}"?', default_no=True
|
|
490
|
+
)
|
|
491
|
+
except PromptClosed as exc:
|
|
492
|
+
return _usage(exc.message)
|
|
493
|
+
if not confirmed:
|
|
494
|
+
return SUCCESS
|
|
495
|
+
updated = plan_store.clear_plan(name)
|
|
496
|
+
if isinstance(updated, PlanError):
|
|
497
|
+
return _usage(updated.message)
|
|
498
|
+
print(f'Cleared all items from plan "{name}" (revision {updated.revision}).')
|
|
499
|
+
return SUCCESS
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
def _normalize_plan_tokens(
|
|
503
|
+
plan_args: list[str],
|
|
504
|
+
*,
|
|
505
|
+
create: bool,
|
|
506
|
+
force: bool,
|
|
507
|
+
export_format: str,
|
|
508
|
+
) -> tuple[list[str], bool, bool, str]:
|
|
509
|
+
raw = [token for token in plan_args if token]
|
|
510
|
+
tokens: list[str] = []
|
|
511
|
+
fmt = export_format
|
|
512
|
+
create_flag = create
|
|
513
|
+
force_flag = force
|
|
514
|
+
index = 0
|
|
515
|
+
while index < len(raw):
|
|
516
|
+
token = raw[index]
|
|
517
|
+
if token == "--create":
|
|
518
|
+
create_flag = True
|
|
519
|
+
elif token == "--force":
|
|
520
|
+
force_flag = True
|
|
521
|
+
elif token == "--format" and index + 1 < len(raw):
|
|
522
|
+
fmt = raw[index + 1]
|
|
523
|
+
index += 1
|
|
524
|
+
elif token.startswith("--format="):
|
|
525
|
+
fmt = token.split("=", 1)[1]
|
|
526
|
+
else:
|
|
527
|
+
tokens.append(token)
|
|
528
|
+
index += 1
|
|
529
|
+
return tokens, create_flag, force_flag, fmt
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
def dispatch_plan(
|
|
533
|
+
plan_args: list[str],
|
|
534
|
+
*,
|
|
535
|
+
create: bool,
|
|
536
|
+
force: bool,
|
|
537
|
+
export_format: str,
|
|
538
|
+
stdin_is_tty: bool,
|
|
539
|
+
prompt_reader: Callable[[], str] | None = None,
|
|
540
|
+
) -> int:
|
|
541
|
+
tokens, create, force, export_format = _normalize_plan_tokens(
|
|
542
|
+
plan_args,
|
|
543
|
+
create=create,
|
|
544
|
+
force=force,
|
|
545
|
+
export_format=export_format,
|
|
546
|
+
)
|
|
547
|
+
if not tokens:
|
|
548
|
+
return cmd_plan_show_current()
|
|
549
|
+
head = tokens[0]
|
|
550
|
+
if head == "list":
|
|
551
|
+
return cmd_plan_list()
|
|
552
|
+
if head == "show":
|
|
553
|
+
if len(tokens) < 2:
|
|
554
|
+
return _usage("usage: maproom plan show NAME")
|
|
555
|
+
return cmd_plan_show(tokens[1])
|
|
556
|
+
if head == "use":
|
|
557
|
+
if len(tokens) < 2:
|
|
558
|
+
return _usage("usage: maproom plan use NAME")
|
|
559
|
+
return cmd_plan_use(
|
|
560
|
+
tokens[1],
|
|
561
|
+
stdin_is_tty=stdin_is_tty,
|
|
562
|
+
prompt_reader=prompt_reader,
|
|
563
|
+
)
|
|
564
|
+
if head == "export":
|
|
565
|
+
return cmd_plan_export(export_format)
|
|
566
|
+
if head == "remove":
|
|
567
|
+
item_id = tokens[1] if len(tokens) > 1 else None
|
|
568
|
+
return cmd_plan_remove(
|
|
569
|
+
item_id,
|
|
570
|
+
force=force,
|
|
571
|
+
stdin_is_tty=stdin_is_tty,
|
|
572
|
+
prompt_reader=prompt_reader,
|
|
573
|
+
)
|
|
574
|
+
name = head
|
|
575
|
+
tail = tokens[1:]
|
|
576
|
+
if create:
|
|
577
|
+
return cmd_plan_create(name)
|
|
578
|
+
if tail == ["clear"]:
|
|
579
|
+
return cmd_plan_clear(name, force=force, stdin_is_tty=stdin_is_tty)
|
|
580
|
+
if tail:
|
|
581
|
+
return _usage(f"unknown plan command: {' '.join(tokens)}")
|
|
582
|
+
return cmd_plan_use(
|
|
583
|
+
name,
|
|
584
|
+
stdin_is_tty=stdin_is_tty,
|
|
585
|
+
prompt_reader=prompt_reader,
|
|
586
|
+
)
|
maproom/plan/current.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Current-plan pointer under plans/_current.json."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import tempfile
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from maproom.paths import prepare_data_write, readable_data_file
|
|
11
|
+
from maproom.plan.errors import PlanError
|
|
12
|
+
from maproom.plan.names import validate_plan_name
|
|
13
|
+
from maproom.plan.store import _read_json, plans_dir
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _current_path() -> Path:
|
|
17
|
+
return plans_dir() / "_current.json"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _atomic_write_pointer(path: Path, name: str) -> PlanError | None:
|
|
21
|
+
payload = {"name": name}
|
|
22
|
+
try:
|
|
23
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
24
|
+
fd, tmp_name = tempfile.mkstemp(
|
|
25
|
+
dir=path.parent,
|
|
26
|
+
prefix="._current-",
|
|
27
|
+
suffix=".tmp",
|
|
28
|
+
)
|
|
29
|
+
tmp_path = Path(tmp_name)
|
|
30
|
+
try:
|
|
31
|
+
with os.fdopen(fd, "w", encoding="utf-8") as handle:
|
|
32
|
+
json.dump(payload, handle, indent=2)
|
|
33
|
+
handle.write("\n")
|
|
34
|
+
os.replace(tmp_path, path)
|
|
35
|
+
except OSError:
|
|
36
|
+
tmp_path.unlink(missing_ok=True)
|
|
37
|
+
raise
|
|
38
|
+
except OSError as exc:
|
|
39
|
+
return PlanError(f"could not write current plan pointer at {path}: {exc}")
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def get_current_plan_name() -> str | None:
|
|
44
|
+
path = readable_data_file("plans", "_current.json")
|
|
45
|
+
if not path.is_file():
|
|
46
|
+
return None
|
|
47
|
+
data = _read_json(path)
|
|
48
|
+
if isinstance(data, PlanError):
|
|
49
|
+
return None
|
|
50
|
+
name = data.get("name")
|
|
51
|
+
return str(name) if isinstance(name, str) and name else None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def set_current_plan_name(name: str) -> PlanError | None:
|
|
55
|
+
validated = validate_plan_name(name)
|
|
56
|
+
if isinstance(validated, PlanError):
|
|
57
|
+
return validated
|
|
58
|
+
prepare_data_write()
|
|
59
|
+
return _atomic_write_pointer(_current_path(), validated)
|
maproom/plan/errors.py
ADDED
maproom/plan/export.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Plan export formatting."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from maproom.plan.formatting import format_item_markdown_sections
|
|
9
|
+
from maproom.plan.model import PlanRecord, SCHEMA_VERSION
|
|
10
|
+
from maproom.redaction import redact_tree
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def export_plan_markdown(record: PlanRecord) -> str:
|
|
14
|
+
lines = [
|
|
15
|
+
f"# Plan: {record.name}",
|
|
16
|
+
f"- Revision: {record.revision}",
|
|
17
|
+
f"- Updated: {record.updated_at}",
|
|
18
|
+
"",
|
|
19
|
+
]
|
|
20
|
+
if not record.items:
|
|
21
|
+
lines.append("_No items._")
|
|
22
|
+
return "\n".join(lines) + "\n"
|
|
23
|
+
for item in record.items:
|
|
24
|
+
lines.extend(format_item_markdown_sections(item))
|
|
25
|
+
return "\n".join(lines).rstrip() + "\n"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def export_plan_json(record: PlanRecord) -> str:
|
|
29
|
+
payload: dict[str, Any] = {
|
|
30
|
+
"schema_version": SCHEMA_VERSION,
|
|
31
|
+
"name": record.name,
|
|
32
|
+
"revision": record.revision,
|
|
33
|
+
"updated_at": record.updated_at,
|
|
34
|
+
"items": [item.to_dict() for item in record.items],
|
|
35
|
+
}
|
|
36
|
+
return json.dumps(redact_tree(payload), indent=2) + "\n"
|