Exemple #1
0
 def up(self):
     try:
         container, has_been_created = self._get_container()
         if has_been_created:
             return None
         # Check if the container is out of date, delete it and retry
         if len(container.image.tags) > 0:
             tag: str = container.image.tags[0]
             _, _, version = tag.partition(":")
             if version != __version__:
                 self.logger.info(
                     "Container has mismatched version, re-creating...",
                     has=version,
                     should=__version__,
                 )
                 self.down()
                 return self.up()
         # Check that container values match our values
         if self._comp_env(container):
             self.logger.info("Container has outdated config, re-creating...")
             self.down()
             return self.up()
         if (
             container.attrs.get("HostConfig", {})
             .get("RestartPolicy", {})
             .get("Name", "")
             .lower()
             != "unless-stopped"
         ):
             self.logger.info(
                 "Container has mis-matched restart policy, re-creating..."
             )
             self.down()
             return self.up()
         # Check that container is healthy
         if (
             container.status == "running"
             and container.attrs.get("State", {}).get("Health", {}).get("Status", "")
             != "healthy"
         ):
             # At this point we know the config is correct, but the container isn't healthy,
             # so we just restart it with the same config
             if has_been_created:
                 # Since we've just created the container, give it some time to start.
                 # If its still not up by then, restart it
                 self.logger.info(
                     "Container is unhealthy and new, giving it time to boot."
                 )
                 sleep(60)
             self.logger.info("Container is unhealthy, restarting...")
             container.restart()
             return None
         # Check that container is running
         if container.status != "running":
             self.logger.info("Container is not running, restarting...")
             container.start()
             return None
         return None
     except DockerException as exc:
         raise ControllerException(str(exc)) from exc
Exemple #2
0
    def up(self):
        try:
            for reconcile_key in self.reconcile_order:
                reconciler = self.reconcilers[reconcile_key](self)
                reconciler.up()

        except (OpenApiException, HTTPError, ServiceConnectionInvalid) as exc:
            raise ControllerException(str(exc)) from exc
Exemple #3
0
 def down(self):
     try:
         container, _ = self._get_container()
         if container.status == "running":
             container.kill()
         container.remove(force=True)
     except DockerException as exc:
         raise ControllerException(str(exc)) from exc
Exemple #4
0
    def up(self):
        try:
            for reconcile_key in self.reconcile_order:
                reconciler = self.reconcilers[reconcile_key](self)
                reconciler.up()

        except ApiException as exc:
            raise ControllerException(str(exc)) from exc
Exemple #5
0
    def down(self):
        try:
            for reconcile_key in self.reconcile_order:
                reconciler = self.reconcilers[reconcile_key](self)
                self.logger.debug("Tearing down object", name=reconcile_key)
                reconciler.down()

        except (OpenApiException, HTTPError, ServiceConnectionInvalid) as exc:
            raise ControllerException(str(exc)) from exc
Exemple #6
0
    def down(self):
        try:
            for reconcile_key in self.reconcile_order:
                reconciler = self.reconcilers[reconcile_key](self)
                self.logger.debug("Tearing down object", name=reconcile_key)
                reconciler.down()

        except ApiException as exc:
            raise ControllerException(str(exc)) from exc
Exemple #7
0
 def up_with_logs(self) -> list[str]:
     try:
         all_logs = []
         for reconcile_key in self.reconcile_order:
             with capture_logs() as logs:
                 reconciler = self.reconcilers[reconcile_key](self)
                 reconciler.up()
             all_logs += [f"{reconcile_key.title()}: {x['event']}" for x in logs]
         return all_logs
     except ApiException as exc:
         raise ControllerException(str(exc)) from exc
Exemple #8
0
 def down(self):
     if self.outpost.managed == MANAGED_OUTPOST:
         return
     try:
         container, _ = self._get_container()
         if container.status == "running":
             self.logger.info("Stopping container.")
             container.kill()
         self.logger.info("Removing container.")
         container.remove(force=True)
     except DockerException as exc:
         raise ControllerException(str(exc)) from exc
Exemple #9
0
 def up_with_logs(self) -> list[str]:
     try:
         all_logs = []
         for reconcile_key in self.reconcile_order:
             if reconcile_key in self.outpost.config.kubernetes_disabled_components:
                 all_logs += [f"{reconcile_key.title()}: Disabled"]
                 continue
             with capture_logs() as logs:
                 reconciler = self.reconcilers[reconcile_key](self)
                 reconciler.up()
             all_logs += [
                 f"{reconcile_key.title()}: {x['event']}" for x in logs
             ]
         return all_logs
     except (OpenApiException, HTTPError, ServiceConnectionInvalid) as exc:
         raise ControllerException(str(exc)) from exc
Exemple #10
0
 def up(self, depth=1):
     if self.outpost.managed == MANAGED_OUTPOST:
         return None
     if depth >= 10:
         raise ControllerException(
             "Giving up since we exceeded recursion limit.")
     self._migrate_container_name()
     try:
         container, has_been_created = self._get_container()
         if has_been_created:
             container.start()
             return None
         # Check if the container is out of date, delete it and retry
         if len(container.image.tags) > 0:
             should_image = self.try_pull_image()
             if should_image not in container.image.tags:  # pragma: no cover
                 self.logger.info(
                     "Container has mismatched image, re-creating...",
                     has=container.image.tags,
                     should=should_image,
                 )
                 self.down()
                 return self.up(depth + 1)
         # Check container's ports
         if self._comp_ports(container):
             self.logger.info(
                 "Container has mis-matched ports, re-creating...")
             self.down()
             return self.up(depth + 1)
         # Check that container values match our values
         if self._comp_env(container):
             self.logger.info(
                 "Container has outdated config, re-creating...")
             self.down()
             return self.up(depth + 1)
         # Check that container values match our values
         if self._comp_labels(container):
             self.logger.info(
                 "Container has outdated labels, re-creating...")
             self.down()
             return self.up(depth + 1)
         if (container.attrs.get("HostConfig", {}).get(
                 "RestartPolicy", {}).get("Name", "").lower() !=
                 "unless-stopped"):
             self.logger.info(
                 "Container has mis-matched restart policy, re-creating...")
             self.down()
             return self.up(depth + 1)
         # Check that container is healthy
         if container.status == "running" and container.attrs.get(
                 "State", {}).get("Health", {}).get(
                     "Status", "") not in ["healthy", "starting"]:
             # At this point we know the config is correct, but the container isn't healthy,
             # so we just restart it with the same config
             if has_been_created:
                 # Since we've just created the container, give it some time to start.
                 # If its still not up by then, restart it
                 self.logger.info(
                     "Container is unhealthy and new, giving it time to boot."
                 )
                 sleep(60)
             self.logger.info("Container is unhealthy, restarting...")
             container.restart()
             return None
         # Check that container is running
         if container.status != "running":
             self.logger.info("Container is not running, restarting...")
             container.start()
             return None
         self.logger.info("Container is running")
         return None
     except DockerException as exc:
         raise ControllerException(str(exc)) from exc