diff options
author | Louis Coilliot <louis.coilliot@think.fr> | 2009-02-23 16:02:00 -0500 |
---|---|---|
committer | Adrian Likins <alikins@redhat.com> | 2009-02-23 16:02:00 -0500 |
commit | 8053c907c1647411662ccc01c000a4284398620a (patch) | |
tree | cc7706ee635e50ed82542af8162c7669d6a31c0b | |
parent | 2c9530116234e8034b175a8981a9915b0b89b8ff (diff) | |
download | func-8053c907c1647411662ccc01c000a4284398620a.tar.gz func-8053c907c1647411662ccc01c000a4284398620a.tar.xz func-8053c907c1647411662ccc01c000a4284398620a.zip |
updates to augeas module and test script
Modifications on the module:
- fix for trailing /
- new function=backup
(because in func+augeas we can't use /augeas/save; each action is a new
augeas instance)
- new function=getenv
Modifications on the test script:
- the tests are now 'chrooted' with AUGEAS_ROOT
(but you need to put something like export AUGEAS_ROOT='/tmp'
in /etc/init.d/funcd, I don't know yet of a better way to pass this
variable to func)
- new tests, in particular for extended paths
Todo:
- fix for some extended path failures
- find a way to use /augeas/save in a persistent way
- ability to work on any single conf. file when it will be available in
Augeas
-rw-r--r-- | func/minion/modules/confmgt_augeas.py | 94 | ||||
-rwxr-xr-x | test/test-confmgt_augeas.py | 480 |
2 files changed, 355 insertions, 219 deletions
diff --git a/func/minion/modules/confmgt_augeas.py b/func/minion/modules/confmgt_augeas.py index 06f0e21..3438fd9 100644 --- a/func/minion/modules/confmgt_augeas.py +++ b/func/minion/modules/confmgt_augeas.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + # # Copyright 2008 # Louis Coilliot <louis.coilliot@wazemmes.org> @@ -10,7 +12,8 @@ # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. import func_module -from os import path as ospath +from os import path as ospath,getenv as osgetenv +from time import strftime def lstripstr(the_string,the_prefix): """Return a copy of the string with leading prefix removed.""" @@ -57,14 +60,14 @@ with the help of Augeas, a configuration API (cf http://augeas.net)""" from augeas import Augeas aug=Augeas() except Exception, e: return str(e) - - path=hierarchy+entryPath+'/' + # yes, entryPath.rstrip('/')+'/' is really needed (i.e. entryPath=/) + path=(hierarchy+entryPath.rstrip('/')+'/'+param).rstrip('/') try: - matchtest=aug.match(path+param) + matchtest=aug.match(path) except Exception, e: return str(e) if matchtest: try: - pvalue=aug.get(path+param) + pvalue=aug.get(path) #aug.close() except Exception, e: return str(e) else: @@ -75,7 +78,7 @@ with the help of Augeas, a configuration API (cf http://augeas.net)""" # The node doesn't have a value pvalue='(none)' - return { 'path': entryPath, 'parameter': param, 'value': pvalue } + return { 'path': entryPath, 'parameter': param, 'value': pvalue, 'hierarchy': hierarchy } def set(self,entryPath,param='',pvalue='',hierarchy='/files'): @@ -86,12 +89,12 @@ with the help of Augeas, a configuration API (cf http://augeas.net)""" aug=Augeas() except Exception, e: return str(e) - path=hierarchy+entryPath + path=(hierarchy+entryPath.rstrip('/')+'/'+param).rstrip('/') try: - aug.set(path+"/"+param,pvalue) + aug.set(path,pvalue) except Exception, e: return str(e) - # Here is a little workaround for a bug in save for augeas 0.3.2. + # Here is a little workaround for a bug in save for augeas. # In the future this won't be necessary anymore. try: aug.save() @@ -103,11 +106,11 @@ with the help of Augeas, a configuration API (cf http://augeas.net)""" except Exception, e: return str(e) try: - pvalue=aug.get(path+"/"+param) + pvalue=aug.get(path) #aug.close() except Exception, e: return str(e) - return { 'path': entryPath, 'parameter': param, 'value': pvalue } + return { 'path': entryPath, 'parameter': param, 'value': pvalue, 'hierarchy': hierarchy } def match(self,entryPath,param='',pvalue='',hierarchy='/files'): @@ -118,19 +121,19 @@ with the help of Augeas, a configuration API (cf http://augeas.net)""" aug=Augeas() except Exception, e: return str(e) - path=hierarchy+entryPath - childpath=path+'/*/' + path=(hierarchy+entryPath.rstrip('/')+'/'+param).rstrip('/') + childpath=(hierarchy+entryPath.rstrip('/')+'/*/'+param).rstrip('/') + if pvalue: try: - matchlist = [ ospath.dirname(lstripstr(item,'/files')) for item in aug.match(path+'/'+param) + aug.match(childpath+'/'+param) if ( aug.get(item) == pvalue ) ] + matchlist = [ ospath.dirname(lstripstr(item,'/files')) for item in aug.match(path) + aug.match(childpath) if ( aug.get(item) == pvalue ) ] #aug.close() except Exception, e: return str(e) else: try: - matchlist = [ ospath.dirname(lstripstr(item,'/files')) for item in aug.match(path+'/'+param) + aug.match(childpath+'/'+param) ] + matchlist = [ ospath.dirname(lstripstr(item,'/files')) for item in aug.match(path) + aug.match(childpath) ] #aug.close() except Exception, e: return str(e) - return matchlist @@ -141,12 +144,11 @@ with the help of Augeas, a configuration API (cf http://augeas.net)""" from augeas import Augeas aug=Augeas() except Exception, e: return str(e) - - path=hierarchy+entryPath + path=hierarchy+entryPath.rstrip('/')+'/*' # We can't use a dict here because the same key can appear many times. nodes=[] try: - for match in aug.match(path+'/*'): + for match in aug.match(path): pvalue=aug.get(match) if not pvalue: pvalue='(none)' @@ -157,7 +159,7 @@ with the help of Augeas, a configuration API (cf http://augeas.net)""" # aug.close() #except Exception, e: return str(e) - return { 'path': entryPath, 'nodes': nodes } + return { 'path': entryPath, 'nodes': nodes, 'hierarchy': hierarchy } # print is a reserved word so we use printconf instead @@ -171,7 +173,7 @@ with the help of Augeas, a configuration API (cf http://augeas.net)""" except Exception, e: return str(e) matches = recurmatch(aug, path) # Here we loose the benefit of the generator function: - return { 'path': entryPath, 'nodes':[ [lstripstr(p,'/files'),attr] for (p,attr) in matches ] } + return { 'path': entryPath, 'nodes':[ [lstripstr(p,'/files'),attr] for (p,attr) in matches ], 'hierarchy': hierarchy } @@ -183,14 +185,14 @@ with the help of Augeas, a configuration API (cf http://augeas.net)""" aug=Augeas() except Exception, e: return str(e) - path=hierarchy+entryPath + path=(hierarchy+entryPath.rstrip('/')+'/'+param).rstrip('/') try: - result=aug.remove(path+"/"+param) + result=aug.remove(path) #aug.close() except Exception, e: return str(e) - # Here is a little workaround for a bug in save for augeas 0.3.2. - # In the future this won't be necessary anymore. + # Here is a little workaround for a bug in save for augeas. + # In the future this should not be necessary anymore. try: aug.save() except: @@ -205,6 +207,28 @@ with the help of Augeas, a configuration API (cf http://augeas.net)""" msg = repr(result)+' node(s) removed.' return msg + def getenv(self,varname): + """Get an environment variable.""" + varvalue=osgetenv(varname) + if varvalue == None: + varvalue = '(none)' + return { varname : varvalue } + + def backup(self,entryPath): + """Backup a file with a timestamp. Cautious before applying modifications on a configuration file.""" + try: + import shutil + except Exception, e: return str(e) + backupPath=entryPath+'.'+strftime('%Y%m%d-%H%M') + try: + if not ospath.exists(backupPath): + shutil.copy(entryPath, backupPath) + msg='File '+entryPath+' backed up to '+ backupPath + else: + msg='Backup file '+backupPath+' already exists' + except (OSError, IOError), e: return str(e) + return msg + def register_method_args(self): """ @@ -343,6 +367,26 @@ with the help of Augeas, a configuration API (cf http://augeas.net)""" } }, 'description':"Delete a parameter (and all its children) in a config. file." + }, + 'getenv':{ + 'args':{ + 'varname':{ + 'type':'string', + 'optional':False, + 'description':'The name of the environment variable to get', + } + }, + 'description':"Get an environment variable." + }, + 'backup':{ + 'args':{ + 'entryPath':{ + 'type':'string', + 'optional':False, + 'description':'The path to the config. file', + } + }, + 'description':"Backup a file with a timestamp." } } diff --git a/test/test-confmgt_augeas.py b/test/test-confmgt_augeas.py index 73e0c8e..6fe8dba 100755 --- a/test/test-confmgt_augeas.py +++ b/test/test-confmgt_augeas.py @@ -1,200 +1,292 @@ #!/usr/bin/env python + + import sys import func.overlord.client as fc c = fc.Client("*") -print 'Delete the Parameter PermitRootLogin in sshd_config' -print c.confmgt_augeas.rm('/etc/ssh/sshd_config','PermitRootLogin') -print - -print 'Delete the Parameter Port in sshd_config with an Augeas-style path' -print c.confmgt_augeas.rm('/etc/ssh/sshd_config/Port') -print - -print 'Get sshd_config Port value.' -print c.confmgt_augeas.get('/etc/ssh/sshd_config','Port') -print - -print 'Set Port to 22 in sshd_config' -print c.confmgt_augeas.set('/etc/ssh/sshd_config','Port','22') -print - -print 'Get sshd_config Port value.' -print c.confmgt_augeas.get('/etc/ssh/sshd_config','Port') -print - -print 'Try to delete a non existant parameter in sshd_config' -print c.confmgt_augeas.rm('/etc/ssh/sshd_config','Nawak') -print - -print 'Try to delete a parameter in a non existant file.' -print c.confmgt_augeas.rm('/etc/ssh/nimp','Nawak') -print - -print 'Get sshd_config PermitRootLogin value.' -print c.confmgt_augeas.get('/etc/ssh/sshd_config','PermitRootLogin') -print - -print 'Set PermitRootLogin to yes in sshd_config' -print c.confmgt_augeas.set('/etc/ssh/sshd_config','PermitRootLogin','yes') -print - -print 'Set PermitRootLogin to no in sshd_config with an Augeas-style path.' -print c.confmgt_augeas.set('/etc/ssh/sshd_config/PermitRootLogin','','no') -print - -print 'Set PermitRootLogin to yes in sshd_config with an Augeas-style path.' -print c.confmgt_augeas.set('/etc/ssh/sshd_config/PermitRootLogin','','yes') -print - -print 'Get sshd_config PermitRootLogin value.' -print c.confmgt_augeas.get('/etc/ssh/sshd_config','PermitRootLogin') -print - -print 'Get sshd_config PermitRootLogin value with an Augeas-style path.' -print c.confmgt_augeas.get('/etc/ssh/sshd_config/PermitRootLogin') -print - -print 'Attempt to get a value for a non existant param in sshd_config' -print c.confmgt_augeas.get('/etc/ssh/sshd_config','Nawak') -print - -print 'Attempt to get a value for an empty param in sshd_config' -print c.confmgt_augeas.get('/etc/ssh/sshd_config','Subsystem') -print - -print 'Search for conf. entry in hosts file with canonical hostname = pim' -print c.confmgt_augeas.match('/etc/hosts','canonical','pim') -print - -#print 'List all direct children of hosts (not very useful)' -#print c.confmgt_augeas.ls('/etc/hosts/*') -#print - -print 'List all direct children parameters of 1st hosts entry.' -for host,paramlist in c.confmgt_augeas.ls('/etc/hosts/1').iteritems(): - print "Host: "+host - for node in paramlist['nodes']: - print node[0]+" = "+node[1] -print - -print 'List all children nodes of 1st hosts entry.' -for host,paramlist in c.confmgt_augeas.printconf('/etc/hosts/1').iteritems(): - print "Host: "+host - for node in paramlist['nodes']: - print node[0]+" = "+node[1] -print - -print 'Get values of 1st host entry.' -print c.confmgt_augeas.get('/etc/hosts/','1') -print - -print 'List all values for parameter of 1st fstab entry.' -minionDict=c.confmgt_augeas.ls('/etc/fstab/1') -for host,entry in minionDict.iteritems(): - print "Host: "+host - print "Entry path: "+entry['path'] - for node in entry['nodes']: - print node[0]+" = "+node[1] -print - -print 'Get ipaddr of /etc/hosts 1st entry.' -print c.confmgt_augeas.get('/etc/hosts/1','ipaddr') -print -# -#print 'List all direct children parameters of sshd_config' -#for host,paramlist in c.confmgt_augeas.ls('/etc/ssh/sshd_config').iteritems(): -# print "Host: "+host -# for node in paramlist['nodes']: -# print node[0]+" = "+node[1] -#print -# -#print 'List all children nodes of sshd_config' -#for host,paramlist in c.confmgt_augeas.printconf('/etc/ssh/sshd_config').iteritems(): -# print "Host: "+host -# for node in paramlist['nodes']: -# print node[0]+" = "+node[1] -#print -# -print 'List all direct children of AcceptEnv entries in sshd_config' -for host,paramlist in c.confmgt_augeas.ls('/etc/ssh/sshd_config/AcceptEnv').iteritems(): - print "Host: "+host - for node in paramlist['nodes']: - print node[0]+" = "+node[1] -print - -print 'See all AcceptEnv entries in sshd_config' -for host,paramlist in c.confmgt_augeas.printconf('/etc/ssh/sshd_config/AcceptEnv').iteritems(): - print "Host: "+host - for node in paramlist['nodes']: - print node[0]+" = "+node[1] -print - -print 'Try to match PermitRootLogin yes in sshd_config' -print c.confmgt_augeas.match('/etc/ssh/sshd_config','PermitRootLogin','yes') -print - -print 'Try to match PermitRootLogin yes in sshd_config with an Augeas-style path' -print c.confmgt_augeas.match('/etc/ssh/sshd_config/PermitRootLogin','','yes') -print - -print 'Try to match PermitRootLogin yes in some config. files.' -print c.confmgt_augeas.match('/etc/*/*','PermitRootLogin','yes') -print - -print 'Try to match AcceptEnv in sshd_config' -print c.confmgt_augeas.match('/etc/ssh/sshd_config','AcceptEnv') -print - -print 'Try to match PermitRootLogin in sshd_config' -print c.confmgt_augeas.match('/etc/ssh/sshd_config','PermitRootLogin') -print - -print 'Try to match PermitRootLogin in sshd_config with an Augeas-style path.' -print c.confmgt_augeas.match('/etc/ssh/sshd_config/PermitRootLogin') -print - -print 'Try to match canonical entries in hosts file.' -print c.confmgt_augeas.match('/etc/hosts','canonical') -print - -print 'Try to match canonical entries in hosts file with an Augeas-style path.' -print c.confmgt_augeas.match('/etc/hosts/*/canonical') -print - -print 'Augeas metainformation.' -print c.confmgt_augeas.ls('/','augeas') -print c.confmgt_augeas.get('/','save','augeas') -print c.confmgt_augeas.set('/','save','backup','augeas') -print c.confmgt_augeas.get('/','save','augeas') -print c.confmgt_augeas.get('/files/etc/hosts/lens/','info','augeas') -print - -print 'Change the (canonical) hostname associated to a specific IP in hosts file.' -hostfile='/etc/hosts' -ip='192.168.0.253' -newCanonical='fozzie' -#newCanonical='piggy' -# We search which entry in /etc/hosts refers to the IP -ipmatch = c.confmgt_augeas.match(hostfile,'ipaddr',ip) -# for each minion concerned -for host,entry in ipmatch.iteritems(): - # The first and unique entry in the list, entry[0], is what we searched for - # We check that the target canonical hostname is not already set - oldCanonical=c.confmgt_augeas.get(entry[0],'canonical')[host]['value'] - if oldCanonical != newCanonical: - print c.confmgt_augeas.set(entry[0],'canonical',newCanonical) - else: - print 'Nothing to do' -print - - -print 'Add a new variable FOO at the end of the last AcceptEnv line of sshd_config' -print "And we don't want to do this twice." -foomatch=c.confmgt_augeas.match('/etc/ssh/sshd_config','AcceptEnv/*','FOO') -for host,matchlist in foomatch.iteritems(): - if not matchlist: +print c.configmgt_augeas.getenv('AUGEAS_ROOT') + +def chroottest(): + envdict={} + print 'Get the env. variable AUGEAS_ROOT' + for host,envlist in c.confmgt_augeas.getenv('AUGEAS_ROOT').iteritems(): + print "Host: "+host + augroot=envlist.get('AUGEAS_ROOT') + print 'AUGEAS_ROOT: '+augroot + envdict[host]=augroot + if augroot == '/' or augroot == '(none)': + print "The node "+host+" is not chrooted with AUGEAS_ROOT" + print "You should consider setting this variable" + print "before launching this test." + sys.exit() + print + + print 'Prepare the test environment' + for host in envdict: + augroot=envdict[host] client = fc.Client(host) - print client.confmgt_augeas.set('/etc/ssh/sshd_config/AcceptEnv[last()]','10000','FOO') -print - + print client.command.run('mkdir -p '+augroot+'/etc/ssh') + print client.command.run('cp /etc/hosts '+augroot+'/etc') + print client.command.run('cp -r /etc/ssh/* '+augroot+'/etc/ssh') + print + +chroottest() + + +def basictest(): + #print 'Backup sshd_config' + #print c.confmgt_augeas.backup('/etc/ssh/sshd_config') + #print + + print 'Delete the Parameter PermitRootLogin in sshd_config' + print c.confmgt_augeas.rm('/etc/ssh/sshd_config','PermitRootLogin') + print + + print 'Delete the Parameter Port in sshd_config with an Augeas-style path' + print c.confmgt_augeas.rm('/etc/ssh/sshd_config/Port') + print + + print 'Get sshd_config Port value.' + print c.confmgt_augeas.get('/etc/ssh/sshd_config','Port') + print + + print 'Set Port to 22 in sshd_config' + print c.confmgt_augeas.set('/etc/ssh/sshd_config','Port','22') + print + + print 'Get sshd_config Port value.' + print c.confmgt_augeas.get('/etc/ssh/sshd_config','Port') + print + + print 'Try to delete a non existant parameter in sshd_config' + print c.confmgt_augeas.rm('/etc/ssh/sshd_config','Nawak') + print + + print 'Try to delete a parameter in a non existant file.' + print c.confmgt_augeas.rm('/etc/ssh/nimp','Nawak') + print + + print 'Get sshd_config PermitRootLogin value.' + print c.confmgt_augeas.get('/etc/ssh/sshd_config','PermitRootLogin') + print + + print 'Set PermitRootLogin to yes in sshd_config' + print c.confmgt_augeas.set('/etc/ssh/sshd_config','PermitRootLogin','yes') + print + + print 'Set PermitRootLogin to no in sshd_config with an Augeas-style path.' + print c.confmgt_augeas.set('/etc/ssh/sshd_config/PermitRootLogin','','no') + print + + print 'Set PermitRootLogin to yes in sshd_config with an Augeas-style path.' + print c.confmgt_augeas.set('/etc/ssh/sshd_config/PermitRootLogin','','yes') + print + + print 'Get sshd_config PermitRootLogin value.' + print c.confmgt_augeas.get('/etc/ssh/sshd_config','PermitRootLogin') + print + + print 'Get sshd_config PermitRootLogin value with an Augeas-style path.' + print c.confmgt_augeas.get('/etc/ssh/sshd_config/PermitRootLogin') + print + + print 'Attempt to get a value for a non existant param in sshd_config' + print c.confmgt_augeas.get('/etc/ssh/sshd_config','Nawak') + print + + print 'Attempt to get a value for an empty param in sshd_config' + print c.confmgt_augeas.get('/etc/ssh/sshd_config','Subsystem') + print + + print 'Search for conf. entry in hosts file with canonical hostname = localhost' + print c.confmgt_augeas.match('/etc/hosts','canonical','localhost') + print + + #print 'List all direct children of hosts (not very useful)' + #print c.confmgt_augeas.ls('/etc/hosts/*') + #print + + print 'List all direct children parameters of 1st hosts entry.' + for host,paramlist in c.confmgt_augeas.ls('/etc/hosts/1').iteritems(): + print "Host: "+host + if type(paramlist) == type({}): + for node in paramlist['nodes']: + print node[0]+" = "+node[1] + else: + print paramlist + print + + print 'List all children nodes of 1st hosts entry.' + for host,paramlist in c.confmgt_augeas.printconf('/etc/hosts/1').iteritems(): + print "Host: "+host + if type(paramlist) == type({}): + for node in paramlist['nodes']: + print node[0]+" = "+node[1] + else: + print paramlist + print + + print 'Get values of 1st host entry.' + print c.confmgt_augeas.get('/etc/hosts/','1') + print + + print 'List all values for parameter of 1st fstab entry.' + minionDict=c.confmgt_augeas.ls('/etc/fstab/1') + for host,entry in minionDict.iteritems(): + print "Host: "+host + if type(entry) == type({}): + print "Entry path: "+entry['path'] + for node in entry['nodes']: + print node[0]+" = "+node[1] + else: + print entry + print + + print 'Get ipaddr of /etc/hosts 1st entry.' + print c.confmgt_augeas.get('/etc/hosts/1','ipaddr') + print + # + #print 'List all direct children parameters of sshd_config' + #for host,paramlist in c.confmgt_augeas.ls('/etc/ssh/sshd_config').iteritems(): + # print "Host: "+host + # for node in paramlist['nodes']: + # print node[0]+" = "+node[1] + #print + # + print 'List all children nodes of sshd_config' + for host,paramlist in c.confmgt_augeas.printconf('/etc/ssh/sshd_config').iteritems(): + print "Host: "+host + for node in paramlist['nodes']: + print node[0]+" = "+node[1] + print + + print 'List all direct children of AcceptEnv entries in sshd_config' + for host,paramlist in c.confmgt_augeas.ls('/etc/ssh/sshd_config/AcceptEnv').iteritems(): + print "Host: "+host + if type(paramlist)==type({}): + for node in paramlist['nodes']: + print node[0]+" = "+node[1] + else: + print paramlist + print + + print 'See all AcceptEnv entries in sshd_config' + for host,paramlist in c.confmgt_augeas.printconf('/etc/ssh/sshd_config/AcceptEnv').iteritems(): + print "Host: "+host + if type(paramlist)==type({}): + for node in paramlist['nodes']: + print node[0]+" = "+node[1] + else: + print paramlist + print + + print 'Try to match PermitRootLogin yes in sshd_config' + print c.confmgt_augeas.match('/etc/ssh/sshd_config','PermitRootLogin','yes') + print + + print 'Try to match PermitRootLogin yes in sshd_config with an Augeas-style path' + print c.confmgt_augeas.match('/etc/ssh/sshd_config/PermitRootLogin','','yes') + print + + print 'Try to match PermitRootLogin yes in some config. files.' + print c.confmgt_augeas.match('/etc/*/*','PermitRootLogin','yes') + print + + print 'Try to match AcceptEnv in sshd_config' + print c.confmgt_augeas.match('/etc/ssh/sshd_config','AcceptEnv') + print + + print 'Try to match PermitRootLogin in sshd_config' + print c.confmgt_augeas.match('/etc/ssh/sshd_config','PermitRootLogin') + print + + print 'Try to match PermitRootLogin in sshd_config with an Augeas-style path.' + print c.confmgt_augeas.match('/etc/ssh/sshd_config/PermitRootLogin') + print + + print 'Try to match canonical entries in hosts file.' + print c.confmgt_augeas.match('/etc/hosts','canonical') + print + + print 'Try to match canonical entries in hosts file with an Augeas-style path.' + print c.confmgt_augeas.match('/etc/hosts/*/canonical') + print + + print 'Augeas metainformation.' + print c.confmgt_augeas.ls('/','/augeas') + print c.confmgt_augeas.get('/','save','/augeas') + + #Not supposed to work: + print c.confmgt_augeas.set('/','save','backup','/augeas') + print c.confmgt_augeas.set('/save','','backup','/augeas') + + print c.confmgt_augeas.get('/save','','/augeas') + print c.confmgt_augeas.get('/files/etc/hosts/lens','info','/augeas') + + + + print 'Add a new variable FOO at the end of the last AcceptEnv line of sshd_config' + print "And we don't want to do this twice." + foomatch=c.confmgt_augeas.match('/etc/ssh/sshd_config','AcceptEnv/*','FOO') + for host,matchlist in foomatch.iteritems(): + if not matchlist: + client = fc.Client(host) + print client.confmgt_augeas.set('/etc/ssh/sshd_config/AcceptEnv[last()]','10000','FOO') + print + + + print 'Change the (canonical) hostname associated to a specific IP in hosts file.' + hostfile='/etc/hosts' + ip='1.2.3.4' + newCanonical='fozzie' + #newCanonical='piggy' + # We search which entry in /etc/hosts refers to the IP + ipmatch = c.confmgt_augeas.match(hostfile,'ipaddr',ip) + # for each minion concerned + for host,entry in ipmatch.iteritems(): + # The first and unique entry in the list, entry[0], is what we searched for + # We check that the target canonical hostname is not already set + if (type(entry) == type([]) and entry): + oldCanonical=c.confmgt_augeas.get(entry[0],'canonical')[host]['value'] + if oldCanonical != newCanonical: + print c.confmgt_augeas.set(entry[0],'canonical',newCanonical) + else: + print 'Nothing to do' + else: + print repr(entry)+' - no match' + print + +basictest() + +# Extended path syntax +def extendedtest(): + print 'Tests on extended paths' + # not working: + print c.confmgt_augeas.match('//error/descendant-or-self::*','/augeas') + print + + print c.confmgt_augeas.get('/etc/hosts/*[ipaddr = "127.0.0.1"]/canonical') + print c.confmgt_augeas.get('/etc/hosts/*[ipaddr = "127.0.0.1"]/','canonical') + print + + print c.confmgt_augeas.get("/etc//ipaddr[. = '127.0.0.1']") + print + + print c.confmgt_augeas.match('/etc/hosts/*/ipaddr') + print c.confmgt_augeas.match('/etc/hosts/*/','ipaddr') + + # not working: + #print c.confmgt_augeas.printconf('/etc/hosts/*/','ipaddr') + #print + + print c.confmgt_augeas.match('/etc/pam.d/*[.//module="pam_limits.so"]') + print + + # not working (wrong): + print c.confmgt_augeas.match('/etc//1') + + # not working (wrong): + print c.confmgt_augeas.match('/descendant-or-self::4') + +extendedtest() |