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

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

beautified

  • Property svn:executable set to *
File size: 6.8 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        host = self.rpc.host_getHashes( [], {"id":src} )[0]
47        print "  IP:", host["ipAddress"]
48        print "  MAC:", host["hardwareAddress"]
49        print "  inventory:", host["inventoryNumber"]
50        print "  last seen:", host["lastSeen"]
51        print "  notes:", host["notes"]
52       
53        print "  products:"
54        products = self.getProductOnClient( src, [] )
55        for i in products:
56            print "    " + i['productId'] + ":"
57            print "      " + i['installationStatus'], "(",
58            if i['actionRequest']:
59                print i['actionRequest'],
60                if i['actionProgress']:
61                    print i['actionProgress'],
62            print ")"
63            print "      ",
64            pprint( i, indent=8 )
65        return True
66
67    def clean(self, src):
68        if not self.exists( src ):
69            return False
70        products = self.rpc.productOnClient_getObjects( [], { 'clientId': src } )
71        self.rpc.productOnClient_deleteObjects( products )
72        if self.debug:
73            pprint( self.getProductOnClient( src ) )
74        return True
75       
76    def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None ):
77   
78        print "create/update", dst, "from template", src + ":",
79        #method host_createOpsiClient id *opsiHostKey *description *notes *hardwareAddress *ipAddress *inventoryNumber *oneTimePassword *created *lastSeen
80        #id=dst
81        opsiHostKey=None
82        description=""
83        notes="copy of " + src
84        self.rpc.host_createOpsiClient( dst, opsiHostKey, description, notes, hardwareAddress, ipAddress )
85        if depot:
86            self.rpc.configState_create( "clientconfig.depot.id", dst, depot )       
87        if self.debug:
88            pprint( self.getProductOnClient( src ) )
89        self.copyProductOnClient( src, dst )
90        # TODO:
91        #  copy product properties:
92        #  opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
93        print "done"
94        return True
95
96       
97    def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
98        #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
99        #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
100        return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
101       
102    def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ):
103        products = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } )
104        for i in products:
105            if self.debug:
106                print i['productId']
107                pprint( i )
108            i['clientId'] = dst
109            self.rpc.productOnClient_createObjects( i )
110        if self.debug:
111            pprint( self.getProductOnClient( dst ) )
112       
113
114
115if __name__ == '__main__':
116    parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.')
117    parser.add_argument( '--debug', action='store_true', help="enable debugging output" )
118    #parser.add_argument( '--verbose', type=bool, help="add debugging output" )
119    parser.add_argument( '--url', help="OPSI Server JSON-RPC url, normally https://<username>:<password>@<OPSI-SERVER>:4447/rpc" )
120
121    subparsers = parser.add_subparsers(title='subcommands',
122        description='valid subcommands',
123        help='additional help',
124        dest='subcommand' )
125
126    parser_list = subparsers.add_parser('list', help='list all opsi clients' )
127
128    parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' )
129    parser_exists.add_argument( 'src', help="source opsi client" )
130
131    parser_info = subparsers.add_parser('info', help='print information about a opsi client' )
132    parser_info.add_argument( 'src', help="opsi client" )
133       
134    parser_clean = subparsers.add_parser('clean', help='remove all product states from a opsi client' )
135    parser_clean.add_argument( 'src', help="source opsi client to clean" )
136               
137    parser_copy = subparsers.add_parser('copy', help='copy/create a opsi client from a template opsi client')   
138    parser_copy.add_argument( 'src', help="source/template opsi client" )
139    parser_copy.add_argument( 'dst', help="opsi client to be created" )
140    parser_copy.add_argument( '--ip', help="IP address of the new opsi client" )
141    parser_copy.add_argument( '--mac', help="MAC address of the new opsi client" )
142    parser_copy.add_argument( '--depot', help="depot server the new opsi client should be located" )
143   
144    args = parser.parse_args()
145   
146   
147    urlJsonRpc = UrlJsonRpc
148    if args.url:
149        urlJsonRpc = args.url
150   
151    opsi=OpsiRpc( urlJsonRpc, args.debug )
152   
153    result = True
154
155    if args.subcommand == "list":
156        print( "\n".join( opsi.list() ) )
157    elif args.subcommand == "exists":
158        result = opsi.exists( args.src )
159    elif args.subcommand == "info":
160        result = opsi.info( args.src )       
161    elif args.subcommand == "clean":
162        result = opsi.clean( args.src )
163    elif args.subcommand == "copy":
164        result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
165    else:
166        print "not yet implemented"
167
168    if args.debug: print result   
169       
170    if result:
171        exit(0)
172    else:
173        exit(1)
Note: See TracBrowser for help on using the repository browser.