source: vanHelsing/trunk/src/bcfg.py@ 819

Last change on this file since 819 was 816, checked in by slederer, on Jan 15, 2010 at 4:56:01 PM

Aufräumen, Funktionen zum Filtern nach Ressourcen-Typ

  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-python
File size: 6.8 KB
RevLine 
[798]1#!/usr/bin/python
[775]2# -*- coding: utf-8 -*-
3"""Classes and functions for configuration file handling
4"""
5
6import sys
7import re
[798]8import resource
9import directive
[813]10import random
[775]11
12RESOURCE_TYPES = ('dird', 'console', 'filed', 'stored')
13
[799]14rxp_item = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=(.*)') # xxx = xxx matchen
[816]15rxp_item2 = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=\\s*{(.*)}\\s*') # match type = { a = 1; b= 2}
[799]16rxp_openbrace = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=?\\s*\\{\\s*') # match xxx {
[815]17rxp_open = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*$') # match xxx
[799]18rxp_closebrace = re.compile('[^{]*}.*') # match }
[798]19rxp_comment = re.compile('^\s*#.*')
20
21
22
[775]23#'dird', 'console', 'filed' or 'stored'
[798]24class Config(object):
[775]25 """Class for bacula configuration access"""
26
27 def __init__(self, resource_type, filename=""):
28 self.resource_type = resource_type
29 self.filename = filename
[812]30 self.resources = resource.Root(level=0)
[816]31 self.resources.type="<root>"
[775]32
33 def read(self):
[799]34 self.parse(self.resources,open(self.filename,"r"),0)
35 #print self.resources
[798]36
[812]37 def write(self, filename=None):
38 if(filename==None):
39 filename=self.filename
40 f=open(filename,"w")
41 f.write(str(self.getRoot()))
42 f.close()
[799]43
44 def getRoot(self):
45 return self.resources
46
[815]47 def addResource(self,resname,level,curRes):
48 try:
49 resClass = getattr(resource,resname);
50 except:
51 resClass = resource.Resource
52
53 newRes=resClass(level+1)
[816]54 newRes.type=resname
[815]55 curRes.add_item(newRes);
56 return newRes
57
[799]58 def parse(self,curRes,f,level):
[816]59 #print "START",curRes.type
[799]60
61 while True:
62 line=f.readline()
63 if not line:
64 break
65
66 inlinecomment=None
67
68 commentStart=line.find("#")
69 if commentStart!=-1:
70 inlinecomment=line[commentStart:]
71 line=line[:commentStart].strip()
72 #curRes.add_comment(inlinecomment)
73
[813]74 include=line.strip()
75 if include.startswith("@"):
76 includepath=include[1:].strip()
77 print "include " +includepath
78 self.parse(curRes,open(includepath,"r"),level)
[812]79
[799]80 if rxp_closebrace.match(line):
81 #print "closebraceline"
82 break
83
84 item2 = rxp_item2.match(line)
85 if item2:
86 #print "item2"
87 name = item2.group(1)
88 value = item2.group(2)
89 #print "item:",name,value
90 newRes=resource.Resource(level+1)
[816]91 newRes.type=name
[799]92 newRes.value="{"+value+"}"
93 curRes.add_item(newRes)
[798]94 continue
[799]95
[798]96 openbraceline = rxp_openbrace.match(line)
97 if openbraceline:
[799]98 #print "openbraceline"
99 resname = openbraceline.group(1)
[815]100 newRes=self.addResource(resname,level,curRes)
101 self.parse(newRes,f,level+1);
102 continue
[798]103
[815]104 open = rxp_open.match(line)
105 if open:
106 resname = open.group(1)
107 newRes=self.addResource(resname,level,curRes)
[799]108 self.parse(newRes,f,level+1);
109 continue
110
111 item = rxp_item.match(line)
112 if item:
113 name = item.group(1)
[812]114 value = item.group(2).strip()
[799]115 #print "item:",name,value
116 newRes=resource.Resource(level+1)
[816]117 newRes.type=name
[799]118 newRes.value=value
[798]119 curRes.add_item(newRes)
[799]120 continue
121
122 #print "END",curRes.name
[798]123
[775]124class DirdConfig(Config):
[798]125
126 def __init__(self, filename=""):
127 Config.__init__(self, 'dird', filename)
128
[775]129class ConsoleConfig(Config):
130 pass
131
132class FiledConfig(Config):
[813]133 def __init__(self, filename=""):
134 Config.__init__(self, 'fd', filename)
[775]135
[813]136 def setDefaults(self,directorName,directorPassword,myName):
137 director=resource.Director(1,"Director")
138 director.add("Name",directorName)
139 director.add("Password",'"'+directorPassword+'"')
140 self.getRoot().add_item(director)
141
142 fileDaemon=resource.FileDaemon(1,"FileDaemon")
143 fileDaemon.add("Name",myName)
144 #fileDaemon.add("FDport","9102")
145 fileDaemon.add("WorkingDirectory","/var/lib/bacula")
146 fileDaemon.add("Pid Directory","/var/run")
147 fileDaemon.add("Maximum Concurrent Jobs","20")
148 self.getRoot().add_item(fileDaemon)
149
150 messages=resource.Messages(1,"Messages")
151 messages.add("Name","Standard")
152 messages.add("director",directorName+" = all, !skipped, !restored")
153 self.getRoot().add_item(messages)
154
[775]155class StoredConfig(Config):
156 pass
157
[813]158def genpasswd(len=32):
159 charset="01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!/%()=?@"
160 password=""
161 for a in xrange(0,len):
162 password += random.choice(charset)
163 return password
[815]164
[816]165
166def createClientConfig(dirdcfg,clientName,clientPassword=genpasswd()):
167 director=dirdcfg.getRoot().getByType("Director")[0]
168 dirdName=director.getName()
169 r=dirdcfg.getRoot().getByName(clientName)
170 if len(r)==1:
171 client=r[0]
172 else:
173 return
174
175 filename=clientName+".conf"
176 fdcfg=FiledConfig(filename)
177 fdcfg.setDefaults(dirdName,clientPassword,clientName)
178 fdcfg.write()
179
[815]180def createAllClientConfigs(dirdcfg):
[816]181 clients=dirdcfg.getRoot().getByType("Client",1)
182 director=dirdcfg.getRoot().getByType("Director")[0]
[815]183 dirdName=director.getName()
184
185 for c in clients:
[816]186 print c
[815]187 clientName=c.getName()
188 if clientName!=None:
[816]189 r=c.getByType("Password")
[815]190 if len(r)>0:
191 clientPassword=r[0].value
192 clientPassword=clientPassword.replace('"','')
193 else:
194 clientPassword=""
195 filename=clientName+".conf"
196 fdcfg=FiledConfig(filename)
197 fdcfg.setDefaults(dirdName,clientPassword,clientName)
198 fdcfg.write()
199
[813]200
[798]201if __name__ == "__main__":
[813]202 if len(sys.argv) < 2:
203 filename="test.conf"
204 else:
205 filename=sys.argv[1]
[816]206
[813]207 dirdcfg = DirdConfig(filename)
[798]208 dirdcfg.read()
[799]209
[816]210 root=dirdcfg.getRoot()
211
[799]212 job=resource.Job(1,"Job")
[816]213 job.setDefaults("test2-job","test2","testdefs","Full Set")
[799]214
215 root.add_item(job)
216
[813]217 pwd=genpasswd()
[816]218 newclient=resource.Client(1,"Client")
219 newclient.setDefaults("test-fd","testclient","MyCatalog",pwd)
[812]220 root.add_item(newclient)
221
222 dirdcfg.write("test.conf.out")
[813]223
224 fdcfg=FiledConfig("test-fd.conf")
225 fdcfg.setDefaults("bacula-dird",pwd,"test-fd")
226 fdcfg.write()
227
[816]228
229 createClientConfig(dirdcfg,"gaffel-fd")
230 createClientConfig(dirdcfg,"test-fd")
231
[815]232 createAllClientConfigs(dirdcfg)
[798]233 sys.exit(0)
Note: See TracBrowser for help on using the repository browser.