docker.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. """
  16. from config import Config
  17. class Docker:
  18. """
  19. Docker represents a Docker container, each with its IP and domain name
  20. """
  21. def __init__(self, ip, mask, weave_domain_name):
  22. self.ip = ip
  23. self.mask = mask
  24. self.weave_domain_name = weave_domain_name
  25. def to_json(self):
  26. """
  27. create a map to hold the information of the Docker instance
  28. :return: A map, which is JSON format object.
  29. """
  30. docker_json = {}
  31. docker_json["weave_ip"] = "{0}/{1}".format(self.ip, self.mask)
  32. docker_json["weave_domain_name"] = self.weave_domain_name
  33. return docker_json
  34. @staticmethod
  35. def load_from_json(json_data):
  36. """
  37. load the docker information from a JSON object
  38. :param json_data: a map, which is a JSON object
  39. :return: a Docker object
  40. """
  41. ip = json_data["weave_ip"].split("/")[0]
  42. mask = json_data["weave_ip"].split("/")[1]
  43. weave_domain_name = json_data["weave_domain_name"]
  44. return Docker(ip, mask, weave_domain_name)
  45. def __str__(self):
  46. return str(self.ip) + "/" + str(self.mask) + " " + self.weave_domain_name
  47. @staticmethod
  48. def get_weave_domain_name(cluster_name, index):
  49. """
  50. given the index and the name of cluster, generate the Weave domain name for the docker
  51. :param cluster_name: the name of the cluster
  52. :param index: a number
  53. :return: Weave domain name of the docker container
  54. """
  55. return "{0}-{1}-{2}.{3}".format(Config.ATTRIBUTES["container_hostname_fix"],
  56. index, cluster_name, "weave.local")
  57. @staticmethod
  58. def get_pattern_presentation(cluster_name, range_str):
  59. return Docker.get_weave_domain_name(cluster_name, range_str)
  60. def get_index(self):
  61. """
  62. extract the index of the docker within the cluster
  63. :return: the index
  64. """
  65. return self.weave_domain_name.split("-")[1]
  66. def get_container_name(self):
  67. """
  68. :return: the name of the container
  69. """
  70. return self.get_hostname()
  71. def get_hostname(self):
  72. return self.weave_domain_name.split(".")[0]