[953] | 1 | '''
|
---|
| 2 | Resource Objects
|
---|
| 3 | Created on 30.06.2009
|
---|
| 4 |
|
---|
| 5 | @author: hmueller
|
---|
| 6 | '''
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | # $Id: resource.py 10982 2010-09-15 11:02:25Z pstorz $
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | # spezielle Ressources:
|
---|
| 13 | # RunScript
|
---|
| 14 | # FileSet
|
---|
| 15 | # Schedule
|
---|
| 16 | #
|
---|
| 17 |
|
---|
| 18 | from nosferatu.config_classes import *
|
---|
| 19 | import re
|
---|
| 20 |
|
---|
| 21 | # possible ressourcenames
|
---|
| 22 | bacResourceNames = ['director', 'schedule', 'storage', 'fileset', 'client', 'pool', 'jobdefs', 'device', 'job']
|
---|
| 23 |
|
---|
| 24 | class Resource(object):
|
---|
| 25 | itemRegEx = re.compile('\s*([\S ]+)\s*=\s*(.*)$')
|
---|
| 26 | braceRegEx = re.compile('.*[{}].*')
|
---|
| 27 | closeBraceRegEx = re.compile('.*}.*')
|
---|
| 28 | openBraceRegEx = re.compile('.*{.*')
|
---|
| 29 |
|
---|
| 30 | ''' Parse Lines of Configuration'''
|
---|
| 31 | def __init__(self, configlines, name):
|
---|
| 32 | self.name = name
|
---|
| 33 | self.items = []
|
---|
| 34 | self.comments = []
|
---|
| 35 | #print self.name
|
---|
| 36 | for line in configlines:
|
---|
| 37 | itemLine = self.itemRegEx.match(line)
|
---|
| 38 | braceLine = self.braceRegEx.match(line)
|
---|
| 39 | if braceLine:
|
---|
| 40 | print "Braces found; special treatment needed"
|
---|
| 41 | print "this should be covered by special constructors in derivered classes"
|
---|
| 42 | print line
|
---|
| 43 | elif itemLine:
|
---|
| 44 | self.add_item_name_value(itemLine.group(1).lower(), itemLine.group(2))
|
---|
| 45 | else:
|
---|
| 46 | print "ERROR: special Line:", line
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | #self.DIRECTIVE = ''
|
---|
| 50 |
|
---|
| 51 | def __str__(self):
|
---|
| 52 | s = "\n".join(self.comments)
|
---|
| 53 | s += self.DIRECTIVE + " {\n"
|
---|
| 54 | if self.name:
|
---|
| 55 | s += " " + "Name = " + self.name + "\n"
|
---|
| 56 | for d in self.items:
|
---|
| 57 | s += " " + str(d) + "\n"
|
---|
| 58 | s += "}\n"
|
---|
| 59 | return s
|
---|
| 60 |
|
---|
| 61 | def add_comment(self, comment):
|
---|
| 62 | if not comment.lstrip().startswith("#"):
|
---|
| 63 | comment = "# " + comment
|
---|
| 64 | self.comments.append(comment)
|
---|
| 65 |
|
---|
| 66 | def add_item_name_value (self, Name, Value):
|
---|
| 67 | resItem = ResourceItem(Name,Value)
|
---|
| 68 | print Name
|
---|
| 69 | self.items.append(resItem)
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | def add_item (self,item):
|
---|
| 73 | self.items.append(item)
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | class Director(Resource):
|
---|
| 77 | #
|
---|
| 78 | #DirAddresses = {
|
---|
| 79 | #
|
---|
| 80 | dirAdrRegEx = re.compile('\s*diraddresses\s*=\s*{(.*)',re.I)
|
---|
| 81 |
|
---|
| 82 | def __init__(self, configlines,name):
|
---|
| 83 | # treat special config options
|
---|
| 84 | inDirAddress = False
|
---|
| 85 | dirAddrLines = []
|
---|
| 86 | braceLevel = 0;
|
---|
| 87 | # TODO: Evtl dies ebenfalls im Hauptprogramm machen und dann nur die
|
---|
| 88 | # referenzen eintragen?
|
---|
| 89 | for line in configlines:
|
---|
| 90 | print 'LINE:' + line,
|
---|
| 91 | #print ">",braceLevel, line,"<"
|
---|
| 92 | dirAdrStart = self.dirAdrRegEx.match(line)
|
---|
| 93 | braceLine = self.braceRegEx.match(line)
|
---|
| 94 | closeBrace = self.closeBraceRegEx.match(line)
|
---|
| 95 | openBrace = self.openBraceRegEx.match(line)
|
---|
| 96 |
|
---|
| 97 | if closeBrace:
|
---|
| 98 | print "closebrace found"
|
---|
| 99 | braceLevel -= 1
|
---|
| 100 | if inDirAddress & braceLevel == 2:
|
---|
| 101 | print "inDirAddress end"
|
---|
| 102 | inDirAddress = False
|
---|
| 103 | if openBrace:
|
---|
| 104 | print "openBrace found"
|
---|
| 105 | braceLevel += 1
|
---|
| 106 | if inDirAddress:
|
---|
| 107 | print "inDirAddress"
|
---|
| 108 | dirAddrLines.append(line)
|
---|
| 109 | configlines.remove(line)
|
---|
| 110 | if dirAdrStart:
|
---|
| 111 | print "DirAddress Start found"
|
---|
| 112 | inDirAddress = True
|
---|
| 113 | dirAddrLines.append(line)
|
---|
| 114 | configlines.remove(line)
|
---|
| 115 |
|
---|
| 116 | Resource.__init__(self,myconfiglines,name)
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | # Name = bacula-dir
|
---|
| 120 | # DirAddresses = {
|
---|
| 121 | # ip = { addr = 1.2.3.4; port = 1205;}
|
---|
| 122 | # ipv4 = {
|
---|
| 123 | # addr = 1.2.3.4; port = http;}
|
---|
| 124 | # ipv6 = {
|
---|
| 125 | # addr = 1.2.3.4;
|
---|
| 126 | # port = 1205;
|
---|
| 127 | # }
|
---|
| 128 | # ip = {
|
---|
| 129 | # addr = 1.2.3.4
|
---|
| 130 | # port = 1205
|
---|
| 131 | # }
|
---|
| 132 | # ip = { addr = 1.2.3.4 }
|
---|
| 133 | # ip = { addr = 201:220:222::2 }
|
---|
| 134 | # ip = {
|
---|
| 135 | # addr = bluedot.thun.net
|
---|
| 136 | # }
|
---|
| 137 | #}
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | class Job(Resource):
|
---|
| 141 | pass
|
---|
| 142 |
|
---|
| 143 | class JobDefs(Resource):
|
---|
| 144 | pass
|
---|
| 145 |
|
---|
| 146 | class Schedule(Resource):
|
---|
| 147 | pass
|
---|
| 148 |
|
---|
| 149 | class FileSet(Resource):
|
---|
| 150 | pass
|
---|
| 151 |
|
---|
| 152 | class Client(Resource):
|
---|
| 153 | pass
|
---|
| 154 |
|
---|
| 155 | class Storage(Resource):
|
---|
| 156 | pass
|
---|
| 157 |
|
---|
| 158 | class Pool(Resource):
|
---|
| 159 | pass
|
---|
| 160 |
|
---|
| 161 | class Catalog(Resource):
|
---|
| 162 | pass
|
---|
| 163 |
|
---|
| 164 | class Messages(Resource):
|
---|
| 165 | pass
|
---|
| 166 |
|
---|
| 167 | class Console(Resource):
|
---|
| 168 | pass
|
---|
| 169 |
|
---|
| 170 | class Counter(Resource):
|
---|
| 171 | pass
|
---|
| 172 |
|
---|
| 173 | class Device(Resource):
|
---|
| 174 | pass
|
---|
| 175 |
|
---|
| 176 | class Autochanger(Resource):
|
---|
| 177 | pass
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 | #other subconfiguration items
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | # DirAdresses:
|
---|
| 189 | #
|
---|
| 190 | # DirAddresses = {
|
---|
| 191 | # ip = { addr = 1.2.3.4; port = 1205;}
|
---|
| 192 | # ipv4 = {
|
---|
| 193 | # addr = 1.2.3.4; port = http;}
|
---|
| 194 | # ipv6 = {
|
---|
| 195 | # addr = 1.2.3.4;
|
---|
| 196 | # port = 1205;
|
---|
| 197 | # }
|
---|
| 198 | # ip = {
|
---|
| 199 | # addr = 1.2.3.4
|
---|
| 200 | # port = 1205
|
---|
| 201 | # }
|
---|
| 202 | # ip = { addr = 1.2.3.4 }
|
---|
| 203 | # ip = { addr = 201:220:222::2 }
|
---|
| 204 | # ip = {
|
---|
| 205 | # addr = bluedot.thun.net
|
---|
| 206 | # }
|
---|
| 207 | #}
|
---|
| 208 |
|
---|
| 209 | class DirAddressItem(object):
|
---|
| 210 | def __init__(self,addresstype,address,port):
|
---|
| 211 | self.addresstype = addresstype # ip, ipv4, ipv6
|
---|
| 212 | self.address = address # 192.18.12.1 / www.dass-it.de
|
---|
| 213 | self.port = port # 9103 ...
|
---|
| 214 |
|
---|
| 215 | def __str__(self):
|
---|
| 216 |
|
---|
| 217 | s = " " +self.addresstype + " = { \n"
|
---|
| 218 | s += " addr = " + self.address + "\n"
|
---|
| 219 | if self.port:
|
---|
| 220 | s += " port = " + str(self.port) + "\n"
|
---|
| 221 | s += " }\n"
|
---|
| 222 |
|
---|
| 223 | return s
|
---|
| 224 |
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | class DirAddresses(Resource):
|
---|
| 228 |
|
---|
| 229 | def __str__(self):
|
---|
| 230 | s = "DirAddresses = {\n"
|
---|
| 231 | for d in self.items:
|
---|
| 232 | s += str(d)
|
---|
| 233 | s += "}\n"
|
---|
| 234 | return s
|
---|
| 235 |
|
---|
| 236 | def __init__(self,name):
|
---|
| 237 | self.DIRECTIVE = 'DirAddresses'
|
---|
| 238 | self.comments = []
|
---|
| 239 | self.items = []
|
---|
| 240 | self.name = ''
|
---|
| 241 |
|
---|
| 242 | def add_item(self,addresstype,address,port=None):
|
---|
| 243 | self.items.append( DirAddressItem( addresstype, address, port ) )
|
---|
| 244 |
|
---|
| 245 | def parse_string(self, string_to_parse):
|
---|
| 246 | m = Resource.p.match(string_to_parse)
|
---|
| 247 | if m:
|
---|
| 248 | #print 'Resource:>',m.group(1),'< = >',m.group(2)
|
---|
| 249 | n = m.group(1).lower().replace(' ','')
|
---|
| 250 | #print '>'+n+'<'
|
---|
| 251 | if n == 'name':
|
---|
| 252 | self.name = m.group(2)
|
---|
| 253 | else:
|
---|
| 254 | self.add_item(Item(m.group(1),m.group(2)))
|
---|
| 255 | #else:
|
---|
| 256 | # print 'Resource: unbearbeiteter string: |'+string_to_parse+'|'
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 |
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 | # RunScript {
|
---|
| 265 | # Console = "update stats days=3"
|
---|
| 266 | # Console = "prune stats yes"
|
---|
| 267 | # RunsWhen = After
|
---|
| 268 | # RunsOnClient = no
|
---|
| 269 | # }
|
---|
| 270 |
|
---|
| 271 | class RunScript(Resource):
|
---|
| 272 | DIRECTIVE = "RunScript"
|
---|