mirror of
https://gh.llkk.cc/https://github.com/WeblateOrg/scripts.git
synced 2025-10-03 15:01:00 +08:00
69 lines
1.7 KiB
Python
Executable file
69 lines
1.7 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
# ruff: noqa: T201,SIM108,TRY003
|
|
"""Removes server form production setup."""
|
|
|
|
from cloudflare import Cloudflare
|
|
import os
|
|
from configparser import ConfigParser
|
|
import sys
|
|
import subprocess
|
|
import json
|
|
|
|
sentry_org = "weblate"
|
|
|
|
if len(sys.argv) not in (2, 3, 4):
|
|
raise ValueError("Usage: remove-server DNS_NAME [PUBLIC_NAME [SERVER_NAME]]")
|
|
|
|
dns_name = f"{sys.argv[1]}.weblate.cloud"
|
|
if len(sys.argv) >= 3: # noqa: PLR2004
|
|
public_name = sys.argv[2]
|
|
else:
|
|
public_name = dns_name
|
|
if len(sys.argv) >= 4: # noqa: PLR2004
|
|
server_name = sys.argv[3]
|
|
else:
|
|
server_name = f"{sys.argv[1]}-weblate"
|
|
|
|
# CloudFlare
|
|
cp = ConfigParser()
|
|
cp.read([os.path.expanduser("~/.cloudflare/cloudflare.cfg")])
|
|
token = cp.get("Cloudflare", "token")
|
|
cf = Cloudflare(api_token=token)
|
|
|
|
zone = cf.zones.list(name="weblate.cloud").result[0]
|
|
|
|
dns_records = list(
|
|
cf.dns.records.list(
|
|
name=dns_name,
|
|
zone_id=zone.id,
|
|
)
|
|
)
|
|
|
|
# Hetzner
|
|
servers_json = subprocess.run(
|
|
["hcloud", "server", "list", "-o", "json"], capture_output=True, check=True
|
|
)
|
|
servers = [
|
|
server
|
|
for server in json.loads(servers_json.stdout)
|
|
if server["name"] == server_name
|
|
]
|
|
|
|
print("== Summary ==")
|
|
print("Sentry environment:", public_name)
|
|
print("DNS entries:", len(dns_records))
|
|
print(dns_records)
|
|
print("Servers:", len(servers))
|
|
print(servers)
|
|
print()
|
|
print("Press Ctrl+C to cancel or Enter to continue...")
|
|
input()
|
|
|
|
print("Removing DNS entries...")
|
|
for record in dns_records:
|
|
cf.dns.records.delete(record.id, zone_id=zone.id)
|
|
print("Removing server...")
|
|
for server in servers:
|
|
subprocess.run(["hcloud", "server", "delete", str(server["id"])], check=True)
|
|
print("Disabling sentry cron monitors...")
|
|
subprocess.sun(["./disable-sentry-monitors", public_name])
|