source: opsi/server/dass-opsi-tools/usr/bin/opsi-bacula-server-conf.py@ 1099

Last change on this file since 1099 was 1096, checked in by joergs, on Sep 21, 2012 at 12:32:10 PM

moved to dass-opsi-tools package

  • Property svn:executable set to *
File size: 4.0 KB
RevLine 
[1027]1#!/usr/bin/env python
2
3# get client properties for bacula
4
[1094]5import logging
[1027]6import subprocess
[1095]7import time
[1027]8import json
9from pprint import pprint
10
11opsi={
12 'server': "https://degdepot2.joergs:4447/rpc",
13 'username': "joergs",
14 'password': "linuxlinux",
15 }
16
17# "-d"
18opsiCallPrefix=[ "opsi-admin", "-a", opsi['server'], "-u", opsi['username'], "-p", opsi['password'] ]
19
20opsiCallClientsWithBacula=[ "method", "productOnClient_getObjects", "[]", '{"productId":"bacula", "installationStatus": "installed"}']
21
22opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
23
[1028]24
[1095]25def write_client_conf( fd, client, properties ):
[1028]26 #Client {
27 #Name = ting-fd
28 #Address = ting.dass-it
29 #FDPort = 9102
30 #Catalog = MyCatalog
31 #Password = "D5w2V5w6B8a9H5Z"
32 #File Retention = 6 months
33 #Job Retention = 6 months
34 #AutoPrune = yes
35 #}
36 params = [ "FDPort", "Catalog", "FileRetention", "JobRetention", "AutoPrune" ]
[1095]37 fd.write( "Client {\n" )
38 fd.write( " Name = " + properties['filedaemon_full_name'] + "\n" )
39 fd.write( " Address = " + client['clientId'] + "\n" )
[1028]40 # ipAddress: method host_getObjects [] '{"id":client['clientId']}'
41 #print " # Address =", ipAddress
[1095]42 fd.write( " Password = " + properties['filedaemon_full_password'] + "\n" )
[1028]43 for i in params:
44 try:
[1095]45 fd.write( " " + i + " = " + properties[i.lower()] + "\n" )
[1028]46 except KeyError:
[1095]47 fd.write( " # " + i + " = " + "\n" )
48 fd.write( "}\n")
49 fd.write( "\n" )
[1028]50
51
52
53
[1095]54def write_job_conf( fd, client, properties ):
[1028]55 #Job {
56 #FileSet = "tingfileset"
57 #Name = "ting"
58 #Client = ting-fd
59 #JobDefs = "LaptopJob"
60 ## Write Bootstrap = "/var/lib/bacula/ting.bsr"
61 #}
62 params = [ "Fileset", "JobDefs" ]
[1095]63 fd.write( "Job {" + "\n" )
64 fd.write( " Name = " + client['clientId'] + "-job" + "\n" )
65 fd.write( " Client = " + properties['filedaemon_full_name'] + "\n" )
[1028]66 for i in params:
[1095]67 fd.write( " " )
[1028]68 try:
69 if not properties[i.lower()]:
[1095]70 fd.write( "# " )
71 fd.write( i + " = " + properties[i.lower()] + "\n" )
[1028]72 except KeyError:
[1095]73 fd.write( "# " + i + " = " + "\n" )
74 fd.write( "}" + "\n" )
75 fd.write( "\n" )
[1028]76
77#
78# main
79#
80
[1094]81#logging.basicConfig(format='%(asctime)s %(levelname)8s %(message)s')
82logging.basicConfig(format='%(message)s')
83logger = logging.getLogger(__name__)
84#logger.setLevel(logging.INFO)
85logger.setLevel(logging.DEBUG)
86
87logger.debug( "start" )
88
[1027]89try:
90 clientsWithBacula=json.loads( subprocess.check_output( opsiCallPrefix + opsiCallClientsWithBacula ) )
[1094]91#except subprocess.CalledProcessError as e:
92except e:
93 logger.exception( "%s: failed:" %(clientId) )
94 exit(1)
[1027]95
[1094]96#pprint( clientsWithBacula )
[1095]97
98if clientsWithBacula:
99 try:
100 file_opsi_clients = open('opsi-clients-generated.conf', 'w')
101 file_opsi_clients.write( "#\n" )
102 file_opsi_clients.write( "# automatically generated at {}\n".format( time.asctime() ) )
103 file_opsi_clients.write( "#\n\n" )
[1027]104
[1095]105 file_opsi_jobs = open('opsi-jobs-generated.conf', 'w')
106 file_opsi_jobs.write( "#\n" )
107 file_opsi_jobs.write( "# automatically generated at {}\n".format( time.asctime() ) )
108 file_opsi_jobs.write( "#\n\n" )
109 except e:
110 logger.exception( "failed to create files" )
111 exit(1)
112
[1027]113for client in clientsWithBacula:
[1028]114 clientId = client['clientId']
[1027]115
116 try:
117 clientBaculaProperties=json.loads( subprocess.check_output( opsiCallPrefix + opsiCallClientBaculaProperties + [ client['clientId'] ] ) )
[1094]118 except ValueError as e:
119 logger.warn( "%s: no valid information found: %s" %(clientId, e) )
120 except subprocess.CalledProcessError as e:
121 logger.exception( "%s: failed:" %(clientId) )
122 #exit( 1 )
123 else:
124 #pprint( clientBaculaProperties )
[1095]125 write_client_conf( file_opsi_clients, client, clientBaculaProperties )
126 write_job_conf( file_opsi_jobs, client, clientBaculaProperties )
[1094]127 logger.info( "%s: OK" % clientId )
128
129logger.debug( "finished" )
Note: See TracBrowser for help on using the repository browser.