Changeset 1239 for opsi/server/dass-opsi-tools/usr/bin/opsiclient
- Timestamp:
- Feb 28, 2018, 6:41:10 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
opsi/server/dass-opsi-tools/usr/bin/opsiclient
r1234 r1239 8 8 9 9 __author__ = "Joerg Steffens" 10 __copyright__ = "Copyright 2012-201 7, dass IT GmbH"10 __copyright__ = "Copyright 2012-2018, dass IT GmbH" 11 11 __license__ = "GPL" 12 12 __version__ = "1.2" … … 21 21 22 22 import argparse 23 from datetime import datetime, timedelta 24 from dateutil import parser as dateparser 25 import jsonrpc 23 from datetime import datetime, timedelta 24 from dateutil import parser as dateparser 26 25 import logging 27 26 import os 28 from pprint import pprint, pformat27 from pprint import pprint, pformat 29 28 import sys 29 import ssl 30 30 import time 31 31 32 try: 33 import jsonrpc 34 except ImportError: 35 import jsonrpclib 36 37 32 38 UrlJsonRpc="https://<username>:<password>@opsi:4447/rpc" 33 39 34 HelpEpilog="WARNING: python-json-rpc is known to have problems with HTTP proxies. In case of problems, make sure, the environment variables http_proxy and/or https_proxy are *not* set. "40 HelpEpilog="WARNING: python-json-rpc is known to have problems with HTTP proxies. In case of problems, make sure, the environment variables http_proxy and/or https_proxy are *not* set. It might also be neccesary to set variable PYTHONHTTPSVERIFY=0" 35 41 36 42 class Nrpe: … … 83 89 self.nagios=nagios 84 90 self.urlJsonRpc=urlJsonRpc 85 self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc) 91 if 'jsonrpc' in sys.modules: 92 self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc) 93 else: 94 self.rpc=jsonrpclib.ServerProxy(self.urlJsonRpc) 86 95 self.logger.debug( "initialized: " + self.urlJsonRpc ) 96 self.logger.debug(dir(self.rpc)) 87 97 88 98 89 99 def list(self): 100 exceptions = [] 101 if 'jsonrpc' in sys.modules: 102 exceptions = [ jsonrpc.json.JSONDecodeException ] 90 103 try: 91 104 print( "\n".join( self.rpc.getClientIds_list() ) ) 92 except jsonrpc.json.JSONDecodeExceptionas e:105 except exceptions as e: 93 106 self.logger.debug( pformat(self.rpc.getClientIds_list()) ) 94 107 self.logger.exception( "failed" ) … … 314 327 315 328 329 330 def write_value_conf(self, fd, key, properties, default=None): 331 value = None 332 comment = '' 333 try: 334 value = properties[key.lower()] 335 except KeyError: 336 pass 337 if not value and default: 338 value = default 339 if not value: 340 # prevent a None to be written 341 value = '' 342 comment = '# ' 343 fd.write(' {}{} = "{}"\n'.format(comment, key, value)) 344 345 346 316 347 def write_client_conf( self, fd, client, properties ): 317 348 #Client { … … 319 350 #Address = ting.dass-it 320 351 #FDPort = 9102 352 #Password = "D5w2V5w6B8a9H5Z" 321 353 #Catalog = MyCatalog 322 #Password = "D5w2V5w6B8a9H5Z"323 354 #File Retention = 6 months 324 355 #Job Retention = 6 months 325 356 #AutoPrune = yes 326 357 #} 327 params = [ "FDPort", "FileRetention", "JobRetention", "AutoPrune" ]358 params = [ 'catalog', "FDPort", "FileRetention", "JobRetention", "AutoPrune" ] 328 359 fd.write( "Client {\n" ) 329 360 fd.write( ' Name = "' + properties['filedaemon_full_name'] + '"' + "\n" ) 330 fd.write( ' Address = "' + client['clientId'] + '"' + "\n" )361 fd.write( ' Address = "' + properties['filedaemon_client_address'] + '"' + "\n" ) 331 362 # ipAddress: method host_getObjects [] '{"id":client['clientId']}' 332 363 #print(" # Address =", ipAddress) 333 364 fd.write( ' Password = "' + properties['filedaemon_full_password'] + '"' + "\n" ) 334 try:335 catalog = properties['catalog']336 except KeyError:337 catalog = "MyCatalog"338 fd.write( ' Catalog = "' + catalog + '"' + "\n" )339 365 for i in params: 340 try: 341 fd.write( ' ' + i + ' = "' + properties[i.lower()] + '"' + "\n" ) 342 except KeyError: 343 fd.write( ' # ' + i + " = \n" ) 366 self.write_value_conf(fd, i, properties) 344 367 fd.write( "}\n") 345 368 fd.write( "\n" ) 346 369 347 370 348 def write_job_conf( self, fd, client, properties):371 def write_job_conf(self, fd, client, properties, defaultjobdefs, defaultfileset): 349 372 #Job { 350 373 #FileSet = "tingfileset" … … 354 377 ## Write Bootstrap = "/var/lib/bacula/ting.bsr" 355 378 #} 356 params = [ " Fileset", "JobDefs" ]379 params = [ "JobDefs", "FileSet" ] 357 380 fd.write( "Job {" + "\n" ) 358 381 fd.write( ' Name = "' + client['clientId'] + '-job"' + "\n" ) 359 382 fd.write( ' Client = "' + properties['filedaemon_full_name'] + '"' + "\n" ) 360 for i in params: 361 fd.write( " " ) 362 try: 363 if not properties[i.lower()]: 364 fd.write( "# " ) 365 fd.write( i + ' = "' + properties[i.lower()] + '"' + "\n" ) 366 except KeyError: 367 fd.write( "# " + i + " = " + "\n" ) 383 self.write_value_conf(fd, 'JobDefs', properties, defaultjobdefs) 384 self.write_value_conf(fd, 'FileSet', properties, defaultfileset) 368 385 fd.write( "}" + "\n" ) 369 386 fd.write( "\n" ) … … 381 398 382 399 383 def createBaculaConfigFiles( self ): 384 clientsWithBacula=self.getClientsWithProduct( "bacula" ) 400 401 def createBareosConfigFiles(self, defaultjobdefs, defaultfileset): 402 bareosDirConfigPath = '/etc/bareos/bareos-dir.d/' 403 clientsWithBacula=self.getClientsWithProduct('winbareos') 385 404 if clientsWithBacula: 386 405 try: 387 file_opsi_clients = open('opsi-clients-generated.conf', 'w') 406 configfile = bareosDirConfigPath + 'client/opsi-clients-generated.conf' 407 file_opsi_clients = open(configfile, 'w') 388 408 self.write_config_file_header( file_opsi_clients ) 389 390 file_opsi_jobs = open('opsi-jobs-generated.conf', 'w') 409 except (BaseException, IOError) as e: 410 self.logger.exception( "failed to create configuration file {}".format(configfile) ) 411 return False 412 413 try: 414 configfile = bareosDirConfigPath + 'job/opsi-jobs-generated.conf' 415 file_opsi_jobs = open(configfile, 'w') 391 416 self.write_config_file_header( file_opsi_jobs ) 392 except BaseExceptionas e:393 self.logger.exception( "failed to create files")417 except (BaseException, IOError) as e: 418 self.logger.exception( "failed to create configuration file {}".format(configfile) ) 394 419 return False 420 395 421 for client in clientsWithBacula: 396 422 clientId = client['clientId'] 397 423 try: 398 clientBaculaProperties=self.getClientProductProperty( clientId, "bacula")424 clientBaculaProperties=self.getClientProductProperty( clientId, 'winbareos' ) 399 425 except ValueError as e: 400 426 self.logger.warn( "%s: no valid information found: %s" %(clientId, e) ) … … 402 428 if clientBaculaProperties: 403 429 #pprint( clientBaculaProperties ) 404 self.write_client_conf( file_opsi_clients, client, clientBaculaProperties)405 self.write_job_conf( file_opsi_jobs, client, clientBaculaProperties)430 self.write_client_conf(file_opsi_clients, client, clientBaculaProperties) 431 self.write_job_conf(file_opsi_jobs, client, clientBaculaProperties, defaultjobdefs, defaultfileset) 406 432 self.logger.info( "%s: OK" % clientId ) 407 433 else: … … 508 534 logger.setLevel(logging.INFO) 509 535 510 parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.', epilog=HelpEpilog ) 536 parser = argparse.ArgumentParser( 537 description='Command line tool for OPSI configuration.', 538 epilog=HelpEpilog 539 ) 511 540 512 541 parser.add_argument( '--debug', action='store_true', help="enable debugging output" ) … … 538 567 #parser_copy.add_argument( '--no-properties', action='store_false', help="don't copy product properties" ) 539 568 540 parser_createBaculaConfigFiles = subparsers.add_parser('createBaculaConfigFiles', help='create Bacula config files for all clients that have bacula installed') 569 parser_createBareosConfigFiles = subparsers.add_parser( 570 'createBareosConfigFiles', 571 help='create Bareos config files for all clients that have winbareos installed', 572 formatter_class=argparse.ArgumentDefaultsHelpFormatter 573 ) 574 parser_createBareosConfigFiles.add_argument( 575 '--defaultjobdefs', 576 metavar='JobDefs', 577 default='DefaultJob', 578 help="use this JobDefs if no other is defined for a client" 579 ) 580 parser_createBareosConfigFiles.add_argument( 581 '--defaultfileset', 582 metavar='FileSet', 583 help="use this FileSet if no other is defined for a client" 584 ) 541 585 542 586 parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' ) … … 595 639 elif args.subcommand == "copy": 596 640 result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot ) 597 elif args.subcommand == "createBa culaConfigFiles":598 result = opsi.createBa culaConfigFiles()641 elif args.subcommand == "createBareosConfigFiles": 642 result = opsi.createBareosConfigFiles(args.defaultjobdefs, args.defaultfileset) 599 643 elif args.subcommand == "exists": 600 644 result = opsi.exists( args.src )
Note:
See TracChangeset
for help on using the changeset viewer.