- Timestamp:
- Feb 18, 2015, 4:43:07 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
opsi/server/dass-opsi-tools/usr/bin/opsiclient
r1118 r1174 6 6 7 7 __author__ = "Joerg Steffens" 8 __copyright__ = "Copyright 2012 , dass IT GmbH"8 __copyright__ = "Copyright 2012-2015, dass IT GmbH" 9 9 __license__ = "GPL" 10 10 __version__ = "1.1" … … 16 16 #ip_address) 17 17 #self.command("opsi-admin -d method configState_create clientconfig.depot.id " + \ 18 #computername + " " + depotName) 19 18 #computername + " " + depotName) 19 20 20 import argparse 21 21 import jsonrpc 22 22 import logging 23 23 import os 24 from pprint import pprint 24 from pprint import pprint, pformat 25 25 import time 26 26 … … 32 32 33 33 UrlJsonRpcDefault="https://opsi:4447/rpc" 34 34 35 35 ProductAttributesCopy = ['actionRequest','actionResult','installationStatus','packageVersion','productVersion'] 36 36 37 37 def __init__(self, urlJsonRpc = UrlJsonRpcDefault, debug=False ): 38 self.logger=logging.getLogger(__name__) 38 self.logger=logging.getLogger(__name__) 39 39 self.debug=debug 40 40 self.urlJsonRpc=urlJsonRpc … … 42 42 self.logger.debug( "initialized: " + self.urlJsonRpc ) 43 43 44 44 45 45 def list(self): 46 46 print( "\n".join( self.rpc.getClientIds_list() ) ) 47 47 return True 48 48 49 49 50 50 def getClientsWithProduct( self, product ): 51 51 return self.rpc.productOnClient_getObjects( [], { "productId": product, "installationStatus": "installed" } ) … … 59 59 return self.list() 60 60 return True 61 62 61 63 62 def exists(self, src): 64 63 return len( self.rpc.host_getObjects( [], {"id":src} ) ) == 1 65 64 66 65 def info(self, src): 67 66 if not self.exists( src ): … … 75 74 print " last seen:", host["lastSeen"] 76 75 print " notes:", host["notes"] 77 76 78 77 print " products:" 79 78 products = self.getProductOnClient( src, [] ) … … 98 97 products = self.rpc.productPropertyState_getObjects( [], { 'objectId': src } ) 99 98 self.rpc.productPropertyState_deleteObjects( products ) 100 99 101 100 if self.debug: 102 101 pprint( self.getProductOnClient( src ) ) 103 102 return True 104 103 104 def getOpsiConfigserverId(self): 105 # there should always be only one OpsiConfigserver 106 opsiConfigservers=self.rpc.host_getHashes( [], { "type": "OpsiConfigserver" } ) 107 try: 108 return opsiConfigservers[0]['id'] 109 except (KeyError,IndexError) as e: 110 self.logger.error( "failed to retreive OpsiConfigserver" ) 111 105 112 def createClient(self, name, opsiHostKey, description, notes, hardwareAddress, ipAddress): 106 113 # self.rpc.host_createOpsiClient( name, opsiHostKey, description, notes, hardwareAddress, ipAddress ) … … 117 124 obj['description'] = description 118 125 if notes: 119 obj['notes'] = notes 126 obj['notes'] = notes 120 127 if hardwareAddress: 121 128 obj['hardwareAddress'] = hardwareAddress … … 127 134 else: 128 135 self.rpc.host_insertObject(obj) 129 136 130 137 if depot: 131 138 self.clientSetDepot(src,depot) … … 133 140 134 141 135 136 142 def clientSetDepot(self, name, depot): 137 self.rpc.configState_create( "clientconfig.depot.id", name, depot ) 143 self.rpc.configState_create( "clientconfig.depot.id", name, depot ) 138 144 139 145 def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None, description = "", copyProperties = True ): 140 146 141 147 print "create/update", dst, "from template", src + ":", 142 148 obj = { … … 156 162 else: 157 163 self.rpc.host_insertObject(obj) 158 164 159 165 if depot: 160 166 self.clientSetDepot(dst,depot) 161 167 162 168 if self.debug: 163 169 pprint( self.getProductOnClient( src ) ) 164 170 self.copyProductOnClient( src, dst ) 165 # TODO:166 # copy product properties:167 # opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]168 171 if copyProperties: 169 172 if self.debug: 170 173 print "copy product properties" 171 self.copyProductPropertyState( src, dst ) 174 if not depot: 175 # get default Properties from Master Depot Server (OpsiConfigserver) 176 depot = self.getOpsiConfigserverId() 177 self.copyProductPropertyState( src, dst, depot ) 172 178 print "done" 173 179 return True 174 175 176 180 177 181 def getProductOnClient( self, client, attributes = ProductAttributesCopy ): 178 182 return self.rpc.productOnClient_getHashes( [], { 'clientId': client } ) 179 180 181 183 182 184 def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ): … … 193 195 pprint( self.getProductOnClient( dst ) ) 194 196 197 195 198 def getProductPropertyState( self, client, attributes = [] ): 196 199 return self.rpc.productPropertyState_getHashes( [], { 'objectId': client } ) 197 198 def copyProductPropertyState( self, src, dst, attributes = [] ): 200 201 202 def copyProductPropertyState( self, src, dst, default = None, attributes = [] ): 203 if default: 204 productProperties_default = self.getProductPropertyState( default, attributes ) 205 else: 206 productProperties_default = [] 199 207 productProperties_src = self.getProductPropertyState( src, attributes ) 200 208 productProperties_dst = [] 201 209 for i in productProperties_src: 202 if self.debug: 203 print i['productId'], "-", i["propertyId"] + ": ", 204 pprint(i["values"]) 205 #pprint( i ) 206 i['objectId'] = dst 207 productProperties_dst.append(i) 210 use_default=False 211 for j in productProperties_default: 212 if i['productId'] == j['productId'] and i["propertyId"] == j["propertyId"]: 213 if i['values'] == j['values']: 214 use_default=True 215 if self.debug: 216 print i['productId'], "-", i["propertyId"] + ": ", pformat(i["values"]), 217 if use_default: 218 print "(use default)" 219 else: 220 print "(set)" 221 if not use_default: 222 i['objectId'] = dst 223 productProperties_dst.append(i) 208 224 self.rpc.productPropertyState_createObjects( productProperties_dst ) 209 225 if self.debug: … … 251 267 fd.write( "}\n") 252 268 fd.write( "\n" ) 253 254 255 269 256 270 … … 280 294 281 295 def write_config_file_header( self, fd ): 282 try: 296 try: 283 297 fd.write( "#\n" ) 284 298 fd.write( "# automatically generated at {0}\n".format( time.asctime() ) ) … … 288 302 return False 289 303 return True 290 304 291 305 292 306 def createBaculaConfigFiles( self ): … … 296 310 file_opsi_clients = open('opsi-clients-generated.conf', 'w') 297 311 self.write_config_file_header( file_opsi_clients ) 298 312 299 313 file_opsi_jobs = open('opsi-jobs-generated.conf', 'w') 300 314 self.write_config_file_header( file_opsi_jobs ) 301 except BaseException as e: 315 except BaseException as e: 302 316 self.logger.exception( "failed to create files" ) 303 317 return False 304 318 for client in clientsWithBacula: 305 clientId = client['clientId'] 319 clientId = client['clientId'] 306 320 try: 307 321 clientBaculaProperties=self.getClientProductProperty( clientId, "bacula" ) … … 345 359 parser_clean = subparsers.add_parser('clean', help='remove all product states from a opsi client' ) 346 360 parser_clean.add_argument( 'src', help="source opsi client to clean" ) 347 348 parser_copy = subparsers.add_parser('copy', help='copy/create a opsi client from a template opsi client') 361 362 parser_copy = subparsers.add_parser('copy', help='copy/create a opsi client from a template opsi client') 349 363 parser_copy.add_argument( 'src', help="source/template opsi client" ) 350 364 parser_copy.add_argument( 'dst', help="opsi client to be created" ) … … 353 367 parser_copy.add_argument( '--depot', help="depot server the new opsi client should be located" ) 354 368 #parser_copy.add_argument( '--no-properties', action='store_false', help="don't copy product properties" ) 355 369 356 370 parser_createBaculaConfigFiles = subparsers.add_parser('createBaculaConfigFiles', help='create Bacula config files for all clients that have bacula installed') 357 371 358 372 parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' ) 359 373 parser_exists.add_argument( 'src', help="source opsi client" ) 360 361 374 #parser_list = subparsers.add_parser('list', help='list all opsi clients' ) 362 375 363 376 parser_listClients = subparsers.add_parser('listClients', help='list opsi clients') 364 377 parser_listClients.add_argument( '--product', help="only list clients, that have product installed" ) 365 378 366 379 parser_info = subparsers.add_parser('info', help='print information about a opsi client' ) 367 380 parser_info.add_argument( 'src', help="opsi client" ) 368 381 369 382 parser_update = subparsers.add_parser('update', help='update/create a opsi client') 370 383 parser_update.add_argument( 'src', help="opsi client to be created" ) … … 391 404 else: 392 405 parser.error( "argument --url is required" ) 393 406 394 407 opsi=OpsiRpc( url, args.debug ) 395 408 396 409 result = True 397 410 398 if args.subcommand == "clean": 399 result = opsi.clean( args.src ) 400 elif args.subcommand == "copy": 401 result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot ) 402 elif args.subcommand == "createBaculaConfigFiles": 403 result = opsi.createBaculaConfigFiles() 404 elif args.subcommand == "exists": 405 result = opsi.exists( args.src ) 406 elif args.subcommand == "list": 407 result = opsi.list() 408 elif args.subcommand == "listClients": 409 result = opsi.listClients( args.product ) 410 elif args.subcommand == "info": 411 result = opsi.info( args.src ) 412 elif args.subcommand == "update": 413 result = opsi.updateClient( args.src, None, args.description, args.notes, args.mac, args.ip, args.depot ) 414 else: 415 print "not yet implemented" 416 417 if args.debug: print result 418 411 try: 412 if args.subcommand == "clean": 413 result = opsi.clean( args.src ) 414 elif args.subcommand == "copy": 415 result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot ) 416 elif args.subcommand == "createBaculaConfigFiles": 417 result = opsi.createBaculaConfigFiles() 418 elif args.subcommand == "exists": 419 result = opsi.exists( args.src ) 420 elif args.subcommand == "list": 421 result = opsi.list() 422 elif args.subcommand == "listClients": 423 result = opsi.listClients( args.product ) 424 elif args.subcommand == "info": 425 result = opsi.info( args.src ) 426 elif args.subcommand == "update": 427 result = opsi.updateClient( args.src, None, args.description, args.notes, args.mac, args.ip, args.depot ) 428 else: 429 print "not yet implemented" 430 except IOError as e: 431 result = False 432 # connection refused 433 print "failed:", e 434 435 if args.debug: print result 436 419 437 if result: 420 438 exit(0)
Note:
See TracChangeset
for help on using the changeset viewer.