forked from uncloud/uncloud
++celery/tasks
This commit is contained in:
parent
63191c0a88
commit
5e870f04b1
5 changed files with 63 additions and 9 deletions
|
@ -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)
|
||||||
|
|
19
uncloud/migrations/0002_uncloudtasks.py
Normal file
19
uncloud/migrations/0002_uncloudtasks.py
Normal 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)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
17
uncloud/migrations/0003_auto_20201220_1728.py
Normal file
17
uncloud/migrations/0003_auto_20201220_1728.py
Normal 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',
|
||||||
|
),
|
||||||
|
]
|
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue