示例#1
0
    def save(self):
        meta_dir = get_meta_dir(self.name)
        if not os.path.isdir(meta_dir):
            os.makedirs(meta_dir)
        with open(get_meta_file(self.name), "w") as f:
            f.write(json.dumps(self.Metadata))

        tls_dir = get_tls_dir(self.name)
        for endpoint, tls in self.tls_cfg.items():
            if not os.path.isdir(os.path.join(tls_dir, endpoint)):
                os.makedirs(os.path.join(tls_dir, endpoint))

            ca_file = tls.ca_cert
            if ca_file:
                copyfile(
                    ca_file,
                    os.path.join(tls_dir, endpoint, os.path.basename(ca_file)))

            if tls.cert:
                cert_file, key_file = tls.cert
                copyfile(
                    cert_file,
                    os.path.join(tls_dir, endpoint,
                                 os.path.basename(cert_file)))
                copyfile(
                    key_file,
                    os.path.join(tls_dir, endpoint,
                                 os.path.basename(key_file)))

        self.meta_path = get_meta_dir(self.name)
        self.tls_path = get_tls_dir(self.name)
示例#2
0
    def contexts(cls):
        """Context list.
        Returns:
            (Context): List of context objects.
        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        """
        names = []
        for dirname, dirnames, fnames in os.walk(get_meta_dir()):
            for filename in fnames + dirnames:
                if filename == METAFILE:
                    try:
                        data = json.load(
                            open(os.path.join(dirname, filename), "r"))
                        names.append(data["Name"])
                    except Exception as e:
                        raise errors.ContextException(
                            "Failed to load metafile {}: {}".format(
                                filename, e))

        contexts = [cls.DEFAULT_CONTEXT]
        for name in names:
            contexts.append(Context.load_context(name))
        return contexts
示例#3
0
 def load_context(cls, name):
     name, orchestrator, endpoints = Context._load_meta(name)
     if name:
         instance = cls(name, orchestrator, endpoints=endpoints)
         instance._load_certs()
         instance.meta_path = get_meta_dir(name)
         return instance
     return None
示例#4
0
文件: context.py 项目: yfix/docker-py
 def load_context(cls, name):
     meta = Context._load_meta(name)
     if meta:
         instance = cls(meta["Name"],
                        orchestrator=meta["Metadata"].get(
                            "StackOrchestrator", None),
                        endpoints=meta.get("Endpoints", None))
         instance.context_type = meta["Metadata"].get("Type", None)
         instance._load_certs()
         instance.meta_path = get_meta_dir(name)
         return instance
     return None