launcher_agent.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. def replace_conf(server_ip):
  19. """
  20. replace the server host IP in the Ambari-agent configuration file
  21. :param server_ip: internal Weave IP address of Ambari-server
  22. :return: None
  23. """
  24. lines = []
  25. with open("/etc/ambari-agent/conf/ambari-agent.ini") as f:
  26. lines = f.readlines()
  27. with open("/etc/ambari-agent/conf/ambari-agent.ini", "w+") as f:
  28. for line in lines:
  29. line = line.replace("hostname=localhost", "hostname=" + server_ip)
  30. f.write(line)
  31. def run_ssh():
  32. """
  33. run SSH service on this Docker Container
  34. :return: None
  35. """
  36. subprocess.call("/run_ssh.sh")
  37. def run_ambari_agent():
  38. """
  39. command line to run Ambari-agent
  40. :return: None
  41. """
  42. subprocess.call("/ambari_agent_start.sh")
  43. def set_weave_ip(weave_ip):
  44. """
  45. set the IP and hostname mapping for this Container
  46. Docker will assign an IP to each Container, and map it to hostname, which is not we want
  47. We want our Weave IP to be mapped to hostname
  48. :param weave_ip:
  49. :return: None
  50. """
  51. with open("/etc/hosts") as etc_hosts:
  52. all_resolution = etc_hosts.readlines()
  53. with open("/etc/hosts", "w") as etc_hosts:
  54. for index in range(len(all_resolution)):
  55. if index == 0:
  56. token = all_resolution[index].split()
  57. etc_hosts.write("{0} {1} {2}\n".format(weave_ip, token[1], token[2]))
  58. else:
  59. etc_hosts.write(all_resolution[index])
  60. def main():
  61. ambari_server_ip = sys.argv[1]
  62. my_weave_ip = sys.argv[2]
  63. replace_conf(ambari_server_ip)
  64. set_weave_ip(my_weave_ip)
  65. run_ambari_agent()
  66. run_ssh()
  67. if __name__ == "__main__":
  68. main()