Scott Baker | 31acc65 | 2016-06-23 15:47:56 -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 |
Murat Parlakisik | 638c65f | 2017-05-31 11:10:24 +0300 | [diff] [blame^] | 9 | from synchronizers.new_base.syncstep import SyncStep |
| 10 | from synchronizers.new_base.ansible_helper import run_template_ssh |
| 11 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 12 | from modelaccessor import * |
| 13 | #from core.models import Service, Slice |
| 14 | #from services.monitoring.models import SFlowService |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 15 | from xos.logger import Logger, logging |
| 16 | |
| 17 | # hpclibrary will be in steps/.. |
| 18 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 19 | sys.path.insert(0,parentdir) |
| 20 | |
| 21 | logger = Logger(level=logging.INFO) |
| 22 | |
| 23 | class SyncSFlowService(SyncInstanceUsingAnsible): |
| 24 | provides=[SFlowService] |
| 25 | observes=SFlowService |
| 26 | requested_interval=0 |
| 27 | template_name = "sync_sflowservice.yaml" |
Srikanth Vavilapalli | d84b7b7 | 2016-06-28 00:19:07 +0000 | [diff] [blame] | 28 | service_key_name = "/opt/xos/synchronizers/monitoring/monitoring_channel_private_key" |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 29 | |
| 30 | def __init__(self, *args, **kwargs): |
| 31 | super(SyncSFlowService, self).__init__(*args, **kwargs) |
| 32 | |
| 33 | def fetch_pending(self, deleted): |
| 34 | if (not deleted): |
| 35 | objs = SFlowService.get_service_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 36 | else: |
| 37 | objs = SFlowService.get_deleted_service_objects() |
| 38 | |
| 39 | return objs |
| 40 | |
| 41 | def get_instance(self, o): |
| 42 | # We assume the ONOS service owns a slice, so pick one of the instances |
| 43 | # inside that slice to sync to. |
| 44 | |
| 45 | serv = o |
| 46 | |
| 47 | if serv.slices.exists(): |
| 48 | slice = serv.slices.all()[0] |
| 49 | if slice.instances.exists(): |
| 50 | return slice.instances.all()[0] |
| 51 | |
| 52 | return None |
| 53 | |
| 54 | def get_extra_attributes(self, o): |
| 55 | fields={} |
| 56 | fields["instance_hostname"] = self.get_instance(o).instance_name.replace("_","-") |
| 57 | fields["sflow_port"] = o.sflow_port |
| 58 | fields["sflow_api_port"] = o.sflow_api_port |
| 59 | fields["sflow_container"] = "sflowpubsub" |
| 60 | return fields |
| 61 | |
| 62 | def sync_fields(self, o, fields): |
| 63 | # the super causes the playbook to be run |
| 64 | super(SyncSFlowService, self).sync_fields(o, fields) |
| 65 | |
| 66 | def run_playbook(self, o, fields): |
| 67 | instance = self.get_instance(o) |
| 68 | if (instance.isolation=="container"): |
| 69 | # If the instance is already a container, then we don't need to |
| 70 | # install ONOS. |
| 71 | return |
| 72 | super(SyncSFlowService, self).run_playbook(o, fields) |
| 73 | |
| 74 | def delete_record(self, m): |
| 75 | pass |