Changeset 1110
- Timestamp:
- Sep 22, 2012, 9:13:30 PM (12 years ago)
- Location:
- opsi/server/dass-opsi-tools/usr/bin
- Files:
-
- 1 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
opsi/server/dass-opsi-tools/usr/bin/opsiclient
r1109 r1110 8 8 __copyright__ = "Copyright 2012, dass IT GmbH" 9 9 __license__ = "GPL" 10 __version__ = "1. 0"10 __version__ = "1.1" 11 11 __email__ = "joerg.steffens@dass-it.de" 12 13 #14 # Skript, dass ein OPSI-Rechner-Eintrag kopiert.15 # D.h. die Produkte, Anforderung.16 # Ggf. optional Stand und ggf. Versionsnummer17 # Ggf. optional ProductProperties18 #19 12 20 13 #self.command("opsi-admin -d method host_createOpsiClient "+ \ … … 27 20 import argparse 28 21 import jsonrpc 22 import logging 23 import os 29 24 from pprint import pprint 25 import time 30 26 31 27 UrlJsonRpc="https://<username>:<password>@opsi:4447/rpc" … … 40 36 41 37 def __init__(self, urlJsonRpc = UrlJsonRpcDefault, debug=False ): 38 self.logger=logging.getLogger(__name__) 42 39 self.debug=debug 43 40 self.urlJsonRpc=urlJsonRpc 44 41 self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc) 45 46 def dump(self): 47 print self.urlJsonRpc 48 print self.rpc.getClientIds_list() 49 42 self.logger.debug( "initialized: " + self.urlJsonRpc ) 43 44 50 45 def list(self): 51 return self.rpc.getClientIds_list() 46 print( "\n".join( self.rpc.getClientIds_list() ) ) 47 return True 48 49 50 def getClientsWithProduct( self, product ): 51 return self.rpc.productOnClient_getObjects( [], { "productId": product, "installationStatus": "installed" } ) 52 53 54 def listClients( self, product ): 55 if product: 56 for client in self.getClientsWithProduct( product ): 57 print client['clientId'] 58 else: 59 return self.list() 60 return True 61 52 62 53 63 def exists(self, src): … … 135 145 return True 136 146 137 147 148 138 149 def getProductOnClient( self, client, attributes = ProductAttributesCopy ): 139 #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )140 #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )141 150 return self.rpc.productOnClient_getHashes( [], { 'clientId': client } ) 142 151 152 153 143 154 def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ): 144 155 products_src = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } ) … … 170 181 if self.debug: 171 182 pprint( self.getProductPropertyState( dst ) ) 183 184 185 def getClientProductProperty( self, client, product ): 186 return self.rpc.getProductProperties_hash( product, [ client ] ) 187 188 def write_client_conf( self, fd, client, properties ): 189 #Client { 190 #Name = ting-fd 191 #Address = ting.dass-it 192 #FDPort = 9102 193 #Catalog = MyCatalog 194 #Password = "D5w2V5w6B8a9H5Z" 195 #File Retention = 6 months 196 #Job Retention = 6 months 197 #AutoPrune = yes 198 #} 199 params = [ "FDPort", "FileRetention", "JobRetention", "AutoPrune" ] 200 fd.write( "Client {\n" ) 201 fd.write( ' Name = "' + properties['filedaemon_full_name'] + '"' + "\n" ) 202 fd.write( ' Address = "' + client['clientId'] + '"' + "\n" ) 203 # ipAddress: method host_getObjects [] '{"id":client['clientId']}' 204 #print " # Address =", ipAddress 205 fd.write( ' Password = "' + properties['filedaemon_full_password'] + '"' + "\n" ) 206 try: 207 catalog = properties['catalog'] 208 except KeyError: 209 catalog = "MyCatalog" 210 fd.write( ' Catalog = "' + catalog + '"' + "\n" ) 211 for i in params: 212 try: 213 fd.write( ' ' + i + ' = "' + properties[i.lower()] + '"' + "\n" ) 214 except KeyError: 215 fd.write( ' # ' + i + " = \n" ) 216 fd.write( "}\n") 217 fd.write( "\n" ) 218 219 220 221 222 def write_job_conf( self, fd, client, properties ): 223 #Job { 224 #FileSet = "tingfileset" 225 #Name = "ting" 226 #Client = ting-fd 227 #JobDefs = "LaptopJob" 228 ## Write Bootstrap = "/var/lib/bacula/ting.bsr" 229 #} 230 params = [ "Fileset", "JobDefs" ] 231 fd.write( "Job {" + "\n" ) 232 fd.write( ' Name = "' + client['clientId'] + '-job"' + "\n" ) 233 fd.write( ' Client = "' + properties['filedaemon_full_name'] + '"' + "\n" ) 234 for i in params: 235 fd.write( " " ) 236 try: 237 if not properties[i.lower()]: 238 fd.write( "# " ) 239 fd.write( i + ' = "' + properties[i.lower()] + '"' + "\n" ) 240 except KeyError: 241 fd.write( "# " + i + " = " + "\n" ) 242 fd.write( "}" + "\n" ) 243 fd.write( "\n" ) 244 245 246 def write_config_file_header( self, fd ): 247 try: 248 fd.write( "#\n" ) 249 fd.write( "# automatically generated at {}\n".format( time.asctime() ) ) 250 fd.write( "#\n\n" ) 251 except BaseException as e: 252 self.logger.exception( "failed to create files" ) 253 return False 254 return True 172 255 173 256 257 def createBaculaConfigFiles( self ): 258 clientsWithBacula=self.getClientsWithProduct( "bacula" ) 259 if clientsWithBacula: 260 try: 261 file_opsi_clients = open('opsi-clients-generated.conf', 'w') 262 self.write_config_file_header( file_opsi_clients ) 263 264 file_opsi_jobs = open('opsi-jobs-generated.conf', 'w') 265 self.write_config_file_header( file_opsi_jobs ) 266 except BaseException as e: 267 self.logger.exception( "failed to create files" ) 268 return False 269 for client in clientsWithBacula: 270 clientId = client['clientId'] 271 try: 272 clientBaculaProperties=self.getClientProductProperty( clientId, "bacula" ) 273 except ValueError as e: 274 self.logger.warn( "%s: no valid information found: %s" %(clientId, e) ) 275 else: 276 if clientBaculaProperties: 277 #pprint( clientBaculaProperties ) 278 self.write_client_conf( file_opsi_clients, client, clientBaculaProperties ) 279 self.write_job_conf( file_opsi_jobs, client, clientBaculaProperties ) 280 self.logger.info( "%s: OK" % clientId ) 281 else: 282 self.logger.warn( "%s: failed: no product properties defined" %(clientId) ) 283 return True 284 174 285 175 286 176 287 if __name__ == '__main__': 288 logging.basicConfig(format='%(message)s') 289 logger = logging.getLogger(__name__) 290 logger.setLevel(logging.INFO) 291 177 292 parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.', epilog=HelpEpilog ) 178 293 179 parser.add_argument( '--url', required=True, help="OPSI Server JSON-RPC url, in following format: " + UrlJsonRpc )180 181 294 parser.add_argument( '--debug', action='store_true', help="enable debugging output" ) 182 #parser.add_argument( '--verbose', type=bool, help="add debugging output" ) 183 295 296 parser_url = parser.add_mutually_exclusive_group(required=True) 297 parser_url.add_argument( '--url', help="OPSI Server JSON-RPC url, in following format: " + UrlJsonRpc ) 298 299 parser_url.add_argument( '--server', help="OPSI Server (instead of URL)" ) 300 username_default=os.getlogin() 301 302 parser.add_argument( '--username', help="username (instead of URL), default: " + username_default, default=username_default ) 303 parser.add_argument( '--password', help="password (instead of URL)" ) 304 184 305 subparsers = parser.add_subparsers(title='subcommands', 185 306 description='valid subcommands', … … 187 308 dest='subcommand' ) 188 309 189 parser_list = subparsers.add_parser('list', help='list all opsi clients' )190 191 parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' )192 parser_exists.add_argument( 'src', help="source opsi client" )193 194 parser_info = subparsers.add_parser('info', help='print information about a opsi client' )195 parser_info.add_argument( 'src', help="opsi client" )196 197 310 parser_clean = subparsers.add_parser('clean', help='remove all product states from a opsi client' ) 198 311 parser_clean.add_argument( 'src', help="source opsi client to clean" ) … … 205 318 parser_copy.add_argument( '--depot', help="depot server the new opsi client should be located" ) 206 319 #parser_copy.add_argument( '--no-properties', action='store_false', help="don't copy product properties" ) 320 321 parser_createBaculaConfigFiles = subparsers.add_parser('createBaculaConfigFiles', help='create Bacula config files for all clients that have bacula installed') 322 323 parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' ) 324 parser_exists.add_argument( 'src', help="source opsi client" ) 325 326 #parser_list = subparsers.add_parser('list', help='list all opsi clients' ) 327 328 parser_listClients = subparsers.add_parser('listClients', help='list opsi clients') 329 parser_listClients.add_argument( '--product', help="only list clients, that have product installed" ) 330 331 parser_info = subparsers.add_parser('info', help='print information about a opsi client' ) 332 parser_info.add_argument( 'src', help="opsi client" ) 207 333 208 334 args = parser.parse_args() 209 335 210 opsi=OpsiRpc( args.url, args.debug ) 336 if args.debug: 337 logger.setLevel(logging.DEBUG) 338 339 url=args.url 340 if (not url): 341 if args.server: 342 account="" 343 if args.username and args.password: 344 account=args.username + ":" + args.password + "@" 345 elif args.username: 346 account=args.username + "@" 347 url="https://" + account + args.server + ":4447/rpc" 348 else: 349 parser.error( "argument --url is required" ) 350 351 opsi=OpsiRpc( url, args.debug ) 211 352 212 353 result = True 213 354 214 if args.subcommand == "list": 215 print( "\n".join( opsi.list() ) ) 355 if args.subcommand == "clean": 356 result = opsi.clean( args.src ) 357 elif args.subcommand == "copy": 358 result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot ) 359 elif args.subcommand == "createBaculaConfigFiles": 360 result = opsi.createBaculaConfigFiles() 216 361 elif args.subcommand == "exists": 217 result = opsi.exists( args.src ) 362 result = opsi.exists( args.src ) 363 elif args.subcommand == "list": 364 result = opsi.list() 365 elif args.subcommand == "listClients": 366 result = opsi.listClients( args.product ) 218 367 elif args.subcommand == "info": 219 368 result = opsi.info( args.src ) 220 elif args.subcommand == "clean":221 result = opsi.clean( args.src )222 elif args.subcommand == "copy":223 #result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot, not args.no-properties )224 result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )225 369 else: 226 370 print "not yet implemented"
Note:
See TracChangeset
for help on using the changeset viewer.