uncloud/uncloud_net/tasks.py

46 lines
1.1 KiB
Python
Raw Normal View History

from celery import shared_task
from .models import *
2020-12-13 18:50:36 +00:00
import os
@shared_task
def whereami():
print(os.uname())
return os.uname()
@shared_task
def configure_wireguard_server(vpnpool):
print(f"Configuring {vpnpool.vpn_server_hostname}")
wireguard_config_filename = '/etc/wireguard/{}.conf'.format(vpnpool.network)
@property
def wireguard_config(self):
wireguard_config = [
"""
[Interface]
ListenPort = 51820
PrivateKey = {privatekey}
""".format(privatekey=self.wireguard_private_key) ]
peers = []
for reservation in self.vpnnetworkreservation_set.filter(status='used'):
public_key = reservation.vpnnetwork_set.first().wireguard_public_key
peer_network = "{}/{}".format(reservation.address, self.subnetwork_size)
owner = reservation.vpnnetwork_set.first().owner
peers.append("""
# Owner: {owner}
[Peer]
PublicKey = {public_key}
AllowedIPs = {peer_network}
""".format(
owner=owner,
public_key=public_key,
peer_network=peer_network))
wireguard_config.extend(peers)
return "\n".join(wireguard_config)