blob: c43498f4d426b7153381e2b0558628493e457927 [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001import os
2import base64
3import socket
Scott Bakerb63ea792016-08-11 10:24:48 -07004from xos.config import Config
Scott Baker8b75e852016-08-16 15:04:59 -07005from synchronizers.openstack.openstacksyncstep import OpenStackSyncStep
Scott Bakeraf599eb2017-03-21 12:43:26 -07006from synchronizers.new_base.ansible_helper import *
7from synchronizers.new_base.syncstep import *
Scott Bakerb63ea792016-08-11 10:24:48 -07008from xos.logger import observer_logger as logger
Scott Bakeraf599eb2017-03-21 12:43:26 -07009from synchronizers.new_base.modelaccessor import *
Scott Bakerb63ea792016-08-11 10:24:48 -070010
Scott Baker8c542842017-03-22 15:45:44 -070011RESTAPI_HOSTNAME = getattr(Config(), "server_restapi_hostname", getattr(Config(), "server_hostname", socket.gethostname()))
12RESTAPI_PORT = int(getattr(Config(), "server_restapi_port", getattr(Config(), "server_port", "8000")))
13
Scott Bakerb63ea792016-08-11 10:24:48 -070014def escape(s):
15 s = s.replace('\n',r'\n').replace('"',r'\"')
16 return s
17
18class SyncInstances(OpenStackSyncStep):
19 provides=[Instance]
20 requested_interval=0
21 observes=Instance
22 playbook='sync_instances.yaml'
23
24 def fetch_pending(self, deletion=False):
25 objs = super(SyncInstances, self).fetch_pending(deletion)
26 objs = [x for x in objs if x.isolation=="vm"]
27 return objs
28
29 def get_userdata(self, instance, pubkeys):
30 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))
31 userdata += 'ssh_authorized_keys:\n'
32 for key in pubkeys:
33 userdata += ' - %s\n' % key
34 return userdata
35
Andy Bavier4435bf02017-02-01 16:17:09 -050036 def get_nic_for_first_slot(self, nics):
37 # Try to find a NIC with "public" visibility
38 for nic in nics[:]:
39 network=nic.get("network", None)
40 if network:
41 tem = network.template
42 if (tem.visibility == "public"):
43 return nic
Scott Bakerb63ea792016-08-11 10:24:48 -070044
Andy Bavier4435bf02017-02-01 16:17:09 -050045 # Otherwise try to find a private network
Scott Bakerb63ea792016-08-11 10:24:48 -070046 for nic in nics[:]:
47 network=nic.get("network", None)
48 if network:
49 tem = network.template
50 if (tem.visibility == "private") and (tem.translation=="none") and ("management" not in tem.name):
Andy Bavier4435bf02017-02-01 16:17:09 -050051 return nic
52
53 raise Exception("Could not find a NIC for first slot")
54
55 def sort_nics(self, nics):
56 result = []
57
58 # Enforce VTN's network order requirement for vSG. The access network must be
59 # inserted into the first slot. The management network must be inserted
60 # into the second slot.
61 #
62 # Some VMs may connect to multiple networks that advertise gateways. In this case, the
63 # default gateway is enforced on eth0. So give priority to "public" networks when
64 # choosing a network for the first slot.
65
66 nic = self.get_nic_for_first_slot(nics)
67 result.append(nic)
68 nics.remove(nic)
Scott Bakerb63ea792016-08-11 10:24:48 -070069
70 # move the management network to the second spot
Scott Bakeread6af42016-10-11 16:40:40 -070071 for nic in nics[:]:
Scott Bakerb63ea792016-08-11 10:24:48 -070072 network=nic.get("network", None)
73 if network:
74 tem = network.template
75 if (tem.visibility == "private") and (tem.translation=="none") and ("management" in tem.name):
76#MCORD
77# if len(result)!=1:
78# raise Exception("Management network needs to be inserted in slot 1, but there are %d private nics" % len(result))
79 result.append(nic)
80 nics.remove(nic)
81
82 # add everything else. For VTN there probably shouldn't be any more.
83 result.extend(nics)
84
85 return result
86
87 def map_sync_inputs(self, instance):
Scott Bakere13170a2017-01-26 09:57:37 -080088
89 # sanity check - make sure model_policy for slice has run
90 if ((not instance.slice.policed) or (instance.slice.policed < instance.slice.updated)):
91 raise DeferredException("Instance %s waiting on Slice %s to execute model policies" % (instance, slice.name))
92
93 # sanity check - make sure model_policy for all slice networks have run
94 for network in instance.slice.ownedNetworks.all():
95 if ((not network.policed) or (network.policed < network.updated)):
96 raise DeferredException("Instance %s waiting on Network %s to execute model policies" % (instance, network.name))
97
Scott Bakerb63ea792016-08-11 10:24:48 -070098 inputs = {}
99 metadata_update = {}
100 if (instance.numberCores):
101 metadata_update["cpu_cores"] = str(instance.numberCores)
102
103 for tag in instance.slice.tags.all():
104 if tag.name.startswith("sysctl-"):
105 metadata_update[tag.name] = tag.value
106
Scott Bakeraf599eb2017-03-21 12:43:26 -0700107 slice_memberships = SlicePrivilege.objects.filter(slice_id=instance.slice.id)
Scott Bakerb63ea792016-08-11 10:24:48 -0700108 pubkeys = set([sm.user.public_key for sm in slice_memberships if sm.user.public_key])
109 if instance.creator.public_key:
110 pubkeys.add(instance.creator.public_key)
111
112 if instance.slice.creator.public_key:
113 pubkeys.add(instance.slice.creator.public_key)
114
115 if instance.slice.service and instance.slice.service.public_key:
116 pubkeys.add(instance.slice.service.public_key)
117
118 nics=[]
119
120 # handle ports the were created by the user
121 port_ids=[]
Scott Bakeraf599eb2017-03-21 12:43:26 -0700122 for port in Port.objects.filter(instance_id=instance.id):
Scott Bakerb63ea792016-08-11 10:24:48 -0700123 if not port.port_id:
124 raise DeferredException("Instance %s waiting on port %s" % (instance, port))
125 nics.append({"kind": "port", "value": port.port_id, "network": port.network})
126
127 # we want to exclude from 'nics' any network that already has a Port
Scott Bakeraf599eb2017-03-21 12:43:26 -0700128 existing_port_networks = [port.network for port in Port.objects.filter(instance_id=instance.id)]
129 existing_port_network_ids = [x.id for x in existing_port_networks]
Scott Bakerb63ea792016-08-11 10:24:48 -0700130
Scott Bakeraf599eb2017-03-21 12:43:26 -0700131 networks = [ns.network for ns in NetworkSlice.objects.filter(slice_id=instance.slice.id) if ns.network.id not in existing_port_network_ids]
132 networks_ids = [x.id for x in networks]
133 controller_networks = ControllerNetwork.objects.filter(controller_id=instance.node.site_deployment.controller.id)
134 controller_networks = [x for x in controller_networks if x.id in networks_ids]
135
Scott Bakerb63ea792016-08-11 10:24:48 -0700136
Scott Baker6c69a122016-12-07 16:08:55 -0800137 for network in networks:
Scott Bakeraf599eb2017-03-21 12:43:26 -0700138 if not ControllerNetwork.objects.filter(network_id=network.id, controller_id=instance.node.site_deployment.controller.id).exists():
Scott Baker6c69a122016-12-07 16:08:55 -0800139 raise DeferredException("Instance %s Private Network %s lacks ControllerNetwork object" % (instance, network.name))
140
Scott Bakerb63ea792016-08-11 10:24:48 -0700141 for controller_network in controller_networks:
142 # Lenient exception - causes slow backoff
Andy Bavier4435bf02017-02-01 16:17:09 -0500143 if controller_network.network.template.translation == 'none':
Scott Bakerb63ea792016-08-11 10:24:48 -0700144 if not controller_network.net_id:
145 raise DeferredException("Instance %s Private Network %s has no id; Try again later" % (instance, controller_network.network.name))
146 nics.append({"kind": "net", "value": controller_network.net_id, "network": controller_network.network})
147
148 # now include network template
149 network_templates = [network.template.shared_network_name for network in networks \
150 if network.template.shared_network_name]
151
Scott Bakerb63ea792016-08-11 10:24:48 -0700152 driver = self.driver.admin_driver(tenant='admin', controller=instance.node.site_deployment.controller)
153 nets = driver.shell.neutron.list_networks()['networks']
154 for net in nets:
155 if net['name'] in network_templates:
156 nics.append({"kind": "net", "value": net['id'], "network": None})
157
158 if (not nics):
159 for net in nets:
160 if net['name']=='public':
161 nics.append({"kind": "net", "value": net['id'], "network": None})
162
163 nics = self.sort_nics(nics)
164
165 image_name = None
Scott Bakeraf599eb2017-03-21 12:43:26 -0700166 controller_images = instance.image.controllerimages.all()
167 controller_images = [x for x in controller_images if x.controller_id==instance.node.site_deployment.controller.id]
Scott Bakerb63ea792016-08-11 10:24:48 -0700168 if controller_images:
169 image_name = controller_images[0].image.name
170 logger.info("using image from ControllerImage object: " + str(image_name))
171
172 if image_name is None:
173 controller_driver = self.driver.admin_driver(controller=instance.node.site_deployment.controller)
174 images = controller_driver.shell.glanceclient.images.list()
175 for image in images:
176 if image.name == instance.image.name or not image_name:
177 image_name = image.name
178 logger.info("using image from glance: " + str(image_name))
179
180 try:
181 legacy = Config().observer_legacy
182 except:
183 legacy = False
184
185 if (legacy):
186 host_filter = instance.node.name.split('.',1)[0]
187 else:
188 host_filter = instance.node.name.strip()
189
190 availability_zone_filter = 'nova:%s'%host_filter
191 instance_name = '%s-%d'%(instance.slice.name,instance.id)
192 self.instance_name = instance_name
193
194 userData = self.get_userdata(instance, pubkeys)
195 if instance.userData:
196 userData += instance.userData
197
198 controller = instance.node.site_deployment.controller
199 fields = {'endpoint':controller.auth_url,
200 'endpoint_v3': controller.auth_url_v3,
201 'domain': controller.domain,
202 'admin_user': instance.creator.email,
203 'admin_password': instance.creator.remote_password,
204 'project_name': instance.slice.name,
205 'tenant': instance.slice.name,
206 'tenant_description': instance.slice.description,
207 'name':instance_name,
208 'ansible_tag':instance_name,
209 'availability_zone': availability_zone_filter,
210 'image_name':image_name,
211 'flavor_name':instance.flavor.name,
212 'nics':nics,
213 'meta':metadata_update,
214 'user_data':r'%s'%escape(userData)}
215 return fields
216
217
218 def map_sync_outputs(self, instance, res):
219 instance_id = res[0]['openstack']['OS-EXT-SRV-ATTR:instance_name']
220 instance_uuid = res[0]['id']
221
222 try:
223 hostname = res[0]['openstack']['OS-EXT-SRV-ATTR:hypervisor_hostname']
224 ip = socket.gethostbyname(hostname)
225 instance.ip = ip
226 except:
227 pass
228
229 instance.instance_id = instance_id
230 instance.instance_uuid = instance_uuid
231 instance.instance_name = self.instance_name
232 instance.save()
233
234
235 def map_delete_inputs(self, instance):
236 controller_register = json.loads(instance.node.site_deployment.controller.backend_register)
237
238 if (controller_register.get('disabled',False)):
239 raise InnocuousException('Controller %s is disabled'%instance.node.site_deployment.controller.name)
240
241 instance_name = '%s-%d'%(instance.slice.name,instance.id)
242 controller = instance.node.site_deployment.controller
243 input = {'endpoint':controller.auth_url,
244 'admin_user': instance.creator.email,
245 'admin_password': instance.creator.remote_password,
Andy Bavier9e65a352016-09-30 15:39:02 -0400246 'project_name': instance.slice.name,
Scott Bakerb63ea792016-08-11 10:24:48 -0700247 'tenant': instance.slice.name,
248 'tenant_description': instance.slice.description,
249 'name':instance_name,
250 'ansible_tag':instance_name,
251 'delete': True}
252 return input