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