1 | #!/usr/bin/python
|
---|
2 |
|
---|
3 | from bcfg import *
|
---|
4 | import resource
|
---|
5 | import directive
|
---|
6 | import ResourceInfo
|
---|
7 |
|
---|
8 | import sys
|
---|
9 | import random
|
---|
10 |
|
---|
11 | def genpasswd(len=32):
|
---|
12 | charset="01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!/%()=?@"
|
---|
13 | password=""
|
---|
14 | for a in xrange(0,len):
|
---|
15 | password += random.choice(charset)
|
---|
16 | return password
|
---|
17 |
|
---|
18 |
|
---|
19 | def createClientConfig(dirdcfg,clientName,clientPassword=genpasswd()):
|
---|
20 | director=dirdcfg.getRoot().getByType("Director")[0]
|
---|
21 | dirdName=director.getName()
|
---|
22 | r=dirdcfg.getRoot().getByName(clientName)
|
---|
23 | if len(r)==1:
|
---|
24 | client=r[0]
|
---|
25 | else:
|
---|
26 | return
|
---|
27 |
|
---|
28 | filename=clientName+".conf"
|
---|
29 | fdcfg=FiledConfig(filename)
|
---|
30 | fdcfg.setDefaults(dirdName,clientPassword,clientName)
|
---|
31 | fdcfg.write()
|
---|
32 |
|
---|
33 | def createAllClientConfigs(dirdcfg):
|
---|
34 | clients=dirdcfg.getRoot().getByType("Client",1)
|
---|
35 | director=dirdcfg.getRoot().getByType("Director")[0]
|
---|
36 | dirdName=director.getName()
|
---|
37 |
|
---|
38 | for c in clients:
|
---|
39 | print c
|
---|
40 | clientName=c.getName()
|
---|
41 | if clientName!=None:
|
---|
42 | r=c.getByType("Password")
|
---|
43 | if len(r)>0:
|
---|
44 | clientPassword=r[0].value
|
---|
45 | clientPassword=clientPassword.replace('"','')
|
---|
46 | else:
|
---|
47 | clientPassword=""
|
---|
48 | filename=clientName+".conf"
|
---|
49 | fdcfg=FiledConfig(filename)
|
---|
50 | fdcfg.setDefaults(dirdName,clientPassword,clientName)
|
---|
51 | fdcfg.write()
|
---|
52 |
|
---|
53 | def getDirectors():
|
---|
54 | return [ "bacula-dir" ];
|
---|
55 |
|
---|
56 | def loadDirectorConfig(filename):
|
---|
57 | global currentConfig
|
---|
58 | currentConfig=DirdConfig(filename)
|
---|
59 | currentConfig.read()
|
---|
60 |
|
---|
61 | def getDirectorConfig(directorName):
|
---|
62 | global currentConfig
|
---|
63 | return currentConfig
|
---|
64 |
|
---|
65 | def getClients(directorName):
|
---|
66 | return getResources(directorName,"Client")
|
---|
67 |
|
---|
68 | def getResources(directorName,resType):
|
---|
69 | dirdcfg=getDirectorConfig(directorName)
|
---|
70 | resources=dirdcfg.getRoot().getByType(resType,1)
|
---|
71 | result=[]
|
---|
72 | for r in resources:
|
---|
73 | result.append((r.id,r.getName()))
|
---|
74 | return result
|
---|
75 |
|
---|
76 | def keyValueList(res):
|
---|
77 | result=[]
|
---|
78 | for i in res.items:
|
---|
79 | #if i.type=="Name":
|
---|
80 | # continue
|
---|
81 |
|
---|
82 | result.append((i.type,i.value))
|
---|
83 |
|
---|
84 | return result
|
---|
85 |
|
---|
86 | def getResourceById(directorName,resId):
|
---|
87 | dirdcfg=getDirectorConfig(directorName)
|
---|
88 | return dirdcfg.getRoot().getById(resId)
|
---|
89 |
|
---|
90 | def setResourceById(resId, directorName, attributes, resInfo):
|
---|
91 | if resId > 0:
|
---|
92 | myResource = getResourceById(directorName, resId)
|
---|
93 | myResource.items = []
|
---|
94 | for a in attributes:
|
---|
95 | k = a.get_element_key()
|
---|
96 | v = a.get_element_value()
|
---|
97 | myResource.add(k, v)
|
---|
98 |
|
---|
99 | else:
|
---|
100 | resource = resource.Resource()
|
---|
101 | resource.name = resInfo.get_element_resName()
|
---|
102 | setResourceById(resource.getId, directorName, attributes, resInfo)
|
---|
103 |
|
---|
104 | status = "wtf"
|
---|
105 |
|
---|
106 | return resId, status
|
---|
107 |
|
---|
108 | def getResourceByName(directorName,resType,name):
|
---|
109 | dirdcfg=getDirectorConfig(directorName)
|
---|
110 | return dirdcfg.getRoot().getByName(name,resType)[0]
|
---|
111 |
|
---|
112 | # main
|
---|
113 | if __name__ == "__main__":
|
---|
114 | if len(sys.argv) < 2:
|
---|
115 | filename="test.conf"
|
---|
116 | else:
|
---|
117 | filename=sys.argv[1]
|
---|
118 |
|
---|
119 | loadDirectorConfig(filename)
|
---|
120 |
|
---|
121 | clientIds=getResources("bacula-dir","Client")
|
---|
122 |
|
---|
123 | #c=getResourceById("bacula-dir",clientIds[0][0])
|
---|
124 | #print c
|
---|
125 | #print keyValueList(c)
|
---|
126 | attList = [("name", "klaus"), ("director", "bernd"), ("FileSet","homeAll")]
|
---|
127 | resId, status = setResourceById(100, "bacula-dir", attList)
|
---|