xml_config.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. Ambari Agent
  17. """
  18. import time
  19. from resource_management import *
  20. class XmlConfigProvider(Provider):
  21. def action_create(self):
  22. filename = self.resource.filename
  23. conf_dir = self.resource.conf_dir
  24. # |e - for html-like escaping of <,>,',"
  25. config_content = InlineTemplate('''<!--{{time.asctime(time.localtime())}}-->
  26. <configuration>
  27. {% for key, value in configurations_dict.items() %}
  28. <property>
  29. <name>{{ key|e }}</name>
  30. <value>{{ value|e }}</value>
  31. {%- if not configuration_attrs is none -%}
  32. {%- for attrib_name, attrib_occurances in configuration_attrs.items() -%}
  33. {%- for property_name, attrib_value in attrib_occurances.items() -%}
  34. {% if property_name == key and attrib_name %}
  35. <{{attrib_name|e}}>{{attrib_value|e}}</{{attrib_name|e}}>
  36. {%- endif -%}
  37. {%- endfor -%}
  38. {%- endfor -%}
  39. {%- endif %}
  40. </property>
  41. {% endfor %}
  42. </configuration>''', extra_imports=[time], configurations_dict=self.resource.configurations,
  43. configuration_attrs=self.resource.configuration_attributes)
  44. Logger.info(format("Generating config: {conf_dir}/{filename}"))
  45. with Environment.get_instance_copy() as env:
  46. File (format("{conf_dir}/{filename}"),
  47. content = config_content,
  48. owner = self.resource.owner,
  49. group = self.resource.group,
  50. mode = self.resource.mode
  51. )