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 |
Tony Mack | 91463d9 | 2014-06-19 20:42:03 -0400 | [diff] [blame^] | 8 | from core.models.site import Deployment, SiteDeployments |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 9 | from core.models.slice import Slice, SliceDeployments |
Tony Mack | 91463d9 | 2014-06-19 20:42:03 -0400 | [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 |
| 12 | |
| 13 | logger = Logger(level=logging.INFO) |
| 14 | |
| 15 | class SyncSliceDeployments(OpenStackSyncStep): |
Tony Mack | b84fccc | 2014-04-12 10:28:52 -0400 | [diff] [blame] | 16 | provides=[SliceDeployments] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 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 | |
Tony Mack | b84fccc | 2014-04-12 10:28:52 -0400 | [diff] [blame] | 32 | all_deployments = Deployment.objects.all() |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 33 | for slice in Slice.objects.all(): |
Tony Mack | 6042aaf | 2014-04-07 19:45:46 -0400 | [diff] [blame] | 34 | # slices are added to all deployments for now |
| 35 | expected_deployments = all_deployments |
| 36 | #expected_deployments = site_deploy_lookup[slice.site] |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 37 | for expected_deployment in expected_deployments: |
| 38 | if slice not in slice_deploy_lookup or \ |
| 39 | expected_deployment not in slice_deploy_lookup[slice]: |
| 40 | sd = SliceDeployments(slice=slice, deployment=expected_deployment) |
| 41 | sd.save() |
| 42 | |
| 43 | # now we can return all slice deployments that need to be enacted |
| 44 | return SliceDeployments.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 45 | |
| 46 | def get_next_subnet(self, deployment=None): |
| 47 | # limit ourself to 10.0.x.x for now |
| 48 | valid_subnet = lambda net: net.startswith('10.0') |
| 49 | driver = self.driver.admin_driver(deployment=deployment) |
| 50 | subnets = driver.shell.quantum.list_subnets()['subnets'] |
| 51 | ints = [int(IPNetwork(subnet['cidr']).ip) for subnet in subnets \ |
| 52 | if valid_subnet(subnet['cidr'])] |
| 53 | ints.sort() |
Tony Mack | 6042aaf | 2014-04-07 19:45:46 -0400 | [diff] [blame] | 54 | if ints: |
| 55 | last_ip = IPAddress(ints[-1]) |
| 56 | else: |
| 57 | last_ip = IPAddress('10.0.0.1') |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 58 | last_ip = IPAddress(ints[-1]) |
| 59 | last_network = IPNetwork(str(last_ip) + "/24") |
| 60 | next_network = IPNetwork(str(IPAddress(last_network) + last_network.size) + "/24") |
| 61 | return next_network |
| 62 | |
Tony Mack | 29bf5e8 | 2014-04-29 21:40:24 -0400 | [diff] [blame] | 63 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 64 | def sync_record(self, slice_deployment): |
| 65 | logger.info("sync'ing slice deployment %s" % slice_deployment) |
| 66 | if not slice_deployment.tenant_id: |
| 67 | nova_fields = {'tenant_name': slice_deployment.slice.name, |
| 68 | 'description': slice_deployment.slice.description, |
| 69 | 'enabled': slice_deployment.slice.enabled} |
| 70 | driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name) |
| 71 | tenant = driver.create_tenant(**nova_fields) |
| 72 | slice_deployment.tenant_id = tenant.id |
| 73 | |
| 74 | # XXX give caller an admin role at the tenant they've created |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 75 | deployment_users = UserDeployments.objects.filter(user=slice_deployment.slice.creator, |
| 76 | deployment=slice_deployment.deployment) |
Tony Mack | e5968eb | 2014-03-17 19:19:13 -0400 | [diff] [blame] | 77 | if not deployment_users: |
| 78 | 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] | 79 | else: |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 80 | deployment_user = deployment_users[0] |
Tony Mack | e5968eb | 2014-03-17 19:19:13 -0400 | [diff] [blame] | 81 | # lookup user id at this deployment |
| 82 | kuser= driver.shell.keystone.users.find(email=slice_deployment.slice.creator.email) |
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 | # add required roles at the slice's tenant |
| 85 | driver.add_user_role(kuser.id, tenant.id, 'admin') |
| 86 | |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 87 | # refresh credentials using this tenant |
Tony Mack | d1a17e1 | 2014-03-19 15:18:15 -0400 | [diff] [blame] | 88 | client_driver = self.driver.client_driver(caller=deployment_user.user, |
| 89 | tenant=tenant.name, |
Tony Mack | 69f1bc3 | 2014-03-12 13:20:34 -0400 | [diff] [blame] | 90 | deployment=slice_deployment.deployment.name) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 91 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 92 | |
| 93 | if slice_deployment.id and slice_deployment.tenant_id: |
Tony Mack | 29bf5e8 | 2014-04-29 21:40:24 -0400 | [diff] [blame] | 94 | # update existing tenant |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 95 | driver = self.driver.admin_driver(deployment=slice_deployment.deployment.name) |
| 96 | driver.update_tenant(slice_deployment.tenant_id, |
| 97 | description=slice_deployment.slice.description, |
Tony Mack | 29bf5e8 | 2014-04-29 21:40:24 -0400 | [diff] [blame] | 98 | enabled=slice_deployment.slice.enabled) |
| 99 | |
| 100 | if slice_deployment.tenant_id: |
| 101 | # update slice/tenant quota |
Scott Baker | 3949bc6 | 2014-05-12 10:42:16 -0700 | [diff] [blame] | 102 | driver = self.driver.client_driver(deployment=slice_deployment.deployment.name, tenant=slice_deployment.slice.name) |
Tony Mack | 29bf5e8 | 2014-04-29 21:40:24 -0400 | [diff] [blame] | 103 | driver.shell.nova.quotas.update(tenant_id=slice_deployment.tenant_id, instances=int(slice_deployment.slice.max_slivers)) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 104 | |
| 105 | slice_deployment.save() |