Exemple #1
0
 def is_up_clear_cache(self):
     self.unset_lazy("container_id")
     unset_lazy(self.lib, "container_ps")
     unset_lazy(self.lib, "running_instance_ids")
     unset_lazy(self.lib, "container_by_label")
     unset_lazy(self.lib, "container_by_name")
     unset_lazy(self.lib, "containers_inspect")
Exemple #2
0
 def get_ps(self, refresh=False):
     """
     Return the "docker ps" output from cache or from the command
     execution depending on <refresh>.
     """
     if refresh:
         unset_lazy(self, "container_ps")
     return self.container_ps
Exemple #3
0
 def get_running_instance_ids(self, refresh=False):
     """
     Return the list of running docker instances id.
     """
     if refresh:
         unset_lazy(self, "container_ps")
         unset_lazy(self, "running_instance_ids")
     return self.running_instance_ids
Exemple #4
0
    def docker_stop(self):
        """
        Stop the docker daemon if possible.
        """
        def can_stop():
            """
            Return True if the docker daemon can be stopped.
            """
            if not self.docker_daemon_private:
                return False
            if not self.docker_running():
                return False
            if self.container_data_dir is None:
                return False
            if not os.path.exists(self.docker_pid_file):
                return False
            if len(self.get_running_instance_ids(refresh=True)) > 0:
                return False
            return True

        if not can_stop():
            return

        try:
            with open(self.docker_pid_file, "r") as ofile:
                pid = int(ofile.read())
        except (OSError, IOError):
            self.svc.log.warning("can't read %s. skip docker daemon kill",
                                 self.docker_pid_file)
            return

        self.svc.log.info(
            "no more container handled by docker daemon (pid %d)."
            " shut it down", pid)
        import signal
        import time
        tries = 15
        os.kill(pid, signal.SIGTERM)
        unset_lazy(self, "docker_info")

        while self.docker_running() and tries > 0:
            unset_lazy(self, "docker_info")
            tries -= 1
            time.sleep(1)
        if self.docker_running() and tries == 0:
            self.svc.log.warning("dockerd did not stop properly. send a kill "
                                 "signal")
            try:
                os.kill(pid, signal.SIGKILL)
            except OSError as exc:
                if exc.errno == errno.ESRCH:
                    # already dead
                    pass
                else:
                    raise ex.excError("failed to kill docker daemon: %s" %
                                      str(exc))
Exemple #5
0
 def get_container_id_by_name(self, resource, refresh=False):
     """
     Return the container id for the <resource> container resource.
     Lookup in docker ps by docker name <namepace>..<name>.container.<n>
     where <n> is the identifier part of the resource id.
     """
     if refresh:
         unset_lazy(self, "container_ps")
         unset_lazy(self, "container_by_name")
     try:
         return self.container_by_name[resource.name][0]["ID"]
     except Exception:
         return
Exemple #6
0
 def get_container_id_by_label(self, resource, refresh=False):
     """
     Return the container id for the <resource> container resource.
     Lookup in docker ps by docker label com.opensvc.id=<...>.
     """
     if refresh:
         unset_lazy(self, "container_ps")
         unset_lazy(self, "container_by_label")
     try:
         return self.container_by_label[
             resource.container_label_id][0]["ID"]
     except Exception as exc:
         return
Exemple #7
0
    def _read_config(self):
        """
        Reload the node configuration file and notify the threads to do the
        same, if the file's mtime has changed since the last load.
        """
        mtime = self.get_config_mtime()
        if mtime is None:
            return
        if self.last_config_mtime is not None and \
           self.last_config_mtime >= mtime:
            return
        try:
            with shared.NODE_LOCK:
                if shared.NODE:
                    shared.NODE.close()
                shared.NODE = node_mod.Node()
                shared.NODE.set_rlimit()
                shared.NODE.network_setup()
            unset_lazy(self, "config_hbs")
            if self.last_config_mtime:
                self.log.info("node config reloaded (changed)")
            else:
                self.log.info("node config loaded")
            self.last_config_mtime = mtime

            # signal the node config change to threads
            for thr in self.threads.values():
                if thr.stopped():
                    thr.unstop()
                else:
                    thr.notify_config_change()
            shared.wake_monitor(reason="config change", immediate=True)

            # signal the caller the config has changed
            return True
        except Exception as exc:
            self.log.warning("failed to load config: %s", str(exc))
Exemple #8
0
 def get_container_id(self, resource, refresh=False):
     if refresh:
         unset_lazy(self, "container_ps")
         unset_lazy(self, "container_by_label")
         unset_lazy(self, "container_by_name")
     cid = self.get_container_id_by_label(resource, refresh=False)
     if cid:
         return cid
     try:
         return self.get_container_id_by_name(resource, refresh=False)
     except:
         return
Exemple #9
0
 def unset_lazy(self, prop):
     """
     Expose the self.unset_lazy(...) utility function as a method,
     so Node() users don't have to import it from rcUtilities.
     """
     unset_lazy(self, prop)
Exemple #10
0
 def clear_daemon_caches(self):
     unset_lazy(self, "container_ps")
     unset_lazy(self, "container_by_name")
     unset_lazy(self, "container_by_label")
     unset_lazy(self, "docker_info")
     unset_lazy(self, "running_instance_ids")
     unset_lazy(self, "images")
     unset_lazy(self, "containers_inspect")