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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
#!/usr/bin/python
'''bcfg2-admin is a script that helps to administrate a bcfg2 deployment'''
import difflib, logging, lxml.etree, os, popen2, re, socket, sys, ConfigParser
import Bcfg2.Server.Core, Bcfg2.Logging
log = logging.getLogger('bcfg-admin')
colors = ['steelblue1', 'chartreuse', 'gold', 'magenta', 'indianred1', 'limegreen',
'orange1', 'lightblue2']
usage = '''
bcfg2-admin [options]
init - initialize the bcfg2 repository( this is interactive; only run once )
mineentry <client> <entry type> <entry name> - mine statistics for entry information
minestruct <client> - mine statistics for extra entries
viz [-h] [-b] [-k] [-o output.png] [-r]
'''
config = '''
[server]
repository = %s
structures = Bundler,Base
generators = SSHbase,Cfg,Pkgmgr,Svcmgr,Rules
[statistics]
sendmailpath = /usr/sbin/sendmail
[communication]
protocol = xmlrpc/ssl
password = %s
key = /etc/bcfg2.key
[components]
bcfg2 = %s
'''
groups = '''
<Groups version='3.0'>
<Group profile='true' public='false' name='basic'>
<Group name='%s'/>
</Group>
<Group name='ubuntu' toolset='debian'/>
<Group name='debian' toolset='debian'/>
<Group name='redhat' toolset='rh'/>
<Group name='suse' toolset='rh'/>
<Group name='mandrake' toolset='rh'/>
<Group name='solaris' toolset='solaris'/>
<Group name='gentoo' toolset='gentoo'/>
</Groups>
'''
clients = '''
<Clients version="3.0">
<Client profile="basic" pingable="Y" pingtime="0" name="%s"/>
</Clients>
'''
prompt = '''
please select which operating system your machine is running:
a. Redhat/Fedora/RHEL/RHAS/Centos
b. SUSE/SLES
c. Mandrake
d. Debian
e. Ubuntu
f. Solaris
g. Gentoo
'''
def err_exit(msg):
print msg
raise SystemExit, 1
#build bcfg2.conf file
def initialize_repo():
'''Setup a new repo'''
repo = raw_input( "location of bcfg2 repository [/var/lib/bcfg2]: " )
if repo == '':
repo = '/var/lib/bcfg2'
password = ''
while ( password == '' ):
password = raw_input( "please provide the password used for communication verification: " )
#get the hostname
server = "https://%s:6789" % socket.getfqdn()
uri = raw_input( "please provide the server location[%s]: " % server)
if uri == '':
uri = server
open("/etc/bcfg2.conf","w").write(config % ( repo, password, uri ))
#generate the ssl key
print "Now we will generate the ssl key used for secure communitcation"
os.popen('openssl req -x509 -nodes -days 1000 -newkey rsa:1024 -out /etc/bcfg2.key -keyout /etc/bcfg2.key')
try:
os.chmod('/etc/bcfg2.key','0600')
except:
pass
#create the repo dirs
for subdir in ['SSHbase', 'Cfg', 'Pkgmgr', 'Svcmgr', 'Rules', 'etc', 'Metadata',
'Base', 'Bundler']:
path = "%s/%s" % (repo, subdir)
newpath = ''
for subdir in path.split('/'):
newpath = newpath + subdir + '/'
try:
os.mkdir(newpath)
except:
pass
#create the groups.xml file
selection = ''
while ( selection == '' ):
print prompt
selection = raw_input(" selection: ")
if selection.lower() not in 'abcdefg':
selection = ''
if selection.lower() == 'a':
selection = 'redhat'
elif selection.lower() == 'b':
selection = 'suse'
elif selection.lower() == 'c':
selection = 'mandrake'
elif selection.lower() == 'd':
selection = 'debian'
elif selection.lower() == 'e':
selection = 'ubuntu'
elif selection.lower() == 'f':
selection = 'solaris'
elif selection.lower() == 'g':
selection = 'gentoo'
open("%s/Metadata/groups.xml"%repo, "w").write(groups%selection)
#now the clients file
open("%s/Metadata/clients.xml"%repo, "w").write(clients%socket.getfqdn())
print "Repository created successfuly in %s" % (repo)
def update_file(path, diff):
'''Update file at path using diff'''
newdata = '\n'.join(difflib.restore(diff.split('\n'), 1))
print "writing file, %s" % path
open(path, 'w').write(newdata)
def get_repo_path(cfile='/etc/bcfg2.conf'):
'''return repository path'''
cfp = ConfigParser.ConfigParser()
cfp.read(cfile)
return cfp.get('server', 'repository')
def load_stats(client):
repo = get_repo_path()
stats = lxml.etree.parse("%s/etc/statistics.xml" % (repo))
hostent = stats.xpath('.//Node[@name="%s"]' % client)
if not hostent:
err_exit("Could not find stats for client %s" % (client))
return hostent[0]
def do_pull(client, etype, ename):
'''Make currently recorded client state correct for entry'''
cfile = '/etc/bcfg2.conf'
sdata = load_stats(client)
if sdata.xpath('.//Statistics[@state="dirty"]'):
state = 'dirty'
else:
state = 'clean'
# need to pull entry out of statistics
entry = sdata.xpath('.//Statistics[@state="%s"]/Bad/%s[@name="%s"]' % \
(state, etype, ename))
if not entry:
err_exit("Could not find state data for entry; rerun bcfg2 on client system")
# fail for unsupported etypes
if etype not in ['ConfigFile', 'Package']:
err_exit("Unsupported entry type %s" % (etype))
try:
bcore = Bcfg2.Server.Core.Core({}, cfile)
except Bcfg2.Server.Core.CoreInitError, msg:
print "Core load failed because %s" % msg
raise SystemExit, 1
[bcore.fam.Service() for x in range(10)]
while bcore.fam.Service():
pass
m = bcore.metadata.get_metadata(client)
# find appropriate location in repo
if etype == 'ConfigFile':
rversion = lxml.etree.Element('ConfigFile', name=ename)
bcore.Bind(rversion, m)
current = rversion.text
diff = entry[0].get('current_diff')[1:-1]
basefile = [frag for frag in \
bcore.plugins['Cfg'].entries[ename].fragments \
if frag.applies(m)][-1]
if ".H_%s" % (m.hostname) in basefile.name:
answer = raw_input("Found host-specific file %s; Should it be updated (n/Y): ")
if answer in 'Yy':
update_file(basefile.name, diff)
else:
raise SystemExit, 1
else:
# there are two possibilities
msg = "Should this change apply to this host of all hosts effected by file %s? (N/y): " % (basefile.name)
choice = raw_input(msg)
if choice in 'Yy':
newname = basefile.name
else:
# figure out host-specific filename
if '.G_' in basefile.name:
idx = basefile.name.find(".G_")
newname = basefile.name[:idx] + ".H_%s" % (m.hostname)
else:
newname = basefile.name + ".H_%s" % (m.hostname)
print "This file will be installed as file %s" % newname
if raw_input("Should it be installed? (N/y): ") in 'Yy':
update_file(newname, diff)
else:
err_exit("Don't support entry type %s yet" % etype)
# svn commit if running under svn
def do_minestruct(argdata):
'''Pull client entries into structure'''
if len(argdata) != 1:
err_exit("minestruct must be called with a client name")
client = argdata[0]
stats = load_stats(client)
if len(stats.getchildren()) == 2:
# client is dirty
current = [ent for ent in stats.getchildren() if ent.get('state') == 'dirty'][0]
else:
current = stats.getchildren()[0]
extra = current.find('Extra').getchildren()
log.info("Found %d extra entries" % (len(extra)))
log.info(["%s: %s" % (entry.tag, entry.get('name')) for entry in extra])
def do_tidy(args):
'''Clean up unused or unusable files from the repository'''
hostmatcher = re.compile('.*\.H_(\S+)$')
repo = get_repo_path()
score = ([], [])
# clean up unresolvable hosts in SSHbase
for name in os.listdir("%s/SSHbase" % (repo)):
if not hostmatcher.match(name):
print "could not match name %s" % (name)
else:
hostname = hostmatcher.match(name).group(1)
if hostname in score[0] + score[1]:
continue
try:
socket.gethostbyname(hostname)
score[0].append(hostname)
except:
print "could not resolve %s" % (hostname)
score[1].append(hostname)
for name in os.listdir("%s/SSHbase" % (repo)):
if not hostmatcher.match(name):
print "could not match name %s" % (name)
else:
if hostmatcher.match(name).group(1) in score[1]:
if '-f' in args:
os.unlink("%s/SSHbase/%s" % (repo, name))
else:
answer = raw_input("Unlink file %s? [yN] " % name)
if answer in ['y', 'Y']:
os.unlink("%s/SSHbase/%s" % (repo, name))
# clean up file~
# clean up files without parsable names in Cfg
def do_viz(args):
'''Build visualization of groups file'''
if '-R' in args:
repo = args[args.index('-R') + 1]
elif '-C' in args:
repo = get_repo_path(args[args.index('-C') + 1])
else:
repo = get_repo_path()
groupdata = lxml.etree.parse(repo + '/Metadata/groups.xml')
groupdata.xinclude()
groups = groupdata.getroot()
if '-r' in args:
dotpipe = popen2.Popen4("dd bs=4M 2>/dev/null")
else:
dotpipe = popen2.Popen4("dot -Tpng")
categories = {'default':'grey83'}
instances = {}
for group in groups.findall('.//Groups/Group'):
if group.get('category', False):
if not categories.has_key(group.get('category')):
categories[group.get('category')] = colors.pop()
try:
dotpipe.tochild.write("digraph groups {\n")
except:
print "write to dot process failed. Is graphviz installed?"
raise SystemExit, 1
dotpipe.tochild.write('\trankdir="LR";\n')
if '-h' in args:
clients = lxml.etree.parse(repo + '/Metadata/clients.xml').getroot()
for client in clients.findall('Client'):
if instances.has_key(client.get('profile')):
instances[client.get('profile')].append(client.get('name'))
else:
instances[client.get('profile')] = [client.get('name')]
for profile, clist in instances.iteritems():
clist.sort()
dotpipe.tochild.write('''\t"%s-instances" [ label="%s", shape="record" ];\n''' % (profile, '|'.join(clist)))
dotpipe.tochild.write('''\t"%s-instances" -> "group-%s";\n''' % (profile, profile))
if '-b' in args:
bundles = []
[bundles.append(bund.get('name')) for bund in groups.findall('.//Bundle')
if bund.get('name') not in bundles]
bundles.sort()
for bundle in bundles:
dotpipe.tochild.write('''\t"bundle-%s" [ label="%s", shape="septagon"];\n''' % (bundle, bundle))
gseen = []
for group in groups.findall('.//Groups/Group'):
color = categories[group.get('category', 'default')]
if group.get('profile', 'false') == 'true':
style="filled, bold"
else:
style = "filled"
gseen.append(group.get('name'))
dotpipe.tochild.write('\t"group-%s" [label="%s", style="%s", fillcolor=%s];\n' %
(group.get('name'), group.get('name'), style, color))
if '-b' in args:
for bundle in group.findall('Bundle'):
dotpipe.tochild.write('\t"group-%s" -> "bundle-%s";\n' %
(group.get('name'), bundle.get('name')))
for group in groups.findall('.//Groups/Group'):
for parent in group.findall('Group'):
if parent.get('name') not in gseen:
dotpipe.tochild.write('\t"group-%s" [label="%s", style="filled", fillcolor="grey83"];\n' %
(parent.get('name'), parent.get('name')))
gseen.append(parent.get("name"))
dotpipe.tochild.write('\t"group-%s" -> "group-%s" ;\n' %
(group.get('name'), parent.get('name')))
if '-k' in args:
dotpipe.tochild.write("\tsubgraph cluster_key {\n")
dotpipe.tochild.write('''\tstyle="filled";\n''')
dotpipe.tochild.write('''\tcolor="lightblue";\n''')
dotpipe.tochild.write('''\tBundle [ shape="septagon" ];\n''')
dotpipe.tochild.write('''\tGroup [shape="ellipse"];\n''')
dotpipe.tochild.write('''\tProfile [style="bold", shape="ellipse"];\n''')
dotpipe.tochild.write('''\tHblock [label="Host1|Host2|Host3", shape="record"];\n''')
dotpipe.tochild.write('''\tlabel="Key";\n''')
dotpipe.tochild.write("\t}\n")
dotpipe.tochild.write("}\n")
dotpipe.tochild.close()
data = dotpipe.fromchild.read()
if '-o' in args:
output = open(args[args.index('-o') + 1], 'w').write(data)
else:
print data
if __name__ == '__main__':
Bcfg2.Logging.setup_logging('bcfg2-admin', to_console=True)
if sys.argv[1] == "init":
initialize_repo()
elif sys.argv[1] == 'pull':
if len(sys.argv) != 5:
print usage
raise SystemExit, 1
do_pull(sys.argv[2], sys.argv[3], sys.argv[4])
elif sys.argv[1] == 'minestruct':
do_minestruct(sys.argv[2:])
elif sys.argv[1] == 'tidy':
do_tidy(sys.argv[2:])
elif sys.argv[1] == 'viz':
do_viz(sys.argv[2:])
else:
print usage
|