mirror of
https://github.com/FAUSheppy/athq-vm-management
synced 2025-12-06 13:51:35 +01:00
wip: support for wireguard config generator
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,3 +7,4 @@ __pycache__/
|
|||||||
password.txt
|
password.txt
|
||||||
ssh_config_for_clients
|
ssh_config_for_clients
|
||||||
virsh_backup
|
virsh_backup
|
||||||
|
.wireguard_keys
|
||||||
|
|||||||
@@ -6,3 +6,6 @@
|
|||||||
cd ansible/files/
|
cd ansible/files/
|
||||||
git clone git@git_atlantis_hub_content.atlantishq.de:FAUSheppy/atlantis-hub-content.git
|
git clone git@git_atlantis_hub_content.atlantishq.de:FAUSheppy/atlantis-hub-content.git
|
||||||
|
|
||||||
|
# Requires
|
||||||
|
|
||||||
|
apt install wireguard
|
||||||
|
|||||||
56
wireguard.py
Normal file
56
wireguard.py
Normal 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
|
||||||
Reference in New Issue
Block a user