| 1 |
# This code is part of the Ansible collection community.docker, but is an independent component. |
| 2 |
# This particular file, and this file only, is based on the Docker SDK for Python (https://github.com/docker/docker-py/) |
| 3 |
# |
| 4 |
# Copyright (c) 2016-2025 Docker, Inc. |
| 5 |
# |
| 6 |
# It is licensed under the Apache 2.0 license (see LICENSES/Apache-2.0.txt in this collection) |
| 7 |
# SPDX-License-Identifier: Apache-2.0 |
| 8 |
|
| 9 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 10 |
# Do not use this from other collections or standalone plugins/modules! |
| 11 |
|
| 12 |
from __future__ import annotations |
| 13 |
|
| 14 |
import hashlib |
| 15 |
import json |
| 16 |
import os |
| 17 |
|
| 18 |
from ..constants import DEFAULT_UNIX_SOCKET, IS_WINDOWS_PLATFORM |
| 19 |
from ..utils.config import find_config_file, get_default_config_file |
| 20 |
from ..utils.utils import parse_host |
| 21 |
|
| 22 |
METAFILE = "meta.json" |
| 23 |
|
| 24 |
|
| 25 |
def get_current_context_name_with_source() -> tuple[str, str]: |
| 26 |
if os.environ.get("DOCKER_HOST"): |
| 27 |
return "default", "DOCKER_HOST environment variable set" |
| 28 |
if os.environ.get("DOCKER_CONTEXT"): |
| 29 |
return os.environ["DOCKER_CONTEXT"], "DOCKER_CONTEXT environment variable set" |
| 30 |
docker_cfg_path = find_config_file() |
| 31 |
if docker_cfg_path: |
| 32 |
try: |
| 33 |
with open(docker_cfg_path, "rt", encoding="utf-8") as f: |
| 34 |
return ( |
| 35 |
json.load(f).get("currentContext", "default"), |
| 36 |
f"configuration file {docker_cfg_path}", |
| 37 |
) |
| 38 |
except Exception: # pylint: disable=broad-exception-caught |
| 39 |
pass |
| 40 |
return "default", "fallback value" |
| 41 |
|
| 42 |
|
| 43 |
def get_current_context_name() -> str: |
| 44 |
return get_current_context_name_with_source()[0] |
| 45 |
|
| 46 |
|
| 47 |
def write_context_name_to_docker_config(name: str | None = None) -> Exception | None: |
| 48 |
if name == "default": |
| 49 |
name = None |
| 50 |
docker_cfg_path = find_config_file() |
| 51 |
config = {} |
| 52 |
if docker_cfg_path: |
| 53 |
try: |
| 54 |
with open(docker_cfg_path, "rt", encoding="utf-8") as f: |
| 55 |
config = json.load(f) |
| 56 |
except Exception as e: # pylint: disable=broad-exception-caught |
| 57 |
return e |
| 58 |
current_context = config.get("currentContext", None) |
| 59 |
if current_context and not name: |
| 60 |
del config["currentContext"] |
| 61 |
elif name: |
| 62 |
config["currentContext"] = name |
| 63 |
else: |
| 64 |
return None |
| 65 |
if not docker_cfg_path: |
| 66 |
docker_cfg_path = get_default_config_file() |
| 67 |
try: |
| 68 |
with open(docker_cfg_path, "wt", encoding="utf-8") as f: |
| 69 |
json.dump(config, f, indent=4) |
| 70 |
return None |
| 71 |
except Exception as e: # pylint: disable=broad-exception-caught |
| 72 |
return e |
| 73 |
|
| 74 |
|
| 75 |
def get_context_id(name: str) -> str: |
| 76 |
return hashlib.sha256(name.encode("utf-8")).hexdigest() |
| 77 |
|
| 78 |
|
| 79 |
def get_context_dir() -> str: |
| 80 |
docker_cfg_path = find_config_file() or get_default_config_file() |
| 81 |
return os.path.join(os.path.dirname(docker_cfg_path), "contexts") |
| 82 |
|
| 83 |
|
| 84 |
def get_meta_dir(name: str | None = None) -> str: |
| 85 |
meta_dir = os.path.join(get_context_dir(), "meta") |
| 86 |
if name: |
| 87 |
return os.path.join(meta_dir, get_context_id(name)) |
| 88 |
return meta_dir |
| 89 |
|
| 90 |
|
| 91 |
def get_meta_file(name: str) -> str: |
| 92 |
return os.path.join(get_meta_dir(name), METAFILE) |
| 93 |
|
| 94 |
|
| 95 |
def get_tls_dir(name: str | None = None, endpoint: str = "") -> str: |
| 96 |
context_dir = get_context_dir() |
| 97 |
if name: |
| 98 |
return os.path.join(context_dir, "tls", get_context_id(name), endpoint) |
| 99 |
return os.path.join(context_dir, "tls") |
| 100 |
|
| 101 |
|
| 102 |
def get_context_host(path: str | None = None, tls: bool = False) -> str: |
| 103 |
host = parse_host(path, IS_WINDOWS_PLATFORM, tls) |
| 104 |
if host == DEFAULT_UNIX_SOCKET and host.startswith("http+"): |
| 105 |
# remove http+ from default docker socket url |
| 106 |
host = host[5:] |
| 107 |
return host |