1 | #!/usr/bin/python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | """Classes and functions for configuration file handling
|
---|
4 | """
|
---|
5 |
|
---|
6 | import sys
|
---|
7 | import re
|
---|
8 | import resource
|
---|
9 | import directive
|
---|
10 |
|
---|
11 |
|
---|
12 | RESOURCE_TYPES = ('dird', 'console', 'filed', 'stored')
|
---|
13 |
|
---|
14 | rxp_item = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=(.*)') # xxx = xxx matchen
|
---|
15 | rxp_item2 = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=\\s*{(.*)}\\s*') # match type = { a = 1; b= 2}
|
---|
16 | rxp_openbrace = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*\\=?\\s*\\{\\s*') # match xxx {
|
---|
17 | rxp_open = re.compile('^\\s*(\\w[\\w ]*\\w+)\\s*$') # match xxx
|
---|
18 | rxp_closebrace = re.compile('[^{]*}.*') # match }
|
---|
19 | rxp_comment = re.compile('^\s*#.*')
|
---|
20 |
|
---|
21 |
|
---|
22 | currentConfig = None
|
---|
23 |
|
---|
24 | #'dird', 'console', 'filed' or 'stored'
|
---|
25 | class Config(object):
|
---|
26 | """Class for bacula configuration access"""
|
---|
27 |
|
---|
28 | def __init__(self, resource_type, filename=""):
|
---|
29 | self.resource_type = resource_type
|
---|
30 | self.filename = filename
|
---|
31 | self.resources = resource.Root(level=0)
|
---|
32 | self.resources.type="<root>"
|
---|
33 |
|
---|
34 | def read(self):
|
---|
35 | self.parse(self.resources,open(self.filename,"r"),0)
|
---|
36 | #print self.resources
|
---|
37 |
|
---|
38 | def write(self, filename=None):
|
---|
39 | if(filename==None):
|
---|
40 | filename=self.filename
|
---|
41 | f=open(filename,"w")
|
---|
42 | f.write(str(self.getRoot()))
|
---|
43 | f.close()
|
---|
44 |
|
---|
45 | def getRoot(self):
|
---|
46 | return self.resources
|
---|
47 |
|
---|
48 | def addResource(self,resname,level,curRes):
|
---|
49 | try:
|
---|
50 | resClass = getattr(resource,resname);
|
---|
51 | except:
|
---|
52 | resClass = resource.Resource
|
---|
53 |
|
---|
54 | newRes=resClass(level+1)
|
---|
55 | newRes.type=resname
|
---|
56 | curRes.add_item(newRes);
|
---|
57 | return newRes
|
---|
58 |
|
---|
59 | def parse(self,curRes,f,level):
|
---|
60 | #print "START",curRes.type
|
---|
61 |
|
---|
62 | while True:
|
---|
63 | line=f.readline()
|
---|
64 | if not line:
|
---|
65 | break
|
---|
66 |
|
---|
67 | inlinecomment=None
|
---|
68 |
|
---|
69 | commentStart=line.find("#")
|
---|
70 | if commentStart!=-1:
|
---|
71 | inlinecomment=line[commentStart:]
|
---|
72 | line=line[:commentStart].strip()
|
---|
73 | #curRes.add_comment(inlinecomment)
|
---|
74 |
|
---|
75 | include=line.strip()
|
---|
76 | if include.startswith("@"):
|
---|
77 | includepath=include[1:].strip()
|
---|
78 | print "include " +includepath
|
---|
79 | self.parse(curRes,open(includepath,"r"),level)
|
---|
80 |
|
---|
81 | if rxp_closebrace.match(line):
|
---|
82 | #print "closebraceline"
|
---|
83 | break
|
---|
84 |
|
---|
85 | item2 = rxp_item2.match(line)
|
---|
86 | if item2:
|
---|
87 | #print "item2"
|
---|
88 | name = item2.group(1)
|
---|
89 | value = item2.group(2)
|
---|
90 | #print "item:",name,value
|
---|
91 | newRes=resource.Resource(level+1)
|
---|
92 | newRes.type=name
|
---|
93 | newRes.value="{"+value+"}"
|
---|
94 | curRes.add_item(newRes)
|
---|
95 | continue
|
---|
96 |
|
---|
97 | openbraceline = rxp_openbrace.match(line)
|
---|
98 | if openbraceline:
|
---|
99 | #print "openbraceline"
|
---|
100 | resname = openbraceline.group(1)
|
---|
101 | newRes=self.addResource(resname,level,curRes)
|
---|
102 | self.parse(newRes,f,level+1);
|
---|
103 | continue
|
---|
104 |
|
---|
105 | open = rxp_open.match(line)
|
---|
106 | if open:
|
---|
107 | resname = open.group(1)
|
---|
108 | newRes=self.addResource(resname,level,curRes)
|
---|
109 | self.parse(newRes,f,level+1);
|
---|
110 | continue
|
---|
111 |
|
---|
112 | item = rxp_item.match(line)
|
---|
113 | if item:
|
---|
114 | name = item.group(1)
|
---|
115 | value = item.group(2).strip()
|
---|
116 | #print "item:",name,value
|
---|
117 | newRes=resource.Resource(level+1)
|
---|
118 | newRes.type=name
|
---|
119 | newRes.value=value
|
---|
120 | curRes.add_item(newRes)
|
---|
121 | continue
|
---|
122 |
|
---|
123 | #print "END",curRes.name
|
---|
124 |
|
---|
125 | class DirdConfig(Config):
|
---|
126 |
|
---|
127 | def __init__(self, filename=""):
|
---|
128 | Config.__init__(self, 'dird', filename)
|
---|
129 |
|
---|
130 | class ConsoleConfig(Config):
|
---|
131 | pass
|
---|
132 |
|
---|
133 | class FiledConfig(Config):
|
---|
134 | def __init__(self, filename=""):
|
---|
135 | Config.__init__(self, 'fd', filename)
|
---|
136 |
|
---|
137 | def setDefaults(self,directorName,directorPassword,myName):
|
---|
138 | director=resource.Director(1,"Director")
|
---|
139 | director.add("Name",directorName)
|
---|
140 | director.add("Password",'"'+directorPassword+'"')
|
---|
141 | self.getRoot().add_item(director)
|
---|
142 |
|
---|
143 | fileDaemon=resource.FileDaemon(1,"FileDaemon")
|
---|
144 | fileDaemon.add("Name",myName)
|
---|
145 | #fileDaemon.add("FDport","9102")
|
---|
146 | fileDaemon.add("WorkingDirectory","/var/lib/bacula")
|
---|
147 | fileDaemon.add("Pid Directory","/var/run")
|
---|
148 | fileDaemon.add("Maximum Concurrent Jobs","20")
|
---|
149 | self.getRoot().add_item(fileDaemon)
|
---|
150 |
|
---|
151 | messages=resource.Messages(1,"Messages")
|
---|
152 | messages.add("Name","Standard")
|
---|
153 | messages.add("director",directorName+" = all, !skipped, !restored")
|
---|
154 | self.getRoot().add_item(messages)
|
---|
155 |
|
---|
156 | class StoredConfig(Config):
|
---|
157 | pass
|
---|
158 |
|
---|
159 | #
|
---|
160 | # nur zum Testen
|
---|
161 | from VanHelsingSOAP import *
|
---|
162 |
|
---|
163 | if __name__ == "__main__":
|
---|
164 | if len(sys.argv) < 2:
|
---|
165 | filename="test.conf"
|
---|
166 | else:
|
---|
167 | filename=sys.argv[1]
|
---|
168 |
|
---|
169 | loadDirectorConfig(filename)
|
---|
170 |
|
---|
171 | print getClients("bacula-dir")
|
---|
172 | print getResources("bacula-dir","Client")
|
---|
173 | print getResources("bacula-dir","Job")
|
---|
174 | print getResourceById("bacula-dir",100)
|
---|
175 | print getResourceByName("Bacula-dir","Client","cryptoserver-fd")
|
---|
176 | sys.exit(0)
|
---|