[1027] | 1 | #!/usr/bin/env python
|
---|
| 2 |
|
---|
| 3 | # get client properties for bacula
|
---|
| 4 |
|
---|
[1094] | 5 | import logging
|
---|
[1027] | 6 | import subprocess
|
---|
[1095] | 7 | import time
|
---|
[1027] | 8 | import json
|
---|
| 9 | from pprint import pprint
|
---|
| 10 |
|
---|
| 11 | opsi={
|
---|
[1100] | 12 | 'server': "https://opsi.joergs.dass-it:4447/rpc",
|
---|
[1027] | 13 | 'username': "joergs",
|
---|
| 14 | 'password': "linuxlinux",
|
---|
| 15 | }
|
---|
| 16 |
|
---|
[1100] | 17 | catalog="MyCatalog"
|
---|
| 18 |
|
---|
[1027] | 19 | # "-d"
|
---|
| 20 | opsiCallPrefix=[ "opsi-admin", "-a", opsi['server'], "-u", opsi['username'], "-p", opsi['password'] ]
|
---|
| 21 |
|
---|
| 22 | opsiCallClientsWithBacula=[ "method", "productOnClient_getObjects", "[]", '{"productId":"bacula", "installationStatus": "installed"}']
|
---|
| 23 |
|
---|
| 24 | opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
|
---|
| 25 |
|
---|
[1028] | 26 |
|
---|
[1095] | 27 | def 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 | #}
|
---|
[1105] | 38 | params = [ "FDPort", "FileRetention", "JobRetention", "AutoPrune" ]
|
---|
[1095] | 39 | fd.write( "Client {\n" )
|
---|
[1105] | 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
|
---|
[1105] | 44 | fd.write( ' Password = "' + properties['filedaemon_full_password'] + '"' + "\n" )
|
---|
| 45 | fd.write( ' Catalog = "' + properties['catalog'] + '"' + "\n" )
|
---|
[1028] | 46 | for i in params:
|
---|
| 47 | try:
|
---|
[1105] | 48 | fd.write( ' ' + i + ' = "' + properties[i.lower()] + '"' + "\n" )
|
---|
[1028] | 49 | except KeyError:
|
---|
[1105] | 50 | fd.write( ' # ' + i + " = \n" )
|
---|
[1095] | 51 | fd.write( "}\n")
|
---|
| 52 | fd.write( "\n" )
|
---|
[1028] | 53 |
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 |
|
---|
[1095] | 57 | def 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" )
|
---|
[1105] | 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( "# " )
|
---|
[1105] | 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')
|
---|
| 85 | logging.basicConfig(format='%(message)s')
|
---|
| 86 | logger = logging.getLogger(__name__)
|
---|
| 87 | #logger.setLevel(logging.INFO)
|
---|
| 88 | logger.setLevel(logging.DEBUG)
|
---|
| 89 |
|
---|
| 90 | logger.debug( "start" )
|
---|
| 91 |
|
---|
[1027] | 92 | try:
|
---|
| 93 | clientsWithBacula=json.loads( subprocess.check_output( opsiCallPrefix + opsiCallClientsWithBacula ) )
|
---|
[1094] | 94 | #except subprocess.CalledProcessError as e:
|
---|
[1100] | 95 | except BaseException as e:
|
---|
| 96 | logger.exception( "failed: %s" %(e) )
|
---|
[1094] | 97 | exit(1)
|
---|
[1027] | 98 |
|
---|
[1094] | 99 | #pprint( clientsWithBacula )
|
---|
[1095] | 100 |
|
---|
| 101 | if 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] | 116 | for 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 |
|
---|
| 132 | logger.debug( "finished" )
|
---|