ActualConfigHandler.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import json
  18. import logging
  19. import os
  20. logger = logging.getLogger()
  21. class ActualConfigHandler:
  22. CONFIG_NAME = 'config.json'
  23. def __init__(self, config, configTags):
  24. self.config = config
  25. self.configTags = configTags
  26. def findRunDir(self):
  27. runDir = '/var/run/ambari-agent'
  28. if self.config.has_option('agent', 'prefix'):
  29. runDir = self.config.get('agent', 'prefix')
  30. if not os.path.exists(runDir):
  31. runDir = '/tmp'
  32. return runDir
  33. def write_actual(self, tags):
  34. self.write_file(self.CONFIG_NAME, tags)
  35. def write_actual_component(self, component, tags):
  36. self.configTags[component] = tags
  37. filename = component + "_" + self.CONFIG_NAME
  38. self.write_file(filename, tags)
  39. def write_client_components(self, serviceName, tags, components):
  40. from LiveStatus import LiveStatus
  41. for comp in LiveStatus.CLIENT_COMPONENTS:
  42. if comp['serviceName'] == serviceName:
  43. componentName = comp['componentName']
  44. if componentName in self.configTags and \
  45. tags != self.configTags[componentName] and \
  46. (components == ["*"] or componentName in components):
  47. self.write_actual_component(componentName, tags)
  48. pass
  49. def write_file(self, filename, tags):
  50. runDir = self.findRunDir()
  51. conf_file = open(os.path.join(runDir, filename), 'w')
  52. json.dump(tags, conf_file)
  53. conf_file.close()
  54. def read_file(self, filename):
  55. runDir = self.findRunDir()
  56. fullname = os.path.join(runDir, filename)
  57. if os.path.isfile(fullname):
  58. res = None
  59. conf_file = open(os.path.join(runDir, filename), 'r')
  60. try:
  61. res = json.load(conf_file)
  62. if (0 == len(res)):
  63. res = None
  64. except Exception, e:
  65. logger.error("Error parsing " + filename + ": " + repr(e))
  66. res = None
  67. pass
  68. conf_file.close()
  69. return res
  70. return None
  71. def read_actual(self):
  72. return self.read_file(self.CONFIG_NAME)
  73. def read_actual_component(self, componentName):
  74. if componentName not in self.configTags.keys():
  75. self.configTags[componentName] = \
  76. self.read_file(componentName + "_" + self.CONFIG_NAME)
  77. return self.configTags[componentName]
  78. def update_component_tag(self, componentName, tag, version):
  79. self.read_actual_component(componentName)
  80. self.configTags[componentName][tag] = version
  81. filename = componentName + "_" + self.CONFIG_NAME
  82. self.write_file(filename, self.configTags[componentName])