def dispatch(self, instance: Instance): if instance.get_enabled(): instance_running = self._command.is_running(instance) just_enabled = self._property_changed('enabled', instance) if just_enabled or not instance_running: self._foreman.run(instance) if not just_enabled and not instance_running: raise StartedBecauseShouldBeRunning( "Instance {0} started because not running on {1}".format(instance.get_name(), instance.get_host())) else: self._foreman.stop(instance) return instance
def __init__(self, supervisor, instance: Instance = None): self._before = {} if instance: self._before = instance.to_dict() self._foreman = supervisor.get_foreman_class()(supervisor) self._command = Command(HostObjects(supervisor.get_hosts()))
def update(self, instance: Instance): self._database.update(instance.to_dict(), self._instances.name == instance.get_name())
def delete(self, instance: Instance): self._database.remove(self._instances.name == instance.get_name())
def find_one_by_name(self, name: str) -> Instance: search_results = self._database.search(self._instances.name == name) if not search_results: raise NotFound('No instance with name "{0}"'.format(name)) return Instance.from_dict(search_results.pop())
def find_by_host(self, host: Host) -> List[Instance]: """ :param host: host instance filter :return: list of instances """ return [Instance.from_dict(data) for data in self._database.search(self._instances.host == host.get_name())]
def get_all(self) -> list: return [Instance.from_dict(instance_data) for instance_data in self._database.all()]
def is_running(self, instance: Instance): host = self._hosts.find_one_by_name(instance.get_host()) return self._container_exist(host, instance.get_name(), {'filters': {'status': 'running'}})
def exist(self, instance: Instance): instance_name = instance.get_name() if not instance_name: return False host = self._hosts.find_one_by_name(instance.get_host()) return self._container_exist(host, instance.get_name())