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 getResourceByName(directorName,resType,name):
|
---|
91 | dirdcfg=getDirectorConfig(directorName)
|
---|
92 | return dirdcfg.getRoot().getByName(name,resType)[0]
|
---|
93 |
|
---|
94 | # main
|
---|
95 | if __name__ == "__main__":
|
---|
96 | if len(sys.argv) < 2:
|
---|
97 | filename="test.conf"
|
---|
98 | else:
|
---|
99 | filename=sys.argv[1]
|
---|
100 |
|
---|
101 | loadDirectorConfig(filename)
|
---|
102 |
|
---|
103 | clientIds=getResources("bacula-dir","Client")
|
---|
104 |
|
---|
105 | c=getResourceById("bacula-dir",clientIds[0][0])
|
---|
106 | print c
|
---|
107 | print keyValueList(c)
|
---|
108 |
|
---|