Matteo Scandolo | 7781b5b | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 17 | import hashlib |
| 18 | import os |
| 19 | import socket |
| 20 | import sys |
| 21 | import base64 |
| 22 | import time |
| 23 | import re |
| 24 | import json |
| 25 | from collections import OrderedDict |
Matteo Scandolo | d374412 | 2017-06-02 16:09:31 -0700 | [diff] [blame] | 26 | from xosconfig import Config |
Scott Baker | 3bce78b | 2017-09-21 12:45:45 -0700 | [diff] [blame] | 27 | from synchronizers.new_base.syncstep import DeferredException |
Scott Baker | 91ee5e4 | 2017-03-14 10:33:52 -0700 | [diff] [blame] | 28 | from synchronizers.new_base.ansible_helper import run_template |
| 29 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 30 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 31 | from xos.logger import Logger, logging |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 32 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 33 | logger = Logger(level=logging.INFO) |
| 34 | |
Matteo Scandolo | 0d41a86 | 2016-09-01 13:21:41 -0700 | [diff] [blame] | 35 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 36 | class SyncONOSApp(SyncInstanceUsingAnsible): |
| 37 | provides=[ONOSApp] |
| 38 | observes=ONOSApp |
| 39 | requested_interval=0 |
| 40 | template_name = "sync_onosapp.yaml" |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 41 | |
| 42 | def __init__(self, *args, **kwargs): |
| 43 | super(SyncONOSApp, self).__init__(*args, **kwargs) |
| 44 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 45 | def get_instance(self, o): |
| 46 | # We assume the ONOS service owns a slice, so pick one of the instances |
| 47 | # inside that slice to sync to. |
| 48 | |
| 49 | serv = self.get_onos_service(o) |
| 50 | |
| 51 | if serv.no_container: |
| 52 | raise Exception("get_instance() was called on a service that was marked no_container") |
| 53 | |
| 54 | if serv.slices.exists(): |
| 55 | slice = serv.slices.all()[0] |
| 56 | if slice.instances.exists(): |
| 57 | return slice.instances.all()[0] |
| 58 | |
| 59 | return None |
| 60 | |
| 61 | def get_onos_service(self, o): |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 62 | if not o.owner: |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 63 | return None |
| 64 | |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 65 | onoses = ONOSService.objects.filter(id=o.owner.id) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 66 | if not onoses: |
| 67 | return None |
| 68 | |
| 69 | return onoses[0] |
| 70 | |
| 71 | def is_no_container(self, o): |
| 72 | return self.get_onos_service(o).no_container |
| 73 | |
| 74 | def skip_ansible_fields(self, o): |
| 75 | return self.is_no_container(o) |
| 76 | |
| 77 | def get_files_dir(self, o): |
Matteo Scandolo | d374412 | 2017-06-02 16:09:31 -0700 | [diff] [blame] | 78 | step_dir = Config.get("steps_dir") |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 79 | |
| 80 | return os.path.join(step_dir, "..", "files", str(self.get_onos_service(o).id), o.name) |
| 81 | |
| 82 | def get_cluster_configuration(self, o): |
| 83 | instance = self.get_instance(o) |
| 84 | if not instance: |
| 85 | raise Exception("No instance for ONOS App") |
| 86 | node_ips = [socket.gethostbyname(instance.node.name)] |
| 87 | |
| 88 | ipPrefix = ".".join(node_ips[0].split(".")[:3]) + ".*" |
| 89 | result = '{ "nodes": [' |
| 90 | result = result + ",".join(['{ "ip": "%s"}' % ip for ip in node_ips]) |
| 91 | result = result + '], "ipPrefix": "%s"}' % ipPrefix |
| 92 | return result |
| 93 | |
| 94 | def get_dynamic_parameter_value(self, o, param): |
| 95 | instance = self.get_instance(o) |
| 96 | if not instance: |
| 97 | raise Exception("No instance for ONOS App") |
| 98 | if param == 'rabbit_host': |
| 99 | return instance.controller.rabbit_host |
| 100 | if param == 'rabbit_user': |
| 101 | return instance.controller.rabbit_user |
| 102 | if param == 'rabbit_password': |
| 103 | return instance.controller.rabbit_password |
| 104 | if param == 'keystone_tenant_id': |
| 105 | cslice = ControllerSlice.objects.get(slice=instance.slice) |
| 106 | if not cslice: |
| 107 | raise Exception("Controller slice object for %s does not exist" % instance.slice.name) |
| 108 | return cslice.tenant_id |
| 109 | if param == 'keystone_user_id': |
| 110 | cuser = ControllerUser.objects.get(user=instance.creator) |
| 111 | if not cuser: |
| 112 | raise Exception("Controller user object for %s does not exist" % instance.creator) |
| 113 | return cuser.kuser_id |
| 114 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 115 | def write_configs(self, o): |
Scott Baker | f458f68 | 2017-03-14 17:10:36 -0700 | [diff] [blame] | 116 | if hasattr(o, "create_attr"): |
| 117 | # new API doesn't let us setattr for things that don't already exist |
| 118 | o.create_attr("config_fns") |
| 119 | o.create_attr("rest_configs") |
| 120 | o.create_attr("component_configs") |
| 121 | o.create_attr("files_dir") |
| 122 | o.create_attr("node_key_fn") |
| 123 | o.create_attr("early_rest_configs") |
| 124 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 125 | o.config_fns = [] |
| 126 | o.rest_configs = [] |
| 127 | o.component_configs = [] |
| 128 | o.files_dir = self.get_files_dir(o) |
| 129 | |
| 130 | if not os.path.exists(o.files_dir): |
| 131 | os.makedirs(o.files_dir) |
| 132 | |
| 133 | # Combine the service attributes with the tenant attributes. Tenant |
| 134 | # attribute can override service attributes. |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 135 | attrs = o.owner.serviceattribute_dict |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 136 | attrs.update(o.tenantattribute_dict) |
| 137 | |
Scott Baker | 3bce78b | 2017-09-21 12:45:45 -0700 | [diff] [blame] | 138 | # Check to see if we're waiting on autoconfig |
| 139 | if (attrs.get("autogenerate") in ["vtn-network-cfg",]) and \ |
| 140 | (not attrs.get("rest_onos/v1/network/configuration/")): |
| 141 | raise DeferredException("Network configuration is not populated yet") |
| 142 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 143 | ordered_attrs = attrs.keys() |
| 144 | |
| 145 | onos = self.get_onos_service(o) |
| 146 | if onos.node_key: |
| 147 | file(os.path.join(o.files_dir, "node_key"),"w").write(onos.node_key) |
| 148 | o.node_key_fn="node_key" |
| 149 | else: |
| 150 | o.node_key_fn=None |
| 151 | |
| 152 | o.early_rest_configs=[] |
| 153 | if ("cordvtn" in o.dependencies) and (not self.is_no_container(o)): |
| 154 | # For VTN, since it's running in a docker host container, we need |
| 155 | # to make sure it configures the cluster using the right ip addresses. |
| 156 | # NOTE: rest_onos/v1/cluster/configuration/ will reboot the cluster and |
| 157 | # must go first. |
| 158 | name="rest_onos/v1/cluster/configuration/" |
| 159 | value= self.get_cluster_configuration(o) |
| 160 | fn = name[5:].replace("/","_") |
| 161 | endpoint = name[5:] |
| 162 | file(os.path.join(o.files_dir, fn),"w").write(" " +value) |
| 163 | o.early_rest_configs.append( {"endpoint": endpoint, "fn": fn} ) |
| 164 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 165 | for name in attrs.keys(): |
| 166 | value = attrs[name] |
| 167 | if name.startswith("config_"): |
| 168 | fn = name[7:] # .replace("_json",".json") |
| 169 | o.config_fns.append(fn) |
| 170 | file(os.path.join(o.files_dir, fn),"w").write(value) |
| 171 | if name.startswith("rest_"): |
| 172 | fn = name[5:].replace("/","_") |
| 173 | endpoint = name[5:] |
| 174 | # Ansible goes out of it's way to make our life difficult. If |
| 175 | # 'lookup' sees a file that it thinks contains json, then it'll |
| 176 | # insist on parsing and return a json object. We just want |
| 177 | # a string, so prepend a space and then strip the space off |
| 178 | # later. |
| 179 | file(os.path.join(o.files_dir, fn),"w").write(" " +value) |
| 180 | o.rest_configs.append( {"endpoint": endpoint, "fn": fn} ) |
| 181 | if name.startswith("component_config"): |
| 182 | components = json.loads(value,object_pairs_hook=OrderedDict) |
| 183 | for component in components.keys(): |
| 184 | config = components[component] |
| 185 | for key in config.keys(): |
| 186 | config_val = config[key] |
| 187 | found = re.findall('<(.+?)>',config_val) |
| 188 | for x in found: |
| 189 | #Get value corresponding to that string |
| 190 | val = self.get_dynamic_parameter_value(o, x) |
| 191 | if val: |
| 192 | config_val = re.sub('<'+x+'>', val, config_val) |
| 193 | #TODO: else raise an exception? |
| 194 | o.component_configs.append( {"component": component, "config_params": "'{\""+key+"\":\""+config_val+"\"}'"} ) |
| 195 | |
| 196 | def prepare_record(self, o): |
| 197 | self.write_configs(o) |
| 198 | |
| 199 | def get_extra_attributes_common(self, o): |
| 200 | fields = {} |
| 201 | |
| 202 | # These are attributes that are not dependent on Instance. For example, |
| 203 | # REST API stuff. |
| 204 | |
| 205 | onos = self.get_onos_service(o) |
| 206 | |
| 207 | fields["files_dir"] = o.files_dir |
| 208 | fields["appname"] = o.name |
| 209 | fields["rest_configs"] = o.rest_configs |
| 210 | fields["rest_hostname"] = onos.rest_hostname |
| 211 | fields["rest_port"] = onos.rest_port |
| 212 | |
| 213 | if o.dependencies: |
| 214 | fields["dependencies"] = [x.strip() for x in o.dependencies.split(",")] |
| 215 | else: |
| 216 | fields["dependencies"] = [] |
| 217 | |
Andy Bavier | 4a503b1 | 2016-06-21 11:41:29 -0400 | [diff] [blame] | 218 | if o.install_dependencies: |
| 219 | fields["install_dependencies"] = [x.strip() for x in o.install_dependencies.split(",")] |
| 220 | else: |
| 221 | fields["install_dependencies"] = [] |
| 222 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 223 | return fields |
| 224 | |
| 225 | def get_extra_attributes_full(self, o): |
| 226 | instance = self.get_instance(o) |
| 227 | |
| 228 | fields = self.get_extra_attributes_common(o) |
| 229 | |
| 230 | fields["config_fns"] = o.config_fns |
| 231 | fields["early_rest_configs"] = o.early_rest_configs |
| 232 | fields["component_configs"] = o.component_configs |
| 233 | fields["node_key_fn"] = o.node_key_fn |
| 234 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 235 | if (instance.isolation=="container"): |
| 236 | fields["ONOS_container"] = "%s-%s" % (instance.slice.name, str(instance.id)) |
| 237 | else: |
| 238 | fields["ONOS_container"] = "ONOS" |
| 239 | return fields |
| 240 | |
| 241 | def get_extra_attributes(self, o): |
| 242 | if self.is_no_container(o): |
| 243 | return self.get_extra_attributes_common(o) |
| 244 | else: |
| 245 | return self.get_extra_attributes_full(o) |
| 246 | |
| 247 | def sync_fields(self, o, fields): |
| 248 | # the super causes the playbook to be run |
| 249 | super(SyncONOSApp, self).sync_fields(o, fields) |
| 250 | |
| 251 | def run_playbook(self, o, fields): |
| 252 | if self.is_no_container(o): |
| 253 | # There is no machine to SSH to, so use the synchronizer's |
| 254 | # run_template method directly. |
Sapan Bhatia | 8140552 | 2017-02-04 09:26:31 -0800 | [diff] [blame] | 255 | run_template("sync_onosapp_nocontainer.yaml", fields, object=o) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 256 | else: |
| 257 | super(SyncONOSApp, self).run_playbook(o, fields) |
| 258 | |
| 259 | def delete_record(self, m): |
| 260 | pass |