util.py 5.1 KB

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