source: opsi/server/dass-opsi-tools/usr/bin/opsi-client.py @ 1051

Last change on this file since 1051 was 1051, checked in by joergs, 9 years ago

added more functions

  • Property svn:executable set to *
File size: 6.3 KB
Line 
1#!/usr/bin/env python
2
3# Skript, dass ein OPSI-Rechner-Eintrag kopiert.
4# D.h. die Produkte, Anforderung.
5# Ggf. optional Stand und ggf. Versionsnummer
6# Ggf. optional ProductProperties
7
8#self.command("opsi-admin -d method host_createOpsiClient "+ \
9        #computername + " null " + "\\'"+description+"\\'" + \
10        #" \\'created by dassadmin\\' " + mac_address + " " + \
11        #ip_address)
12#method host_createOpsiClient id *opsiHostKey *description *notes *hardwareAddress *ipAddress *inventoryNumber *oneTimePassword *created *lastSeen
13#self.command("opsi-admin -d method configState_create clientconfig.depot.id " + \
14        #computername + " " + depotName)                       
15                       
16import argparse
17import jsonrpc
18from pprint import pprint
19
20UrlJsonRpc="https://joergs:linuxlinux@opsi4.joergs.dass-it:4447/rpc"
21
22class OpsiRpc:
23   
24    ProductAttributesCopy = ['actionRequest','actionResult','installationStatus','packageVersion','productVersion']
25   
26    def __init__(self, urlJsonRpc, debug=False ):
27        self.debug=debug
28        self.urlJsonRpc=urlJsonRpc
29        self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc)
30       
31    def dump(self):
32        print self.urlJsonRpc
33        print self.rpc.getClientIds_list()
34
35    def list(self):
36        return self.rpc.getClientIds_list()
37       
38    def exists(self, src):
39        return len( self.rpc.host_getObjects( [], {"id":src} ) ) == 1
40       
41    def info(self, src):
42        if not self.exists( src ):
43            print "failed: opsi client", src, "does not exist"
44            return False
45        print src + ":"
46        products = self.getProductOnClient( src, [] )
47        for i in products:
48            print i['productId'] + ": (", i['actionRequest'], i['actionProgress'], ")", i['installationStatus']
49            pprint( i, indent=4 )
50        return True
51
52    def clean(self, src):
53        if not self.exists( src ):
54            return False
55        products = self.rpc.productOnClient_getObjects( [], { 'clientId': src } )
56        self.rpc.productOnClient_deleteObjects( products )
57        if self.debug:
58            pprint( self.getProductOnClient( src ) )
59        return True
60       
61    def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None ):
62   
63        print "create/update", dst, "from template", src + ":",
64        #method host_createOpsiClient id *opsiHostKey *description *notes *hardwareAddress *ipAddress *inventoryNumber *oneTimePassword *created *lastSeen
65        #id=dst
66        opsiHostKey=None
67        description=""
68        notes="copy of " + src
69        self.rpc.host_createOpsiClient( dst, opsiHostKey, description, notes, hardwareAddress, ipAddress )
70        if depot:
71            self.rpc.configState_create( "clientconfig.depot.id", dst, depot )       
72        if self.debug:
73            pprint( self.getProductOnClient( src ) )
74        self.copyProductOnClient( src, dst )
75        # TODO:
76        #  copy product properties:
77        #  opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
78        print "done"
79
80       
81    def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
82        #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
83        #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
84        return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
85       
86    def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ):
87        products = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } )
88        for i in products:
89            if self.debug:
90                print i['productId']
91                pprint( i )
92            i['clientId'] = dst
93            self.rpc.productOnClient_createObjects( i )
94        if self.debug:
95            pprint( self.getProductOnClient( dst ) )
96       
97
98
99if __name__ == '__main__':
100    parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.')
101    parser.add_argument( '--debug', action='store_true', help="enable debugging output" )
102    #parser.add_argument( '--verbose', type=bool, help="add debugging output" )
103    parser.add_argument( '--url', help="OPSI Server JSON-RPC url, normally https://<username>:<password>@<OPSI-SERVER>:4447/rpc" )
104
105    subparsers = parser.add_subparsers(title='subcommands',
106        description='valid subcommands',
107        help='additional help',
108        dest='subcommand' )
109
110    parser_list = subparsers.add_parser('list', help='list all opsi clients' )
111
112    parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' )
113    parser_exists.add_argument( 'src', help="source opsi client" )
114
115    parser_info = subparsers.add_parser('info', help='print information about a opsi client' )
116    parser_info.add_argument( 'src', help="opsi client" )
117       
118    parser_clean = subparsers.add_parser('clean', help='remove all product states from a opsi client' )
119    parser_clean.add_argument( 'src', help="source opsi client to clean" )
120               
121    parser_copy = subparsers.add_parser('copy', help='copy/create a opsi client from a template opsi client')   
122    parser_copy.add_argument( 'src', help="source/template opsi client" )
123    parser_copy.add_argument( 'dst', help="opsi client to be created" )
124    parser_copy.add_argument( '--ip', help="IP address of the new opsi client" )
125    parser_copy.add_argument( '--mac', help="MAC address of the new opsi client" )
126    parser_copy.add_argument( '--depot', help="depot server the new opsi client should be located" )
127   
128    args = parser.parse_args()
129   
130   
131    urlJsonRpc = UrlJsonRpc
132    if args.url:
133        urlJsonRpc = args.url
134   
135    opsi=OpsiRpc( urlJsonRpc, args.debug )
136   
137    result = True
138
139    if args.subcommand == "list":
140        print( "\n".join( opsi.list() ) )
141    elif args.subcommand == "exists":
142        result = opsi.exists( args.src )
143    elif args.subcommand == "info":
144        result = opsi.info( args.src )       
145    elif args.subcommand == "clean":
146        result = opsi.clean( args.src )
147    elif args.subcommand == "copy":
148        opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
149    else:
150        print "not yet implemented"
151
152    if args.debug: print result   
153       
154    if result:
155        exit(0)
156    else:
157        exit(1)
Note: See TracBrowser for help on using the repository browser.