diff options
-rw-r--r-- | files/xml.py | 122 | ||||
-rw-r--r-- | manifests/volume.pp | 2 | ||||
-rw-r--r-- | manifests/volume/property.pp | 2 | ||||
-rw-r--r-- | manifests/xml.pp | 5 |
4 files changed, 115 insertions, 16 deletions
diff --git a/files/xml.py b/files/xml.py index 1e9a724..1f5f3ec 100644 --- a/files/xml.py +++ b/files/xml.py @@ -17,30 +17,67 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # EXAMPLE: -# $ gluster peer status --xml | ./xml.py --connected <PEER1> <PEER2> <PEERn> +# $ gluster peer status --xml | ./xml.py connected <PEER1> <PEER2> <PEERn> # <BOOL> # EXAMPLE: -# $ gluster volume --xml info <VOLNAME> | ./xml.py --property <KEY> +# $ gluster volume info --xml <VOLNAME> | ./xml.py property --key <KEY> # <VALUE> +# EXAMPLE: +# $ gluster volume status --xml [<VOLNAME>] | ./xml.py port --volume <VOLUME> --host <HOST> --path <PATH> +# <PORT> + +# EXAMPLE: +# $ gluster volume status --xml [<VOLNAME>] | ./xml.py ports [--volume <VOLUME>] [--host <HOST>] +# <PORT1>[,<PORT2>[,<PORTn>]] + import sys +import argparse import lxml.etree as etree -argv = sys.argv -argv.pop(0) # get rid of $0 +parser = argparse.ArgumentParser(description='gluster xml parsing tools') +#parser.add_argument('--debug', dest='debug', action='store_true', default=False) +subparsers = parser.add_subparsers(dest='mode') + +# +# 'connected' parser +# +parser_connected = subparsers.add_parser('connected') +parser_connected.add_argument('peers', type=str, nargs='*', action='store') + +# +# 'property' parser +# +parser_property = subparsers.add_parser('property') +parser_property.add_argument('--key', dest='key', action='store') + +# +# 'port' parser +# +parser_port = subparsers.add_parser('port') +parser_port.add_argument('--volume', dest='volume', action='store', required=True) +parser_port.add_argument('--host', dest='host', action='store', required=True) +parser_port.add_argument('--path', dest='path', action='store', required=True) -if len(argv) < 1: - sys.exit(3) +# +# 'ports' parser +# +parser_ports = subparsers.add_parser('ports') +parser_ports.add_argument('--volume', dest='volume', action='store', required=False) +parser_ports.add_argument('--host', dest='host', action='store', required=False) -mode = argv.pop(0) +# +# final setup... +# +args = parser.parse_args() tree = etree.parse(sys.stdin) root = tree.getroot() # are all the hostnames in argv connected ? -if mode == '--connected': +if args.mode == 'connected': store = {} - peers = [x for x in argv if x != ''] + peers = args.peers for i in root.findall('.//peerStatus'): p = i.find('peer') @@ -64,13 +101,10 @@ if mode == '--connected': # must be good! sys.exit(0) -elif mode == '--property': - if len(argv) != 1: - sys.exit(3) - +elif args.mode == 'property': store = [] for i in root.findall('.//option'): - if i.find('name').text == str(argv[0]): + if str(i.find('name').text) == args.key: store.append(i.find('value').text) if len(store) == 1: @@ -79,6 +113,66 @@ elif mode == '--property': else: # more than one value found sys.exit(1) +elif args.mode == 'port': + port = 0 + found = False + #print args.volume # volume + #print args.host # hostname + #print args.path # path + for i in root.findall('.//volumes'): + for j in i.findall('.//volume'): + v = str(j.find('volName').text) + #print v + for k in j.findall('.//node'): + h = str(k.find('hostname').text) + p = str(k.find('path').text) + #print h, p + #if v == args.volume and h == args.host and p == args.path: + if (v, h, p) == (args.volume, args.host, args.path): + if found: + # we have already found a match. + # there's a bug somewhere... + sys.exit(2) + found = True + port = int(k.find('port').text) + + if found and port > 0: + print(port) + sys.exit(0) + else: # no value found + sys.exit(1) + +# list all the ports used by one volume +elif args.mode == 'ports': + ports = [] + found = False + #print args.volume # volume (optional) + for i in root.findall('.//volumes'): + for j in i.findall('.//volume'): + v = str(j.find('volName').text) + #print v + # if no volume is specified, we use all of them... + if args.volume is None or args.volume == v: + for k in j.findall('.//node'): + h = str(k.find('hostname').text) + p = str(k.find('path').text) + #print h, p + if args.host is None or args.host == h: + try: + ports.append(int(k.find('port').text)) + found = True + except ValueError, e: + pass + + if found and len(ports) > 0: + # NOTE: you may get duplicates if you lookup multiple hosts... + # here we remove any duplicates and convert each int to strings + print(','.join([str(x) for x in list(set(ports))])) + sys.exit(0) + else: # no value found + sys.exit(1) + # else: sys.exit(3) +# vim: ts=8 diff --git a/manifests/volume.pp b/manifests/volume.pp index b9db88d..b1bc712 100644 --- a/manifests/volume.pp +++ b/manifests/volume.pp @@ -76,7 +76,7 @@ define gluster::volume( $others = inline_template("<%= bricks.find_all{|x| x.split(':')[0] != '${fqdn}' }.collect {|y| y.split(':')[0] }.join(' ') %>") $fping = sprintf("/usr/sbin/fping -q %s", $others) - $status = sprintf("/usr/sbin/gluster peer status --xml | ${vardir}/xml.py --connected %s", $others) + $status = sprintf("/usr/sbin/gluster peer status --xml | ${vardir}/xml.py connected %s", $others) $onlyif = $ping ? { false => "${status}", diff --git a/manifests/volume/property.pp b/manifests/volume/property.pp index e71e5d2..b0d2ad0 100644 --- a/manifests/volume/property.pp +++ b/manifests/volume/property.pp @@ -87,7 +87,7 @@ define gluster::volume::property( # FIXME: check that the value we're setting isn't the default # FIXME: you can check defaults with... gluster volume set help | ... exec { "/usr/sbin/gluster volume set ${volume} ${key} ${safe_value}": - unless => "/usr/bin/test \"`/usr/sbin/gluster volume --xml info ${volume} | ${vardir}/xml.py --property ${key}`\" = '${safe_value}'", + unless => "/usr/bin/test \"`/usr/sbin/gluster volume --xml info ${volume} | ${vardir}/xml.py property --key '${key}'`\" = '${safe_value}'", onlyif => "/usr/sbin/gluster volume list | /bin/grep -qxF '${volume}' -", logoutput => on_failure, require => [ diff --git a/manifests/xml.pp b/manifests/xml.pp index 5335cc3..82d5a02 100644 --- a/manifests/xml.pp +++ b/manifests/xml.pp @@ -18,6 +18,10 @@ class gluster::xml { include gluster::vardir + package { 'python-argparse': + ensure => present, + } + package { 'python-lxml': # for parsing gluster xml output ensure => present, } @@ -33,6 +37,7 @@ class gluster::xml { backup => false, # don't backup to filebucket ensure => present, require => [ + Package['python-argparse'], Package['python-lxml'], File["${vardir}/"], ], |