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
|
'''This module implements a config file repository'''
__revision__ = '$Revision$'
from binascii import b2a_base64
from os import stat
from re import compile as regcompile
from stat import S_ISDIR, ST_MODE
from syslog import syslog, LOG_INFO
from Bcfg2.Server.Generator import Generator, DirectoryBacked, FileBacked
from Bcfg2.Server.Metadata import Metadata
class CfgFileException(Exception):
'''Raised for repository errors'''
pass
class FileEntry(FileBacked):
'''The File Entry class pertains to the config files contained in a particular directory.
This includes :info, all base files and deltas'''
def __init__(self, name, metadata):
FileBacked.__init__(self, name)
self.metadata = metadata
def Applies(self, other):
'''redirect to metadata.Applies'''
return self.metadata.Applies(other)
def __cmp__(self, other):
'''figure out if self is more or less specific than other'''
return self.metadata.__cmp__(other.metadata)
class ConfigFileEntry(object):
'''ConfigFileEntry is a repository entry for a single file, containing
all data for all clients.'''
mx = regcompile("(^(?P<filename>.*)(\.((B(?P<bprio>\d+)_(?P<bundle>\S+))|(A(?P<aprio>\d+)_(?P<attr>\S+))|(I(?P<iprio>\d+)_(?P<image>\S+))|(I(?P<cprio>\d+)_(?P<class>\S+))|(H_(?P<hostname>\S+)))(\.(?P<op>cat|udiff))?)?$)")
info = regcompile('^owner:(\s)*(?P<owner>\w+)|group:(\s)*(?P<group>\w+)|perms:(\s)*(?P<perms>\w+)|encoding:(\s)*(?P<encoding>\w+)|(?P<paranoid>paranoid(\s)*)$')
def __init__(self, path):
object.__init__(self)
self.path = path
self.basefiles = []
self.deltas = []
self.encoding = 'ascii'
self.owner = 'root'
self.group = 'root'
self.perms = '0644'
self.paranoid = False
def read_info(self, filename):
'''read in :info metadata'''
for line in open(filename).readlines():
match = self.info.match(line)
if not match:
continue
else:
mgd = match.groupdict()
if mgd['owner']:
self.owner = mgd['owner']
elif mgd['group']:
self.group = mgd['group']
elif mgd['encoding']:
self.encoding = mgd['encoding']
elif mgd['perms']:
self.perms = mgd['perms']
if len(self.perms) == 3:
self.perms = "0%s" % (self.perms)
elif mgd['paranoid']:
self.paranoid = True
def AddEntry(self, name):
'''add new file additions for a single cf file'''
if name[-5:] == ':info':
return self.read_info(name)
g = self.mx.match(name)
if g == None:
print "match failed for file name %s" % (name)
return
data = {}
for attr in ['bundle', 'attr', 'hostname', 'class']:
if g.group(attr) != None:
data[attr] = g.group(attr)
if data == {}:
all = True
else:
all = False
# metadata args (global, image, classes, bundles, attributes, hostname)
arg = (all, data.get('image', None))
for mtype in ['class', 'bundle', 'attr']:
arg = arg + (data.get(mtype, []),)
arg = arg + (data.get('hostname', None),)
m = apply(Metadata, arg)
if g.group("op") != None:
self.deltas.append(FileEntry(name, m))
# need to sort here
else:
self.basefiles.append(FileEntry(name, m))
# need to sort here
def HandleEvent(self, event):
'''Handle FAM updates'''
action = event.code2str()
if event.filename[-5:] == ':info':
return self.read_info(event.filename)
for l in [self.basefiles, self.deltas]:
for entry in l:
if entry.name.split('/')[-1] == event.filename:
if action == 'changed':
entry.HandleEvent(event)
elif action == 'deleted':
l.remove(entry)
else:
print "unhandled action %s" % (action)
def GetConfigFile(self, entry, metadata):
'''Fetch config file from repository'''
name = entry.attrib['name']
filedata = ""
# first find basefile
try:
basefile = [x for x in self.basefiles if x.Applies(metadata)][0]
except IndexError:
raise CfgFileException, ('basefile', name)
filedata += basefile.data
# find applicable deltas
deltas = [x for x in self.deltas if x.Applies(metadata)]
# filter for more specific
for delta in deltas:
pass
# apply diffs, etc
entry.attrib.update({'owner':self.owner, 'group':self.group,
'perms':self.perms, 'encoding':self.encoding})
if self.paranoid:
entry.attrib['paranoid'] = 'true'
if self.encoding == 'base64':
entry.text = b2a_base64(filedata)
else:
entry.text = filedata
class ConfigFileRepository(DirectoryBacked):
'''This class implements repos and all change handling'''
def __init__(self, name, fam):
self.name = name
self.fam = fam
self.entries = {}
self.provides = {}
self.famID = {}
self.directories = []
self.AddDirectoryMonitor(self.name)
# eventually flush fam events here so that all entries built here
# ready to go
def AddDirectoryMonitor(self, name):
'''Add new directory to FAM structures'''
if name not in self.directories:
try:
stat(name)
except OSError:
print "Failed to open %s" % (name)
return
reqid = self.fam.AddMonitor(name, self)
self.famID[reqid] = name
self.directories.append(name)
def AddEntry(self, name):
'''Add new entry to FAM structures'''
try:
sdata = stat(name)[ST_MODE]
except OSError:
return
if S_ISDIR(sdata):
self.AddDirectoryMonitor(name)
else:
# file entries shouldn't contain path-to-repo
shortname = '/'+ '/'.join(name[len(self.name)+1:].split('/')[:-1])
if not self.entries.has_key(shortname):
self.entries[shortname] = ConfigFileEntry(shortname)
self.provides[shortname] = self.entries[shortname].GetConfigFile
self.entries[shortname].AddEntry(name)
#self.entries[shortname].HandleEvent()
def HandleEvent(self, event):
'''Handle FAM updates'''
action = event.code2str()
if event.filename[0] != '/':
filename = "%s/%s" % (self.famID[event.requestID], event.filename)
else:
filename = event.filename
if action == 'exists':
if filename != self.name:
self.AddEntry(filename)
elif action == 'created':
self.AddEntry(filename)
elif action == 'changed':
# pass the event down the chain to the ConfigFileEntry
configfile = filename[len(self.name):-(len(event.filename)+1)]
if event.filename == ':info':
event.filename = filename
if self.entries.has_key(configfile):
self.entries[configfile].HandleEvent(event)
else:
syslog(LOG_INFO, "Ignoring event for %s"%(configfile))
elif action == 'deleted':
configfile = filename[len(self.name):-(len(event.filename)+1)]
self.entries[configfile].HandleEvent(event)
elif action in ['endExist']:
pass
else:
print "Got unknown event %s %s %s" % (event.requestID, event.code2str(), event.filename)
class cfg(Generator):
'''This generator manages the configuration file repository for bcfg2'''
__name__ = 'cfg'
__version__ = '$Id$'
__author__ = 'bcfg-dev@mcs.anl.gov'
__provides__ = {}
def __setup__(self):
self.repo = ConfigFileRepository(self.data, self.core.fam)
self.__provides__['ConfigFile'] = self.repo.provides
|