#!/usr/bin/env python # # Copyright 2012 ancoron. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import sys import os import argparse import re def main(): try: from lxml import etree except ImportError: try: # Python 2.5 import xml.etree.cElementTree as etree except ImportError: try: # Python 2.5 import xml.etree.ElementTree as etree except ImportError: try: # normal cElementTree install import cElementTree as etree except ImportError: try: # normal ElementTree install import elementtree.ElementTree as etree except ImportError: print("Failed to import ElementTree from any known place") sys.exit(1) parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter, description='List or modify persistent JVM options for a GlassFish Domain.', epilog="""The command arguments (arg) are as follows: Command Required arguments list None set delete """ ) parser.add_argument('domainXml', type=file, help='The path to the GlassFish Domain configuration file (domain.xml)') parser.add_argument('command', choices=['list','set','delete'], help='What to do with the JVM options') parser.add_argument('-c', '--config', default='server-config', metavar='name', help='Name of the configuration within the specified domain', required=False ) parser.add_argument('args', metavar='...', nargs=argparse.REMAINDER, help='Optional arguments for the specific command') args = parser.parse_args() docPath = args.domainXml conf = args.config if args.command == 'list': doc = etree.parse(docPath) options = doc.xpath('//domain/configs/config[@name="{0}"]/java-config'.format(conf))[0] for element in options.iter(): if etree.iselement(element) and element.tag == "jvm-options": print(element.text) elif args.command == 'set': if len(args.args) == 0: print("Please specify property name and value") sys.exit(1) elif len(args.args) == 1 and "=" in args.args[0]: # print("Please specify property name AND value in separate arguments. Working around here...") a = args.args[0].split("=") args.args[0] = a[0] args.args.append(a[1]) elif len(args.args) > 2: print("Ignoring additional command arguments.") newProperty = args.args[0] newValue = None if len(args.args) > 1: newValue = args.args[1] doc = etree.parse(docPath) options = doc.xpath('//domain/configs/config[@name="{0}"]/java-config'.format(conf))[0] found = False txt = None if newValue == None: txt = newProperty else: txt = "{0}={1}".format(newProperty, newValue) for element in options.iter(): eProp = element.text.split("=") eName = eProp[0] if eName == newProperty: element.text = txt found = True if not found: etree.SubElement(options, 'jvm-options').text = txt domainXml = open(docPath.name, 'w') doc.write(domainXml) sys.exit(0) elif args.command == 'delete': if len(args.args) == 0: print("Please specify property name and value") sys.exit(1) elif len(args.args) > 1: print("Ignoring additional command arguments.") newProperty = args.args[0] doc = etree.parse(docPath) options = doc.xpath('//domain/configs/config[@name="{0}"]/java-config'.format(conf))[0] found = [] search = "^{0}(|=.*)$".format(newProperty) for element in options.iter(): eProp = element.text.split("=") eName = eProp[0] if eName == newProperty: found.append(element) if len(found) == 0: print("JVM-Option '{0}' not found.".format(newProperty)) sys.exit(0) else: for e in found: options.remove(e) domainXml = open(docPath.name, 'w') doc.write(domainXml) sys.exit(0) else: print("Sub-command '%s' not available!" % args[1]) sys.exit(1) if __name__ == '__main__': main()