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 |
| 9 | from synchronizers.base.syncstep import SyncStep |
Sapan Bhatia | 7e9f4d3 | 2017-01-24 19:32:59 +0100 | [diff] [blame] | 10 | from synchronizers.base.ansible_helper import run_template_ssh |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 11 | from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 12 | from core.models import Service, Slice |
Srikanth Vavilapalli | d84b7b7 | 2016-06-28 00:19:07 +0000 | [diff] [blame] | 13 | from services.monitoring.models import SFlowService |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 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 SyncSFlowService(SyncInstanceUsingAnsible): |
| 23 | provides=[SFlowService] |
| 24 | observes=SFlowService |
| 25 | requested_interval=0 |
| 26 | template_name = "sync_sflowservice.yaml" |
Srikanth Vavilapalli | d84b7b7 | 2016-06-28 00:19:07 +0000 | [diff] [blame] | 27 | service_key_name = "/opt/xos/synchronizers/monitoring/monitoring_channel_private_key" |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 28 | |
| 29 | def __init__(self, *args, **kwargs): |
| 30 | super(SyncSFlowService, self).__init__(*args, **kwargs) |
| 31 | |
| 32 | def fetch_pending(self, deleted): |
| 33 | if (not deleted): |
| 34 | objs = SFlowService.get_service_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 35 | else: |
| 36 | objs = SFlowService.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["sflow_port"] = o.sflow_port |
| 57 | fields["sflow_api_port"] = o.sflow_api_port |
| 58 | fields["sflow_container"] = "sflowpubsub" |
| 59 | return fields |
| 60 | |
| 61 | def sync_fields(self, o, fields): |
| 62 | # the super causes the playbook to be run |
| 63 | super(SyncSFlowService, self).sync_fields(o, fields) |
| 64 | |
| 65 | def run_playbook(self, o, fields): |
| 66 | instance = self.get_instance(o) |
| 67 | if (instance.isolation=="container"): |
| 68 | # If the instance is already a container, then we don't need to |
| 69 | # install ONOS. |
| 70 | return |
| 71 | super(SyncSFlowService, self).run_playbook(o, fields) |
| 72 | |
| 73 | def delete_record(self, m): |
| 74 | pass |