source: opsi/server/dass-opsi-tools/usr/bin/opsiclient@ 1098

Last change on this file since 1098 was 1088, checked in by joergs, on Aug 29, 2012 at 7:24:39 PM

copyProductPropertyState

  • Property svn:executable set to *
File size: 8.9 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
20#self.command("opsi-admin -d method host_createOpsiClient "+ \
21 #computername + " null " + "\\'"+description+"\\'" + \
22 #" \\'created by dassadmin\\' " + mac_address + " " + \
23 #ip_address)
24#self.command("opsi-admin -d method configState_create clientconfig.depot.id " + \
25 #computername + " " + depotName)
26
27import argparse
28import jsonrpc
29from pprint import pprint
30
31UrlJsonRpc="https://<username>:<password>@opsi:4447/rpc"
32
33HelpEpilog="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
35class 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
88 products = self.rpc.productPropertyState_getObjects( [], { 'clientId': src } )
89 self.rpc.productPropertyState_deleteObjects( products )
90
91 if self.debug:
92 pprint( self.getProductOnClient( src ) )
93 return True
94
95 def createClient(self, name, opsiHostKey, description, notes, hardwareAddress, ipAddress):
96 self.rpc.host_createOpsiClient( name, opsiHostKey, description, notes, hardwareAddress, ipAddress )
97
98 def clientSetDepot(self, name, depot):
99 self.rpc.configState_create( "clientconfig.depot.id", name, depot )
100
101 def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None, description = "", copyProperties = True ):
102
103 print "create/update", dst, "from template", src + ":",
104 obj = {
105 "id" : dst,
106 "type" : "OpsiClient",
107 "notes" : "copy of " + src,
108 "description" : description,
109 #"inventoryNumber" : "",
110 }
111 if hardwareAddress:
112 obj['hardwareAddress'] = hardwareAddress
113 if ipAddress:
114 obj['ipAddress'] = ipAddress
115
116 if self.exists( dst ):
117 self.rpc.host_updateObject(obj)
118 else:
119 self.rpc.host_insertObject(obj)
120
121 if depot:
122 self.clientSetDepot(dst,depot)
123
124 if self.debug:
125 pprint( self.getProductOnClient( src ) )
126 self.copyProductOnClient( src, dst )
127 # TODO:
128 # copy product properties:
129 # opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
130 if copyProperties:
131 if self.debug:
132 print "copy product properties"
133 self.copyProductPropertyState( src, dst )
134 print "done"
135 return True
136
137
138 def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
139 #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
140 #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
141 return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
142
143 def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ):
144 products_src = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } )
145 products_dst = []
146 for i in products_src:
147 if self.debug:
148 print i['productId']
149 pprint( i )
150 i['clientId'] = dst
151 products_dst.append(i)
152 self.rpc.productOnClient_createObjects( products_dst )
153 if self.debug:
154 pprint( self.getProductOnClient( dst ) )
155
156 def getProductPropertyState( self, client, attributes = [] ):
157 return self.rpc.productPropertyState_getHashes( [], { 'objectId': client } )
158
159 def copyProductPropertyState( self, src, dst, attributes = [] ):
160 productProperties_src = self.getProductPropertyState( src, attributes )
161 productProperties_dst = []
162 for i in productProperties_src:
163 if self.debug:
164 print i['productId'], "-", i["propertyId"] + ": ",
165 pprint(i["values"])
166 #pprint( i )
167 i['objectId'] = dst
168 productProperties_dst.append(i)
169 self.rpc.productPropertyState_createObjects( productProperties_dst )
170 if self.debug:
171 pprint( self.getProductPropertyState( dst ) )
172
173
174
175
176if __name__ == '__main__':
177 parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.', epilog=HelpEpilog )
178
179 parser.add_argument( '--url', required=True, help="OPSI Server JSON-RPC url, in following format: " + UrlJsonRpc )
180
181 parser.add_argument( '--debug', action='store_true', help="enable debugging output" )
182 #parser.add_argument( '--verbose', type=bool, help="add debugging output" )
183
184 subparsers = parser.add_subparsers(title='subcommands',
185 description='valid subcommands',
186 help='additional help',
187 dest='subcommand' )
188
189 parser_list = subparsers.add_parser('list', help='list all opsi clients' )
190
191 parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' )
192 parser_exists.add_argument( 'src', help="source opsi client" )
193
194 parser_info = subparsers.add_parser('info', help='print information about a opsi client' )
195 parser_info.add_argument( 'src', help="opsi client" )
196
197 parser_clean = subparsers.add_parser('clean', help='remove all product states from a opsi client' )
198 parser_clean.add_argument( 'src', help="source opsi client to clean" )
199
200 parser_copy = subparsers.add_parser('copy', help='copy/create a opsi client from a template opsi client')
201 parser_copy.add_argument( 'src', help="source/template opsi client" )
202 parser_copy.add_argument( 'dst', help="opsi client to be created" )
203 parser_copy.add_argument( '--ip', help="IP address of the new opsi client" )
204 parser_copy.add_argument( '--mac', help="MAC address of the new opsi client" )
205 parser_copy.add_argument( '--depot', help="depot server the new opsi client should be located" )
206 #parser_copy.add_argument( '--no-properties', action='store_false', help="don't copy product properties" )
207
208 args = parser.parse_args()
209
210 opsi=OpsiRpc( args.url, args.debug )
211
212 result = True
213
214 if args.subcommand == "list":
215 print( "\n".join( opsi.list() ) )
216 elif args.subcommand == "exists":
217 result = opsi.exists( args.src )
218 elif args.subcommand == "info":
219 result = opsi.info( args.src )
220 elif args.subcommand == "clean":
221 result = opsi.clean( args.src )
222 elif args.subcommand == "copy":
223 #result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot, not args.no-properties )
224 result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
225 else:
226 print "not yet implemented"
227
228 if args.debug: print result
229
230 if result:
231 exit(0)
232 else:
233 exit(1)
Note: See TracBrowser for help on using the repository browser.