Scott Baker | b0eb23e | 2016-06-01 16:08:04 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import base64 |
| 4 | from django.db.models import F, Q |
| 5 | from xos.config import Config |
Scott Baker | 0baadc4 | 2016-06-08 17:32:45 -0700 | [diff] [blame] | 6 | from synchronizers.base.syncstep import SyncStep, DeferredException |
Scott Baker | b0eb23e | 2016-06-01 16:08:04 -0700 | [diff] [blame] | 7 | from core.models import XOS |
| 8 | from xos.logger import Logger, logging |
Scott Baker | 8d252b6 | 2016-06-01 23:08:04 -0700 | [diff] [blame] | 9 | from synchronizers.base.ansible import run_template |
Scott Baker | b0eb23e | 2016-06-01 16:08:04 -0700 | [diff] [blame] | 10 | |
| 11 | # xosbuilder will be in steps/.. |
| 12 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 13 | sys.path.insert(0,parentdir) |
| 14 | |
| 15 | from xosbuilder import XOSBuilder |
| 16 | |
| 17 | logger = Logger(level=logging.INFO) |
| 18 | |
| 19 | class SyncXOS(SyncStep, XOSBuilder): |
| 20 | provides=[XOS] |
| 21 | observes=XOS |
| 22 | requested_interval=0 |
Scott Baker | 8d252b6 | 2016-06-01 23:08:04 -0700 | [diff] [blame] | 23 | playbook = "sync_xos.yaml" |
Scott Baker | b0eb23e | 2016-06-01 16:08:04 -0700 | [diff] [blame] | 24 | |
| 25 | def __init__(self, **args): |
| 26 | SyncStep.__init__(self, **args) |
| 27 | XOSBuilder.__init__(self) |
| 28 | |
Scott Baker | 8d252b6 | 2016-06-01 23:08:04 -0700 | [diff] [blame] | 29 | def sync_record(self, xos): |
| 30 | logger.info("Sync'ing XOS %s" % xos) |
| 31 | |
Scott Baker | 3a7a69c | 2016-06-15 15:41:08 -0700 | [diff] [blame] | 32 | if not xos.docker_project_name: |
| 33 | raise Exception("xos.docker_project_name is not set") |
| 34 | |
Scott Baker | 0c0139a | 2016-06-22 14:54:23 -0700 | [diff] [blame] | 35 | if not xos.db_container_name: |
| 36 | raise Exception("xos.db_container_name is not set") |
| 37 | |
Scott Baker | 0baadc4 | 2016-06-08 17:32:45 -0700 | [diff] [blame] | 38 | if (not xos.enable_build): |
| 39 | raise DeferredException("XOS build is currently disabled") |
| 40 | |
Scott Baker | aa1ac6f | 2016-07-28 16:50:09 -0700 | [diff] [blame^] | 41 | # 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 Baker | aab8a29 | 2016-06-03 16:32:45 -0700 | [diff] [blame] | 53 | self.create_docker_compose() |
| 54 | |
Scott Baker | 8d252b6 | 2016-06-01 23:08:04 -0700 | [diff] [blame] | 55 | dockerfiles = [self.create_ui_dockerfile()] |
| 56 | tenant_fields = {"dockerfiles": dockerfiles, |
| 57 | "build_dir": self.build_dir, |
Scott Baker | 83b3fad | 2016-06-07 10:32:28 -0700 | [diff] [blame] | 58 | "docker_project_name": xos.docker_project_name, |
Scott Baker | 8d252b6 | 2016-06-01 23:08:04 -0700 | [diff] [blame] | 59 | "ansible_tag": xos.__class__.__name__ + "_" + str(xos.id)} |
| 60 | |
| 61 | path="XOS" |
| 62 | res = run_template(self.playbook, tenant_fields, path=path) |
Scott Baker | b0eb23e | 2016-06-01 16:08:04 -0700 | [diff] [blame] | 63 | |
| 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 | |