++celery/tasks

This commit is contained in:
Nico Schottelius 2020-12-20 18:36:46 +01:00
parent 63191c0a88
commit 5e870f04b1
5 changed files with 63 additions and 9 deletions

View file

@ -1,6 +1,6 @@
from django.contrib import admin from django.contrib import admin
from .models import UncloudProvider, UncloudNetwork from .models import *
for m in [ UncloudProvider, UncloudNetwork ]: for m in [ UncloudProvider, UncloudNetwork, UncloudTask ]:
admin.site.register(m) admin.site.register(m)

View file

@ -0,0 +1,19 @@
# Generated by Django 3.1 on 2020-12-20 17:16
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='UncloudTasks',
fields=[
('task_id', models.UUIDField(primary_key=True, serialize=False)),
],
),
]

View file

@ -0,0 +1,17 @@
# Generated by Django 3.1 on 2020-12-20 17:28
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud', '0002_uncloudtasks'),
]
operations = [
migrations.RenameModel(
old_name='UncloudTasks',
new_name='UncloudTask',
),
]

View file

@ -162,3 +162,11 @@ class UncloudProvider(UncloudAddress):
def __str__(self): def __str__(self):
return f"{self.full_name} {self.country}" return f"{self.full_name} {self.country}"
class UncloudTask(models.Model):
"""
Class to store dispatched tasks to be handled
"""
task_id = models.UUIDField(primary_key=True)

View file

@ -1,8 +1,14 @@
from celery import shared_task from celery import shared_task
from .models import * from .models import *
from uncloud.models import UncloudTask
import os import os
import subprocess import subprocess
import logging
import uuid
log = logging.getLogger(__name__)
@shared_task @shared_task
def whereami(): def whereami():
@ -20,9 +26,11 @@ def configure_wireguard_server(wireguardvpnpool):
config = wireguardvpnpool.wireguard_config config = wireguardvpnpool.wireguard_config
server = wireguardvpnpool.vpn_server_hostname server = wireguardvpnpool.vpn_server_hostname
print(f"Configuring VPN server {server} (async)") log.info(f"Configuring VPN server {server} (async)")
# cdist_configure_wireguard_server.apply_async((config, server), queue='cdist')
cdist_configure_wireguard_server.apply_async((config, server)) task_id = uuid.UUID(cdist_configure_wireguard_server.apply_async((config, server)).id)
UncloudTasks.objects.create(task_id=task_id)
@shared_task @shared_task
def cdist_configure_wireguard_server(config, server): def cdist_configure_wireguard_server(config, server):
@ -35,15 +43,17 @@ def cdist_configure_wireguard_server(config, server):
dirname= "/home/app/.cdist/type/__ungleich_wireguard/files/" dirname= "/home/app/.cdist/type/__ungleich_wireguard/files/"
fname = os.path.join(dirname,server) fname = os.path.join(dirname,server)
print(f"Configuring VPN server {server} (on cdist host)") log.info(f"Configuring VPN server {server} (on cdist host)")
with open(fname, "w") as fd: with open(fname, "w") as fd:
fd.write(config) fd.write(config)
subprocess.run(f"cd {dirname} && git pull && git add {server} && git commit -m 'Updating config for ${server}' && git push", log.debug("git committing wireguard changes")
shell=True) subprocess.run(f"cd /aa{dirname} && git pull && git add {server} && git commit -m 'Updating config for ${server}' && git push",
shell=True, check=True)
subprocess.run(f"cdist config -vv {server}", shell=True) log.debug(f"Configuring VPN server {server} with cdist")
subprocess.run(f"cdist config {server}", shell=True, check=True)
# FIXME: # FIXME:
# ensure logs are on the server # ensure logs are on the server