launcher_cluster.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 time
  17. import sys
  18. from cluster import Cluster
  19. from config import Config
  20. from data import Data
  21. import subprocess
  22. def request_cluster(argv):
  23. """
  24. only request cluster on GCE, and output all configuration information
  25. :param argv: sys.argv
  26. :return: None
  27. """
  28. if len(argv) < 7:
  29. print_help()
  30. exit(1)
  31. cluster_name = argv[2]
  32. ambari_agent_vm_num = int(argv[3])
  33. docker_num = int(argv[4])
  34. service_server_num = int(argv[5])
  35. with_ambari_server = False
  36. ambari_server_num = int(argv[6])
  37. if ambari_server_num > 0:
  38. with_ambari_server = True
  39. cluster = Cluster()
  40. cluster.request_gce_cluster(ambari_agent_vm_num, docker_num, service_server_num,
  41. with_ambari_server, cluster_name)
  42. time_to_wait = Config.ATTRIBUTES["gce_boot_time"]
  43. print "wait ", str(time_to_wait), " seconds for the cluster to boot ... ..."
  44. time.sleep(int(time_to_wait))
  45. data = Data()
  46. data.add_new_cluster(cluster)
  47. print "complete"
  48. def up_cluster(argv):
  49. """
  50. run all Ambari-agents in Docker container and VMs,
  51. run Ambari-server if there is according to the configuration file
  52. :param argv: sys.argv
  53. :return: None
  54. """
  55. if len(argv) < 3:
  56. print_help()
  57. exit(1)
  58. cluster_name = argv[2]
  59. cluster = Cluster.load_from_json(cluster_name)
  60. if cluster is None:
  61. print cluster_name, " cluster not found"
  62. exit(1)
  63. if cluster.state != Cluster.STATE_FREE:
  64. print cluster_name, " cluster is already running"
  65. exit(1)
  66. ambari_server = cluster.get_ambari_server_vm()
  67. if ambari_server is None:
  68. print "Unable to run cluster", cluster_name,\
  69. " no Ambari-server in this cluster, you can only merge this cluster into another one"
  70. exit(1)
  71. print "Configuring cluster"
  72. print "Check output folder: ", Config.ATTRIBUTES["output_folder"]
  73. cluster.run_cluster(ambari_server.weave_internal_ip, ambari_server.external_ip)
  74. data = Data()
  75. data.set_cluster_state(cluster_name, Cluster.STATE_RUNNING)
  76. # reset terminal. The SSH subprocess call of the program cause the terminal display to be abnormal.
  77. # This is an unsolved minor issue.
  78. subprocess.call(["reset"])
  79. print "Complete"
  80. def merge_cluster(argv):
  81. """
  82. Merge the cluster to another running cluster
  83. :param argv: sys.argv
  84. :return: None
  85. """
  86. if len(argv) < 4:
  87. print_help()
  88. exit(1)
  89. merged_cluster_name = argv[2]
  90. merged_cluster = Cluster.load_from_json(merged_cluster_name)
  91. if merged_cluster is None:
  92. print merged_cluster_name, " cluster not found"
  93. exit(1)
  94. if merged_cluster.state != Cluster.STATE_FREE:
  95. print merged_cluster_name, " cluster is already running"
  96. exit(1)
  97. weave_ip = ""
  98. external_ip = ""
  99. extended_cluster_name = ""
  100. if len(argv) == 4:
  101. extended_cluster_name = argv[3]
  102. extended_cluster = Cluster.load_from_json(extended_cluster_name)
  103. if extended_cluster is None:
  104. print extended_cluster_name, " cluster not found"
  105. exit(1)
  106. if extended_cluster.state != Cluster.STATE_RUNNING:
  107. if extended_cluster.state == Cluster.STATE_FREE:
  108. print extended_cluster_name, " cluster is not running, can't be extended"
  109. elif extended_cluster.state.startswith(Cluster.STATE_MERGE):
  110. print extended_cluster_name, " cluster is merged to another cluster, can't be extended"
  111. exit(1)
  112. ambari_server = extended_cluster.get_ambari_server_vm()
  113. weave_ip = ambari_server.weave_internal_ip
  114. external_ip = ambari_server.external_ip
  115. elif len(argv) == 5:
  116. weave_ip = argv[3]
  117. external_ip = argv[4]
  118. else:
  119. print_help()
  120. exit(1)
  121. if merged_cluster.get_ambari_server_vm() is not None:
  122. print merged_cluster, " cluster has one VM to install Ambari-server, which will NOT be merged"
  123. print "Configuring cluster"
  124. print "Check output folder: ", Config.ATTRIBUTES["output_folder"]
  125. merged_cluster.run_cluster(weave_ip, external_ip)
  126. data = Data()
  127. data.set_cluster_state(merged_cluster_name, "{0} to {1}".format(Cluster.STATE_MERGE, extended_cluster_name))
  128. # reset terminal. The SSH subprocess call of the program cause the terminal display to be abnormal.
  129. # This is an unsolved minor issue.
  130. subprocess.call(["reset"])
  131. print "Complete"
  132. def list_cluster():
  133. """
  134. list the cluster creation history
  135. :return: None
  136. """
  137. data = Data()
  138. data.print_cluster_summary_list()
  139. def show_cluster(argv):
  140. """
  141. show detail information about a cluster
  142. :param argv: sys.argv
  143. :return: None
  144. """
  145. if len(argv) < 3:
  146. print_help()
  147. exit(1)
  148. cluster_name = argv[2]
  149. cluster = Cluster.load_from_json(cluster_name)
  150. if cluster is None:
  151. print cluster_name, " cluster not found"
  152. exit(1)
  153. cluster.print_description()
  154. def print_help():
  155. """
  156. print help information
  157. :return: None
  158. """
  159. print "usage:"
  160. print
  161. print "request", " ", "--request a cluster from GCE, generate the configuration for the cluster"
  162. print "\t\t", "<the name of the cluster>"
  163. print "\t\t", "<number of VMs>"
  164. print "\t\t", "<number of dockers each VM>"
  165. print "\t\t", "<number of service servers>, directly install Ambari-Agent, not inside Dockers"
  166. print "\t\t", "<number of ambari-server>, either 0 or 1"
  167. print
  168. print "up", " ", "--run all Ambari-agents and Ambari-server of the cluster"
  169. print "\t\t", "<the name of the cluster>"
  170. print
  171. print "merge", " ", "--run one cluster, and add to another cluster"
  172. print "\t\t", "<the name of the cluster to be merged>"
  173. print "\t\t", "<the name of the cluster to be extended>"
  174. print
  175. print "merge", " ", "--run one cluster, and add to another cluster"
  176. print "\t\t", "<the name of the cluster to be merged>"
  177. print "\t\t", "<Weave IP of the Ambari-server>"
  178. print "\t\t", "<External IP of the Ambari-server>"
  179. print
  180. print "list", " ", "--list all the cluster"
  181. print
  182. print "show", " ", "--show cluster information"
  183. print "\t\t", "<the name of the cluster>"
  184. print
  185. print "help", " ", "help info"
  186. print
  187. def main(argv):
  188. # the first argument is the python file name
  189. if len(argv) < 2:
  190. print_help()
  191. exit(1)
  192. command = argv[1]
  193. if command == "request":
  194. request_cluster(argv)
  195. elif command == "up":
  196. up_cluster(argv)
  197. elif command == "merge":
  198. merge_cluster(argv)
  199. elif command == "list":
  200. list_cluster()
  201. elif command == "show":
  202. show_cluster(argv)
  203. elif command == "help":
  204. print_help()
  205. else:
  206. print_help()
  207. if __name__ == "__main__":
  208. Config.load()
  209. main(sys.argv)