#!/usr/bin/python2.7 -E # # Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. # ''' Retrieve the default manifest for a service. ''' import cgi import logging from lxml import etree import os.path import simplejson import sys from solaris_install import SCRIPT_LOGGER_NAME from solaris_install.ai.server import Server DEFAULT_DEFAULT_MANIFEST = "/usr/share/auto_install/manifest/default.xml" def element_to_dict(element): '''Convert element to dictionary''' my_dict = dict() #for element in tree.getroot().iter(tag=etree.Element): for sub_element in element.iterchildren(tag=etree.Element): sub_dict = element_to_dict(sub_element) value = sub_dict[sub_element.tag] try: # add to existing list for this tag my_dict[sub_element.tag].append(value) except AttributeError: # turn existing entry into a list my_dict[sub_element.tag] = [my_dict[sub_element.tag], value] except KeyError: # add a new non-list entry my_dict[sub_element.tag] = value # Add attributes for key, value in element.items(): my_dict['@' + key] = value # Add text sub-element if element.text is not None: my_dict['#text'] = element.text return {element.tag: my_dict} def get_default_manifest(this_service): '''Retrieve the default manifest for this service''' manifest_file = None service = \ Server.get_instance().get_services(filter_by_name=this_service)[0] manifest_file = os.path.join(service.image_path, 'auto_install/default.xml') return manifest_file def main(): '''Main defaultManifest method''' # - locate correct default.xml file depending on the AI service # - convert XML file to element tree with etree # - convert element tree to dictionary (elements are not JSON serializable) # - convert dictionary to JSON with simplejson # - write output # process CGI params this_service = None try: form = cgi.FieldStorage() if ('service' in form): this_service = form['service'].value.strip('\'') except SystemExit: pass # Configure script logger to output to stderr so logging gets stored # in apache error_log logging.basicConfig(stream=sys.stderr, level=logging.INFO, format="%(message)s") logging.getLogger(SCRIPT_LOGGER_NAME) # Ensure default handlers created are at the level we want here. for handler in logging.getLogger().handlers: handler.setLevel(logging.INFO) # Get the correct default.xml file for this AI service, or fall back # to the one in /usr/share/... manifest_file = DEFAULT_DEFAULT_MANIFEST if this_service is not None: manifest_file = get_default_manifest(this_service) # Create the XML parser parser = etree.XMLParser(remove_blank_text=True, dtd_validation=True, attribute_defaults=True) # parse the XML file try: tree = etree.parse(manifest_file, parser) except IOError, _error: print "Cannot access Manifest file [%s]" % (manifest_file) except etree.XMLSyntaxError, _error: print "XML syntax error in manifest [%s]" % (manifest_file) # convert from etree.Element to python dictionary my_dict = element_to_dict(tree.getroot()) my_dict['auto_install']['ai_instance']['save_server'] = \ Server.get_instance().wizard_saves_to_server # convert python dictionary to JSON out_text = simplejson.dumps(my_dict) # write output print out_text if __name__ == "__main__": print "Content-Type: text/json\n" # Ensure we always make a valid response by catching # any exceptions that occur try: main() except: pass