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 |
| 8 | from core.models.site import SiteDeployments |
| 9 | from core.models.slice import Slice, SliceDeployments |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 10 | from core.models.user import UserDeployments |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 11 | from util.logger import Logger, logging |
| 12 | |
| 13 | logger = Logger(level=logging.INFO) |
| 14 | |
| 15 | class SyncSliceDeployments(OpenStackSyncStep): |
| 16 | provides=[Slice, SliceDeployments] |
| 17 | requested_interval=0 |
| 18 | |
| 19 | def fetch_pending(self): |
| 20 | # slice deployments are not visible to users. We must ensure |
| 21 | # slices are deployed at all deploymets available to their site. |
Tony Mack | e5968eb | 2014-03-17 19:19:13 -0400 | [diff] [blame] | 22 | site_deployments = SiteDeployments.objects.all() |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 23 | site_deploy_lookup = defaultdict(list) |
| 24 | for site_deployment in site_deployments: |
| 25 | site_deploy_lookup[site_deployment.site].append(site_deployment.deployment) |
| 26 | |
Tony Mack | e5968eb | 2014-03-17 19:19:13 -0400 | [diff] [blame] | 27 | slice_deployments = SliceDeployments.objects.all() |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 28 | slice_deploy_lookup = defaultdict(list) |
| 29 | for slice_deployment in slice_deployments: |
| 30 | slice_deploy_lookup[slice_deployment.slice].append(slice_deployment.deployment) |
| 31 | |
| 32 | for slice in Slice.objects.all(): |
| 33 | expected_deployments = site_deploy_lookup[slice.site] |
| 34 | for expected_deployment in expected_deployments: |
| 35 | if slice not in slice_deploy_lookup or \ |
| 36 | expected_deployment not in slice_deploy_lookup[slice]: |
| 37 | sd = SliceDeployments(slice=slice, deployment=expected_deployment) |
| 38 | sd.save() |
| 39 | |
| 40 | # now we can return all slice deployments that need to be enacted |
| 41 | return SliceDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 42 | |
| 43 | def get_next_subnet(self, deployment=None): |
| 44 | # limit ourself to 10.0.x.x for now |
| 45 | valid_subnet = lambda net: net.startswith('10.0') |
| 46 | driver = self.driver.admin_driver(deployment=deployment) |
| 47 | subnets = driver.shell.quantum.list_subnets()['subnets'] |
| 48 | ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \ |
| 49 | if valid_subnet(subnet['cidr'])] |
| 50 | ints.sort() |
| 51 | last_ip = IPAddress(ints[-1]) |
| 52 | last_network = IPNetwork(str(last_ip) + "/24") |
| 53 | next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24") |
| 54 | return next_network |
| 55 | |
| 56 | def sync_record(self, slice_deployment): |
| 57 | logger.info("sync'ing slice deployment %s" % slice_deployment) |
| 58 | if not slice_deployment.tenant_id: |
| 59 | nova_fields = {'tenant_name': slice_deployment.slice.name, |
| 60 | 'description': slice_deployment.slice.description, |
| 61 | 'enabled': slice_deployment.slice.enabled} |
| 62 | driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name) |
| 63 | tenant = driver.create_tenant(**nova_fields) |
| 64 | slice_deployment.tenant_id = tenant.id |
| 65 | |
| 66 | # XXX give caller an admin role at the tenant they've created |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 67 | deployment_users = UserDeployments.objects.filter(user=slice_deployment.slice.creator, |
| 68 | deployment=slice_deployment.deployment) |
Tony Mack | e5968eb | 2014-03-17 19:19:13 -0400 | [diff] [blame] | 69 | if not deployment_users: |
| 70 | logger.info("slice createor %s has not accout at deployment %s" % (slice_deployment.slice.creator, slice_deployment.deployment.name)) |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 71 | else: |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 72 | deployment_user = deployment_users[0] |
Tony Mack | e5968eb | 2014-03-17 19:19:13 -0400 | [diff] [blame] | 73 | # lookup user id at this deployment |
| 74 | kuser= driver.shell.keystone.users.find(email=slice_deployment.slice.creator.email) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 75 | |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 76 | # add required roles at the slice's tenant |
| 77 | driver.add_user_role(kuser.id, tenant.id, 'admin') |
| 78 | |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 79 | # refresh credentials using this tenant |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 80 | client_driver = self.driver.client_driver(caller=deployment_user.user, |
| 81 | tenant=tenant.name, |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 82 | deployment=slice_deployment.deployment.name) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 83 | |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 84 | # create a public key for the slice creator |
| 85 | if deployment_user.user.public_key: |
| 86 | keyname = deployment_user.user.email.lower().replace('@', 'AT').replace('.', '') +\ |
| 87 | slice_deployment.slice.name |
| 88 | slice_deployment.keyname = keyname |
| 89 | key_fields = {'name': keyname, |
| 90 | 'public_key': deployment_user.user.public_key} |
| 91 | client_driver.create_keypair(**key_fields) |
| 92 | |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 93 | # create network |
Tony Mack | e5968eb | 2014-03-17 19:19:13 -0400 | [diff] [blame] | 94 | network = client_driver.create_network(slice_deployment.slice.name) |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 95 | slice_deployment.network_id = network['id'] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 96 | |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 97 | # create router |
Tony Mack | e5968eb | 2014-03-17 19:19:13 -0400 | [diff] [blame] | 98 | router = client_driver.create_router(slice_deployment.slice.name) |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 99 | slice_deployment.router_id = router['id'] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 100 | |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 101 | # create subnet for slice's private network |
| 102 | next_subnet = self.get_next_subnet(deployment=slice_deployment.deployment.name) |
| 103 | cidr = str(next_subnet.cidr) |
| 104 | ip_version = next_subnet.version |
| 105 | start = str(next_subnet[2]) |
| 106 | end = str(next_subnet[-2]) |
Tony Mack | e5968eb | 2014-03-17 19:19:13 -0400 | [diff] [blame] | 107 | subnet = client_driver.create_subnet(name=slice_deployment.slice.name, |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 108 | network_id = network['id'], |
| 109 | cidr_ip = cidr, |
| 110 | ip_version = ip_version, |
| 111 | start = start, |
| 112 | end = end) |
| 113 | slice_deployment.subnet_id = subnet['id'] |
| 114 | # add subnet as interface to slice's router |
| 115 | client_driver.add_router_interface(router['id'], subnet['id']) |
| 116 | # add external route |
| 117 | client_driver.add_external_route(subnet) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 118 | |
| 119 | |
| 120 | if slice_deployment.id and slice_deployment.tenant_id: |
| 121 | driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name) |
| 122 | driver.update_tenant(slice_deployment.tenant_id, |
| 123 | description=slice_deployment.slice.description, |
| 124 | enabled=slice_deployment.slice.enabled) |
| 125 | |
| 126 | slice_deployment.save() |