2020-12-13 17:34:43 +00:00
|
|
|
from celery import shared_task
|
|
|
|
from .models import *
|
|
|
|
|
2020-12-13 18:50:36 +00:00
|
|
|
import os
|
2020-12-20 12:00:36 +00:00
|
|
|
import subprocess
|
2020-12-13 18:50:36 +00:00
|
|
|
|
|
|
|
@shared_task
|
|
|
|
def whereami():
|
|
|
|
print(os.uname())
|
|
|
|
return os.uname()
|
|
|
|
|
2020-12-20 11:20:54 +00:00
|
|
|
def configure_wireguard_server(wireguardvpnpool):
|
|
|
|
"""
|
|
|
|
- Create wireguard config (DB query -> string)
|
|
|
|
- Submit config to cdist worker
|
|
|
|
- Change config locally on worker / commit / shared
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
config = wireguardvpnpool.wireguard_config
|
|
|
|
server = wireguardvpnpool.vpn_server_hostname
|
|
|
|
|
2020-12-20 11:54:02 +00:00
|
|
|
print(f"Configuring VPN server {server} (async)")
|
|
|
|
# cdist_configure_wireguard_server.apply_async((config, server), queue='cdist')
|
|
|
|
cdist_configure_wireguard_server.apply_async((config, server))
|
2020-12-20 11:20:54 +00:00
|
|
|
|
2020-12-13 17:34:43 +00:00
|
|
|
@shared_task
|
2020-12-20 11:20:54 +00:00
|
|
|
def cdist_configure_wireguard_server(config, server):
|
|
|
|
"""
|
|
|
|
Create config and configure server.
|
|
|
|
|
|
|
|
To be executed on the cdist workers.
|
|
|
|
"""
|
|
|
|
|
2020-12-20 12:00:36 +00:00
|
|
|
dirname= "/home/app/.cdist/type/__ungleich_wireguard/files/"
|
|
|
|
fname = os.path.join(dirname,server)
|
2020-12-20 11:20:54 +00:00
|
|
|
|
2020-12-20 11:54:02 +00:00
|
|
|
print(f"Configuring VPN server {server} (on cdist host)")
|
2020-12-20 11:20:54 +00:00
|
|
|
with open(fname, "w") as fd:
|
|
|
|
fd.write(config)
|
2020-12-20 11:45:36 +00:00
|
|
|
|
|
|
|
|
2020-12-20 12:00:36 +00:00
|
|
|
subprocess.run(f"cd ${dirname} && git pull && git add {server} && git commit -m 'Updating config for ${server}' && git push",
|
|
|
|
shell=True)
|
|
|
|
|
|
|
|
subprocess.run(f"cdist config -vv {server}", shell=True)
|
|
|
|
|
|
|
|
# FIXME:
|
|
|
|
# ensure logs are on the server
|
|
|
|
# ensure exit codes are known
|
2020-12-20 11:45:36 +00:00
|
|
|
return "All good"
|