diff --git a/setup/create-servers.py b/setup/create-servers.py new file mode 100644 index 0000000..4891bec --- /dev/null +++ b/setup/create-servers.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +import yaml +import requests + +ips = {} +config = yaml.load(open('hetzner-config.yaml', 'r')) +headers = {"Authorization": "Bearer {}".format(config['api_key'])} + + +def create_server(server_name, server_type='cx11', server_location='ngb1'): + location = input(server_name + ' location: ') + frontend = { + "name": server_name, + "server_type": server_type, + "location": server_location, + "start_after_create": True, + "image": "debian-9", + "ssh_keys": [key.get('name') for key in config.get('ssh_keys')], + "user_data": "#cloud-config\nruncmd:\n- [touch, /root/cloud-init-worked]\n", + "automount": False, + } + resp = requests.post('https://api.hetzner.cloud/v1/servers', headers=headers, json=frontend) + ip = resp.json().get('server').get('public_net').get('ipv4').get('ip') + frontend_dns = { + "ip": ip, + "dns_ptr": "{}.{}".format(server_name, config.get('domain')) + } + requests.post('https://api.hetzner.cloud/v1/servers/{}/actions/change_dns_ptr'.format( + resp.json().get('server').get('id') + ), headers=headers, json=frontend_dns) + return ip + + +for ssh_key in config['ssh_keys']: + requests.post('https://api.hetzner.cloud/v1/ssh_keys', headers=headers, json={ + "name": ssh_key.get('name'), + "public_key": ssh_key.get('pubkey'), + }) + +locations_req = requests.get('https://api.hetzner.cloud/v1/locations', headers=headers) +locations = [loc['name'] for loc in locations_req.json().get('locations')] +print('valid locations: {}'.format(locations)) + + +for host in ['control01', 'control02', 'control03']: + ips[host] = server_create(host) + + +print("YOUR NEW INVENTORY") +print("[frontend]") +print("cluster-frontend ansible_ssh_host={}".format(ips['frontend'])) + +print("[control]") +print("cluster-control01 ansible_ssh_host={}".format(ips['control01'])) +print("#cluster-control02 ansible_ssh_host={}".format(ips['control02'])) +print("#cluster-control03 ansible_ssh_host={}".format(ips['control03'])) + diff --git a/setup/hetzner-config.yaml b/setup/hetzner-config.yaml new file mode 100644 index 0000000..79312bd --- /dev/null +++ b/setup/hetzner-config.yaml @@ -0,0 +1,5 @@ +api_key: "123" +domain: 'cluster.kritis.systems' +ssh_keys: + - name: your_key + pubkey: ssh-ed25519 AAAA1