Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 1 | import hashlib |
| 2 | import os |
| 3 | import socket |
| 4 | import sys |
| 5 | import base64 |
| 6 | import time |
Scott Baker | 91ee5e4 | 2017-03-14 10:33:52 -0700 | [diff] [blame] | 7 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 8 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 9 | from xos.logger import Logger, logging |
| 10 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 11 | logger = Logger(level=logging.INFO) |
| 12 | |
| 13 | class SyncONOSService(SyncInstanceUsingAnsible): |
| 14 | provides=[ONOSService] |
| 15 | observes=ONOSService |
| 16 | requested_interval=0 |
| 17 | template_name = "sync_onosservice.yaml" |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 18 | |
| 19 | def __init__(self, *args, **kwargs): |
| 20 | super(SyncONOSService, self).__init__(*args, **kwargs) |
| 21 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 22 | def get_instance(self, o): |
| 23 | # We assume the ONOS service owns a slice, so pick one of the instances |
| 24 | # inside that slice to sync to. |
| 25 | |
| 26 | serv = o |
| 27 | |
| 28 | if serv.slices.exists(): |
| 29 | slice = serv.slices.all()[0] |
| 30 | if slice.instances.exists(): |
| 31 | return slice.instances.all()[0] |
| 32 | |
| 33 | return None |
| 34 | |
| 35 | def get_extra_attributes(self, o): |
| 36 | fields={} |
| 37 | fields["instance_hostname"] = self.get_instance(o).instance_name.replace("_","-") |
| 38 | fields["appname"] = o.name |
| 39 | fields["ONOS_container"] = "ONOS" |
| 40 | return fields |
| 41 | |
| 42 | def sync_record(self, o): |
| 43 | if o.no_container: |
| 44 | logger.info("no work to do for onos service, because o.no_container is set",extra=o.tologdict()) |
| 45 | o.save() |
| 46 | else: |
| 47 | super(SyncONOSService, self).sync_record(o) |
| 48 | |
| 49 | def sync_fields(self, o, fields): |
| 50 | # the super causes the playbook to be run |
| 51 | super(SyncONOSService, self).sync_fields(o, fields) |
| 52 | |
| 53 | def run_playbook(self, o, fields): |
| 54 | instance = self.get_instance(o) |
| 55 | if (instance.isolation=="container"): |
| 56 | # If the instance is already a container, then we don't need to |
| 57 | # install ONOS. |
| 58 | return |
| 59 | super(SyncONOSService, self).run_playbook(o, fields) |
| 60 | |
| 61 | def delete_record(self, m): |
| 62 | pass |