config.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. """
  16. import ConfigParser
  17. import os
  18. class Config:
  19. ATTRIBUTES = {}
  20. AMBARI_AGENT_VM = "ambari_agent_vm"
  21. AMBARI_SERVER_VM = "ambari_server_vm"
  22. SERVICE_SERVER_VM = "service_server_vm"
  23. RELATIVE_CONFIG_FILE_PATH = "config/config.ini"
  24. def __init__(self):
  25. pass
  26. @staticmethod
  27. def load():
  28. """
  29. load configuration from file, add all configuration to the map ATTRIBUTES
  30. :return: None
  31. """
  32. config = ConfigParser.RawConfigParser()
  33. # keep file case sensitive
  34. config.optionxform = str
  35. config.read(Config.RELATIVE_CONFIG_FILE_PATH)
  36. for section in config.sections():
  37. for key in config.options(section):
  38. Config.ATTRIBUTES[key] = config.get(section, key)
  39. # set output file path
  40. for key in config.options("output"):
  41. if key == "output_folder":
  42. # create the folder
  43. if not os.path.exists(Config.ATTRIBUTES["output_folder"]):
  44. os.makedirs(Config.ATTRIBUTES["output_folder"])
  45. else:
  46. Config.ATTRIBUTES[key] = Config.ATTRIBUTES["output_folder"] + "/" + Config.ATTRIBUTES[key]
  47. @staticmethod
  48. def update(section, key, value):
  49. """
  50. Update the key value of the configuration file
  51. :param section: section is inside []
  52. :param key: the key
  53. :param value: the value
  54. :return: None
  55. """
  56. config = ConfigParser.RawConfigParser()
  57. config.read(Config.RELATIVE_CONFIG_FILE_PATH)
  58. config.set(section, key, value)
  59. with open(Config.RELATIVE_CONFIG_FILE_PATH, 'wb') as configfile:
  60. config.write(configfile)