upgrade_agent_configs.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/ambari-python-wrap
  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 os
  18. import ConfigParser
  19. PROPERTIES_TO_REWRITE = [
  20. ('heartbeat', 'dirs')
  21. ]
  22. SECTIONS_TO_REMOVE = [
  23. 'stack', 'puppet', 'command', 'python'
  24. ]
  25. CONFIG_FILE_BACKUP = '/etc/ambari-agent/conf/ambari-agent.ini.old'
  26. CONFIG_FILE = '/etc/ambari-agent/conf/ambari-agent.ini'
  27. if os.path.isfile(CONFIG_FILE_BACKUP):
  28. if os.path.isfile(CONFIG_FILE):
  29. print "Upgrading configs in {0}".format(CONFIG_FILE)
  30. print "Values will be updated from {0} except the following list: {1}, {2}".format(CONFIG_FILE_BACKUP, PROPERTIES_TO_REWRITE, SECTIONS_TO_REMOVE)
  31. agent_config_backup = ConfigParser.ConfigParser()
  32. agent_config_backup.read(CONFIG_FILE_BACKUP)
  33. agent_config = ConfigParser.ConfigParser()
  34. agent_config.read(CONFIG_FILE)
  35. for section in agent_config_backup.sections():
  36. for (property_name, property_val) in agent_config_backup.items(section):
  37. if section not in SECTIONS_TO_REMOVE and (section, property_name) not in PROPERTIES_TO_REWRITE:
  38. try:
  39. agent_config.set(section, property_name, property_val)
  40. except ConfigParser.NoSectionError:
  41. agent_config.add_section(section)
  42. agent_config.set(section, property_name, property_val)
  43. with (open(CONFIG_FILE, "wb")) as new_agent_config:
  44. agent_config.write(new_agent_config)
  45. else:
  46. print "Values are not updated, configs {0} is not found".format(CONFIG_FILE)
  47. else:
  48. print "Values are not updated, backup {0} is not found".format(CONFIG_FILE_BACKUP)