3 from django.db.models import F, Q
4 from planetstack.config import Config
5 from observer.openstacksyncstep import OpenStackSyncStep
6 from core.models.sliver import Sliver
7 from core.models.slice import Slice, SlicePrivilege, ControllerSlice
8 from core.models.network import Network, NetworkSlice, ControllerNetwork
9 from util.logger import Logger, logging
10 from observer.ansible import *
12 logger = Logger(level=logging.INFO)
15 s = s.replace('\n',r'\n').replace('"',r'\"')
18 class SyncSlivers(OpenStackSyncStep):
22 def get_userdata(self, sliver):
23 userdata = 'opencloud:\n slicename: "%s"\n hostname: "%s"\n' % (sliver.slice.name, sliver.node.name)
26 def sync_record(self, sliver):
27 logger.info("sync'ing sliver:%s slice:%s controller:%s " % (sliver, sliver.slice.name, sliver.node.site_deployment.controller))
30 if (sliver.numberCores):
31 metadata_update["cpu_cores"] = str(sliver.numberCores)
33 for tag in sliver.slice.tags.all():
34 if tag.name.startswith("sysctl-"):
35 metadata_update[tag.name] = tag.value
38 slice_memberships = SlicePrivilege.objects.filter(slice=sliver.slice)
39 pubkeys = set([sm.user.public_key for sm in slice_memberships if sm.user.public_key])
40 if sliver.creator.public_key:
41 pubkeys.add(sliver.creator.public_key)
43 if sliver.slice.creator.public_key:
44 pubkeys.add(sliver.slice.creator.public_key)
47 networks = [ns.network for ns in NetworkSlice.objects.filter(slice=sliver.slice)]
48 controller_networks = ControllerNetwork.objects.filter(network__in=networks,
49 controller=sliver.node.site_deployment.controller)
51 for controller_network in controller_networks:
52 if controller_network.network.template.visibility == 'private' and \
53 controller_network.network.template.translation == 'none' and controller_network.net_id:
54 nics.append(controller_network.net_id)
56 # now include network template
57 network_templates = [network.template.shared_network_name for network in networks \
58 if network.template.shared_network_name]
60 #driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, controller=sliver.controllerNetwork)
61 driver = self.driver.admin_driver(tenant='admin', controller=sliver.node.site_deployment.controller)
62 nets = driver.shell.quantum.list_networks()['networks']
64 if net['name'] in network_templates:
65 nics.append(net['id'])
69 if net['name']=='public':
70 nics.append(net['id'])
73 controller_driver = self.driver.admin_driver(controller=sliver.node.site_deployment.controller)
75 images = controller_driver.shell.glanceclient.images.list()
77 if image.name == sliver.image.name or not image_id:
80 # look up key name at the controller
81 # create/fetch keypair
83 keyname = sliver.creator.email.lower().replace('@', 'AT').replace('.', '') +\
85 key_fields = {'name': keyname,
86 'public_key': sliver.creator.public_key}
89 legacy = Config().observer_legacy
94 host_filter = sliver.node.name.split('.',1)[0]
96 host_filter = sliver.node.name.strip()
98 availability_zone_filter = 'nova:%s'%host_filter
99 sliver_name = '%s-%d'%(sliver.slice.name,sliver.id)
101 userData = self.get_userdata(sliver)
103 userData = sliver.userData
105 controller = sliver.node.site_deployment.controller
106 tenant_fields = {'endpoint':controller.auth_url,
107 'admin_user': sliver.creator.email,
108 'admin_password': sliver.creator.remote_password,
109 'admin_tenant': sliver.slice.name,
110 'tenant': sliver.slice.name,
111 'tenant_description': sliver.slice.description,
113 'ansible_tag':sliver_name,
114 'availability_zone': availability_zone_filter,
117 'flavor_id':sliver.flavor.id,
119 'meta':metadata_update,
121 'user_data':r'%s'%escape(userData)}
123 res = run_template('sync_slivers.yaml', tenant_fields,path='slivers')
125 raise Exception('Could not sync sliver %s'%sliver.slice.name)
127 sliver_id = res[1]['info']['OS-EXT-SRV-ATTR:instance_name'] # 0 is for the key
128 sliver_uuid = res[1]['id'] # 0 is for the key
131 hostname = res[1]['info']['OS-EXT-SRV-ATTR:hypervisor_hostname']
132 ip = socket.gethostbyname(hostname)
137 sliver.instance_id = sliver_id
138 sliver.instance_uuid = sliver_uuid
139 sliver.instance_name = sliver_name
142 def delete_record(self, sliver):
143 sliver_name = '@'.join([sliver.slice.name,sliver.node.name])
144 tenant_fields = {'name':sliver_name,
145 'ansible_tag':sliver_name
147 res = run_template('delete_slivers.yaml', tenant_fields, path='slivers')