| 1 | #!/usr/bin/env python
|
|---|
| 2 |
|
|---|
| 3 | # get client properties for bacula
|
|---|
| 4 |
|
|---|
| 5 | import logging
|
|---|
| 6 | import subprocess
|
|---|
| 7 | import time
|
|---|
| 8 | import json
|
|---|
| 9 | from pprint import pprint
|
|---|
| 10 |
|
|---|
| 11 | opsi={
|
|---|
| 12 | 'server': "https://opsi4.joergs.dass-it:4447/rpc",
|
|---|
| 13 | 'username': "joergs",
|
|---|
| 14 | 'password': "linuxlinux",
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | catalog="MyCatalog"
|
|---|
| 18 |
|
|---|
| 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 |
|
|---|
| 26 |
|
|---|
| 27 | def write_client_conf( fd, client, properties ):
|
|---|
| 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", "FileRetention", "JobRetention", "AutoPrune" ]
|
|---|
| 39 | fd.write( "Client {\n" )
|
|---|
| 40 | fd.write( ' Name = "' + properties['filedaemon_full_name'] + '"' + "\n" )
|
|---|
| 41 | fd.write( ' Address = "' + client['clientId'] + '"' + "\n" )
|
|---|
| 42 | # ipAddress: method host_getObjects [] '{"id":client['clientId']}'
|
|---|
| 43 | #print " # Address =", ipAddress
|
|---|
| 44 | fd.write( ' Password = "' + properties['filedaemon_full_password'] + '"' + "\n" )
|
|---|
| 45 | try:
|
|---|
| 46 | catalog = properties['catalog']
|
|---|
| 47 | except KeyError:
|
|---|
| 48 | catalog = "MyCatalog"
|
|---|
| 49 | fd.write( ' Catalog = "' + catalog + '"' + "\n" )
|
|---|
| 50 | for i in params:
|
|---|
| 51 | try:
|
|---|
| 52 | fd.write( ' ' + i + ' = "' + properties[i.lower()] + '"' + "\n" )
|
|---|
| 53 | except KeyError:
|
|---|
| 54 | fd.write( ' # ' + i + " = \n" )
|
|---|
| 55 | fd.write( "}\n")
|
|---|
| 56 | fd.write( "\n" )
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | def write_job_conf( fd, client, properties ):
|
|---|
| 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" ]
|
|---|
| 70 | fd.write( "Job {" + "\n" )
|
|---|
| 71 | fd.write( ' Name = "' + client['clientId'] + '-job"' + "\n" )
|
|---|
| 72 | fd.write( ' Client = "' + properties['filedaemon_full_name'] + '"' + "\n" )
|
|---|
| 73 | for i in params:
|
|---|
| 74 | fd.write( " " )
|
|---|
| 75 | try:
|
|---|
| 76 | if not properties[i.lower()]:
|
|---|
| 77 | fd.write( "# " )
|
|---|
| 78 | fd.write( i + ' = "' + properties[i.lower()] + '"' + "\n" )
|
|---|
| 79 | except KeyError:
|
|---|
| 80 | fd.write( "# " + i + " = " + "\n" )
|
|---|
| 81 | fd.write( "}" + "\n" )
|
|---|
| 82 | fd.write( "\n" )
|
|---|
| 83 |
|
|---|
| 84 | #
|
|---|
| 85 | # main
|
|---|
| 86 | #
|
|---|
| 87 |
|
|---|
| 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 |
|
|---|
| 96 | try:
|
|---|
| 97 | clientsWithBacula=json.loads( subprocess.check_output( opsiCallPrefix + opsiCallClientsWithBacula ) )
|
|---|
| 98 | #except subprocess.CalledProcessError as e:
|
|---|
| 99 | except BaseException as e:
|
|---|
| 100 | logger.exception( "failed: %s" %(e) )
|
|---|
| 101 | exit(1)
|
|---|
| 102 |
|
|---|
| 103 | #pprint( clientsWithBacula )
|
|---|
| 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" )
|
|---|
| 111 |
|
|---|
| 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" )
|
|---|
| 116 | except BaseException as e:
|
|---|
| 117 | logger.exception( "failed to create files" )
|
|---|
| 118 | exit(1)
|
|---|
| 119 |
|
|---|
| 120 | for client in clientsWithBacula:
|
|---|
| 121 | clientId = client['clientId']
|
|---|
| 122 |
|
|---|
| 123 | try:
|
|---|
| 124 | clientBaculaProperties=json.loads( subprocess.check_output( opsiCallPrefix + opsiCallClientBaculaProperties + [ client['clientId'] ] ) )
|
|---|
| 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 )
|
|---|
| 132 | write_client_conf( file_opsi_clients, client, clientBaculaProperties )
|
|---|
| 133 | write_job_conf( file_opsi_jobs, client, clientBaculaProperties )
|
|---|
| 134 | logger.info( "%s: OK" % clientId )
|
|---|
| 135 |
|
|---|
| 136 | logger.debug( "finished" )
|
|---|