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 | b55f4a3 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 8 | from core.models.site import Deployment, SiteDeployments |
Sapan Bhatia | 85e0908 | 2014-11-19 15:09:38 -0500 | [diff] [blame] | 9 | from core.models.slice import Slice, SliceDeployments |
Sapan Bhatia | e20777d | 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 | 14356b7 | 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 | 85e0908 | 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 | 85e0908 | 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 | 85e0908 | 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 | 9ae9e4e | 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 | dda8807 | 2014-12-03 00:02:55 -0500 | [diff] [blame] | 51 | deployment_users = UserDeployments.objects.filter(user=slice_deployment.slice.creator, |
| 52 | deployment=slice_deployment.deployment) |
| 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 = ['admin'] |
| 56 | deployment_user = 'Unknown user' |
| 57 | else: |
| 58 | deployment_user = deployment_users[0].user.email |
| 59 | roles = ['admin'] |
| 60 | |
| 61 | max_instances=int(slice_deployment.slice.max_slivers) |
| 62 | tenant_fields = {'endpoint':slice_deployment.deployment.auth_url, |
| 63 | 'admin_user': slice_deployment.deployment.admin_user, |
| 64 | 'admin_password': slice_deployment.deployment.admin_password, |
| 65 | 'admin_tenant': 'admin', |
| 66 | 'tenant': slice_deployment.slice.name, |
| 67 | 'tenant_description': slice_deployment.slice.description, |
| 68 | 'roles':roles, |
| 69 | 'name':deployment_user, |
| 70 | 'ansible_tag':'%s@%s'%(slice_deployment.slice.name,slice_deployment.deployment.name), |
| 71 | 'max_instances':max_instances} |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 72 | |
Sapan | dda8807 | 2014-12-03 00:02:55 -0500 | [diff] [blame] | 73 | res = run_template('sync_slice_deployments.yaml', tenant_fields, path='slice_deployments') |
| 74 | expected_num = len(roles)+1 |
| 75 | if (len(res)!=expected_num): |
| 76 | raise Exception('Could not sync tenants for slice %s'%slice_deployment.slice.name) |
| 77 | else: |
| 78 | tenant_id = res[0]['id'] |
| 79 | if (not slice_deployment.tenant_id): |
| 80 | handle = os.popen('nova quota-update --instances %d %s'%(max_instances,tenant_id)) |
| 81 | output = handle.read() |
| 82 | result = handle.close() |
| 83 | if (result): |
| 84 | logging.info('Could not update quota for %s'%slice_deployment.slice.name) |
| 85 | slice_deployment.tenant_id = tenant_id |
| 86 | slice_deployment.save() |
| 87 | |
Sapan Bhatia | d97caf3 | 2014-07-23 09:53:53 -0400 | [diff] [blame] | 88 | |
| 89 | |
| 90 | def delete_record(self, slice_deployment): |
| 91 | user = User.objects.get(id=slice_deployment.slice.creator.id) |
| 92 | driver = OpenStackDriver().admin_driver(deployment=slice_deployment.deployment.name) |
| 93 | client_driver = driver.client_driver(caller=user, |
| 94 | tenant=slice_deployment.slice.name, |
| 95 | deployment=slice_deployment.deployment.name) |
| 96 | |
| 97 | if slice_deployment.router_id and slice_deployment.subnet_id: |
| 98 | client_driver.delete_router_interface(slice_deployment.router_id, slice_deployment.subnet_id) |
| 99 | if slice_deployment.subnet_id: |
| 100 | client_driver.delete_subnet(slice_deployment.subnet_id) |
Sapan | dda8807 | 2014-12-03 00:02:55 -0500 | [diff] [blame] | 101 | if slice_deployment.router_id: |
Sapan Bhatia | d97caf3 | 2014-07-23 09:53:53 -0400 | [diff] [blame] | 102 | client_driver.delete_router(slice_deployment.router_id) |
| 103 | if slice_deployment.network_id: |
| 104 | client_driver.delete_network(slice_deployment.network_id) |
| 105 | if slice_deployment.tenant_id: |
| 106 | driver.delete_tenant(slice_deployment.tenant_id) |
Sapan | dda8807 | 2014-12-03 00:02:55 -0500 | [diff] [blame] | 107 | |