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

Last change on this file since 1061 was 1061, checked in by joergs, on Aug 15, 2012 at 4:50:56 PM

declare encoding

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