data.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. import os.path
  17. import json
  18. from config import Config
  19. class Data:
  20. def __init__(self):
  21. self.data_filename = Config.ATTRIBUTES["cluster_info_file"]
  22. def _load_data(self):
  23. """
  24. load all data from JSON file
  25. :return: a map, which is a JSON format object
  26. """
  27. json_data = {"clusters": []}
  28. if os.path.isfile(self.data_filename):
  29. with open(self.data_filename) as f:
  30. json_data = json.load(f)
  31. return json_data
  32. def _save_data(self, json_data):
  33. """
  34. save the JSON object into a file
  35. :param json_data: a map, which is a JSON format object
  36. :return: None
  37. """
  38. with open(self.data_filename, "w") as f:
  39. json.dump(json_data, f, indent=4, separators=(',', ': '))
  40. def add_new_cluster(self, cluster):
  41. """
  42. add a new cluster into the JSON file
  43. :param cluster: the cluster instance
  44. :return: None
  45. """
  46. json_data = self._load_data()
  47. new_cluster_json = cluster.to_json()
  48. json_data["clusters"].insert(0, new_cluster_json)
  49. self._save_data(json_data)
  50. def set_cluster_state(self, cluster_name, state):
  51. """
  52. set the state of a cluster into JSON file
  53. :param cluster_name: the name of the cluster
  54. :param state: the name of the state
  55. :return: None
  56. """
  57. json_data = self._load_data()
  58. for cluster in json_data["clusters"]:
  59. if cluster["cluster_name"] == cluster_name:
  60. cluster["state"] = state
  61. break
  62. self._save_data(json_data)
  63. def read_cluster_json(self, cluster_name):
  64. """
  65. get the JSON object for the cluster
  66. :param cluster_name: the name of cluster
  67. :return: a map which is a JSON object or None if the cluster is not found
  68. """
  69. json_data = self._load_data()
  70. for cluster_json in json_data["clusters"]:
  71. if cluster_json["cluster_name"] == cluster_name:
  72. return cluster_json
  73. return None
  74. def print_cluster_summary_list(self):
  75. """
  76. get a brief description of all the cluster from the JSON file
  77. :return: a list of tuple. The elements of the tuple are:
  78. cluster_name, state, agent_number,
  79. service_server_num, ambari_server_num, create_time
  80. """
  81. print "(cluster_name, state, agent_number, service_server_num, ambari_server_num, create_time)"
  82. json_data = self._load_data()
  83. for cluster in json_data["clusters"]:
  84. cluster_name = cluster["cluster_name"]
  85. state = cluster["state"]
  86. create_time = cluster["create_time"]
  87. agent_number = 0
  88. for agent_vm in cluster["ambari_agent_vm_list"]:
  89. agent_number += len(agent_vm["docker_list"])
  90. service_server_num = len(cluster["service_server_vm_list"])
  91. ambari_server_num = len(cluster["ambari_server_vm"])
  92. print cluster_name, state, agent_number, service_server_num, ambari_server_num, create_time