[824] | 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 getResourceById(directorName,resId):
|
---|
| 77 | dirdcfg=getDirectorConfig(directorName)
|
---|
| 78 | return dirdcfg.getRoot().getById(resId)
|
---|
| 79 |
|
---|
| 80 | def getResourceByName(directorName,resType,name):
|
---|
| 81 | dirdcfg=getDirectorConfig(directorName)
|
---|
| 82 | return dirdcfg.getRoot().getByName(name,resType)[0]
|
---|
| 83 |
|
---|
| 84 | # main
|
---|
| 85 | if __name__ == "__main__":
|
---|
| 86 | if len(sys.argv) < 2:
|
---|
| 87 | filename="test.conf"
|
---|
| 88 | else:
|
---|
| 89 | filename=sys.argv[1]
|
---|
| 90 |
|
---|
| 91 | loadDirectorConfig(filename)
|
---|
| 92 |
|
---|
| 93 | print getResources("bacula-dir","Client")
|
---|
| 94 |
|
---|