util.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 argparse
  16. import platform
  17. import logging
  18. import os
  19. import subprocess
  20. from ..common.error import Error
  21. from ..common.shell import Shell
  22. def isPositiveIntNum(string):
  23. for i in range(0, len(string)):
  24. if (string[i] < '0') or (string[i] > '9'):
  25. return False
  26. return True
  27. def checkPqParam(string):
  28. param = str(string)
  29. if isPositiveIntNum(string) or (string == '-1'):
  30. value = int(string)
  31. else:
  32. msg = "Wrong value of threadNumber parameter: '"+string+"' !"
  33. raise argparse.ArgumentTypeError(msg)
  34. return value
  35. def checkXParam(string):
  36. param=str(string)
  37. if len(param):
  38. if ('=' in param) or ('None' == param):
  39. value = param
  40. else:
  41. #logging.error("%s. Missing or wrong argument '%s' after -X parameter!\nIt should be 'name=val[,name2=val2..]'\n5000\n" % (1, param))
  42. value="5000"
  43. else:
  44. msg = "Missing argument of -X parameter!"
  45. raise argparse.ArgumentTypeError(msg)
  46. return value
  47. def getVersionNumbers():
  48. version = platform.python_version_tuple()
  49. verNum = {'main':0, 'minor':0, 'patch':0}
  50. if isPositiveIntNum(version[0]):
  51. verNum['main'] = int(version[0])
  52. if isPositiveIntNum(version[1]):
  53. verNum['minor'] = int(version[1])
  54. if isPositiveIntNum(version[2]):
  55. verNum['patch'] = int(version[2])
  56. return(verNum);
  57. def parentPath(osPath):
  58. # remove current dir
  59. [osPath, sep, curDir] = osPath.rpartition(os.sep)
  60. return osPath
  61. def convertPath(osPath):
  62. hpccPath = ''
  63. osPath = osPath.lstrip(os.sep)
  64. osPath = osPath.replace(os.sep, '::')
  65. for i in range(0, len(osPath)):
  66. if osPath[i] >= 'A' and osPath[i] <= 'Z':
  67. hpccPath = hpccPath +'^'
  68. hpccPath = hpccPath +osPath[i]
  69. return hpccPath
  70. import json
  71. import urllib2
  72. gConfig = None
  73. def setConfig(config):
  74. global gConfig
  75. gConfig = config
  76. def getConfig():
  77. return gConfig
  78. def queryWuid(jobname, taskId):
  79. shell = Shell()
  80. cmd = shell.which('ecl')
  81. defaults = []
  82. args = []
  83. args.append('status')
  84. args.append('-v')
  85. args.append('-n=' + jobname)
  86. args.append('--server=' + gConfig.ip)
  87. args.append('--username=' + gConfig.username)
  88. args.append('--password=' + gConfig.password)
  89. res = shell.command(cmd, *defaults)(*args)
  90. logging.debug("%3d. queryWuid(%s, cmd :'%s') result is: '%s'", taskId, jobname, cmd, res)
  91. wuid = "Not found"
  92. state = 'N/A'
  93. result = 'Fail'
  94. if len(res):
  95. resultItems = res.split(',')
  96. if len(resultItems) == 3:
  97. result = 'OK'
  98. for resultItem in resultItems:
  99. resultItem = resultItem.strip()
  100. [key, val] = resultItem.split(':')
  101. if key == 'ID':
  102. wuid = val
  103. if key == 'state':
  104. state = val
  105. return {'wuid':wuid, 'state':state, 'result':result}
  106. def abortWorkunit(wuid):
  107. shell = Shell()
  108. cmd = shell.which('ecl')
  109. defaults=[]
  110. args = []
  111. args.append('abort')
  112. args.append('-wu=' + wuid)
  113. args.append('--server=' + gConfig.ip)
  114. args.append('--username=' + gConfig.username)
  115. args.append('--password=' + gConfig.password)
  116. state=shell.command(cmd, *defaults)(*args)
  117. return state
  118. import subprocess
  119. def getRealIPAddress():
  120. ipAddress = '127.0.0.1'
  121. try:
  122. result = subprocess.Popen("ifconfig", shell=False, bufsize=8192, stdout=subprocess.PIPE).stdout.read()
  123. ethernetFound=False
  124. results = result.split('\n')
  125. for line in results:
  126. if 'Ethernet' in line:
  127. ethernetFound=True
  128. if ethernetFound and 'inet addr' in line:
  129. items = line.split()
  130. ipAddress = items[1].split(':')[1]
  131. break;
  132. except OSError:
  133. pass
  134. finally:
  135. pass
  136. return ipAddress
  137. def checkClusters(clusters, targetSet):
  138. targetClusters =[]
  139. if 'all' in clusters:
  140. for cluster in gConfig.Clusters:
  141. targetClusters.append(str(cluster))
  142. else:
  143. for cluster in clusters:
  144. cluster = cluster.strip()
  145. if cluster in gConfig.Clusters:
  146. targetClusters.append(cluster)
  147. else:
  148. logging.error("%s. Unknown cluster:'%s' in %s:'%s'!" % (1, cluster, targetSet, clusters))
  149. raise Error("4000")
  150. return targetClusters
  151. def checkHpccStatus():
  152. status='OK'
  153. try:
  154. myProc = subprocess.Popen(["ecl --version"], shell=True, bufsize=8192, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  155. result = myProc.stdout.read() + myProc.stderr.read()
  156. results = result.split('\n')
  157. for line in results:
  158. if 'not found' in line:
  159. err = Error("6000")
  160. logging.error("%s. %s:'%s'" % (1, err, line))
  161. raise err
  162. break
  163. myProc = subprocess.Popen("ecl getname --wuid 'W*'", shell=True, bufsize=8192, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  164. result = myProc.stdout.read() + myProc.stderr.read()
  165. results = result.split('\n')
  166. for line in results:
  167. if "Error connecting" in line:
  168. err = Error("6001")
  169. logging.error("%s. %s:'%s'" % (1, err, line))
  170. raise (err)
  171. break
  172. if "command not found" in line:
  173. err = Error("6002")
  174. logging.error("%s. %s:'%s'" % (1, err, line))
  175. raise (err)
  176. break
  177. except OSError:
  178. err = Error("6002")
  179. logging.error("%s. checkHpccStatus error:%s!" % (1, err))
  180. raise Error(err)
  181. except ValueError:
  182. err = Error("6003")
  183. logging.error("%s. checkHpccStatus error:%s!" % (1, err))
  184. raise Error(err)
  185. finally:
  186. pass