Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | from collections import defaultdict |
| 4 | from netaddr import IPAddress, IPNetwork |
| 5 | from django.db.models import F, Q |
| 6 | from planetstack.config import Config |
| 7 | from observer.openstacksyncstep import OpenStackSyncStep |
Sapan Bhatia | 39e8558 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 8 | from core.models.site import Deployment, SiteDeployments |
Sapan Bhatia | bf7856b | 2014-11-19 15:09:38 -0500 | [diff] [blame] | 9 | from core.models.slice import Slice, SliceDeployments |
Sapan Bhatia | 3f87969 | 2014-11-19 15:10:51 -0500 | [diff] [blame] | 10 | from core.models.userdeployments import UserDeployments |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 11 | from util.logger import Logger, logging |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 12 | from observer.ansible import * |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 13 | |
| 14 | logger = Logger(level=logging.INFO) |
| 15 | |
Sapan Bhatia | bf7856b | 2014-11-19 15:09:38 -0500 | [diff] [blame] | 16 | class SyncSliceDeployments(OpenStackSyncStep): |
| 17 | provides=[SliceDeployments] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 18 | requested_interval=0 |
| 19 | |
Sapan Bhatia | d97caf3 | 2014-07-23 09:53:53 -0400 | [diff] [blame] | 20 | def fetch_pending(self, deleted): |
| 21 | if (deleted): |
Sapan Bhatia | bf7856b | 2014-11-19 15:09:38 -0500 | [diff] [blame] | 22 | return SliceDeployments.deleted_objects.all() |
Sapan Bhatia | d97caf3 | 2014-07-23 09:53:53 -0400 | [diff] [blame] | 23 | else: |
Sapan Bhatia | bf7856b | 2014-11-19 15:09:38 -0500 | [diff] [blame] | 24 | return SliceDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 25 | |
| 26 | def get_next_subnet(self, deployment=None): |
| 27 | # limit ourself to 10.0.x.x for now |
| 28 | valid_subnet = lambda net: net.startswith('10.0') |
| 29 | driver = self.driver.admin_driver(deployment=deployment) |
| 30 | subnets = driver.shell.quantum.list_subnets()['subnets'] |
| 31 | ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \ |
| 32 | if valid_subnet(subnet['cidr'])] |
| 33 | ints.sort() |
Tony Mack | 6042aaf | 2014-04-07 19:45:46 -0400 | [diff] [blame] | 34 | if ints: |
| 35 | last_ip = IPAddress(ints[-1]) |
| 36 | else: |
| 37 | last_ip = IPAddress('10.0.0.1') |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 38 | last_ip = IPAddress(ints[-1]) |
| 39 | last_network = IPNetwork(str(last_ip) + "/24") |
| 40 | next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24") |
| 41 | return next_network |
| 42 | |
Tony Mack | 29bf5e8 | 2014-04-29 21:40:24 -0400 | [diff] [blame] | 43 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 44 | def sync_record(self, slice_deployment): |
| 45 | logger.info("sync'ing slice deployment %s" % slice_deployment) |
Scott Baker | 1e10f06 | 2014-09-04 15:13:40 -0700 | [diff] [blame] | 46 | |
| 47 | if not slice_deployment.deployment.admin_user: |
| 48 | logger.info("deployment %r has no admin_user, skipping" % slice_deployment.deployment) |
| 49 | return |
| 50 | |
Sapan Bhatia | 3f87969 | 2014-11-19 15:10:51 -0500 | [diff] [blame] | 51 | deployment_users = UserDeployments.objects.filter(user=slice_deployment.slice.creator, |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 52 | deployment=slice_deployment.deployment) |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 53 | if not deployment_users: |
| 54 | logger.info("slice createor %s has not accout at deployment %s" % (slice_deployment.slice.creator, slice_deployment.deployment.name)) |
| 55 | roles = [] |
| 56 | else: |
| 57 | deployment_user = deployment_users[0] |
| 58 | roles = ['admin'] |
| 59 | |
| 60 | max_instances=int(slice_deployment.slice.max_slivers) |
| 61 | tenant_fields = {'endpoint':slice_deployment.deployment.auth_url, |
| 62 | 'admin_user': slice_deployment.deployment.admin_user, |
| 63 | 'admin_password': slice_deployment.deployment.admin_password, |
| 64 | 'admin_tenant': 'admin', |
| 65 | 'tenant': slice_deployment.slice.name, |
| 66 | 'tenant_description': slice_deployment.slice.description, |
| 67 | 'roles':roles, |
| 68 | 'name':deployment_user.email, |
| 69 | 'max_instances':max_instances} |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 70 | |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 71 | res = run_template('sync_slice_deployments.yaml', tenant_fields) |
| 72 | expected_num = len(roles)+1 |
| 73 | if (len(res)!=expected_num): |
| 74 | raise Exception('Could not sync tenants for slice %s'%slice_deployment.slice.name) |
| 75 | else: |
| 76 | tenant_id = res[0]['id'] |
| 77 | if (not slice_deployment.tenant_id): |
| 78 | handle = os.popen('nova quota-update --instances %d %s'%(max_instances,tenant_id)) |
| 79 | output = handle.read() |
| 80 | result = handle.close() |
| 81 | if (result): |
| 82 | logging.info('Could not update quota for %s'%slice_deployment.slice.name) |
| 83 | slice_deployment.tenant_id = tenant_id |
| 84 | slice_deployment.save() |
| 85 | |
Sapan Bhatia | d97caf3 | 2014-07-23 09:53:53 -0400 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def delete_record(self, slice_deployment): |
| 89 | user = User.objects.get(id=slice_deployment.slice.creator.id) |
| 90 | driver = OpenStackDriver().admin_driver(deployment=slice_deployment.deployment.name) |
| 91 | client_driver = driver.client_driver(caller=user, |
| 92 | tenant=slice_deployment.slice.name, |
| 93 | deployment=slice_deployment.deployment.name) |
| 94 | |
| 95 | if slice_deployment.router_id and slice_deployment.subnet_id: |
| 96 | client_driver.delete_router_interface(slice_deployment.router_id, slice_deployment.subnet_id) |
| 97 | if slice_deployment.subnet_id: |
| 98 | client_driver.delete_subnet(slice_deployment.subnet_id) |
| 99 | if slice_deployment.router_id: |
| 100 | client_driver.delete_router(slice_deployment.router_id) |
| 101 | if slice_deployment.network_id: |
| 102 | client_driver.delete_network(slice_deployment.network_id) |
| 103 | if slice_deployment.tenant_id: |
| 104 | driver.delete_tenant(slice_deployment.tenant_id) |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 105 | |