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