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
RevLine 
[1027]1#!/usr/bin/env python
2
[1061]3# -*- coding: utf-8 -*-
4
[1055]5"""ospi-client: performs operation for opsi clients on opsi server via JSON-RPC."""
6
[1061]7__author__ = "Joerg Steffens"
[1055]8__copyright__ = "Copyright 2012, dass IT GmbH"
9__license__ = "GPL"
10__version__ = "1.0"
11__email__ = "joerg.steffens@dass-it.de"
12
13#
[1051]14# Skript, dass ein OPSI-Rechner-Eintrag kopiert.
[1046]15# D.h. die Produkte, Anforderung.
16# Ggf. optional Stand und ggf. Versionsnummer
17# Ggf. optional ProductProperties
[1055]18#
[1027]19
[1051]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)
[1047]26
27import argparse
[1046]28import jsonrpc
[1027]29from pprint import pprint
30
[1063]31UrlJsonRpc="https://<username>:<password>@opsi:4447/rpc"
[1027]32
[1063]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
[1046]35class OpsiRpc:
[1047]36
[1063]37
[1047]38 ProductAttributesCopy = ['actionRequest','actionResult','installationStatus','packageVersion','productVersion']
39
40 def __init__(self, urlJsonRpc, debug=False ):
41 self.debug=debug
[1046]42 self.urlJsonRpc=urlJsonRpc
[1047]43 self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc)
[1046]44
45 def dump(self):
46 print self.urlJsonRpc
[1047]47 print self.rpc.getClientIds_list()
[1051]48
49 def list(self):
50 return self.rpc.getClientIds_list()
[1046]51
[1051]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 + ":"
[1052]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:"
[1051]68 products = self.getProductOnClient( src, [] )
69 for i in products:
[1052]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 )
[1051]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
[1047]90 def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None ):
[1028]91
[1047]92 print "create/update", dst, "from template", src + ":",
[1064]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
[1047]110 if depot:
111 self.rpc.configState_create( "clientconfig.depot.id", dst, depot )
[1064]112
[1047]113 if self.debug:
[1051]114 pprint( self.getProductOnClient( src ) )
[1047]115 self.copyProductOnClient( src, dst )
116 # TODO:
117 # copy product properties:
118 # opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
119 print "done"
[1052]120 return True
[1047]121
[1046]122
[1051]123 def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
[1047]124 #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
125 #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
[1051]126 return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
[1047]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:
[1051]132 print i['productId']
[1047]133 pprint( i )
134 i['clientId'] = dst
135 self.rpc.productOnClient_createObjects( i )
[1051]136 if self.debug:
137 pprint( self.getProductOnClient( dst ) )
[1047]138
[1028]139
140
[1047]141if __name__ == '__main__':
[1063]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
[1047]146 parser.add_argument( '--debug', action='store_true', help="enable debugging output" )
147 #parser.add_argument( '--verbose', type=bool, help="add debugging output" )
[1028]148
[1050]149 subparsers = parser.add_subparsers(title='subcommands',
150 description='valid subcommands',
151 help='additional help',
152 dest='subcommand' )
[1047]153
[1050]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" )
[1051]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
[1050]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
[1047]172 args = parser.parse_args()
173
[1063]174 opsi=OpsiRpc( args.url, args.debug )
[1050]175
[1051]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":
[1052]187 result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
[1050]188 else:
189 print "not yet implemented"
190
[1051]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.