Commit f54804ca by Szabolcs Gelencser

Implement Azure network creation functionality.

parent ab26a51e
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.utils.timezone
import model_utils.fields
class Migration(migrations.Migration):
dependencies = [
('firewall', '0005_auto_20150520_2250'),
]
operations = [
migrations.AddField(
model_name='vlan',
name='status',
field=model_utils.fields.StatusField(default=b'CREATING', max_length=100, verbose_name='status', no_check_for_status=True, choices=[(0, 'dummy')]),
),
migrations.AddField(
model_name='vlan',
name='status_changed',
field=model_utils.fields.MonitorField(default=django.utils.timezone.now, verbose_name='status changed', monitor='status'),
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import model_utils.fields
class Migration(migrations.Migration):
dependencies = [
('firewall', '0006_auto_20161004_2003'),
]
operations = [
migrations.AddField(
model_name='vlan',
name='azure_id',
field=models.TextField(blank=True),
),
migrations.AlterField(
model_name='vlan',
name='status',
field=model_utils.fields.StatusField(default=b'PENDING', max_length=100, verbose_name='status', no_check_for_status=True, choices=[(0, 'dummy')]),
),
]
......@@ -39,13 +39,15 @@ from django.db.models.signals import post_save, post_delete
from celery.exceptions import TimeoutError
from netaddr import IPSet, EUI, IPNetwork, IPAddress, ipv6_full
from model_utils import Choices
from model_utils.models import StatusModel
from common.models import method_cache, WorkerNotFound, HumanSortField
from firewall.tasks.local_tasks import reloadtask
from firewall.tasks.azure_tasks import create_network
from firewall.tasks.remote_tasks import get_dhcp_clients
from .iptables import IptRule
from acl.models import AclBase
logger = logging.getLogger(__name__)
settings = django.conf.settings.FIREWALL_SETTINGS
......@@ -272,7 +274,7 @@ class Rule(models.Model):
)
class Vlan(AclBase, models.Model):
class Vlan(AclBase, StatusModel):
"""
A vlan of the network,
......@@ -291,6 +293,13 @@ class Vlan(AclBase, models.Model):
)
CHOICES_NETWORK_TYPE = (('public', _('public')),
('portforward', _('portforward')))
STATUS = Choices(
('PENDING', _('pending')),
('CREATED', _('created')),
('ERROR', _('error')),
('DESTROYED', _('destroyed')),
)
azure_id = models.TextField(blank=True)
vid = models.IntegerField(unique=True,
verbose_name=_('VID'),
help_text=_('The vlan ID of the subnet.'),
......@@ -988,17 +997,18 @@ class Firewall(models.Model):
@method_cache(20)
def get_dhcp_clients(self):
try:
return get_dhcp_clients.apply_async(
queue=self.get_remote_queue_name(), expires=60).get(timeout=2)
except TimeoutError:
logger.info("get_dhcp_clients task timed out")
except IOError:
logger.exception("get_dhcp_clients failed. "
"maybe syslog isn't readble by firewall worker")
except:
logger.exception("get_dhcp_clients failed")
return {}
#try:
# #return get_dhcp_clients.apply_async(
# queue=self.get_remote_queue_name(), expires=60).get(timeout=2)
#except TimeoutError:
# logger.info("get_dhcp_clients task timed out")
#except IOError:
# logger.exception("get_dhcp_clients failed. "
# "maybe syslog isn't readble by firewall worker")
#except:
# logger.exception("get_dhcp_clients failed")
#return {}
def get_absolute_url(self):
return reverse('network.firewall', kwargs={'pk': self.pk})
......@@ -1198,12 +1208,20 @@ class BlacklistItem(models.Model):
def get_absolute_url(self):
return reverse('network.blacklist', kwargs={'pk': self.pk})
def save_network_sender(sender, instance, created=False, **kwargs):
if created:
logger.debug(
"Send azure create_network task for vlan %s." % instance.name)
azure_id = create_network.apply_async(queue='localhost.firewall',
args=[instance.name, str(instance.network4)]).get(timeout=60)
if azure_id:
logger.debug("created network with id: %s" % str(id))
instance.status = Vlan.STATUS.CREATED
instance.azure_id = azure_id
instance.save()
else:
logger.error("couldn't create network: %s" % instance.name)
instance.status = Vlan.STATUS.ERROR
instance.save()
def send_task(sender, instance, created=False, **kwargs):
reloadtask.apply_async(queue='localhost.man', args=[sender.__name__])
for sender in [Host, Rule, Domain, Record, Vlan, Firewall, Group,
BlacklistItem, SwitchPort, EthernetDevice]:
post_save.connect(send_task, sender=sender)
post_delete.connect(send_task, sender=sender)
post_save.connect(save_network_sender, sender=Vlan)
\ No newline at end of file
# 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/>.
from logging import getLogger
from manager.mancelery import celery
logger = getLogger(__name__)
@celery.task(name='firewall.create_network')
def create_network(params):
pass
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment