blob: aa0a443f406741e0faa1122bec9d38abe2de8469 [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001import os
2import base64
3import socket
4from django.db.models import F, Q
5from xos.config import Config
6from xos.settings import RESTAPI_HOSTNAME, RESTAPI_PORT
Scott Baker8b75e852016-08-16 15:04:59 -07007from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
Scott Bakerb63ea792016-08-11 10:24:48 -07008from core.models.instance import Instance
9from core.models.slice import Slice, SlicePrivilege, ControllerSlice
10from core.models.network import Network, NetworkSlice, ControllerNetwork
11from synchronizers.base.ansible import *
12from synchronizers.base.syncstep import *
13from xos.logger import observer_logger as logger
14
15def escape(s):
16 s = s.replace('\n',r'\n').replace('"',r'\"')
17 return s
18
19class SyncInstances(OpenStackSyncStep):
20 provides=[Instance]
21 requested_interval=0
22 observes=Instance
23 playbook='sync_instances.yaml'
24
25 def fetch_pending(self, deletion=False):
26 objs = super(SyncInstances, self).fetch_pending(deletion)
27 objs = [x for x in objs if x.isolation=="vm"]
28 return objs
29
30 def get_userdata(self, instance, pubkeys):
31 userdata = '#cloud-config\n\nopencloud:\n slicename: "%s"\n hostname: "%s"\n restapi_hostname: "%s"\n restapi_port: "%s"\n' % (instance.slice.name, instance.node.name, RESTAPI_HOSTNAME, str(RESTAPI_PORT))
32 userdata += 'ssh_authorized_keys:\n'
33 for key in pubkeys:
34 userdata += ' - %s\n' % key
35 return userdata
36
37 def sort_nics(self, nics):
38 result = []
39
40 # Enforce VTN's network order requirement. The access network must be
41 # inserted into the first slot. The management network must be inserted
42 # into the second slot.
43
44 # move the private and/or access network to the first spot
45 for nic in nics[:]:
46 network=nic.get("network", None)
47 if network:
48 tem = network.template
49 if (tem.visibility == "private") and (tem.translation=="none") and ("management" not in tem.name):
50 result.append(nic)
51 nics.remove(nic)
52
53 # move the management network to the second spot
54 for net in nics[:]:
55 network=nic.get("network", None)
56 if network:
57 tem = network.template
58 if (tem.visibility == "private") and (tem.translation=="none") and ("management" in tem.name):
59#MCORD
60# if len(result)!=1:
61# raise Exception("Management network needs to be inserted in slot 1, but there are %d private nics" % len(result))
62 result.append(nic)
63 nics.remove(nic)
64
65 # add everything else. For VTN there probably shouldn't be any more.
66 result.extend(nics)
67
68 return result
69
70 def map_sync_inputs(self, instance):
71 inputs = {}
72 metadata_update = {}
73 if (instance.numberCores):
74 metadata_update["cpu_cores"] = str(instance.numberCores)
75
76 for tag in instance.slice.tags.all():
77 if tag.name.startswith("sysctl-"):
78 metadata_update[tag.name] = tag.value
79
80 slice_memberships = SlicePrivilege.objects.filter(slice=instance.slice)
81 pubkeys = set([sm.user.public_key for sm in slice_memberships if sm.user.public_key])
82 if instance.creator.public_key:
83 pubkeys.add(instance.creator.public_key)
84
85 if instance.slice.creator.public_key:
86 pubkeys.add(instance.slice.creator.public_key)
87
88 if instance.slice.service and instance.slice.service.public_key:
89 pubkeys.add(instance.slice.service.public_key)
90
91 nics=[]
92
93 # handle ports the were created by the user
94 port_ids=[]
95 for port in Port.objects.filter(instance=instance):
96 if not port.port_id:
97 raise DeferredException("Instance %s waiting on port %s" % (instance, port))
98 nics.append({"kind": "port", "value": port.port_id, "network": port.network})
99
100 # we want to exclude from 'nics' any network that already has a Port
101 existing_port_networks = [port.network for port in Port.objects.filter(instance=instance)]
102
103 networks = [ns.network for ns in NetworkSlice.objects.filter(slice=instance.slice) if ns.network not in existing_port_networks]
104 controller_networks = ControllerNetwork.objects.filter(network__in=networks,
105 controller=instance.node.site_deployment.controller)
106
107 #controller_networks = self.sort_controller_networks(controller_networks)
108 for controller_network in controller_networks:
109 # Lenient exception - causes slow backoff
110 if controller_network.network.template.visibility == 'private' and \
111 controller_network.network.template.translation == 'none':
112 if not controller_network.net_id:
113 raise DeferredException("Instance %s Private Network %s has no id; Try again later" % (instance, controller_network.network.name))
114 nics.append({"kind": "net", "value": controller_network.net_id, "network": controller_network.network})
115
116 # now include network template
117 network_templates = [network.template.shared_network_name for network in networks \
118 if network.template.shared_network_name]
119
120 #driver = self.driver.client_driver(caller=instance.creator, tenant=instance.slice.name, controller=instance.controllerNetwork)
121 driver = self.driver.admin_driver(tenant='admin', controller=instance.node.site_deployment.controller)
122 nets = driver.shell.neutron.list_networks()['networks']
123 for net in nets:
124 if net['name'] in network_templates:
125 nics.append({"kind": "net", "value": net['id'], "network": None})
126
127 if (not nics):
128 for net in nets:
129 if net['name']=='public':
130 nics.append({"kind": "net", "value": net['id'], "network": None})
131
132 nics = self.sort_nics(nics)
133
134 image_name = None
135 controller_images = instance.image.controllerimages.filter(controller=instance.node.site_deployment.controller)
136 if controller_images:
137 image_name = controller_images[0].image.name
138 logger.info("using image from ControllerImage object: " + str(image_name))
139
140 if image_name is None:
141 controller_driver = self.driver.admin_driver(controller=instance.node.site_deployment.controller)
142 images = controller_driver.shell.glanceclient.images.list()
143 for image in images:
144 if image.name == instance.image.name or not image_name:
145 image_name = image.name
146 logger.info("using image from glance: " + str(image_name))
147
148 try:
149 legacy = Config().observer_legacy
150 except:
151 legacy = False
152
153 if (legacy):
154 host_filter = instance.node.name.split('.',1)[0]
155 else:
156 host_filter = instance.node.name.strip()
157
158 availability_zone_filter = 'nova:%s'%host_filter
159 instance_name = '%s-%d'%(instance.slice.name,instance.id)
160 self.instance_name = instance_name
161
162 userData = self.get_userdata(instance, pubkeys)
163 if instance.userData:
164 userData += instance.userData
165
166 controller = instance.node.site_deployment.controller
167 fields = {'endpoint':controller.auth_url,
168 'endpoint_v3': controller.auth_url_v3,
169 'domain': controller.domain,
170 'admin_user': instance.creator.email,
171 'admin_password': instance.creator.remote_password,
172 'project_name': instance.slice.name,
173 'tenant': instance.slice.name,
174 'tenant_description': instance.slice.description,
175 'name':instance_name,
176 'ansible_tag':instance_name,
177 'availability_zone': availability_zone_filter,
178 'image_name':image_name,
179 'flavor_name':instance.flavor.name,
180 'nics':nics,
181 'meta':metadata_update,
182 'user_data':r'%s'%escape(userData)}
183 return fields
184
185
186 def map_sync_outputs(self, instance, res):
187 instance_id = res[0]['openstack']['OS-EXT-SRV-ATTR:instance_name']
188 instance_uuid = res[0]['id']
189
190 try:
191 hostname = res[0]['openstack']['OS-EXT-SRV-ATTR:hypervisor_hostname']
192 ip = socket.gethostbyname(hostname)
193 instance.ip = ip
194 except:
195 pass
196
197 instance.instance_id = instance_id
198 instance.instance_uuid = instance_uuid
199 instance.instance_name = self.instance_name
200 instance.save()
201
202
203 def map_delete_inputs(self, instance):
204 controller_register = json.loads(instance.node.site_deployment.controller.backend_register)
205
206 if (controller_register.get('disabled',False)):
207 raise InnocuousException('Controller %s is disabled'%instance.node.site_deployment.controller.name)
208
209 instance_name = '%s-%d'%(instance.slice.name,instance.id)
210 controller = instance.node.site_deployment.controller
211 input = {'endpoint':controller.auth_url,
212 'admin_user': instance.creator.email,
213 'admin_password': instance.creator.remote_password,
214 'admin_tenant': instance.slice.name,
215 'tenant': instance.slice.name,
216 'tenant_description': instance.slice.description,
217 'name':instance_name,
218 'ansible_tag':instance_name,
219 'delete': True}
220 return input