host.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. user_name = 'hpcc'
  26. user_passowrd = 'hpcc'
  27. admin_password = 'hpcc'
  28. logger = logging.getLogger("hpcc.cluster.Host")
  29. def __init__(self, ip=None):
  30. '''
  31. Constructor
  32. '''
  33. self._ip = ip
  34. self._host_name = None
  35. self._user_name = None
  36. self._user_password = None
  37. self._admin_password = None
  38. @property
  39. def ip(self):
  40. return self._ip
  41. @ip.setter
  42. def ip(self, value):
  43. self._ip = value
  44. @property
  45. def host_name(self):
  46. return self._host_name
  47. @host_name.setter
  48. def host_name(self, value):
  49. self._host_name = value
  50. @property
  51. def user_name(self):
  52. if self._user_name:
  53. return self._user_name
  54. else:
  55. return Host.user_name
  56. @user_name.setter
  57. def user_name(self, value):
  58. self._user_name = value
  59. @property
  60. def user_password(self):
  61. if self._user_password:
  62. return self._user_password
  63. else:
  64. return Host.user_password
  65. @user_password.setter
  66. def user_password(self, value):
  67. self._user_password = value
  68. @property
  69. def admin_password(self):
  70. if self._admin_password:
  71. return self._admin_password
  72. else:
  73. return Host.admin_password
  74. @admin_password.setter
  75. def admin_password(self, value):
  76. self._admin_password = value
  77. @classmethod
  78. def get_hosts_from_env(cls, env_xml="/etc/HPCCSystems/environment.xml",
  79. hpcc_home="/opt/HPCCSystems", exclude_local=False):
  80. cmd = hpcc_home + "/sbin/configgen -env " + env_xml + \
  81. " -machines | awk -F, '{print $1} ' | sort | uniq"
  82. hosts = []
  83. try:
  84. process = subprocess.Popen(cmd, shell=True,
  85. stdout=subprocess.PIPE,
  86. stderr=subprocess.PIPE)
  87. process.wait()
  88. errcode = process.returncode
  89. stdout, stderr = process.communicate()
  90. if (errcode == 0):
  91. ips = stdout
  92. for ip in ips.split():
  93. ip = ip.strip()
  94. if ip:
  95. hosts.append( Host(ip) )
  96. else:
  97. cls.logger.error(stderr)
  98. except Exception as e:
  99. cls.logger.error(e.output)
  100. if exclude_local:
  101. return Host.exclude_local_host(hosts)
  102. else:
  103. return hosts
  104. @classmethod
  105. def get_hosts_from_file(cls, file_name, exclude_local=False):
  106. hosts = []
  107. with open(file_name) as host_file:
  108. for line in host_file:
  109. ip = line.strip()
  110. if ip:
  111. hosts.append( Host(ip) )
  112. if exclude_local:
  113. return Host.exclude_local_host(hosts)
  114. else:
  115. return hosts
  116. @classmethod
  117. def exclude_local_host(cls, in_hosts):
  118. out_hosts = []
  119. try:
  120. addr_list = socket.getaddrinfo(socket.gethostname(), None)
  121. for host in in_hosts:
  122. found = False
  123. for addr in addr_list:
  124. if addr[4][0] == host.ip:
  125. found = True
  126. break
  127. if not found:
  128. out_hosts.append(host)
  129. except Exception as e:
  130. out_hosts = in_hosts
  131. #The above does not work if hostname doesn't match ip
  132. out_hosts_2 = []
  133. cmd = '/sbin/ifconfig -a | grep \"[[:space:]]*inet[[:space:]]\"'
  134. try:
  135. process = subprocess.Popen(cmd, shell=True,
  136. stdout=subprocess.PIPE,
  137. stderr=subprocess.PIPE)
  138. process.wait()
  139. errcode = process.returncode
  140. if errcode != 0:
  141. return out_hosts
  142. inet_out, stderr = process.communicate()
  143. if not inet_out:
  144. return out_hosts
  145. for host in out_hosts:
  146. m = re.search(host.ip, inet_out)
  147. if not m:
  148. out_hosts_2.append(host)
  149. except Exception as e:
  150. out_hosts_2 = out_hosts
  151. return out_hosts_2