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
|
#!/usr/bin/env python
# $Id: $
from getopt import getopt, GetoptError
from socket import gethostbyaddr, herror
from string import split
from syslog import syslog, LOG_INFO, LOG_ERR
from sys import argv, exit, exc_info
from time import time
from traceback import extract_tb
from ConfigParser import ConfigParser
from elementtree.ElementTree import Element, tostring
from Bcfg2.Server.Core import Core
from Bcfg2.Server.Metadata import MetadataStore
from sss.daemonize import daemonize
from sss.restriction import DataSet, Data
from sss.server import Server
def dgetopt(arglist, opt, vopt):
r = {}
for o in opt.values() + vopt.values():
r[o] = False
gstr = join(opt.keys()) + join([x+':' for x in vopt.keys()])
try:
(o, a) = getopt(arglist, gstr)
except GetoptError, g:
print g
print "bcfg2 Usage:"
for (k,v) in opt.iteritems():
print " -%s %s"%(k,v)
for (k,v) in vopt.iteritems():
print " -%s <%s>"%(k,v)
exit(1)
for (gopt,garg) in o:
option = gopt[1:]
if opt.has_key(option):
r[opt[option]] = True
else:
r[vopt[option]] = garg
return r
class BcfgServer(Server):
__implementation__ = 'Bcfg2'
__component__ = 'bcfg2'
__dispatch__ = {'get-config':'BuildConfig', 'get-probes':'GetProbes', 'probe-data':'CommitProbeData', 'upload-statistics':'HandleStats'}
__validate__ = 0
def __setup__(self):
self.setup = self.kwargs['setup']
c = ConfigParser()
c.read([self.kwargs.get('configfile', '/etc/bcfg2.conf')])
repo = c.get('server','repository')
generators = split(c.get('server','generators'),',')
structures = split(c.get('server', 'structures'),',')
mpath = c.get('server','metadata')
self.core = Core(repo, structures, generators)
self.metadata = MetadataStore("%s/metadata.xml"%(mpath), self.core.fam)
self.__progress__()
def __progress__(self):
while self.core.fam.fm.pending():
self.core.fam.HandleEvent()
try:
self.core.RunCronTasks()
except:
self.LogFailure("Cron")
return 0
def BuildConfig(self, xml, (peer,port)):
if setup['client']:
client = setup['client']
else:
try:
client = gethostbyaddr(peer)[0].split('.')[0]
except herror:
return Element("error", type='host resolution error')
t = time()
config = Element("Configuration", version='2.0')
# get metadata for host
if xml.attrib.has_key("profile") and xml.attrib.has_key("image"):
m = self.metadata.FetchMetadata(client, image=xml.attrib['image'], profile=xml.attrib['profile'])
else:
m = self.metadata.FetchMetadata(client)
try:
structures = self.core.GetStructures(m)
except:
self.LogFailure("GetStructures")
return Element("error", type='structure error')
for s in structures:
try:
self.core.BindStructure(s, m)
config.append(s)
except:
self.LogFailure("BindStructure")
#for x in s.getchildren():
# print x.attrib['name'], '\000' in tostring(x)
syslog(LOG_INFO, "Generated config for %s in %s seconds"%(client, time()-t))
return config
def GetProbes(self, xml, (peer,port)):
r = Element('probes')
try:
client = gethostbyaddr(peer)[0].split('.')[0]
except herror:
return Element("error", type='host resolution error')
m = self.GetMetadata(client)
for g in self.core.generators:
for p in g.GetProbes(m):
r.append(p)
return r
def CommitProbeData(self, xml, (peer,port)):
try:
client = gethostbyaddr(peer)[0].split('.')[0]
except herror:
return Element("error", type='host resolution error')
for data in xml.findall(".//probe-data"):
try:
[g] = [x for x in self.core.generators if x.__name__ == data.attrib['source']]
g.AcceptProbeData(client, data)
except:
self.LogFailure("CommitProbeData")
return Element("OK")
def HandleStats(self, xml, (peer, port)):
syslog(LOG_INFO, "Client %s reported state %s"%(peer, xml.attrib['state']))
return Element("ok")
def LogFailure(self, failure):
(t,v,tb)=exc_info()
syslog(LOG_ERR, "Unexpected failure in %s"%(failure))
for line in extract_tb(tb):
syslog(LOG_ERR, ' File "%s", line %i, in %s\n %s\n'%line)
syslog(LOG_ERR, "%s: %s\n"%(t,v))
del t,v,tb
if __name__ == '__main__':
options = {'v':'verbose','d':'debug'}
doptions = {'D':'daemon', 'C':'client'}
setup = dgetopt(argv[1:], options, doptions)
print setup
if setup['daemon']:
daemonize(setup['daemon'])
server = BcfgServer(setup=setup)
for i in range(10):
server.__progress__()
server.ServeForever()
|