vm.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 subprocess
  17. from config import Config
  18. class VM:
  19. def __init__(self, external_ip):
  20. self.external_ip = external_ip
  21. self.docker_list = []
  22. def add_docker(self, docker):
  23. self.docker_list.append(docker)
  24. # install Weave on this VM
  25. def __centos7_weave_install__(self):
  26. subprocess.call(["sudo", "chmod", "755", "Linux/CentOS7/weave_install.sh"])
  27. subprocess.call("./Linux/CentOS7/weave_install.sh")
  28. # launch Weave, make this VM connect with other VM
  29. def __set_weave_network__(self, VMs_external_IP_list, server_external_ip):
  30. # add other VMs and the ambari-server to set up connections
  31. command = ["sudo", "weave", "launch"]
  32. command.extend(VMs_external_IP_list)
  33. command.append(server_external_ip)
  34. subprocess.call(command)
  35. # install Docker on this VM
  36. def __centos7_docker_install__(self):
  37. subprocess.call(["sudo", "chmod", "755", "Linux/CentOS7/docker_install.sh"])
  38. subprocess.call("./Linux/CentOS7/docker_install.sh")
  39. # build docker image
  40. def __build_docker_image__(self, image_name):
  41. subprocess.call(["sudo", "docker", "build", "-t", image_name, "Docker/"])
  42. # launch Docker containers, issue the script to install, configure and launch Agent inside Docker.
  43. def __launch_containers__(self, docker_image, server_weave_ip):
  44. # print docker_ip_list
  45. for docker in self.docker_list:
  46. docker_IP = docker.IP
  47. docker_mask = docker.mask
  48. docker_hostname = docker.hostname
  49. cmd = "python /launcher_agent.py " + server_weave_ip + " " + docker_hostname + "; /bin/bash"
  50. command = ["sudo", "weave", "run", docker_IP + "/" + docker_mask, "-d", "-it", "-h", docker_hostname, \
  51. docker_image, "bash", "-c", cmd]
  52. print command
  53. subprocess.call(command)
  54. def run_docker(self, server_weave_IP, server_external_IP, cluster):
  55. config = Config()
  56. config.load()
  57. VMs_IP_list = []
  58. for vm in cluster.VM_list:
  59. VMs_IP_list.append(vm.external_ip)
  60. cluster.export_hostnames("./Docker/hosts")
  61. self.__centos7_docker_install__()
  62. self.__centos7_weave_install__()
  63. self.__build_docker_image__(config.ATTRIBUTES["Docker_image_name"])
  64. self.__set_weave_network__(VMs_IP_list, server_external_IP)
  65. self.__launch_containers__(config.ATTRIBUTES["Docker_image_name"], server_weave_IP)