This commit is contained in:
Zsolt Ero
2025-10-16 13:09:47 +02:00
parent 96432037e4
commit 7757c82b59

View File

@@ -1,6 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import json import json
from pprint import pprint
import click import click
@@ -34,6 +33,10 @@ def init_static(hostname, user, port, noninteractive):
run_http_host_sync(c) run_http_host_sync(c)
# Check server health after deployment
results = check_server_health(hostname)
print_server_health(results)
@cli.command() @cli.command()
@common_options @common_options
@@ -55,6 +58,10 @@ def init_autoupdate(hostname, user, port, noninteractive, sync):
put(c, config.local_modules_dir / 'http_host' / 'cron.d' / 'ofm_http_host', '/etc/cron.d/') put(c, config.local_modules_dir / 'http_host' / 'cron.d' / 'ofm_http_host', '/etc/cron.d/')
# Check server health after deployment
results = check_server_health(hostname)
print_server_health(results)
@cli.command() @cli.command()
@common_options @common_options
@@ -65,43 +72,98 @@ def sync(hostname, user, port, noninteractive):
c = get_connection(hostname, user, port) c = get_connection(hostname, user, port)
run_http_host_sync(c) run_http_host_sync(c)
# Check server health after sync
results = check_server_health(hostname)
print_server_health(results)
@cli.command() @cli.command()
def debug(): @click.option('--hostname', help='Check only a specific server')
def debug(hostname):
results = check_server_health(hostname)
print_server_health(results)
def check_server_health(hostname: str = None) -> dict:
"""
Check health of servers by verifying deployed version matches expected version.
Args:
hostname: Optional hostname to check. If None, checks all servers in config.
Returns:
dict: Results for each server with structure:
{
'server_hostname': {
'ip': '1.2.3.4',
'all_ok': True/False,
'domains': {
'domain.com': {'status': 'ok'/'failed', 'error': None/'error message'}
}
}
}
"""
config_data = read_jsonc() config_data = read_jsonc()
area = 'monaco' if config_data.get('skip_planet') else 'planet' area = 'monaco' if config_data.get('skip_planet') else 'planet'
version = get_deployed_version(area)['version'] version = get_deployed_version(area)['version']
domains = [d['domain'] for d in config_data['domains']] domains = [d['domain'] for d in config_data['domains']]
servers = [ servers = [
{'hostname': s['hostname'], 'ip': get_ip_from_ssh_alias(s['hostname'])} {'hostname': s['hostname'], 'ip': get_ip_from_ssh_alias(s['hostname'])}
for s in config_data['servers'] for s in config_data['servers']
] ]
# Filter to specific server if requested
if hostname:
servers = [s for s in servers if s['hostname'] == hostname]
if not servers:
raise ValueError(f'Server {hostname} not found in config')
results = {}
for server in servers: for server in servers:
print(f'SERVER {server["hostname"]} ({server["ip"]})') server_hostname = server['hostname']
server_ok = True server_ip = server['ip']
results[server_hostname] = {'ip': server_ip, 'domains': {}, 'all_ok': True}
for domain in domains: for domain in domains:
try: try:
check_host_using_tilejson( check_host_using_tilejson(
url=f'https://{domain}/{area}/{version}', url=f'https://{domain}/{area}/{version}',
ip=server['ip'], ip=server_ip,
version=version, version=version,
) )
print(f' {domain} {click.style("OK", fg="green")}') results[server_hostname]['domains'][domain] = {'status': 'ok', 'error': None}
except AssertionError: except AssertionError:
print( results[server_hostname]['domains'][domain] = {
f' {domain} {click.style("FAILED", fg="red")} - Version mismatch (expected {version})' 'status': 'failed',
) 'error': f'Version mismatch (expected {version})',
server_ok = False }
results[server_hostname]['all_ok'] = False
except Exception as e: except Exception as e:
print(f' {domain} {click.style("FAILED", fg="red")} - {e}') results[server_hostname]['domains'][domain] = {'status': 'failed', 'error': str(e)}
server_ok = False results[server_hostname]['all_ok'] = False
if server_ok: return results
print(f' {click.style("ALL OK", fg="green")}\n')
def print_server_health(results: dict) -> None:
"""Print server health results in a human-readable format."""
for server_hostname, server_data in results.items():
status = (
click.style('OK', fg='green')
if server_data['all_ok']
else click.style('FAILED', fg='red')
)
print(f'SERVER {server_hostname} ({server_data["ip"]}) {status}')
for domain, domain_data in server_data['domains'].items():
if domain_data['status'] == 'ok':
print(f' {domain} {click.style("OK", fg="green")}')
else: else:
print(f' {click.style("FAILED", fg="red")}\n') print(f' {domain} {click.style("FAILED", fg="red")} - {domain_data["error"]}')
print() # Empty line between servers
def check_host_using_tilejson(*, url: str, ip: str, version: str) -> None: def check_host_using_tilejson(*, url: str, ip: str, version: str) -> None: