source: opsi/server/dass-opsi-tools/usr/bin/opsi-client@ 1070

Last change on this file since 1070 was 1067, checked in by slederer, on Aug 16, 2012 at 2:11:15 PM

Methoden clientCreate und clientSetDepot hinzugefügt, Description-Parameter bei copyClient

  • Property svn:executable set to *
File size: 7.5 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 if self.debug:
88 pprint( self.getProductOnClient( src ) )
89 return True
90
91 def createClient(self, name, opsiHostKey, description, notes, hardwareAddress, ipAddress):
92 self.rpc.host_createOpsiClient( name, opsiHostKey, description, notes, hardwareAddress, ipAddress )
93
94 def clientSetDepot(self, name, depot):
95 self.rpc.configState_create( "clientconfig.depot.id", name, depot )
96
97 def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None, description = "" ):
98
99 print "create/update", dst, "from template", src + ":",
100 obj = {
101 "id" : dst,
102 "type" : "OpsiClient",
103 "notes" : "copy of " + src,
104 "description" : description,
105 #"inventoryNumber" : "",
106 }
107 if hardwareAddress:
108 obj['hardwareAddress'] = hardwareAddress
109 if ipAddress:
110 obj['ipAddress'] = ipAddress
111
112 if self.exists( dst ):
113 self.rpc.host_updateObject(obj)
114 else:
115 self.rpc.host_insertObject(obj)
116
117 if depot:
118 self.clientSetDepot(dst,depot)
119
120 if self.debug:
121 pprint( self.getProductOnClient( src ) )
122 self.copyProductOnClient( src, dst )
123 # TODO:
124 # copy product properties:
125 # opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
126 print "done"
127 return True
128
129
130 def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
131 #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
132 #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
133 return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
134
135 def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ):
136 products = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } )
137 for i in products:
138 if self.debug:
139 print i['productId']
140 pprint( i )
141 i['clientId'] = dst
142 self.rpc.productOnClient_createObjects( i )
143 if self.debug:
144 pprint( self.getProductOnClient( dst ) )
145
146
147
148if __name__ == '__main__':
149 parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.', epilog=HelpEpilog )
150
151 parser.add_argument( '--url', required=True, help="OPSI Server JSON-RPC url, in following format: " + UrlJsonRpc )
152
153 parser.add_argument( '--debug', action='store_true', help="enable debugging output" )
154 #parser.add_argument( '--verbose', type=bool, help="add debugging output" )
155
156 subparsers = parser.add_subparsers(title='subcommands',
157 description='valid subcommands',
158 help='additional help',
159 dest='subcommand' )
160
161 parser_list = subparsers.add_parser('list', help='list all opsi clients' )
162
163 parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' )
164 parser_exists.add_argument( 'src', help="source opsi client" )
165
166 parser_info = subparsers.add_parser('info', help='print information about a opsi client' )
167 parser_info.add_argument( 'src', help="opsi client" )
168
169 parser_clean = subparsers.add_parser('clean', help='remove all product states from a opsi client' )
170 parser_clean.add_argument( 'src', help="source opsi client to clean" )
171
172 parser_copy = subparsers.add_parser('copy', help='copy/create a opsi client from a template opsi client')
173 parser_copy.add_argument( 'src', help="source/template opsi client" )
174 parser_copy.add_argument( 'dst', help="opsi client to be created" )
175 parser_copy.add_argument( '--ip', help="IP address of the new opsi client" )
176 parser_copy.add_argument( '--mac', help="MAC address of the new opsi client" )
177 parser_copy.add_argument( '--depot', help="depot server the new opsi client should be located" )
178
179 args = parser.parse_args()
180
181 opsi=OpsiRpc( args.url, args.debug )
182
183 result = True
184
185 if args.subcommand == "list":
186 print( "\n".join( opsi.list() ) )
187 elif args.subcommand == "exists":
188 result = opsi.exists( args.src )
189 elif args.subcommand == "info":
190 result = opsi.info( args.src )
191 elif args.subcommand == "clean":
192 result = opsi.clean( args.src )
193 elif args.subcommand == "copy":
194 result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
195 else:
196 print "not yet implemented"
197
198 if args.debug: print result
199
200 if result:
201 exit(0)
202 else:
203 exit(1)
Note: See TracBrowser for help on using the repository browser.