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://opsi.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", "Catalog", "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 | fd.write( " Catalog = " + catalog + "\n" )
|
---|
46 | for i in params:
|
---|
47 | try:
|
---|
48 | fd.write( " " + i + " = " + properties[i.lower()] + "\n" )
|
---|
49 | except KeyError:
|
---|
50 | fd.write( " # " + i + " = " + "\n" )
|
---|
51 | fd.write( "}\n")
|
---|
52 | fd.write( "\n" )
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | def write_job_conf( fd, client, properties ):
|
---|
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" ]
|
---|
66 | fd.write( "Job {" + "\n" )
|
---|
67 | fd.write( " Name = " + client['clientId'] + "-job" + "\n" )
|
---|
68 | fd.write( " Client = " + properties['filedaemon_full_name'] + "\n" )
|
---|
69 | for i in params:
|
---|
70 | fd.write( " " )
|
---|
71 | try:
|
---|
72 | if not properties[i.lower()]:
|
---|
73 | fd.write( "# " )
|
---|
74 | fd.write( i + " = " + properties[i.lower()] + "\n" )
|
---|
75 | except KeyError:
|
---|
76 | fd.write( "# " + i + " = " + "\n" )
|
---|
77 | fd.write( "}" + "\n" )
|
---|
78 | fd.write( "\n" )
|
---|
79 |
|
---|
80 | #
|
---|
81 | # main
|
---|
82 | #
|
---|
83 |
|
---|
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 |
|
---|
92 | try:
|
---|
93 | clientsWithBacula=json.loads( subprocess.check_output( opsiCallPrefix + opsiCallClientsWithBacula ) )
|
---|
94 | #except subprocess.CalledProcessError as e:
|
---|
95 | except BaseException as e:
|
---|
96 | logger.exception( "failed: %s" %(e) )
|
---|
97 | exit(1)
|
---|
98 |
|
---|
99 | #pprint( clientsWithBacula )
|
---|
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" )
|
---|
107 |
|
---|
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" )
|
---|
112 | except BaseException as e:
|
---|
113 | logger.exception( "failed to create files" )
|
---|
114 | exit(1)
|
---|
115 |
|
---|
116 | for client in clientsWithBacula:
|
---|
117 | clientId = client['clientId']
|
---|
118 |
|
---|
119 | try:
|
---|
120 | clientBaculaProperties=json.loads( subprocess.check_output( opsiCallPrefix + opsiCallClientBaculaProperties + [ client['clientId'] ] ) )
|
---|
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 )
|
---|
128 | write_client_conf( file_opsi_clients, client, clientBaculaProperties )
|
---|
129 | write_job_conf( file_opsi_jobs, client, clientBaculaProperties )
|
---|
130 | logger.info( "%s: OK" % clientId )
|
---|
131 |
|
---|
132 | logger.debug( "finished" )
|
---|