Changeset 1085


Ignore:
Timestamp:
Aug 29, 2012, 1:09:59 PM (12 years ago)
Author:
slederer
Message:

Klasse OpsiRpc in separate Datei ausgelagert

Location:
opsi/server/dass-opsi-tools/usr/bin
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • opsi/server/dass-opsi-tools/usr/bin/opsiclient

    r1078 r1085  
    2626                       
    2727import argparse
    28 import jsonrpc
    2928from pprint import pprint
     29from opsirpc import OpsiRpc
    3030
    3131UrlJsonRpc="https://<username>:<password>@opsi:4447/rpc"
    3232
    3333HelpEpilog="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."
    34 
    35 class OpsiRpc:
    36 
    37     UrlJsonRpcDefault="https://opsi:4447/rpc"
    38    
    39     ProductAttributesCopy = ['actionRequest','actionResult','installationStatus','packageVersion','productVersion']
    40    
    41     def __init__(self, urlJsonRpc = UrlJsonRpcDefault, debug=False ):
    42         self.debug=debug
    43         self.urlJsonRpc=urlJsonRpc
    44         self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc)
    45        
    46     def dump(self):
    47         print self.urlJsonRpc
    48         print self.rpc.getClientIds_list()
    49 
    50     def list(self):
    51         return self.rpc.getClientIds_list()
    52        
    53     def exists(self, src):
    54         return len( self.rpc.host_getObjects( [], {"id":src} ) ) == 1
    55        
    56     def info(self, src):
    57         if not self.exists( src ):
    58             print "failed: opsi client", src, "does not exist"
    59             return False
    60         print src + ":"
    61         host = self.rpc.host_getHashes( [], {"id":src} )[0]
    62         print "  IP:", host["ipAddress"]
    63         print "  MAC:", host["hardwareAddress"]
    64         print "  inventory:", host["inventoryNumber"]
    65         print "  last seen:", host["lastSeen"]
    66         print "  notes:", host["notes"]
    67        
    68         print "  products:"
    69         products = self.getProductOnClient( src, [] )
    70         for i in products:
    71             print "    " + i['productId'] + ":"
    72             print "      " + i['installationStatus'], "(",
    73             if i['actionRequest']:
    74                 print i['actionRequest'],
    75                 if i['actionProgress']:
    76                     print i['actionProgress'],
    77             print ")"
    78             print "      ",
    79             pprint( i, indent=8 )
    80         return True
    81 
    82     def clean(self, src):
    83         if not self.exists( src ):
    84             return False
    85         products = self.rpc.productOnClient_getObjects( [], { 'clientId': src } )
    86         self.rpc.productOnClient_deleteObjects( products )
    87         if self.debug:
    88             pprint( self.getProductOnClient( src ) )
    89         return True
    90        
    91     def createClient(self, name, opsiHostKey, description, notes, hardwareAddress, ipAddress):
    92         self.rpc.host_createOpsiClient( name, opsiHostKey, description, notes, hardwareAddress, ipAddress )
    93 
    94     def clientSetDepot(self, name, depot):
    95         self.rpc.configState_create( "clientconfig.depot.id", name, depot )       
    96 
    97     def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None, description = "" ):
    98    
    99         print "create/update", dst, "from template", src + ":",
    100         obj = {
    101           "id" : dst,
    102           "type" : "OpsiClient",
    103           "notes" : "copy of " + src,
    104           "description" : description,
    105           #"inventoryNumber" : "",
    106         }
    107         if hardwareAddress:
    108             obj['hardwareAddress'] = hardwareAddress
    109         if ipAddress:
    110             obj['ipAddress'] = ipAddress
    111 
    112         if self.exists( dst ):
    113             self.rpc.host_updateObject(obj)
    114         else:
    115             self.rpc.host_insertObject(obj)
    116            
    117         if depot:
    118             self.clientSetDepot(dst,depot)
    119            
    120         if self.debug:
    121             pprint( self.getProductOnClient( src ) )
    122         self.copyProductOnClient( src, dst )
    123         # TODO:
    124         #  copy product properties:
    125         #  opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
    126         print "done"
    127         return True
    128 
    129        
    130     def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
    131         #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
    132         #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
    133         return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
    134        
    135     def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ):
    136         products = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } )
    137         for i in products:
    138             if self.debug:
    139                 print i['productId']
    140                 pprint( i )
    141             i['clientId'] = dst
    142             self.rpc.productOnClient_createObjects( i )
    143         if self.debug:
    144             pprint( self.getProductOnClient( dst ) )
    145        
    146 
    14734
    14835if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.