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
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
[1053]31UrlJsonRpc="https://<USERNAME>:<PASSWORD>@opsi:4447/rpc"
[1027]32
[1046]33class OpsiRpc:
[1047]34
35 ProductAttributesCopy = ['actionRequest','actionResult','installationStatus','packageVersion','productVersion']
36
37 def __init__(self, urlJsonRpc, debug=False ):
38 self.debug=debug
[1046]39 self.urlJsonRpc=urlJsonRpc
[1047]40 self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc)
[1046]41
42 def dump(self):
43 print self.urlJsonRpc
[1047]44 print self.rpc.getClientIds_list()
[1051]45
46 def list(self):
47 return self.rpc.getClientIds_list()
[1046]48
[1051]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 + ":"
[1052]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:"
[1051]65 products = self.getProductOnClient( src, [] )
66 for i in products:
[1052]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 )
[1051]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
[1047]87 def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None ):
[1028]88
[1047]89 print "create/update", dst, "from template", src + ":",
[1046]90 #method host_createOpsiClient id *opsiHostKey *description *notes *hardwareAddress *ipAddress *inventoryNumber *oneTimePassword *created *lastSeen
[1047]91 #id=dst
[1046]92 opsiHostKey=None
93 description=""
94 notes="copy of " + src
[1047]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:
[1051]99 pprint( self.getProductOnClient( src ) )
[1047]100 self.copyProductOnClient( src, dst )
101 # TODO:
102 # copy product properties:
103 # opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
104 print "done"
[1052]105 return True
[1047]106
[1046]107
[1051]108 def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
[1047]109 #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
110 #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
[1051]111 return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
[1047]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:
[1051]117 print i['productId']
[1047]118 pprint( i )
119 i['clientId'] = dst
120 self.rpc.productOnClient_createObjects( i )
[1051]121 if self.debug:
122 pprint( self.getProductOnClient( dst ) )
[1047]123
[1028]124
125
[1047]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" )
[1028]131
[1050]132 subparsers = parser.add_subparsers(title='subcommands',
133 description='valid subcommands',
134 help='additional help',
135 dest='subcommand' )
[1047]136
[1050]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" )
[1051]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
[1050]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
[1047]155 args = parser.parse_args()
156
[1050]157
[1047]158 urlJsonRpc = UrlJsonRpc
159 if args.url:
160 urlJsonRpc = args.url
161
162 opsi=OpsiRpc( urlJsonRpc, args.debug )
[1050]163
[1051]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":
[1052]175 result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
[1050]176 else:
177 print "not yet implemented"
178
[1051]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.