summaryrefslogtreecommitdiffstats
path: root/test/test-confmgt_augeas.py
blob: 73e0c8e19f785069378be9d099becc5ac2c7207b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/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:
        client = fc.Client(host)
        print client.confmgt_augeas.set('/etc/ssh/sshd_config/AcceptEnv[last()]','10000','FOO')
print