2024-07-18 16:58:17 +00:00
|
|
|
import subprocess
|
|
|
|
import configparser
|
|
|
|
from hcloud import Client
|
|
|
|
import argparse
|
|
|
|
import pathlib
|
2024-07-18 17:07:02 +00:00
|
|
|
import sys
|
2024-07-18 16:58:17 +00:00
|
|
|
|
|
|
|
UPDATE_SCRIPT_PATH = pathlib.Path(__file__).parent / "update_local_config.py"
|
|
|
|
CONFIG_PATH = pathlib.Path(__file__).parent / "config.ini"
|
|
|
|
|
2024-07-18 17:07:02 +00:00
|
|
|
|
2024-07-18 16:58:17 +00:00
|
|
|
def get_hetzner_client():
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read(CONFIG_PATH)
|
|
|
|
api_token = config["hetzner"]["api_token"]
|
|
|
|
return Client(token=api_token)
|
|
|
|
|
2024-07-18 17:07:02 +00:00
|
|
|
|
2024-07-18 16:58:17 +00:00
|
|
|
def list_hetzner_servers(client):
|
|
|
|
return client.servers.get_all()
|
|
|
|
|
2024-07-18 17:07:02 +00:00
|
|
|
|
2024-07-18 16:58:17 +00:00
|
|
|
def delete_hetzner_server(client, server):
|
|
|
|
server.delete()
|
|
|
|
|
2024-07-18 17:07:02 +00:00
|
|
|
|
2024-07-18 16:58:17 +00:00
|
|
|
def run_update_script(provider, location, server_type, interface, config_path):
|
|
|
|
command = [
|
2024-07-18 17:09:11 +00:00
|
|
|
sys.executable,
|
2024-07-18 17:07:02 +00:00
|
|
|
UPDATE_SCRIPT_PATH,
|
|
|
|
"--provider",
|
|
|
|
provider,
|
|
|
|
"--location",
|
|
|
|
location,
|
|
|
|
"--server_type",
|
|
|
|
server_type,
|
|
|
|
"--interface",
|
|
|
|
interface,
|
2024-07-18 16:58:17 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
if config_path:
|
|
|
|
command.extend(["--config_path", config_path])
|
|
|
|
|
|
|
|
result = subprocess.run(command, capture_output=True, text=True)
|
|
|
|
if result.returncode != 0:
|
|
|
|
raise Exception(f"Error executing update script: {result.stderr}")
|
|
|
|
print(result.stdout)
|
|
|
|
|
2024-07-18 17:07:02 +00:00
|
|
|
|
2024-07-18 16:58:17 +00:00
|
|
|
def main():
|
2024-07-18 17:07:02 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Manage Hetzner servers and update local Wireguard VPN."
|
|
|
|
)
|
2024-07-18 16:58:17 +00:00
|
|
|
parser.add_argument("--location", type=str, required=True, help="Server location")
|
|
|
|
parser.add_argument("--server_type", type=str, required=True, help="Server type")
|
2024-07-18 17:07:02 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--interface", type=str, required=True, help="Wireguard interface (e.g., wg0)"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--config_path", type=str, help="Path to the Wireguard configuration file"
|
|
|
|
)
|
2024-07-18 16:58:17 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2024-07-18 17:02:51 +00:00
|
|
|
provider = "hetzner"
|
2024-07-18 16:58:17 +00:00
|
|
|
location = args.location
|
|
|
|
server_type = args.server_type
|
|
|
|
interface = args.interface
|
|
|
|
config_path = args.config_path
|
|
|
|
|
|
|
|
# Step 1: Get Hetzner client and list existing servers
|
|
|
|
client = get_hetzner_client()
|
|
|
|
existing_servers = list_hetzner_servers(client)
|
|
|
|
|
|
|
|
# Step 2: Run the update_local_config.py script
|
|
|
|
run_update_script(provider, location, server_type, interface, config_path)
|
|
|
|
|
|
|
|
# Step 3: Delete old servers that existed before running the update script
|
|
|
|
for server in existing_servers:
|
|
|
|
delete_hetzner_server(client, server)
|
|
|
|
print(f"Deleted server: {server.name}")
|
|
|
|
|
2024-07-18 17:07:02 +00:00
|
|
|
|
2024-07-18 16:58:17 +00:00
|
|
|
if __name__ == "__main__":
|
2024-07-18 17:07:02 +00:00
|
|
|
main()
|