local_tasks.py 3.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

18 19 20
from logging import getLogger
from socket import gethostname

21
import django.conf
22
from django.core.cache import cache
23
from celery.exceptions import TimeoutError
24 25

from manager.mancelery import celery
26
from common.models import WorkerNotFound
27

28
settings = django.conf.settings.FIREWALL_SETTINGS
29
logger = getLogger(__name__)
30

31

32 33 34
def _apply_once(name, queues, task, data):
    """Reload given networking component if needed.
    """
35

36 37 38 39 40
    lockname = "%s_lock" % name
    if not cache.get(lockname):
        return
    cache.delete(lockname)

41
    data = data()
42
    for queue in queues:
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        try:
            task.apply_async(args=data, queue=queue, expires=60).get(timeout=5)
            logger.info("%s configuration is reloaded. (queue: %s)",
                        name, queue)
        except TimeoutError as e:
            logger.critical('%s (queue: %s)', e, queue)
        except:
            logger.critical('Unhandled exception: queue: %s data: %s',
                            queue, data, exc_info=True)


def get_firewall_queues():
    from firewall.models import Firewall
    retval = []
    for fw in Firewall.objects.all():
        try:
            retval.append(fw.get_remote_queue_name('firewall'))
        except WorkerNotFound:
            logger.critical('Firewall %s is offline', fw.name)
    return list(retval)
63

64

65
@celery.task(ignore_result=True)
66
def reloadtask_worker():
67
    from firewall.fw import BuildFirewall, dhcp, dns, ipset, vlan
68 69
    from remote_tasks import (reload_dns, reload_dhcp, reload_firewall,
                              reload_firewall_vlan, reload_blacklist)
70

71
    firewall_queues = get_firewall_queues()
72 73
    dns_queues = [("%s.dns" % i) for i in
                  settings.get('dns_queues', [gethostname()])]
74

75 76 77 78 79
    _apply_once('dns', dns_queues, reload_dns,
                lambda: (dns(), ))
    _apply_once('dhcp', firewall_queues, reload_dhcp,
                lambda: (dhcp(), ))
    _apply_once('firewall', firewall_queues, reload_firewall,
80
                lambda: (BuildFirewall().build_ipt()))
81 82 83 84
    _apply_once('firewall_vlan', firewall_queues, reload_firewall_vlan,
                lambda: (vlan(), ))
    _apply_once('blacklist', firewall_queues, reload_blacklist,
                lambda: (list(ipset()), ))
85

86

87
@celery.task
88
def reloadtask(type='Host', timeout=15):
89 90 91 92 93 94 95 96 97 98 99 100
    reload = {
        'Host': ['dns', 'dhcp', 'firewall'],
        'Record': ['dns'],
        'Domain': ['dns'],
        'Vlan': ['dns', 'dhcp', 'firewall', 'firewall_vlan'],
        'Firewall': ['firewall'],
        'Rule': ['firewall'],
        'SwitchPort': ['firewall_vlan'],
        'EthernetDevice': ['firewall_vlan'],
    }[type]
    logger.info("Reload %s on next periodic iteration applying change to %s.",
                ", ".join(reload), type)
Bach Dániel committed
101
    if all([cache.add("%s_lock" % i, 'true', 30) for i in reload]):
102
        reloadtask_worker.apply_async(queue='localhost.man', countdown=5)