local_tasks.py 2.82 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 23 24 25
from django.core.cache import cache

from manager.mancelery import celery

26
settings = django.conf.settings.FIREWALL_SETTINGS
27
logger = getLogger(__name__)
28

29

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

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

    for queue in queues:
40
        task.apply_async(args=data(), queue=queue)
41
    logger.info("%s configuration is reloaded.", name)
42

43

44 45
@celery.task(ignore_result=True)
def periodic_task():
46
    from firewall.fw import BuildFirewall, dhcp, dns, ipset, vlan
47 48
    from remote_tasks import (reload_dns, reload_dhcp, reload_firewall,
                              reload_firewall_vlan, reload_blacklist)
49

50 51 52 53
    firewall_queues = [("%s.firewall" % i) for i in
                       settings.get('firewall_queues', [gethostname()])]
    dns_queues = [("%s.dns" % i) for i in
                  settings.get('dns_queues', [gethostname()])]
54

55 56 57 58 59
    _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,
60
                lambda: (BuildFirewall().build_ipt()))
61 62 63 64
    _apply_once('firewall_vlan', firewall_queues, reload_firewall_vlan,
                lambda: (vlan(), ))
    _apply_once('blacklist', firewall_queues, reload_blacklist,
                lambda: (list(ipset()), ))
65

66

67
@celery.task
68
def reloadtask(type='Host', timeout=15):
69 70 71 72 73 74 75 76 77 78 79 80 81 82
    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)
    for i in reload:
        cache.add("%s_lock" % i, "true", 30)