launcher_agent.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 sys
  17. import subprocess
  18. import time
  19. def replace_conf(server_ip):
  20. f = open("/etc/ambari-agent/conf/ambari-agent.ini")
  21. lines = f.readlines()
  22. f.close()
  23. f = open("/etc/ambari-agent/conf/ambari-agent.ini", "w+")
  24. for line in lines:
  25. line = line.replace("hostname=localhost", "hostname=" + server_ip)
  26. f.write(line)
  27. f.close()
  28. def run_ambari_agent():
  29. # command = ["sudo", "ambari-agent", "start"]
  30. # subprocess.call(command)
  31. subprocess.call("./ambari_agent_start.sh")
  32. # add all the hostnames of other containers to /etc/hosts
  33. def add_hostnames():
  34. etc_hosts = open("/etc/hosts", "a")
  35. etc_hosts.write("\n")
  36. docker_hosts = open("/hosts")
  37. for line in docker_hosts.readlines():
  38. etc_hosts.write(line)
  39. docker_hosts.close()
  40. etc_hosts.close()
  41. def remove_default_hostname(hostname):
  42. etc_hosts = open("/etc/hosts")
  43. all_resolution = etc_hosts.readlines()
  44. etc_hosts.close()
  45. etc_hosts = open("/etc/hosts", "w")
  46. for line in all_resolution:
  47. if hostname not in line:
  48. etc_hosts.write(line)
  49. else:
  50. etc_hosts.write("#")
  51. etc_hosts.write(line)
  52. etc_hosts.close()
  53. ambari_server_ip = sys.argv[1]
  54. my_hostname = sys.argv[2]
  55. replace_conf(ambari_server_ip)
  56. remove_default_hostname(my_hostname)
  57. add_hostnames()
  58. run_ambari_agent()