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 |
| 7 | from django.db.models import F, Q |
| 8 | from xos.config import Config |
| 9 | from synchronizers.base.syncstep import SyncStep |
Sapan Bhatia | 67d3701 | 2017-01-24 19:32:59 +0100 | [diff] [blame] | 10 | from synchronizers.base.ansible_helper import run_template_ssh |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 11 | from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 12 | from core.models import Service, Slice |
| 13 | from services.onos.models import ONOSService, ONOSApp |
| 14 | from xos.logger import Logger, logging |
| 15 | |
| 16 | # hpclibrary will be in steps/.. |
| 17 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 18 | sys.path.insert(0,parentdir) |
| 19 | |
| 20 | logger = Logger(level=logging.INFO) |
| 21 | |
| 22 | class SyncONOSService(SyncInstanceUsingAnsible): |
| 23 | provides=[ONOSService] |
| 24 | observes=ONOSService |
| 25 | requested_interval=0 |
| 26 | template_name = "sync_onosservice.yaml" |
| 27 | #service_key_name = "/opt/xos/synchronizers/onos/onos_key" |
| 28 | |
| 29 | def __init__(self, *args, **kwargs): |
| 30 | super(SyncONOSService, self).__init__(*args, **kwargs) |
| 31 | |
| 32 | def fetch_pending(self, deleted): |
| 33 | if (not deleted): |
| 34 | objs = ONOSService.get_service_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 35 | else: |
| 36 | objs = ONOSService.get_deleted_service_objects() |
| 37 | |
| 38 | return objs |
| 39 | |
| 40 | def get_instance(self, o): |
| 41 | # We assume the ONOS service owns a slice, so pick one of the instances |
| 42 | # inside that slice to sync to. |
| 43 | |
| 44 | serv = o |
| 45 | |
| 46 | if serv.slices.exists(): |
| 47 | slice = serv.slices.all()[0] |
| 48 | if slice.instances.exists(): |
| 49 | return slice.instances.all()[0] |
| 50 | |
| 51 | return None |
| 52 | |
| 53 | def get_extra_attributes(self, o): |
| 54 | fields={} |
| 55 | fields["instance_hostname"] = self.get_instance(o).instance_name.replace("_","-") |
| 56 | fields["appname"] = o.name |
| 57 | fields["ONOS_container"] = "ONOS" |
| 58 | return fields |
| 59 | |
| 60 | def sync_record(self, o): |
| 61 | if o.no_container: |
| 62 | logger.info("no work to do for onos service, because o.no_container is set",extra=o.tologdict()) |
| 63 | o.save() |
| 64 | else: |
| 65 | super(SyncONOSService, self).sync_record(o) |
| 66 | |
| 67 | def sync_fields(self, o, fields): |
| 68 | # the super causes the playbook to be run |
| 69 | super(SyncONOSService, self).sync_fields(o, fields) |
| 70 | |
| 71 | def run_playbook(self, o, fields): |
| 72 | instance = self.get_instance(o) |
| 73 | if (instance.isolation=="container"): |
| 74 | # If the instance is already a container, then we don't need to |
| 75 | # install ONOS. |
| 76 | return |
| 77 | super(SyncONOSService, self).run_playbook(o, fields) |
| 78 | |
| 79 | def delete_record(self, m): |
| 80 | pass |