[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={
|
---|
[1108] | 12 | 'server': "https://opsi4.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" )
|
---|
[1108] | 45 | try:
|
---|
| 46 | catalog = properties['catalog']
|
---|
| 47 | except KeyError:
|
---|
| 48 | catalog = "MyCatalog"
|
---|
| 49 | fd.write( ' Catalog = "' + catalog + '"' + "\n" )
|
---|
[1028] | 50 | for i in params:
|
---|
| 51 | try:
|
---|
[1105] | 52 | fd.write( ' ' + i + ' = "' + properties[i.lower()] + '"' + "\n" )
|
---|
[1028] | 53 | except KeyError:
|
---|
[1105] | 54 | fd.write( ' # ' + i + " = \n" )
|
---|
[1095] | 55 | fd.write( "}\n")
|
---|
| 56 | fd.write( "\n" )
|
---|
[1028] | 57 |
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 |
|
---|
[1095] | 61 | def write_job_conf( fd, client, properties ):
|
---|
[1028] | 62 | #Job {
|
---|
| 63 | #FileSet = "tingfileset"
|
---|
| 64 | #Name = "ting"
|
---|
| 65 | #Client = ting-fd
|
---|
| 66 | #JobDefs = "LaptopJob"
|
---|
| 67 | ## Write Bootstrap = "/var/lib/bacula/ting.bsr"
|
---|
| 68 | #}
|
---|
| 69 | params = [ "Fileset", "JobDefs" ]
|
---|
[1095] | 70 | fd.write( "Job {" + "\n" )
|
---|
[1105] | 71 | fd.write( ' Name = "' + client['clientId'] + '-job"' + "\n" )
|
---|
| 72 | fd.write( ' Client = "' + properties['filedaemon_full_name'] + '"' + "\n" )
|
---|
[1028] | 73 | for i in params:
|
---|
[1095] | 74 | fd.write( " " )
|
---|
[1028] | 75 | try:
|
---|
| 76 | if not properties[i.lower()]:
|
---|
[1095] | 77 | fd.write( "# " )
|
---|
[1105] | 78 | fd.write( i + ' = "' + properties[i.lower()] + '"' + "\n" )
|
---|
[1028] | 79 | except KeyError:
|
---|
[1095] | 80 | fd.write( "# " + i + " = " + "\n" )
|
---|
| 81 | fd.write( "}" + "\n" )
|
---|
| 82 | fd.write( "\n" )
|
---|
[1028] | 83 |
|
---|
| 84 | #
|
---|
| 85 | # main
|
---|
| 86 | #
|
---|
| 87 |
|
---|
[1094] | 88 | #logging.basicConfig(format='%(asctime)s %(levelname)8s %(message)s')
|
---|
| 89 | logging.basicConfig(format='%(message)s')
|
---|
| 90 | logger = logging.getLogger(__name__)
|
---|
| 91 | #logger.setLevel(logging.INFO)
|
---|
| 92 | logger.setLevel(logging.DEBUG)
|
---|
| 93 |
|
---|
| 94 | logger.debug( "start" )
|
---|
| 95 |
|
---|
[1027] | 96 | try:
|
---|
| 97 | clientsWithBacula=json.loads( subprocess.check_output( opsiCallPrefix + opsiCallClientsWithBacula ) )
|
---|
[1094] | 98 | #except subprocess.CalledProcessError as e:
|
---|
[1100] | 99 | except BaseException as e:
|
---|
| 100 | logger.exception( "failed: %s" %(e) )
|
---|
[1094] | 101 | exit(1)
|
---|
[1027] | 102 |
|
---|
[1094] | 103 | #pprint( clientsWithBacula )
|
---|
[1095] | 104 |
|
---|
| 105 | if clientsWithBacula:
|
---|
| 106 | try:
|
---|
| 107 | file_opsi_clients = open('opsi-clients-generated.conf', 'w')
|
---|
| 108 | file_opsi_clients.write( "#\n" )
|
---|
| 109 | file_opsi_clients.write( "# automatically generated at {}\n".format( time.asctime() ) )
|
---|
| 110 | file_opsi_clients.write( "#\n\n" )
|
---|
[1027] | 111 |
|
---|
[1095] | 112 | file_opsi_jobs = open('opsi-jobs-generated.conf', 'w')
|
---|
| 113 | file_opsi_jobs.write( "#\n" )
|
---|
| 114 | file_opsi_jobs.write( "# automatically generated at {}\n".format( time.asctime() ) )
|
---|
| 115 | file_opsi_jobs.write( "#\n\n" )
|
---|
[1100] | 116 | except BaseException as e:
|
---|
[1095] | 117 | logger.exception( "failed to create files" )
|
---|
| 118 | exit(1)
|
---|
| 119 |
|
---|
[1027] | 120 | for client in clientsWithBacula:
|
---|
[1028] | 121 | clientId = client['clientId']
|
---|
[1027] | 122 |
|
---|
| 123 | try:
|
---|
| 124 | clientBaculaProperties=json.loads( subprocess.check_output( opsiCallPrefix + opsiCallClientBaculaProperties + [ client['clientId'] ] ) )
|
---|
[1094] | 125 | except ValueError as e:
|
---|
| 126 | logger.warn( "%s: no valid information found: %s" %(clientId, e) )
|
---|
| 127 | except subprocess.CalledProcessError as e:
|
---|
| 128 | logger.exception( "%s: failed:" %(clientId) )
|
---|
| 129 | #exit( 1 )
|
---|
| 130 | else:
|
---|
| 131 | #pprint( clientBaculaProperties )
|
---|
[1095] | 132 | write_client_conf( file_opsi_clients, client, clientBaculaProperties )
|
---|
| 133 | write_job_conf( file_opsi_jobs, client, clientBaculaProperties )
|
---|
[1094] | 134 | logger.info( "%s: OK" % clientId )
|
---|
| 135 |
|
---|
| 136 | logger.debug( "finished" )
|
---|