33from __future__ import annotations
44
55import argparse
6+ import contextlib
7+ import io
68import json
79import re
810import sys
911import time
1012from collections .abc import Sequence
1113from datetime import datetime , timedelta , timezone
14+ from pathlib import Path
1215from typing import Any
1316
1417import docker
1720from pg_stand .assets import initialize_project
1821from pg_stand .cli_parser import _parser
1922from pg_stand .config import StandConfig , load_config
20- from pg_stand .errors import DockerRuntimeError , PgStandError
23+ from pg_stand .errors import DockerRuntimeError , PgStandError , PreconditionError
24+ from pg_stand .orchestration import EXIT_CODES , envelope , static_capabilities
2125from pg_stand .runtime import StandManager , discover_active_stand
2226
2327_DURATION_RE = re .compile (r"^(?P<value>[0-9]+(?:\.[0-9]+)?)(?P<unit>[smhd])$" )
@@ -69,6 +73,40 @@ def _manager(config: StandConfig) -> StandManager:
6973 return StandManager (config , output = lambda message : print (message , file = sys .stderr ))
7074
7175
76+ def _postgres_parameters_override (value : str | None ) -> dict [str , Any ] | None :
77+ if value is None :
78+ return None
79+ try :
80+ if value == "-" :
81+ document = json .load (sys .stdin )
82+ else :
83+ document = json .loads (Path (value ).expanduser ().read_text (encoding = "utf-8" ))
84+ except (OSError , json .JSONDecodeError ) as exc :
85+ raise DockerRuntimeError (f"cannot read PostgreSQL parameters JSON: { exc } " ) from exc
86+ if not isinstance (document , dict ):
87+ raise DockerRuntimeError ("PostgreSQL parameters JSON must be an object" )
88+ configurator_artifact = "postgresql_conf" in document
89+ if configurator_artifact :
90+ document = document ["postgresql_conf" ]
91+ elif isinstance (document .get ("postgresql" ), dict ) and "parameters" in document ["postgresql" ]:
92+ document = document ["postgresql" ]["parameters" ]
93+ if not isinstance (document , dict ):
94+ raise DockerRuntimeError ("PostgreSQL parameters JSON must resolve to a parameter object" )
95+ if configurator_artifact :
96+ document = {
97+ name : _decode_postgresql_conf_value (parameter_value )
98+ for name , parameter_value in document .items ()
99+ }
100+ return document
101+
102+
103+ def _decode_postgresql_conf_value (value : Any ) -> Any :
104+ """Convert pg_configurator's rendered string literals to semantic values."""
105+ if isinstance (value , str ) and len (value ) >= 2 and value .startswith ("'" ) and value .endswith ("'" ):
106+ return value [1 :- 1 ].replace ("''" , "'" )
107+ return value
108+
109+
72110def _watch (manager : StandManager , args : argparse .Namespace , * , replication_only : bool ) -> int :
73111 samples = 0
74112 while args .count == 0 or samples < args .count :
@@ -150,6 +188,13 @@ def _execute(args: argparse.Namespace, config: StandConfig) -> int:
150188 raise DockerRuntimeError (
151189 "apply requires --reload or --restart unless --dry-run is used"
152190 )
191+ if args .plan_hash :
192+ current_plan = manager .plan (requested_mode = requested_mode )
193+ if current_plan ["plan_hash" ] != args .plan_hash :
194+ raise PreconditionError (
195+ "stale plan: expected "
196+ f"{ args .plan_hash } , current plan is { current_plan ['plan_hash' ]} "
197+ )
153198 _emit (
154199 manager .apply (
155200 mode = requested_mode ,
@@ -300,9 +345,165 @@ def _execute(args: argparse.Namespace, config: StandConfig) -> int:
300345 return 0
301346
302347
348+ def _command_name (args : argparse .Namespace ) -> str :
349+ nested = (
350+ "replication_command" ,
351+ "storage_command" ,
352+ "logical_command" ,
353+ "standby_command" ,
354+ "node_command" ,
355+ "image_command" ,
356+ "cleanup_command" ,
357+ )
358+ parts = [args .command ]
359+ parts .extend (str (value ) for name in nested if (value := getattr (args , name , None )))
360+ return " " .join (parts )
361+
362+
363+ def _machine_supported (args : argparse .Namespace ) -> bool :
364+ if args .command in {"psql" , "ssh" , "watch" }:
365+ return False
366+ if args .command == "dsn" and args .show_password :
367+ return False
368+ if args .command in {"logs" , "events" } and args .follow :
369+ return False
370+ return True
371+
372+
373+ def _decode_machine_result (output : str ) -> Any :
374+ text = output .strip ()
375+ if not text :
376+ return None
377+ try :
378+ return json .loads (text )
379+ except json .JSONDecodeError :
380+ return {"stdout" : text }
381+
382+
383+ def _machine_status (args : argparse .Namespace , result : Any , exit_code : int ) -> str :
384+ if args .command == "plan" and isinstance (result , dict ):
385+ if result .get ("required_action" ) == "blocked" :
386+ return "blocked"
387+ if exit_code == 0 :
388+ return "succeeded"
389+ if exit_code in {1 , EXIT_CODES ["partial" ]}:
390+ return "partial"
391+ return "failed"
392+
393+
394+ def _emit_machine (
395+ args : argparse .Namespace ,
396+ * ,
397+ result : Any = None ,
398+ status : str = "succeeded" ,
399+ error : dict [str , Any ] | None = None ,
400+ config : StandConfig | None = None ,
401+ ) -> None :
402+ artifacts = []
403+ if config is not None :
404+ artifacts .append (
405+ {
406+ "kind" : "StandConfiguration" ,
407+ "schema_version" : "pg_stand/v1" ,
408+ "hash" : config .config_hash ,
409+ "path" : str (config .source ),
410+ }
411+ )
412+ _emit (
413+ envelope (
414+ _command_name (args ),
415+ status ,
416+ request_id = args .request_id ,
417+ result = result ,
418+ artifacts = artifacts ,
419+ error = error ,
420+ ),
421+ as_json = True ,
422+ )
423+
424+
425+ def _run_machine (args : argparse .Namespace ) -> int :
426+ if not _machine_supported (args ):
427+ _emit_machine (
428+ args ,
429+ status = "failed" ,
430+ error = {
431+ "code" : "unsupported" ,
432+ "message" : (
433+ "interactive or unbounded streaming command is not available in machine mode"
434+ ),
435+ },
436+ )
437+ return EXIT_CODES ["unsupported" ]
438+
439+ config : StandConfig | None = None
440+ try :
441+ args .json = True
442+ output = io .StringIO ()
443+ with contextlib .redirect_stdout (output ):
444+ if args .command == "active" :
445+ result = discover_active_stand ()
446+ exit_code = 0 if result ["active" ] else 1
447+ print (json .dumps (result ))
448+ elif args .command == "init" :
449+ result = initialize_project (args .directory , force = args .force )
450+ exit_code = 0
451+ print (json .dumps (result ))
452+ elif args .command == "capabilities" and args .config is None :
453+ result = static_capabilities ()
454+ exit_code = 0
455+ print (json .dumps (result ))
456+ else :
457+ if args .config is None :
458+ raise DockerRuntimeError (f"--config is required for { args .command } " )
459+ config = load_config (
460+ args .config ,
461+ postgres_version = args .postgres_version ,
462+ tls_enabled = args .tls ,
463+ postgres_parameters = _postgres_parameters_override (args .parameters_json ),
464+ )
465+ exit_code = _execute (args , config )
466+ result = _decode_machine_result (output .getvalue ())
467+ if args .command == "capabilities" and config is not None and isinstance (result , dict ):
468+ result = {"contract" : static_capabilities (), "target" : result }
469+ status = _machine_status (args , result , exit_code )
470+ _emit_machine (args , result = result , status = status , config = config )
471+ if status == "blocked" :
472+ return EXIT_CODES ["precondition_failed" ]
473+ if exit_code == 1 :
474+ return EXIT_CODES ["partial" ]
475+ return exit_code
476+ except PreconditionError as exc :
477+ _emit_machine (
478+ args ,
479+ status = "blocked" ,
480+ error = {"code" : "precondition_failed" , "message" : str (exc )},
481+ config = config ,
482+ )
483+ return EXIT_CODES ["precondition_failed" ]
484+ except (PgStandError , docker .errors .DockerException ) as exc :
485+ _emit_machine (
486+ args ,
487+ status = "failed" ,
488+ error = {"code" : "execution_error" , "message" : str (exc )},
489+ config = config ,
490+ )
491+ return EXIT_CODES ["execution_error" ]
492+ except KeyboardInterrupt :
493+ _emit_machine (
494+ args ,
495+ status = "cancelled" ,
496+ error = {"code" : "cancelled" , "message" : "interrupted" },
497+ config = config ,
498+ )
499+ return EXIT_CODES ["cancelled" ]
500+
501+
303502def main (argv : Sequence [str ] | None = None ) -> int :
304503 parser = _parser ()
305504 args = parser .parse_args (argv )
505+ if args .machine :
506+ return _run_machine (args )
306507 try :
307508 if args .command == "active" :
308509 result = discover_active_stand ()
@@ -314,12 +515,16 @@ def main(argv: Sequence[str] | None = None) -> int:
314515 as_json = args .json ,
315516 )
316517 return 0
518+ if args .command == "capabilities" and args .config is None :
519+ _emit (static_capabilities (), as_json = args .json )
520+ return 0
317521 if args .config is None :
318522 raise DockerRuntimeError (f"--config is required for { args .command } " )
319523 config = load_config (
320524 args .config ,
321525 postgres_version = args .postgres_version ,
322526 tls_enabled = args .tls ,
527+ postgres_parameters = _postgres_parameters_override (args .parameters_json ),
323528 )
324529 return _execute (args , config )
325530 except (PgStandError , docker .errors .DockerException ) as exc :
0 commit comments