示例#1
0
def prepare_tmpfs(version_info: VersionInfo, site: SiteContext) -> None:
    if tmpfs_mounted(site.name):
        sys.stdout.write("Temporary filesystem already mounted\n")
        return  # Fine: Mounted

    if site.conf["TMPFS"] != "on":
        sys.stdout.write("Preparing tmp directory %s..." % site.tmp_dir)
        sys.stdout.flush()

        if os.path.exists(site.tmp_dir):
            return

        try:
            os.mkdir(site.tmp_dir)
        except OSError as e:
            if e.errno != errno.EEXIST:  # File exists
                raise
        return

    sys.stdout.write("Creating temporary filesystem %s..." % site.tmp_dir)
    sys.stdout.flush()
    if not os.path.exists(site.tmp_dir):
        os.mkdir(site.tmp_dir)

    mount_options = shlex.split(version_info.MOUNT_OPTIONS)
    p = subprocess.Popen(  # pylint:disable=consider-using-with
        ["mount"] + mount_options + [site.tmp_dir],
        shell=False,
        stdin=open(os.devnull),  # pylint:disable=consider-using-with
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        encoding="utf-8",
    )
    exit_code = p.wait()
    if exit_code == 0:
        ok()
        return  # Fine: Mounted

    if p.stdout is None:
        raise Exception("stdout needs to be set")

    sys.stdout.write(p.stdout.read())
    if is_dockerized():
        sys.stdout.write(
            tty.warn + ": "
            "Could not mount tmpfs. You may either start the container in "
            'privileged mode or use the "docker run" option "--tmpfs" to '
            "make docker do the tmpfs mount for the site.\n"
        )

    sys.stdout.write(
        tty.warn + ": You may continue without tmpfs, but the "
        "performance of Check_MK may be degraded.\n"
    )
示例#2
0
    def exists(self) -> bool:
        # In dockerized environments the tmpfs may be managed by docker (when
        # using the --tmpfs option).  In this case the site directory is
        # created as parent of the tmp directory to mount the tmpfs during
        # container initialization. Detect this situation and don't treat the
        # site as existing in that case.
        if is_dockerized():
            if not os.path.exists(self.dir):
                return False
            if os.listdir(self.dir) == ["tmp"]:
                return False
            return True

        return os.path.exists(self.dir)
示例#3
0
def _tmpfs_is_managed_by_node(site: SiteContext) -> bool:
    """When running in a container, and the tmpfs is managed by the node, the
    mount is visible, but can not be unmounted. umount exits with 32 in this
    case. Treat this case like there is no tmpfs and only the directory needs
    to be cleaned."""
    if not is_dockerized():
        return False

    if not tmpfs_mounted(site.name):
        return False

    return subprocess.call(["umount", site.tmp_dir],
                           stdout=open(os.devnull, "w"),
                           stderr=subprocess.STDOUT) in [1, 32]