Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import os
  2. import json
  3. import hashlib
  4. from docker import utils
  5. from docker.constants import IS_WINDOWS_PLATFORM
  6. from docker.constants import DEFAULT_UNIX_SOCKET
  7. from docker.utils.config import find_config_file
  8. METAFILE = "meta.json"
  9. def get_current_context_name():
  10. name = "default"
  11. docker_cfg_path = find_config_file()
  12. if docker_cfg_path:
  13. try:
  14. with open(docker_cfg_path) as f:
  15. name = json.load(f).get("currentContext", "default")
  16. except Exception:
  17. return "default"
  18. return name
  19. def write_context_name_to_docker_config(name=None):
  20. if name == 'default':
  21. name = None
  22. docker_cfg_path = find_config_file()
  23. config = {}
  24. if docker_cfg_path:
  25. try:
  26. with open(docker_cfg_path) as f:
  27. config = json.load(f)
  28. except Exception as e:
  29. return e
  30. current_context = config.get("currentContext", None)
  31. if current_context and not name:
  32. del config["currentContext"]
  33. elif name:
  34. config["currentContext"] = name
  35. else:
  36. return
  37. try:
  38. with open(docker_cfg_path, "w") as f:
  39. json.dump(config, f, indent=4)
  40. except Exception as e:
  41. return e
  42. def get_context_id(name):
  43. return hashlib.sha256(name.encode('utf-8')).hexdigest()
  44. def get_context_dir():
  45. return os.path.join(os.path.dirname(find_config_file() or ""), "contexts")
  46. def get_meta_dir(name=None):
  47. meta_dir = os.path.join(get_context_dir(), "meta")
  48. if name:
  49. return os.path.join(meta_dir, get_context_id(name))
  50. return meta_dir
  51. def get_meta_file(name):
  52. return os.path.join(get_meta_dir(name), METAFILE)
  53. def get_tls_dir(name=None, endpoint=""):
  54. context_dir = get_context_dir()
  55. if name:
  56. return os.path.join(context_dir, "tls", get_context_id(name), endpoint)
  57. return os.path.join(context_dir, "tls")
  58. def get_context_host(path=None, tls=False):
  59. host = utils.parse_host(path, IS_WINDOWS_PLATFORM, tls)
  60. if host == DEFAULT_UNIX_SOCKET:
  61. # remove http+ from default docker socket url
  62. return host.strip("http+")
  63. return host