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

Last change on this file since 1065 was 1065, checked in by joergs, on Aug 16, 2012 at 8:14:59 AM

default url

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