host.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. '''
  2. /*#############################################################################
  3. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. ############################################################################ */
  14. '''
  15. import subprocess
  16. import socket
  17. import re
  18. import logging
  19. class Host(object):
  20. '''
  21. This class represent cluster host attributions. Currently only ip is required.
  22. Several static help methods are provided to create cluster hast list.
  23. '''
  24. # Default user name and passwords
  25. logger = logging.getLogger("hpcc.cluster.Host")
  26. def __init__(self, ip=None):
  27. '''
  28. Constructor
  29. '''
  30. self._ip = ip
  31. self._host_name = None
  32. self._user_name = None
  33. self._user_password = None
  34. self._admin_password = None
  35. @property
  36. def ip(self):
  37. return self._ip
  38. @ip.setter
  39. def ip(self, value):
  40. self._ip = value
  41. @property
  42. def host_name(self):
  43. return self._host_name
  44. @host_name.setter
  45. def host_name(self, value):
  46. self._host_name = value
  47. @property
  48. def user_name(self):
  49. return self._user_name
  50. @user_name.setter
  51. def user_name(self, value):
  52. self._user_name = value
  53. @property
  54. def user_password(self):
  55. return self._user_password
  56. @user_password.setter
  57. def user_password(self, value):
  58. self._user_password = value
  59. @property
  60. def admin_password(self):
  61. return self._admin_password
  62. @admin_password.setter
  63. def admin_password(self, value):
  64. self._admin_password = value
  65. @classmethod
  66. def get_hosts_from_env(cls, env_xml="/etc/HPCCSystems/environment.xml",
  67. hpcc_home="/opt/HPCCSystems", exclude_local=False):
  68. cmd = hpcc_home + "/sbin/configgen -env " + env_xml + \
  69. " -machines | awk -F, '{print $1} ' | sort | uniq"
  70. hosts = []
  71. try:
  72. process = subprocess.Popen(cmd, shell=True,
  73. stdout=subprocess.PIPE,
  74. stderr=subprocess.PIPE)
  75. process.wait()
  76. errcode = process.returncode
  77. stdout, stderr = process.communicate()
  78. if (errcode == 0):
  79. ips = stdout
  80. for ip in ips.split():
  81. ip = ip.strip()
  82. if ip:
  83. hosts.append( Host(ip) )
  84. else:
  85. cls.logger.error(stderr)
  86. except Exception as e:
  87. cls.logger.error(e.output)
  88. if exclude_local:
  89. return Host.exclude_local_host(hosts)
  90. else:
  91. return hosts
  92. @classmethod
  93. def get_hosts_from_file(cls, file_name, exclude_local=False):
  94. hosts = []
  95. with open(file_name) as host_file:
  96. for line in host_file:
  97. ip = line.strip()
  98. if ip:
  99. hosts.append( Host(ip) )
  100. if exclude_local:
  101. return Host.exclude_local_host(hosts)
  102. else:
  103. return hosts
  104. @classmethod
  105. def exclude_local_host(cls, in_hosts):
  106. out_hosts = []
  107. try:
  108. addr_list = socket.getaddrinfo(socket.gethostname(), None)
  109. for host in in_hosts:
  110. found = False
  111. for addr in addr_list:
  112. if addr[4][0] == host.ip:
  113. found = True
  114. break
  115. if not found:
  116. out_hosts.append(host)
  117. except Exception as e:
  118. out_hosts = in_hosts
  119. #The above does not work if hostname doesn't match ip
  120. out_hosts_2 = []
  121. cmd = '/sbin/ifconfig -a | grep \"[[:space:]]*inet[[:space:]]\"'
  122. try:
  123. process = subprocess.Popen(cmd, shell=True,
  124. stdout=subprocess.PIPE,
  125. stderr=subprocess.PIPE)
  126. process.wait()
  127. errcode = process.returncode
  128. if errcode != 0:
  129. return out_hosts
  130. inet_out, stderr = process.communicate()
  131. if not inet_out:
  132. return out_hosts
  133. for host in out_hosts:
  134. m = re.search(host.ip, inet_out)
  135. if not m:
  136. out_hosts_2.append(host)
  137. except Exception as e:
  138. out_hosts_2 = out_hosts
  139. return out_hosts_2