source: vanHelsing/trunk/src/VanHelsingWrapper.py@ 863

Last change on this file since 863 was 861, checked in by tobias, on Apr 13, 2010 at 5:48:55 PM

new resource operation and fault messages added

File size: 3.3 KB
RevLine 
[824]1#!/usr/bin/python
2
3from bcfg import *
4import resource
5import directive
6import ResourceInfo
7
8import sys
9import random
10
11def genpasswd(len=32):
12 charset="01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!/%()=?@"
13 password=""
14 for a in xrange(0,len):
[855]15 password += random.choice(charset)
[824]16 return password
17
18
19def 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
33def 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
53def getDirectors():
54 return [ "bacula-dir" ];
55
56def loadDirectorConfig(filename):
57 global currentConfig
58 currentConfig=DirdConfig(filename)
59 currentConfig.read()
60
61def getDirectorConfig(directorName):
62 global currentConfig
63 return currentConfig
64
65def getClients(directorName):
66 return getResources(directorName,"Client")
67
68def 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
[839]76def 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
[824]86def getResourceById(directorName,resId):
87 dirdcfg=getDirectorConfig(directorName)
[855]88 return dirdcfg.getRoot().getById(resId)
[824]89
[855]90def 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)
[861]98 try:
99 status
100 except NameError:
101 status = "resourceUpdated"
[855]102 else:
[861]103 MyResource = resource.Resource()
[855]104
[861]105 resId, status = setResourceById(MyResource.id, directorName, attributes, resInfo)
106 status = "resourceCreated"
107 print status
[855]108 return resId, status
109
[824]110def getResourceByName(directorName,resType,name):
111 dirdcfg=getDirectorConfig(directorName)
112 return dirdcfg.getRoot().getByName(name,resType)[0]
113
114# main
115if __name__ == "__main__":
116 if len(sys.argv) < 2:
117 filename="test.conf"
118 else:
119 filename=sys.argv[1]
120
121 loadDirectorConfig(filename)
122
[839]123 clientIds=getResources("bacula-dir","Client")
124
[861]125 c=getResourceById("bacula-dir",clientIds[0][0])
126 print c
127 print keyValueList(c)
Note: See TracBrowser for help on using the repository browser.