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
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:
[1065]36
37 UrlJsonRpcDefault="https://opsi:4447/rpc"
[1047]38
39 ProductAttributesCopy = ['actionRequest','actionResult','installationStatus','packageVersion','productVersion']
40
[1065]41 def __init__(self, urlJsonRpc = UrlJsonRpcDefault, debug=False ):
[1047]42 self.debug=debug
[1046]43 self.urlJsonRpc=urlJsonRpc
[1047]44 self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc)
[1046]45
46 def dump(self):
47 print self.urlJsonRpc
[1047]48 print self.rpc.getClientIds_list()
[1051]49
50 def list(self):
51 return self.rpc.getClientIds_list()
[1046]52
[1051]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 + ":"
[1052]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:"
[1051]69 products = self.getProductOnClient( src, [] )
70 for i in products:
[1052]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 )
[1051]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
[1047]91 def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None ):
[1028]92
[1047]93 print "create/update", dst, "from template", src + ":",
[1064]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
[1047]111 if depot:
112 self.rpc.configState_create( "clientconfig.depot.id", dst, depot )
[1064]113
[1047]114 if self.debug:
[1051]115 pprint( self.getProductOnClient( src ) )
[1047]116 self.copyProductOnClient( src, dst )
117 # TODO:
118 # copy product properties:
119 # opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
120 print "done"
[1052]121 return True
[1047]122
[1046]123
[1051]124 def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
[1047]125 #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
126 #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
[1051]127 return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
[1047]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:
[1051]133 print i['productId']
[1047]134 pprint( i )
135 i['clientId'] = dst
136 self.rpc.productOnClient_createObjects( i )
[1051]137 if self.debug:
138 pprint( self.getProductOnClient( dst ) )
[1047]139
[1028]140
141
[1047]142if __name__ == '__main__':
[1063]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
[1047]147 parser.add_argument( '--debug', action='store_true', help="enable debugging output" )
148 #parser.add_argument( '--verbose', type=bool, help="add debugging output" )
[1028]149
[1050]150 subparsers = parser.add_subparsers(title='subcommands',
151 description='valid subcommands',
152 help='additional help',
153 dest='subcommand' )
[1047]154
[1050]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" )
[1051]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
[1050]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
[1047]173 args = parser.parse_args()
174
[1063]175 opsi=OpsiRpc( args.url, args.debug )
[1050]176
[1051]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":
[1052]188 result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
[1050]189 else:
190 print "not yet implemented"
191
[1051]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.