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 | 0c62dfa | 2017-11-21 09:15:43 -0800 | [diff] [blame] | 62 | return o.owner.leaf_model |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 63 | |
| 64 | def is_no_container(self, o): |
| 65 | return self.get_onos_service(o).no_container |
| 66 | |
| 67 | def skip_ansible_fields(self, o): |
| 68 | return self.is_no_container(o) |
| 69 | |
| 70 | def get_files_dir(self, o): |
Matteo Scandolo | d374412 | 2017-06-02 16:09:31 -0700 | [diff] [blame] | 71 | step_dir = Config.get("steps_dir") |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 72 | |
| 73 | return os.path.join(step_dir, "..", "files", str(self.get_onos_service(o).id), o.name) |
| 74 | |
| 75 | def get_cluster_configuration(self, o): |
| 76 | instance = self.get_instance(o) |
| 77 | if not instance: |
| 78 | raise Exception("No instance for ONOS App") |
| 79 | node_ips = [socket.gethostbyname(instance.node.name)] |
| 80 | |
| 81 | ipPrefix = ".".join(node_ips[0].split(".")[:3]) + ".*" |
| 82 | result = '{ "nodes": [' |
| 83 | result = result + ",".join(['{ "ip": "%s"}' % ip for ip in node_ips]) |
| 84 | result = result + '], "ipPrefix": "%s"}' % ipPrefix |
| 85 | return result |
| 86 | |
| 87 | def get_dynamic_parameter_value(self, o, param): |
| 88 | instance = self.get_instance(o) |
| 89 | if not instance: |
| 90 | raise Exception("No instance for ONOS App") |
| 91 | if param == 'rabbit_host': |
| 92 | return instance.controller.rabbit_host |
| 93 | if param == 'rabbit_user': |
| 94 | return instance.controller.rabbit_user |
| 95 | if param == 'rabbit_password': |
| 96 | return instance.controller.rabbit_password |
| 97 | if param == 'keystone_tenant_id': |
| 98 | cslice = ControllerSlice.objects.get(slice=instance.slice) |
| 99 | if not cslice: |
| 100 | raise Exception("Controller slice object for %s does not exist" % instance.slice.name) |
| 101 | return cslice.tenant_id |
| 102 | if param == 'keystone_user_id': |
| 103 | cuser = ControllerUser.objects.get(user=instance.creator) |
| 104 | if not cuser: |
| 105 | raise Exception("Controller user object for %s does not exist" % instance.creator) |
| 106 | return cuser.kuser_id |
| 107 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 108 | def write_configs(self, o): |
Scott Baker | f458f68 | 2017-03-14 17:10:36 -0700 | [diff] [blame] | 109 | if hasattr(o, "create_attr"): |
| 110 | # new API doesn't let us setattr for things that don't already exist |
| 111 | o.create_attr("config_fns") |
| 112 | o.create_attr("rest_configs") |
| 113 | o.create_attr("component_configs") |
| 114 | o.create_attr("files_dir") |
| 115 | o.create_attr("node_key_fn") |
| 116 | o.create_attr("early_rest_configs") |
| 117 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 118 | o.config_fns = [] |
| 119 | o.rest_configs = [] |
| 120 | o.component_configs = [] |
| 121 | o.files_dir = self.get_files_dir(o) |
| 122 | |
| 123 | if not os.path.exists(o.files_dir): |
| 124 | os.makedirs(o.files_dir) |
| 125 | |
| 126 | # Combine the service attributes with the tenant attributes. Tenant |
| 127 | # attribute can override service attributes. |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame] | 128 | attrs = o.owner.serviceattribute_dict |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 129 | attrs.update(o.tenantattribute_dict) |
| 130 | |
Scott Baker | 3bce78b | 2017-09-21 12:45:45 -0700 | [diff] [blame] | 131 | # Check to see if we're waiting on autoconfig |
| 132 | if (attrs.get("autogenerate") in ["vtn-network-cfg",]) and \ |
| 133 | (not attrs.get("rest_onos/v1/network/configuration/")): |
| 134 | raise DeferredException("Network configuration is not populated yet") |
| 135 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 136 | ordered_attrs = attrs.keys() |
| 137 | |
| 138 | onos = self.get_onos_service(o) |
| 139 | if onos.node_key: |
| 140 | file(os.path.join(o.files_dir, "node_key"),"w").write(onos.node_key) |
| 141 | o.node_key_fn="node_key" |
| 142 | else: |
| 143 | o.node_key_fn=None |
| 144 | |
| 145 | o.early_rest_configs=[] |
| 146 | if ("cordvtn" in o.dependencies) and (not self.is_no_container(o)): |
| 147 | # For VTN, since it's running in a docker host container, we need |
| 148 | # to make sure it configures the cluster using the right ip addresses. |
| 149 | # NOTE: rest_onos/v1/cluster/configuration/ will reboot the cluster and |
| 150 | # must go first. |
| 151 | name="rest_onos/v1/cluster/configuration/" |
| 152 | value= self.get_cluster_configuration(o) |
| 153 | fn = name[5:].replace("/","_") |
| 154 | endpoint = name[5:] |
| 155 | file(os.path.join(o.files_dir, fn),"w").write(" " +value) |
| 156 | o.early_rest_configs.append( {"endpoint": endpoint, "fn": fn} ) |
| 157 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 158 | for name in attrs.keys(): |
| 159 | value = attrs[name] |
| 160 | if name.startswith("config_"): |
| 161 | fn = name[7:] # .replace("_json",".json") |
| 162 | o.config_fns.append(fn) |
| 163 | file(os.path.join(o.files_dir, fn),"w").write(value) |
| 164 | if name.startswith("rest_"): |
| 165 | fn = name[5:].replace("/","_") |
| 166 | endpoint = name[5:] |
| 167 | # Ansible goes out of it's way to make our life difficult. If |
| 168 | # 'lookup' sees a file that it thinks contains json, then it'll |
| 169 | # insist on parsing and return a json object. We just want |
| 170 | # a string, so prepend a space and then strip the space off |
| 171 | # later. |
| 172 | file(os.path.join(o.files_dir, fn),"w").write(" " +value) |
| 173 | o.rest_configs.append( {"endpoint": endpoint, "fn": fn} ) |
| 174 | if name.startswith("component_config"): |
| 175 | components = json.loads(value,object_pairs_hook=OrderedDict) |
| 176 | for component in components.keys(): |
| 177 | config = components[component] |
| 178 | for key in config.keys(): |
| 179 | config_val = config[key] |
| 180 | found = re.findall('<(.+?)>',config_val) |
| 181 | for x in found: |
| 182 | #Get value corresponding to that string |
| 183 | val = self.get_dynamic_parameter_value(o, x) |
| 184 | if val: |
| 185 | config_val = re.sub('<'+x+'>', val, config_val) |
| 186 | #TODO: else raise an exception? |
| 187 | o.component_configs.append( {"component": component, "config_params": "'{\""+key+"\":\""+config_val+"\"}'"} ) |
| 188 | |
| 189 | def prepare_record(self, o): |
| 190 | self.write_configs(o) |
| 191 | |
| 192 | def get_extra_attributes_common(self, o): |
| 193 | fields = {} |
| 194 | |
| 195 | # These are attributes that are not dependent on Instance. For example, |
| 196 | # REST API stuff. |
| 197 | |
| 198 | onos = self.get_onos_service(o) |
| 199 | |
| 200 | fields["files_dir"] = o.files_dir |
| 201 | fields["appname"] = o.name |
| 202 | fields["rest_configs"] = o.rest_configs |
Scott Baker | d84abe8 | 2017-10-19 09:51:10 -0700 | [diff] [blame] | 203 | fields["component_configs"] = o.component_configs |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 204 | fields["rest_hostname"] = onos.rest_hostname |
Scott Baker | 0c62dfa | 2017-11-21 09:15:43 -0800 | [diff] [blame] | 205 | fields["rest_username"] = onos.rest_username |
| 206 | fields["rest_password"] = onos.rest_password |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 207 | fields["rest_port"] = onos.rest_port |
| 208 | |
| 209 | if o.dependencies: |
| 210 | fields["dependencies"] = [x.strip() for x in o.dependencies.split(",")] |
| 211 | else: |
| 212 | fields["dependencies"] = [] |
| 213 | |
Andy Bavier | 4a503b1 | 2016-06-21 11:41:29 -0400 | [diff] [blame] | 214 | if o.install_dependencies: |
| 215 | fields["install_dependencies"] = [x.strip() for x in o.install_dependencies.split(",")] |
| 216 | else: |
| 217 | fields["install_dependencies"] = [] |
| 218 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 219 | return fields |
| 220 | |
| 221 | def get_extra_attributes_full(self, o): |
| 222 | instance = self.get_instance(o) |
| 223 | |
| 224 | fields = self.get_extra_attributes_common(o) |
| 225 | |
| 226 | fields["config_fns"] = o.config_fns |
| 227 | fields["early_rest_configs"] = o.early_rest_configs |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 228 | fields["node_key_fn"] = o.node_key_fn |
| 229 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 230 | if (instance.isolation=="container"): |
| 231 | fields["ONOS_container"] = "%s-%s" % (instance.slice.name, str(instance.id)) |
| 232 | else: |
| 233 | fields["ONOS_container"] = "ONOS" |
| 234 | return fields |
| 235 | |
| 236 | def get_extra_attributes(self, o): |
| 237 | if self.is_no_container(o): |
| 238 | return self.get_extra_attributes_common(o) |
| 239 | else: |
| 240 | return self.get_extra_attributes_full(o) |
| 241 | |
| 242 | def sync_fields(self, o, fields): |
| 243 | # the super causes the playbook to be run |
| 244 | super(SyncONOSApp, self).sync_fields(o, fields) |
| 245 | |
| 246 | def run_playbook(self, o, fields): |
| 247 | if self.is_no_container(o): |
| 248 | # There is no machine to SSH to, so use the synchronizer's |
| 249 | # run_template method directly. |
Sapan Bhatia | 8140552 | 2017-02-04 09:26:31 -0800 | [diff] [blame] | 250 | run_template("sync_onosapp_nocontainer.yaml", fields, object=o) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 251 | else: |
| 252 | super(SyncONOSApp, self).run_playbook(o, fields) |
| 253 | |
| 254 | def delete_record(self, m): |
| 255 | pass |