wireguard/celery fixes

This commit is contained in:
Nico Schottelius 2020-12-20 12:45:36 +01:00
parent b3626369a2
commit 2e6c72c093
3 changed files with 14 additions and 11 deletions

View file

@ -55,22 +55,16 @@ class WireGuardVPNPool(models.Model):
@property @property
def wireguard_config(self): def wireguard_config(self):
wireguard_config = [ wireguard_config = [ f"[Interface]\nListenPort = 51820\nPrivateKey = {self.wireguard_private_key}\n" ]
"[Interface]\nListenPort = 51820\nPrivateKey = {self.wireguard_private_key}\n".format(
privatekey=self.wireguard_private_key)
]
peers = [] peers = []
for vpn in self.wireguardvpn_set.all(): for vpn in self.wireguardvpn_set.all():
public_key = vpn.wireguard_public_key public_key = vpn.wireguard_public_key
peer_network = "{}/{}".format(vpn.address, self.subnetwork_mask) peer_network = f"{vpn.address}/{self.subnetwork_mask}"
owner = vpn.owner owner = vpn.owner
peers.append("# Owner: {owner}\n[Peer]\nPublicKey = {public_key}\nAllowedIPs = {peer_network}\n\n".format( peers.append(f"# Owner: {owner}\n[Peer]\nPublicKey = {public_key}\nAllowedIPs = {peer_network}\n\n")
owner=owner,
public_key=public_key,
peer_network=peer_network))
wireguard_config.extend(peers) wireguard_config.extend(peers)

View file

@ -20,7 +20,12 @@ def configure_wireguard_server(wireguardvpnpool):
server = wireguardvpnpool.vpn_server_hostname server = wireguardvpnpool.vpn_server_hostname
print(f"Configuring VPN server {server}") print(f"Configuring VPN server {server}")
cdist_configure_wireguard_server(config, server) #res = cdist_configure_wireguard_server.delay(config, server)
res = cdist_configure_wireguard_server.apply_async((config, server), queue='cdist')
print(f"res={res}")
res2= res.get()
print(f"res2={res2}")
@shared_task @shared_task
@ -33,5 +38,9 @@ def cdist_configure_wireguard_server(config, server):
fname = f"/home/app/.cdist/type/__ungleich_wireguard/files/{server}" fname = f"/home/app/.cdist/type/__ungleich_wireguard/files/{server}"
print(os.uname())
with open(fname, "w") as fd: with open(fname, "w") as fd:
fd.write(config) fd.write(config)
return "All good"

View file

@ -40,7 +40,7 @@ class WireGuardVPNViewSet(viewsets.ModelViewSet):
public_key=serializer.validated_data['wireguard_public_key'], public_key=serializer.validated_data['wireguard_public_key'],
network_mask=serializer.validated_data['network_mask'] network_mask=serializer.validated_data['network_mask']
) )
configure_wireguard_server.apply_async((vpn.vpnpool,)) configure_wireguard_server(vpn.vpnpool)
return Response(WireGuardVPNSerializer(vpn).data) return Response(WireGuardVPNSerializer(vpn).data)