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

Last change on this file since 1064 was 1064, checked in by joergs, on Aug 16, 2012 at 8:12:00 AM

bugfix: don't overwrite ip and mac, when copying a client a second time

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