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

Last change on this file since 1097 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
Line 
1#!/usr/bin/env python
2
3# get client properties for bacula
4
5import logging
6import subprocess
7import time
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
24
25def write_client_conf( fd, client, properties ):
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" ]
37 fd.write( "Client {\n" )
38 fd.write( " Name = " + properties['filedaemon_full_name'] + "\n" )
39 fd.write( " Address = " + client['clientId'] + "\n" )
40 # ipAddress: method host_getObjects [] '{"id":client['clientId']}'
41 #print " # Address =", ipAddress
42 fd.write( " Password = " + properties['filedaemon_full_password'] + "\n" )
43 for i in params:
44 try:
45 fd.write( " " + i + " = " + properties[i.lower()] + "\n" )
46 except KeyError:
47 fd.write( " # " + i + " = " + "\n" )
48 fd.write( "}\n")
49 fd.write( "\n" )
50
51
52
53
54def write_job_conf( fd, client, properties ):
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" ]
63 fd.write( "Job {" + "\n" )
64 fd.write( " Name = " + client['clientId'] + "-job" + "\n" )
65 fd.write( " Client = " + properties['filedaemon_full_name'] + "\n" )
66 for i in params:
67 fd.write( " " )
68 try:
69 if not properties[i.lower()]:
70 fd.write( "# " )
71 fd.write( i + " = " + properties[i.lower()] + "\n" )
72 except KeyError:
73 fd.write( "# " + i + " = " + "\n" )
74 fd.write( "}" + "\n" )
75 fd.write( "\n" )
76
77#
78# main
79#
80
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
89try:
90 clientsWithBacula=json.loads( subprocess.check_output( opsiCallPrefix + opsiCallClientsWithBacula ) )
91#except subprocess.CalledProcessError as e:
92except e:
93 logger.exception( "%s: failed:" %(clientId) )
94 exit(1)
95
96#pprint( clientsWithBacula )
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" )
104
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
113for client in clientsWithBacula:
114 clientId = client['clientId']
115
116 try:
117 clientBaculaProperties=json.loads( subprocess.check_output( opsiCallPrefix + opsiCallClientBaculaProperties + [ client['clientId'] ] ) )
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 )
125 write_client_conf( file_opsi_clients, client, clientBaculaProperties )
126 write_job_conf( file_opsi_jobs, client, clientBaculaProperties )
127 logger.info( "%s: OK" % clientId )
128
129logger.debug( "finished" )
Note: See TracBrowser for help on using the repository browser.