parse_status.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import sys
  2. import os.path
  3. import yaml
  4. import getopt
  5. import re
  6. #from subprocess import call
  7. def Usage():
  8. print("\nUsage parse_status.py [options]\n")
  9. print(" -?, --help print help")
  10. print(" -f, --file Juju status file name. This is optional")
  11. print(" -q, --query Query information from juju status. Multiple")
  12. print(" query can be requested and seperated by comma")
  13. print(" -s, --service Charm service name. In chance deployed multiple")
  14. print(" service names for difference HPCC cluster this")
  15. print(" option should be provided, otherwise first service")
  16. print(" name will be used.")
  17. print("\n")
  18. def LoadYamlObject():
  19. global config
  20. global status_file_name
  21. stream = file(status_file_name, "r")
  22. config = yaml.load(stream)
  23. def GetServiceName():
  24. global service_name
  25. global charm_name
  26. if (service_name != "" ):
  27. return
  28. pattern = re.compile(".*\/"+charm_name+"-\d+")
  29. for key in config["services"].keys():
  30. if (pattern.match(config["services"][key]["charm"])):
  31. service_name = key
  32. print("service_name=" + key)
  33. break
  34. def GetNumOfUnites():
  35. global service_name
  36. if (service_name == ""):
  37. GetServiceName()
  38. if (service_name == ""):
  39. return
  40. number_of_units = len(config["services"][service_name]["units"].keys())
  41. print("unit_number=" + str(number_of_units))
  42. def ParseStatus():
  43. global query_actions
  44. global query_all
  45. for action in query_all:
  46. if (action == "service"):
  47. GetServiceName()
  48. elif (action == "num_of_units"):
  49. GetNumOfUnites()
  50. if __name__ == "__main__":
  51. charm_name= os.path.basename(
  52. os.path.dirname(
  53. os.path.dirname(
  54. os.path.realpath(__file__))))
  55. config = None
  56. query_all = [
  57. "service",
  58. "num_of_units",
  59. ]
  60. query_actions = ""
  61. query_list = []
  62. service_name = ""
  63. status_file_name = ""
  64. try:
  65. opts, args = getopt.getopt(sys.argv[1:],":q:f:s:",
  66. ["help", "query", "file", "service"])
  67. except getopt.GetoptError as err:
  68. print(str(err))
  69. Usage()
  70. exit()
  71. for arg, value in opts:
  72. if arg in ("-?", "--help"):
  73. Usage()
  74. exit(0)
  75. elif arg in ("-q", "--query"):
  76. query_actions = value
  77. elif arg in ("-s", "--service"):
  78. service_name = value
  79. elif arg in ("-f", "--file"):
  80. status_file_name = value
  81. else:
  82. print("\nUnknown option: " + arg)
  83. Usage()
  84. exit(0)
  85. if ( query_actions != "" ):
  86. query_list = query_actions.split(',')
  87. else:
  88. query_list = query_all
  89. if ( status_file_name == "" ):
  90. status_file_name = "/tmp/juju_status.txt"
  91. os.system("juju status > " + status_file_name)
  92. LoadYamlObject()
  93. ParseStatus()