blob: ce446cf3dbdde9b4ead784e6dde404cbe6d96769 [file] [log] [blame]
Scott Baker7a327592016-06-20 17:34:06 -07001import hashlib
2import os
3import socket
4import sys
5import base64
6import time
7from django.db.models import F, Q
8from xos.config import Config
9from synchronizers.base.syncstep import SyncStep
10from synchronizers.base.ansible import run_template_ssh
11from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
12from core.models import Service, Slice
13from services.onos.models import ONOSService, ONOSApp
14from xos.logger import Logger, logging
15
16# hpclibrary will be in steps/..
17parentdir = os.path.join(os.path.dirname(__file__),"..")
18sys.path.insert(0,parentdir)
19
20logger = Logger(level=logging.INFO)
21
22class 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