Ignore:
Timestamp:
Aug 10, 2012, 7:44:04 PM (12 years ago)
Author:
joergs
Message:

initially working

File:
1 edited

Legend:

Unmodified
Added
Removed
  • opsi/server/opsi-tools-dass-it/usr/bin/opsi-client-copy.py

    r1046 r1047  
    1313                #self.command("opsi-admin -d method configState_create clientconfig.depot.id " + \
    1414                        #computername + " " + depotName)                       
    15 
     15                       
     16import argparse
    1617import jsonrpc
    1718from pprint import pprint
     
    2021
    2122class OpsiRpc:
    22     def __init__(self, urlJsonRpc=UrlJsonRpc):
     23   
     24    ProductAttributesCopy = ['actionRequest','actionResult','installationStatus','packageVersion','productVersion']
     25   
     26    def __init__(self, urlJsonRpc, debug=False ):
     27        self.debug=debug
    2328        self.urlJsonRpc=urlJsonRpc
    24         self.rpcProxy=jsonrpc.ServiceProxy(self.urlJsonRpc)
     29        self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc)
    2530       
    2631    def dump(self):
    2732        print self.urlJsonRpc
    28         print self.rpcProxy.getClientIds_list()
     33        print self.rpc.getClientIds_list()
    2934       
    30     def copyClient( self, src, dst, hardwareAddress, ipAddress ):
     35    def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None ):
    3136   
     37        print "create/update", dst, "from template", src + ":",
    3238        #method host_createOpsiClient id *opsiHostKey *description *notes *hardwareAddress *ipAddress *inventoryNumber *oneTimePassword *created *lastSeen
    33         id=dst
     39        #id=dst
    3440        opsiHostKey=None
    3541        description=""
    3642        notes="copy of " + src
    37         self.rpcProxy.host_createOpsiClient( id, opsiHostKey, description, notes, hardwareAddress, ipAddress )
     43        self.rpc.host_createOpsiClient( dst, opsiHostKey, description, notes, hardwareAddress, ipAddress )
     44        if depot:
     45            self.rpc.configState_create( "clientconfig.depot.id", dst, depot )       
     46        if self.debug:
     47            self.productOnClient( src )
     48        self.copyProductOnClient( src, dst )
     49        # TODO:
     50        #  copy product properties:
     51        #  opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
     52        print "done"
     53
    3854       
    39     def productOnClient( self, client ):
    40         pprint( self.rpcProxy.productOnClient_getObjects( [], { 'clientId': client } ) )
    41 
     55    def productOnClient( self, client, attributes = ProductAttributesCopy ):
     56        #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
     57        #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
     58        pprint( self.rpc.productOnClient_getHashes( [], { 'clientId': client } ) )
     59       
     60    def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ):
     61        products = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } )
     62        for i in products:
     63            if self.debug:
     64                pprint( i )
     65                print i['productId']
     66            i['clientId'] = dst
     67            self.rpc.productOnClient_createObjects( i )
     68            if self.debug:
     69                self.productOnClient( dst )
     70       
    4271
    4372
    4473if __name__ == '__main__':
    45     opsi=OpsiRpc()
    46     #opsi.dump()
    47     opsi.copyClient( "test1.joergs.dass-it.opsi", "testnew1.joergs.dass-it.opsi", "00:00:00:00:01:01", "192.168.0.1" )
    48     opsi.productOnClient( "opsiwin1.joergs.dass-it.opsi" )
    49     opsi.dump()
    50     print "end"
     74    parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.')
     75    parser.add_argument( '--debug', action='store_true', help="enable debugging output" )
     76    #parser.add_argument( '--verbose', type=bool, help="add debugging output" )
     77    parser.add_argument( 'src', help="source/template opsi client" )
     78    parser.add_argument( 'dst', help="opsi client to be created" )
     79    parser.add_argument( '--ip', help="IP address of the new opsi client" )
     80    parser.add_argument( '--mac', help="MAC address of the new opsi client" )
     81    parser.add_argument( '--depot', help="depot server the new opsi client should be located" )
     82    parser.add_argument( '--url', help="OPSI Server JSON-RPC url, normally https://<username>:<password>@<OPSI-SERVER>:4447/rpc" )
     83
     84
     85    args = parser.parse_args()
    5186   
     87    urlJsonRpc = UrlJsonRpc
     88    if args.url:
     89        urlJsonRpc = args.url
     90   
     91    opsi=OpsiRpc( urlJsonRpc, args.debug )
     92    opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
     93    if args.debug: print "end"
     94   
Note: See TracChangeset for help on using the changeset viewer.