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

Last change on this file since 1102 was 1100, checked in by joergs, on Sep 21, 2012 at 4:55:30 PM

fixes

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