source: opsi/server/dass-opsi-tools/usr/bin/opsirpc.py @ 1085

Last change on this file since 1085 was 1085, checked in by slederer, 10 years ago

Klasse OpsiRpc in separate Datei ausgelagert

  • Property svn:executable set to *
File size: 4.3 KB
Line 
1#!/usr/bin/env python
2
3# -*- coding: utf-8 -*-
4
5"""ospi-client: performs operation for opsi clients on opsi server via JSON-RPC."""
6
7__author__ = "Joerg Steffens"
8__copyright__ = "Copyright 2012, dass IT GmbH"
9__license__ = "GPL"
10__version__ = "1.0"
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. Versionsnummer
17# Ggf. optional ProductProperties
18#
19
20import jsonrpc
21from pprint import pprint
22
23class OpsiRpc:
24   
25   
26    ProductAttributesCopy = ['actionRequest','actionResult','installationStatus','packageVersion','productVersion']
27   
28    def __init__(self, urlJsonRpc, debug=False ):
29        self.debug=debug
30        self.urlJsonRpc=urlJsonRpc
31        self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc)
32       
33    def dump(self):
34        print self.urlJsonRpc
35        print self.rpc.getClientIds_list()
36
37    def list(self):
38        return self.rpc.getClientIds_list()
39       
40    def exists(self, src):
41        return len( self.rpc.host_getObjects( [], {"id":src} ) ) == 1
42       
43    def info(self, src):
44        if not self.exists( src ):
45            print "failed: opsi client", src, "does not exist"
46            return False
47        print src + ":"
48        host = self.rpc.host_getHashes( [], {"id":src} )[0]
49        print "  IP:", host["ipAddress"]
50        print "  MAC:", host["hardwareAddress"]
51        print "  inventory:", host["inventoryNumber"]
52        print "  last seen:", host["lastSeen"]
53        print "  notes:", host["notes"]
54       
55        print "  products:"
56        products = self.getProductOnClient( src, [] )
57        for i in products:
58            print "    " + i['productId'] + ":"
59            print "      " + i['installationStatus'], "(",
60            if i['actionRequest']:
61                print i['actionRequest'],
62                if i['actionProgress']:
63                    print i['actionProgress'],
64            print ")"
65            print "      ",
66            pprint( i, indent=8 )
67        return True
68
69    def clean(self, src):
70        if not self.exists( src ):
71            return False
72        products = self.rpc.productOnClient_getObjects( [], { 'clientId': src } )
73        self.rpc.productOnClient_deleteObjects( products )
74        if self.debug:
75            pprint( self.getProductOnClient( src ) )
76        return True
77       
78    def createClient(self, name, opsiHostKey, description, notes, hardwareAddress, ipAddress):
79        self.rpc.host_createOpsiClient( name, opsiHostKey, description, notes, hardwareAddress, ipAddress )
80
81    def clientSetDepot(self, name, depot):
82        self.rpc.configState_create( "clientconfig.depot.id", name, depot )       
83
84    def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None, description = "" ):
85   
86        print "create/update", dst, "from template", src + ":",
87        #method host_createOpsiClient id *opsiHostKey *description *notes *hardwareAddress *ipAddress *inventoryNumber *oneTimePassword *created *lastSeen
88        #id=dst
89        opsiHostKey=None
90        notes="copy of " + src
91        self.createClient(dst, opsiHostKey, description, notes, hardwareAddress, ipAddress)
92        if depot:
93            self.clientSetDepot(dst,depot)
94        if self.debug:
95            pprint( self.getProductOnClient( src ) )
96        self.copyProductOnClient( src, dst )
97        # TODO:
98        #  copy product properties:
99        #  opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
100        print "done"
101        return True
102
103       
104    def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
105        #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
106        #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
107        return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
108       
109    def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ):
110        products = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } )
111        for i in products:
112            if self.debug:
113                print i['productId']
114                pprint( i )
115            i['clientId'] = dst
116            self.rpc.productOnClient_createObjects( i )
117        if self.debug:
118            pprint( self.getProductOnClient( dst ) )
Note: See TracBrowser for help on using the repository browser.