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