YYP0502 commited on
Commit
981ffc0
·
verified ·
1 Parent(s): 8f654f0

Upload run-space.py

Browse files
Files changed (1) hide show
  1. scripts/run-space.py +39 -0
scripts/run-space.py CHANGED
@@ -1,5 +1,6 @@
1
  """Prepare HA configuration and supervise the three Space services."""
2
  import os
 
3
  from pathlib import Path
4
  import yaml
5
  import shutil
@@ -66,6 +67,43 @@ def validate_http(text):
66
  raise ValueError("HTTP proxy configuration must trust local Nginx")
67
 
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  def backup(action):
70
  result = subprocess.run(["python3", BACKUP, action, "--config", str(CONFIG)], timeout=120)
71
  if result.returncode:
@@ -84,6 +122,7 @@ def main():
84
  pending.replace(config_file)
85
  validate_http(config_file.read_text(encoding="utf-8"))
86
  print("HA HTTP configuration verified: port=8123, use_x_forwarded_for=true, trusted_proxy=127.0.0.1", flush=True)
 
87
  components = CONFIG / "custom_components"
88
  components.mkdir(exist_ok=True)
89
  for source in Path("/opt/ha-custom-components").iterdir():
 
1
  """Prepare HA configuration and supervise the three Space services."""
2
  import os
3
+ import json
4
  from pathlib import Path
5
  import yaml
6
  import shutil
 
67
  raise ValueError("HTTP proxy configuration must trust local Nginx")
68
 
69
 
70
+ def prepare_http_storage(config_dir):
71
+ """Sync migrated HA HTTP storage before HA starts; preserve all other settings."""
72
+ path = config_dir / ".storage" / "http"
73
+ if not path.exists():
74
+ print("No stored HTTP configuration; HA will import YAML settings.", flush=True)
75
+ return
76
+ document = json.loads(path.read_text(encoding="utf-8"))
77
+ version = document.get("version")
78
+ data = document.get("data")
79
+ if not isinstance(data, dict):
80
+ raise ValueError("Invalid .storage/http payload; refusing to start")
81
+ settings = {
82
+ "server_port": 8123,
83
+ "use_x_forwarded_for": True,
84
+ "trusted_proxies": ["127.0.0.1/32", "::1/128", "10.0.0.0/8"],
85
+ }
86
+ if version == 1:
87
+ data.update(settings)
88
+ elif version == 2:
89
+ if not isinstance(data.get("stable"), dict):
90
+ raise ValueError("Invalid stable HTTP storage configuration")
91
+ data["stable"].update(settings)
92
+ data["stable"]["error"] = None
93
+ data["stable"]["error_message"] = None
94
+ # Remove an unconfirmed trial so it cannot override the stable config.
95
+ data["pending"] = None
96
+ else:
97
+ raise ValueError(f"Unsupported HTTP storage version {version}; review before migrating")
98
+ original = path.with_name("http.before-space-proxy.json")
99
+ if not original.exists():
100
+ shutil.copy2(path, original)
101
+ pending = path.with_name("http.space.tmp")
102
+ pending.write_text(json.dumps(document, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
103
+ pending.replace(path)
104
+ print(f"HA HTTP storage v{version} verified: port=8123, use_x_forwarded_for=true, trusted_proxy=127.0.0.1", flush=True)
105
+
106
+
107
  def backup(action):
108
  result = subprocess.run(["python3", BACKUP, action, "--config", str(CONFIG)], timeout=120)
109
  if result.returncode:
 
122
  pending.replace(config_file)
123
  validate_http(config_file.read_text(encoding="utf-8"))
124
  print("HA HTTP configuration verified: port=8123, use_x_forwarded_for=true, trusted_proxy=127.0.0.1", flush=True)
125
+ prepare_http_storage(CONFIG)
126
  components = CONFIG / "custom_components"
127
  components.mkdir(exist_ok=True)
128
  for source in Path("/opt/ha-custom-components").iterdir():