-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_config.py
More file actions
executable file
·374 lines (300 loc) · 13 KB
/
Copy pathdeploy_config.py
File metadata and controls
executable file
·374 lines (300 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env python3
"""
Configuration Deployment Script
One-shot deployment of the complete multiroom audio configuration.
Generates configs, installs them, restarts services, and configures
Snapcast groups via JSON-RPC API.
Deployment steps:
1. Generate ALSA config (per-channel, room, and all_rooms PCM devices)
2. Install to /etc/asound.conf (requires sudo)
3. Generate Snapcast server config (stream sources)
4. Install to /etc/snapserver.conf (requires sudo)
5. Install service templates (snapclient, sendspin)
6. Restart snapserver service
7. Restart snapclient and sendspin services for all rooms
8. Wait for snapclients to connect (timeout: 30s)
9. Configure Snapcast groups via JSON-RPC API based on stream_targets
Prerequisites:
- speaker_config.json must exist (run speaker_identify.py first)
- sudo access for installing configs and restarting services
- snapserver and snapclient services configured
- sendspin installed (pip install sendspin)
- Snapcast JSON-RPC API available on localhost:1705
Usage:
python3 deploy_config.py
The script reads stream_targets from speaker_config.json to determine
which rooms should be assigned to which streams. Each stream target
specifies zones, which are resolved to rooms based on zone membership.
Example stream_targets in speaker_config.json:
"stream_targets": {
"spotify_lou": { "zones": ["kinderzimmer_lou"] },
"spotify_alle": { "zones": ["alle"] }
}
After deployment, test with:
aplay -D room_<roomname> /usr/share/sounds/alsa/Front_Center.wav
"""
import json
import socket
import subprocess
import sys
import time
from pathlib import Path
CONFIG_FILE = Path(__file__).parent / "speaker_config.json"
SERVICES_DIR = Path(__file__).parent / "services"
SNAPCAST_HOST = "localhost"
SNAPCAST_PORT = 1705 # TCP JSON-RPC port
SNAPSERVER_CONF = "/etc/snapserver.conf"
ASOUND_CONF = "/etc/asound.conf"
SYSTEMD_DIR = "/etc/systemd/system"
def load_config() -> dict:
"""Load speaker configuration from JSON."""
if not CONFIG_FILE.exists():
print(f"Error: {CONFIG_FILE} not found.")
sys.exit(1)
with open(CONFIG_FILE) as f:
return json.load(f)
def run_command(cmd: list, check: bool = True) -> subprocess.CompletedProcess:
"""Run a shell command."""
print(f" Running: {' '.join(cmd)}")
return subprocess.run(cmd, capture_output=True, text=True, check=check)
def generate_alsa_config() -> str:
"""Generate ALSA configuration."""
result = run_command(["python3", str(Path(__file__).parent / "generate_alsa_config.py")])
return result.stdout
def generate_snapserver_config() -> str:
"""Generate Snapcast server configuration."""
result = run_command(["python3", str(Path(__file__).parent / "generate_snapserver_conf.py")])
return result.stdout
def install_config(content: str, path: str):
"""Install configuration file (requires sudo)."""
print(f" Installing {path}")
proc = subprocess.Popen(
["sudo", "tee", path],
stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
text=True
)
proc.communicate(input=content)
if proc.returncode != 0:
print(f"Error: Failed to install {path}")
sys.exit(1)
def restart_snapserver():
"""Restart the snapserver service."""
print(" Restarting snapserver...")
run_command(["sudo", "systemctl", "restart", "snapserver"])
time.sleep(2) # Give it time to start
def install_service_templates():
"""Install systemd service templates for snapclient and sendspin."""
templates = ["snapclient@.service", "sendspin@.service"]
for template in templates:
src = SERVICES_DIR / template
if src.exists():
print(f" Installing {template}")
run_command(["sudo", "cp", str(src), SYSTEMD_DIR])
else:
print(f" Warning: {template} not found, skipping")
run_command(["sudo", "systemctl", "daemon-reload"])
def restart_room_services(rooms: list):
"""Restart snapclient and sendspin services for all rooms."""
print(" Restarting snapclient services...")
for room in rooms:
service = f"snapclient@room_{room}.service"
run_command(["sudo", "systemctl", "enable", "--now", service], check=False)
print(" Restarting sendspin services...")
for room in rooms:
service = f"sendspin@room_{room}.service"
run_command(["sudo", "systemctl", "enable", "--now", service], check=False)
def snapcast_request(method: str, params: dict = None) -> dict:
"""Send a JSON-RPC request to Snapcast server."""
request = {
"id": 1,
"jsonrpc": "2.0",
"method": method,
}
if params:
request["params"] = params
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
sock.connect((SNAPCAST_HOST, SNAPCAST_PORT))
sock.sendall((json.dumps(request) + "\r\n").encode())
response = b""
while True:
chunk = sock.recv(4096)
if not chunk:
break
response += chunk
if b"\r\n" in response:
break
sock.close()
return json.loads(response.decode().strip())
except Exception as e:
print(f" Snapcast API error: {e}")
return None
def get_snapcast_status() -> dict:
"""Get current Snapcast server status."""
response = snapcast_request("Server.GetStatus")
if response and "result" in response:
return response["result"]["server"]
return None
def wait_for_clients(expected_rooms: list, timeout: int = 30) -> dict:
"""Wait for expected clients to connect."""
print(f" Waiting for {len(expected_rooms)} clients to connect...")
start = time.time()
while time.time() - start < timeout:
status = get_snapcast_status()
if not status:
time.sleep(1)
continue
# Get all connected client names
connected = set()
for group in status.get("groups", []):
for client in group.get("clients", []):
config = client.get("config", {})
name = config.get("name", "")
if name:
connected.add(name)
# Check if all expected rooms have clients
missing = set(expected_rooms) - connected
if not missing:
print(f" All {len(expected_rooms)} clients connected!")
return status
time.sleep(1)
missing = set(expected_rooms) - connected if connected else set(expected_rooms)
print(f" Timeout waiting for clients. Missing: {missing}")
return get_snapcast_status()
def get_snapcast_stream_name(stream_id: str, stream_config: dict) -> str:
"""Get the Snapcast stream name (matches generate_snapserver_conf.py naming)."""
stream_type = stream_config.get("type", "pipe")
name = stream_config.get("name", stream_id)
if stream_type == "librespot":
return f"Spotify {name}"
elif stream_type == "airplay":
return f"AirPlay {name}"
elif stream_type == "pipe" and stream_id == "default":
return "Default"
else:
return name if name != stream_id else stream_id
def resolve_stream_rooms(config: dict, stream_id: str) -> set:
"""Resolve which rooms should be assigned to a stream based on stream_targets."""
stream_targets = config.get("snapcast", {}).get("stream_targets", {})
zones = config.get("zones", {})
rooms = config.get("rooms", {})
targets = stream_targets.get(stream_id, {})
target_zones = targets.get("zones", [])
target_rooms = set(targets.get("rooms", []))
# Resolve zones to rooms
for zone_id in target_zones:
zone_info = zones.get(zone_id, {})
if zone_info.get("include_all"):
return set(rooms.keys())
# Find rooms in this zone
for room_id, room_info in rooms.items():
if zone_id in room_info.get("zones", []):
target_rooms.add(room_id)
return target_rooms
def configure_snapcast_groups(config: dict, status: dict):
"""Configure Snapcast groups based on stream_targets."""
if not status:
print(" No Snapcast status available, skipping group configuration")
return
rooms = config.get("rooms", {})
streams = config.get("snapcast", {}).get("streams", {})
# Build client -> group map and track group IDs
client_to_group = {}
group_ids = set()
for group in status.get("groups", []):
group_ids.add(group["id"])
for client in group.get("clients", []):
client_id = client.get("id", "")
client_to_group[client_id] = group["id"]
# Set client names based on room display names
print("\n Setting client names...")
for room_id, room_info in rooms.items():
room_display_name = room_info.get("name", room_id.title())
# Find clients for this room
for client_id in client_to_group.keys():
if client_id == f"room_{room_id}":
snapcast_request("Client.SetName", {"id": client_id, "name": room_display_name})
print(f" {client_id} -> '{room_display_name}'")
elif client_id.startswith(f"room_{room_id}_"):
# Cross-device client (e.g., room_kueche_left)
suffix = client_id.split("_")[-1].title()
client_name = f"{room_display_name} {suffix}"
snapcast_request("Client.SetName", {"id": client_id, "name": client_name})
print(f" {client_id} -> '{client_name}'")
# Build room -> stream map using stream_targets (with friendly names)
room_to_stream = {}
for stream_id, stream_config in streams.items():
snapcast_name = get_snapcast_stream_name(stream_id, stream_config)
target_rooms = resolve_stream_rooms(config, stream_id)
for room_id in target_rooms:
# Later streams override earlier ones (allows specific overrides)
room_to_stream[room_id] = snapcast_name
# Get default stream name
default_stream_config = streams.get("default", {"type": "pipe"})
default_stream_name = get_snapcast_stream_name("default", default_stream_config)
# For each room, assign to target stream and set group name
print(f"\n Assigning {len(rooms)} rooms to streams...")
groups_named = set()
for room_id, room_info in rooms.items():
target_stream = room_to_stream.get(room_id, default_stream_name)
room_display_name = room_info.get("name", room_id.title())
# Find all clients for this room (including _left/_right for cross-device)
room_clients = []
for client_id in client_to_group.keys():
if client_id == f"room_{room_id}" or client_id.startswith(f"room_{room_id}_"):
room_clients.append(client_id)
for client_id in room_clients:
group_id = client_to_group.get(client_id)
if group_id:
# Set stream for the group
snapcast_request("Group.SetStream", {"id": group_id, "stream_id": target_stream})
# Set group name to match room display name (only once per group)
if group_id not in groups_named:
snapcast_request("Group.SetName", {"id": group_id, "name": room_display_name})
groups_named.add(group_id)
print(f" {room_display_name} -> {target_stream}")
print("\n Room assignment complete!")
def main():
print("=" * 60)
print("CONFIGURATION DEPLOYMENT")
print("=" * 60)
config = load_config()
rooms = list(config.get("rooms", {}).keys())
# Step 1: Generate and install ALSA config
print("\n[1/7] Generating ALSA configuration...")
alsa_config = generate_alsa_config()
install_config(alsa_config, ASOUND_CONF)
# Step 2: Generate and install Snapcast config
print("\n[2/7] Generating Snapcast configuration...")
snap_config = generate_snapserver_config()
install_config(snap_config, SNAPSERVER_CONF)
# Step 3: Install service templates
print("\n[3/7] Installing service templates...")
install_service_templates()
# Step 4: Restart snapserver
print("\n[4/7] Restarting Snapcast server...")
restart_snapserver()
# Step 5: Restart room services (snapclient + sendspin)
print("\n[5/7] Restarting room services...")
restart_room_services(rooms)
# Step 6: Wait for clients
print("\n[6/7] Waiting for Snapcast clients...")
expected_clients = [f"room_{room}" for room in rooms]
status = wait_for_clients(expected_clients, timeout=30)
# Step 7: Configure groups
print("\n[7/7] Configuring Snapcast groups...")
configure_snapcast_groups(config, status)
print("\n" + "=" * 60)
print("DEPLOYMENT COMPLETE")
print("=" * 60)
print("\nServices running:")
print(" - snapclient@room_* -> localhost (local snapserver)")
print(" - sendspin@room_* -> Lox audioserver (remote)")
print("\nTest with:")
for room in sorted(rooms)[:3]:
print(f" aplay -D room_{room} /usr/share/sounds/alsa/Front_Center.wav")
print(" ...")
if __name__ == "__main__":
main()