blob: 80753e4b05054247b020e7f1fc302e7365ca2e8b [file] [log] [blame]
Scott Bakerb0eb23e2016-06-01 16:08:04 -07001import os
2import sys
3import base64
4from django.db.models import F, Q
5from xos.config import Config
Scott Baker0baadc42016-06-08 17:32:45 -07006from synchronizers.base.syncstep import SyncStep, DeferredException
Scott Bakerb0eb23e2016-06-01 16:08:04 -07007from core.models import XOS
8from xos.logger import Logger, logging
Scott Baker8d252b62016-06-01 23:08:04 -07009from synchronizers.base.ansible import run_template
Scott Bakerb0eb23e2016-06-01 16:08:04 -070010
11# xosbuilder will be in steps/..
12parentdir = os.path.join(os.path.dirname(__file__),"..")
13sys.path.insert(0,parentdir)
14
15from xosbuilder import XOSBuilder
16
17logger = Logger(level=logging.INFO)
18
19class SyncXOS(SyncStep, XOSBuilder):
20 provides=[XOS]
21 observes=XOS
22 requested_interval=0
Scott Baker8d252b62016-06-01 23:08:04 -070023 playbook = "sync_xos.yaml"
Scott Bakerb0eb23e2016-06-01 16:08:04 -070024
25 def __init__(self, **args):
26 SyncStep.__init__(self, **args)
27 XOSBuilder.__init__(self)
28
Scott Baker8d252b62016-06-01 23:08:04 -070029 def sync_record(self, xos):
30 logger.info("Sync'ing XOS %s" % xos)
31
Scott Baker3a7a69c2016-06-15 15:41:08 -070032 if not xos.docker_project_name:
33 raise Exception("xos.docker_project_name is not set")
34
Scott Baker0c0139a2016-06-22 14:54:23 -070035 if not xos.db_container_name:
36 raise Exception("xos.db_container_name is not set")
37
Scott Baker0baadc42016-06-08 17:32:45 -070038 if (not xos.enable_build):
39 raise DeferredException("XOS build is currently disabled")
40
Scott Bakeraa1ac6f2016-07-28 16:50:09 -070041 # We've seen the XOS object get synced before the ServiceController object
42 # is synced. This results in the XOS UI container getting built with files
43 # from that controller missing. So let's try to defer.
44 #
45 # It could be argued that we should continue to defer if the ServiceController
46 # is in error state, but it is important that a single broken service does
47 # not takedown the entirety of XOS.
48
49 for scr in xos.service_controllers.all():
50 if (scr.backend_status is not None) and (scr.backend_status.startswith("0")):
51 raise DeferredException("Detected unsynced service controller. Deferring.")
52
Scott Bakeraab8a292016-06-03 16:32:45 -070053 self.create_docker_compose()
54
Scott Baker8d252b62016-06-01 23:08:04 -070055 dockerfiles = [self.create_ui_dockerfile()]
56 tenant_fields = {"dockerfiles": dockerfiles,
57 "build_dir": self.build_dir,
Scott Baker83b3fad2016-06-07 10:32:28 -070058 "docker_project_name": xos.docker_project_name,
Scott Baker8d252b62016-06-01 23:08:04 -070059 "ansible_tag": xos.__class__.__name__ + "_" + str(xos.id)}
60
61 path="XOS"
62 res = run_template(self.playbook, tenant_fields, path=path)
Scott Bakerb0eb23e2016-06-01 16:08:04 -070063
64 def delete_record(self, m):
65 pass
66
67 def fetch_pending(self, deleted=False):
68 pend = super(SyncXOS, self).fetch_pending(deleted)
69 return pend
70