[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 |
|
---|
| 27 | import argparse
|
---|
[1087] | 28 | import jsonrpc
|
---|
[1027] | 29 | from pprint import pprint
|
---|
| 30 |
|
---|
[1063] | 31 | UrlJsonRpc="https://<username>:<password>@opsi:4447/rpc"
|
---|
[1027] | 32 |
|
---|
[1063] | 33 | HelpEpilog="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 |
|
---|
[1087] | 35 | class 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 )
|
---|
[1088] | 87 |
|
---|
| 88 | products = self.rpc.productPropertyState_getObjects( [], { 'clientId': src } )
|
---|
| 89 | self.rpc.productPropertyState_deleteObjects( products )
|
---|
| 90 |
|
---|
[1087] | 91 | if self.debug:
|
---|
| 92 | pprint( self.getProductOnClient( src ) )
|
---|
| 93 | return True
|
---|
| 94 |
|
---|
| 95 | def createClient(self, name, opsiHostKey, description, notes, hardwareAddress, ipAddress):
|
---|
| 96 | self.rpc.host_createOpsiClient( name, opsiHostKey, description, notes, hardwareAddress, ipAddress )
|
---|
| 97 |
|
---|
| 98 | def clientSetDepot(self, name, depot):
|
---|
| 99 | self.rpc.configState_create( "clientconfig.depot.id", name, depot )
|
---|
| 100 |
|
---|
[1088] | 101 | def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None, description = "", copyProperties = True ):
|
---|
[1087] | 102 |
|
---|
| 103 | print "create/update", dst, "from template", src + ":",
|
---|
| 104 | obj = {
|
---|
| 105 | "id" : dst,
|
---|
| 106 | "type" : "OpsiClient",
|
---|
| 107 | "notes" : "copy of " + src,
|
---|
| 108 | "description" : description,
|
---|
| 109 | #"inventoryNumber" : "",
|
---|
| 110 | }
|
---|
| 111 | if hardwareAddress:
|
---|
| 112 | obj['hardwareAddress'] = hardwareAddress
|
---|
| 113 | if ipAddress:
|
---|
| 114 | obj['ipAddress'] = ipAddress
|
---|
| 115 |
|
---|
| 116 | if self.exists( dst ):
|
---|
| 117 | self.rpc.host_updateObject(obj)
|
---|
| 118 | else:
|
---|
| 119 | self.rpc.host_insertObject(obj)
|
---|
| 120 |
|
---|
| 121 | if depot:
|
---|
| 122 | self.clientSetDepot(dst,depot)
|
---|
| 123 |
|
---|
| 124 | if self.debug:
|
---|
| 125 | pprint( self.getProductOnClient( src ) )
|
---|
| 126 | self.copyProductOnClient( src, dst )
|
---|
| 127 | # TODO:
|
---|
| 128 | # copy product properties:
|
---|
| 129 | # opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
|
---|
[1088] | 130 | if copyProperties:
|
---|
| 131 | if self.debug:
|
---|
| 132 | print "copy product properties"
|
---|
| 133 | self.copyProductPropertyState( src, dst )
|
---|
[1087] | 134 | print "done"
|
---|
| 135 | return True
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
|
---|
| 139 | #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
|
---|
| 140 | #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
|
---|
| 141 | return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
|
---|
| 142 |
|
---|
| 143 | def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ):
|
---|
[1088] | 144 | products_src = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } )
|
---|
| 145 | products_dst = []
|
---|
| 146 | for i in products_src:
|
---|
[1087] | 147 | if self.debug:
|
---|
| 148 | print i['productId']
|
---|
| 149 | pprint( i )
|
---|
| 150 | i['clientId'] = dst
|
---|
[1088] | 151 | products_dst.append(i)
|
---|
| 152 | self.rpc.productOnClient_createObjects( products_dst )
|
---|
[1087] | 153 | if self.debug:
|
---|
| 154 | pprint( self.getProductOnClient( dst ) )
|
---|
[1088] | 155 |
|
---|
| 156 | def getProductPropertyState( self, client, attributes = [] ):
|
---|
| 157 | return self.rpc.productPropertyState_getHashes( [], { 'objectId': client } )
|
---|
| 158 |
|
---|
| 159 | def copyProductPropertyState( self, src, dst, attributes = [] ):
|
---|
| 160 | productProperties_src = self.getProductPropertyState( src, attributes )
|
---|
| 161 | productProperties_dst = []
|
---|
| 162 | for i in productProperties_src:
|
---|
| 163 | if self.debug:
|
---|
| 164 | print i['productId'], "-", i["propertyId"] + ": ",
|
---|
| 165 | pprint(i["values"])
|
---|
| 166 | #pprint( i )
|
---|
| 167 | i['objectId'] = dst
|
---|
| 168 | productProperties_dst.append(i)
|
---|
| 169 | self.rpc.productPropertyState_createObjects( productProperties_dst )
|
---|
| 170 | if self.debug:
|
---|
| 171 | pprint( self.getProductPropertyState( dst ) )
|
---|
| 172 |
|
---|
[1087] | 173 |
|
---|
| 174 |
|
---|
| 175 |
|
---|
[1047] | 176 | if __name__ == '__main__':
|
---|
[1063] | 177 | parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.', epilog=HelpEpilog )
|
---|
| 178 |
|
---|
| 179 | parser.add_argument( '--url', required=True, help="OPSI Server JSON-RPC url, in following format: " + UrlJsonRpc )
|
---|
| 180 |
|
---|
[1047] | 181 | parser.add_argument( '--debug', action='store_true', help="enable debugging output" )
|
---|
| 182 | #parser.add_argument( '--verbose', type=bool, help="add debugging output" )
|
---|
[1028] | 183 |
|
---|
[1050] | 184 | subparsers = parser.add_subparsers(title='subcommands',
|
---|
| 185 | description='valid subcommands',
|
---|
| 186 | help='additional help',
|
---|
| 187 | dest='subcommand' )
|
---|
[1047] | 188 |
|
---|
[1050] | 189 | parser_list = subparsers.add_parser('list', help='list all opsi clients' )
|
---|
| 190 |
|
---|
| 191 | parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' )
|
---|
| 192 | parser_exists.add_argument( 'src', help="source opsi client" )
|
---|
[1051] | 193 |
|
---|
| 194 | parser_info = subparsers.add_parser('info', help='print information about a opsi client' )
|
---|
| 195 | parser_info.add_argument( 'src', help="opsi client" )
|
---|
| 196 |
|
---|
[1050] | 197 | parser_clean = subparsers.add_parser('clean', help='remove all product states from a opsi client' )
|
---|
| 198 | parser_clean.add_argument( 'src', help="source opsi client to clean" )
|
---|
| 199 |
|
---|
| 200 | parser_copy = subparsers.add_parser('copy', help='copy/create a opsi client from a template opsi client')
|
---|
| 201 | parser_copy.add_argument( 'src', help="source/template opsi client" )
|
---|
| 202 | parser_copy.add_argument( 'dst', help="opsi client to be created" )
|
---|
| 203 | parser_copy.add_argument( '--ip', help="IP address of the new opsi client" )
|
---|
| 204 | parser_copy.add_argument( '--mac', help="MAC address of the new opsi client" )
|
---|
| 205 | parser_copy.add_argument( '--depot', help="depot server the new opsi client should be located" )
|
---|
[1088] | 206 | #parser_copy.add_argument( '--no-properties', action='store_false', help="don't copy product properties" )
|
---|
[1050] | 207 |
|
---|
[1047] | 208 | args = parser.parse_args()
|
---|
| 209 |
|
---|
[1063] | 210 | opsi=OpsiRpc( args.url, args.debug )
|
---|
[1050] | 211 |
|
---|
[1051] | 212 | result = True
|
---|
| 213 |
|
---|
| 214 | if args.subcommand == "list":
|
---|
| 215 | print( "\n".join( opsi.list() ) )
|
---|
| 216 | elif args.subcommand == "exists":
|
---|
| 217 | result = opsi.exists( args.src )
|
---|
| 218 | elif args.subcommand == "info":
|
---|
| 219 | result = opsi.info( args.src )
|
---|
| 220 | elif args.subcommand == "clean":
|
---|
| 221 | result = opsi.clean( args.src )
|
---|
| 222 | elif args.subcommand == "copy":
|
---|
[1088] | 223 | #result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot, not args.no-properties )
|
---|
[1052] | 224 | result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
|
---|
[1050] | 225 | else:
|
---|
| 226 | print "not yet implemented"
|
---|
| 227 |
|
---|
[1051] | 228 | if args.debug: print result
|
---|
| 229 |
|
---|
| 230 | if result:
|
---|
| 231 | exit(0)
|
---|
| 232 | else:
|
---|
| 233 | exit(1)
|
---|