Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 1 | import hashlib |
| 2 | import os |
| 3 | import socket |
| 4 | import socket |
| 5 | import sys |
| 6 | import base64 |
| 7 | import time |
| 8 | import re |
| 9 | import json |
| 10 | from django.db.models import F, Q |
| 11 | from xos.config import Config |
| 12 | from synchronizers.base.syncstep import SyncStep |
| 13 | from synchronizers.base.ansible import run_template_ssh |
| 14 | from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 15 | from core.models import Service, Slice, ControllerSlice, ControllerUser |
Srikanth Vavilapalli | d84b7b7 | 2016-06-28 00:19:07 +0000 | [diff] [blame] | 16 | from services.monitoring.models import SFlowService, SFlowTenant |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 17 | from xos.logger import Logger, logging |
| 18 | |
| 19 | # hpclibrary will be in steps/.. |
| 20 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 21 | sys.path.insert(0,parentdir) |
| 22 | |
| 23 | logger = Logger(level=logging.INFO) |
| 24 | |
| 25 | class SyncSFlowTenant(SyncInstanceUsingAnsible): |
| 26 | provides=[SFlowTenant] |
| 27 | observes=SFlowTenant |
| 28 | requested_interval=0 |
| 29 | template_name = "sync_sflowtenant.yaml" |
Srikanth Vavilapalli | d84b7b7 | 2016-06-28 00:19:07 +0000 | [diff] [blame] | 30 | service_key_name = "/opt/xos/synchronizers/monitoring/monitoring_channel_private_key" |
Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 31 | |
| 32 | def __init__(self, *args, **kwargs): |
| 33 | super(SyncSFlowTenant, self).__init__(*args, **kwargs) |
| 34 | |
| 35 | def fetch_pending(self, deleted): |
| 36 | if (not deleted): |
| 37 | objs = SFlowTenant.get_tenant_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 38 | else: |
| 39 | objs = SFlowTenant.get_deleted_tenant_objects() |
| 40 | |
| 41 | return objs |
| 42 | |
| 43 | def get_sflow_service(self, o): |
| 44 | sflows = SFlowService.get_service_objects().filter(id=o.provider_service.id) |
| 45 | if not sflows: |
| 46 | raise "No associated SFlow service" |
| 47 | |
| 48 | return sflows[0] |
| 49 | |
| 50 | def get_instance(self, o): |
| 51 | # We assume the SFlow service owns a slice, so pick one of the instances |
| 52 | # inside that slice to sync to. |
| 53 | |
| 54 | serv = self.get_sflow_service(o) |
| 55 | |
| 56 | if serv.slices.exists(): |
| 57 | slice = serv.slices.all()[0] |
| 58 | if slice.instances.exists(): |
| 59 | return slice.instances.all()[0] |
| 60 | |
| 61 | return None |
| 62 | |
| 63 | def get_extra_attributes(self, o): |
| 64 | instance = self.get_instance(o) |
| 65 | |
| 66 | fields={} |
| 67 | fields["sflow_api_base_url"] = self.get_sflow_service(o).sflow_api_url |
| 68 | fields["sflow_api_port"] = self.get_sflow_service(o).sflow_api_port |
| 69 | fields["listening_endpoint"] = o.listening_endpoint |
| 70 | fields["sflow_container"] = "sflowpubsub" |
| 71 | |
| 72 | return fields |
| 73 | |
| 74 | def sync_fields(self, o, fields): |
| 75 | # the super causes the playbook to be run |
| 76 | super(SyncSFlowTenant, self).sync_fields(o, fields) |
| 77 | |
| 78 | def run_playbook(self, o, fields): |
| 79 | super(SyncSFlowTenant, self).run_playbook(o, fields) |
| 80 | |
| 81 | def delete_record(self, m): |
| 82 | pass |