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