Files
mtusi2026lab5/scripts/bootstrap-broker.py
2026-11-13 12:48:18 +03:00

18 lines
710 B
Python

#!/usr/bin/env python3
"""Create synthetic local broker credentials, preserving existing settings."""
from pathlib import Path
import secrets
from urllib.parse import quote
root=Path(__file__).resolve().parents[1]
env=root/'.env.lab5'
values=dict(line.split('=',1) for line in env.read_text().splitlines() if '=' in line and not line.startswith('#'))
password=values.get('BROKER_PASSWORD')
if not password:
password=secrets.token_hex(24)
with env.open('a') as f: f.write('\nBROKER_PASSWORD='+password+'\n')
p=root/'secrets/lab5/broker_url'
if p.exists(): p.chmod(0o600)
p.write_text('amqp://messenger:'+quote(password,safe='')+'@broker:5672/\n')
p.chmod(0o444)
print('Local broker credentials ready.')