wip: support for wireguard config generator

This commit is contained in:
2024-07-27 08:13:02 +00:00
parent fd61f28cd5
commit 32f0fa4512
3 changed files with 60 additions and 0 deletions

1
.gitignore vendored
View File

@@ -7,3 +7,4 @@ __pycache__/
password.txt
ssh_config_for_clients
virsh_backup
.wireguard_keys

View File

@@ -6,3 +6,6 @@
cd ansible/files/
git clone git@git_atlantis_hub_content.atlantishq.de:FAUSheppy/atlantis-hub-content.git
# Requires
apt install wireguard

56
wireguard.py Normal file
View File

@@ -0,0 +1,56 @@
import jinja2
import subprocess
import os
key_cache_dir = ".wireguard_keys/"
def generate_wireguard_keypair(hostname):
# create & sanity check filename & create dir #
assert(hostname.replace(".", "").isalnum())
filename = os.path.join(key_cache_dir, hostname)
os.makedirs(key_cache_dir, exist_ok=True)
# return cache if exists #
if os.path.isfile(filename):
with open(filename) as key_file:
return key_file.read().strip("\n").split(" ")
# otherwise generate private & public key #
private_key = subprocess.check_output(['wg', 'genkey']).strip()
public_key = subprocess.check_output(['wg', 'pubkey'], input=private_key).strip()
# encode
private_key = private_key.decode('utf-8')
public_key = public_key.decode('utf-8')
# save in key cache #
with open(filename, "w") as key_file:
key_file.write(private_key)
key_file.write(" ")
key_file.write(public_key)
return private_key, public_key
def dump_config(vm_list):
vms_sorted_by_ip = sorted(vm_list, key=lambda x: x.ip)
clients = []
for vmo in vms_sorted_by_ip:
private_key, public_key = generate_wireguard_keypair(vmo.hostname)
clients.append({
"name" : vmo.hostname,
"private_key" : private_key
"public_key" : public_key
})
# dump wireguard vars for ansible #
with open("./ansible/vers/wireguard.yaml", "w") as f:
pass
# dump hypervisor config #
with open("/etc/wireguard/hypervisor.conf") as f:
pass