[1027] | 1 | #!/usr/bin/env python
|
---|
| 2 |
|
---|
[1051] | 3 | # Skript, dass ein OPSI-Rechner-Eintrag kopiert.
|
---|
[1046] | 4 | # D.h. die Produkte, Anforderung.
|
---|
| 5 | # Ggf. optional Stand und ggf. Versionsnummer
|
---|
| 6 | # Ggf. optional ProductProperties
|
---|
[1027] | 7 |
|
---|
[1051] | 8 | #self.command("opsi-admin -d method host_createOpsiClient "+ \
|
---|
| 9 | #computername + " null " + "\\'"+description+"\\'" + \
|
---|
| 10 | #" \\'created by dassadmin\\' " + mac_address + " " + \
|
---|
| 11 | #ip_address)
|
---|
| 12 | #method host_createOpsiClient id *opsiHostKey *description *notes *hardwareAddress *ipAddress *inventoryNumber *oneTimePassword *created *lastSeen
|
---|
| 13 | #self.command("opsi-admin -d method configState_create clientconfig.depot.id " + \
|
---|
| 14 | #computername + " " + depotName)
|
---|
[1047] | 15 |
|
---|
| 16 | import argparse
|
---|
[1046] | 17 | import jsonrpc
|
---|
[1027] | 18 | from pprint import pprint
|
---|
| 19 |
|
---|
[1053] | 20 | UrlJsonRpc="https://<USERNAME>:<PASSWORD>@opsi:4447/rpc"
|
---|
[1027] | 21 |
|
---|
[1046] | 22 | class OpsiRpc:
|
---|
[1047] | 23 |
|
---|
| 24 | ProductAttributesCopy = ['actionRequest','actionResult','installationStatus','packageVersion','productVersion']
|
---|
| 25 |
|
---|
| 26 | def __init__(self, urlJsonRpc, debug=False ):
|
---|
| 27 | self.debug=debug
|
---|
[1046] | 28 | self.urlJsonRpc=urlJsonRpc
|
---|
[1047] | 29 | self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc)
|
---|
[1046] | 30 |
|
---|
| 31 | def dump(self):
|
---|
| 32 | print self.urlJsonRpc
|
---|
[1047] | 33 | print self.rpc.getClientIds_list()
|
---|
[1051] | 34 |
|
---|
| 35 | def list(self):
|
---|
| 36 | return self.rpc.getClientIds_list()
|
---|
[1046] | 37 |
|
---|
[1051] | 38 | def exists(self, src):
|
---|
| 39 | return len( self.rpc.host_getObjects( [], {"id":src} ) ) == 1
|
---|
| 40 |
|
---|
| 41 | def info(self, src):
|
---|
| 42 | if not self.exists( src ):
|
---|
| 43 | print "failed: opsi client", src, "does not exist"
|
---|
| 44 | return False
|
---|
| 45 | print src + ":"
|
---|
[1052] | 46 | host = self.rpc.host_getHashes( [], {"id":src} )[0]
|
---|
| 47 | print " IP:", host["ipAddress"]
|
---|
| 48 | print " MAC:", host["hardwareAddress"]
|
---|
| 49 | print " inventory:", host["inventoryNumber"]
|
---|
| 50 | print " last seen:", host["lastSeen"]
|
---|
| 51 | print " notes:", host["notes"]
|
---|
| 52 |
|
---|
| 53 | print " products:"
|
---|
[1051] | 54 | products = self.getProductOnClient( src, [] )
|
---|
| 55 | for i in products:
|
---|
[1052] | 56 | print " " + i['productId'] + ":"
|
---|
| 57 | print " " + i['installationStatus'], "(",
|
---|
| 58 | if i['actionRequest']:
|
---|
| 59 | print i['actionRequest'],
|
---|
| 60 | if i['actionProgress']:
|
---|
| 61 | print i['actionProgress'],
|
---|
| 62 | print ")"
|
---|
| 63 | print " ",
|
---|
| 64 | pprint( i, indent=8 )
|
---|
[1051] | 65 | return True
|
---|
| 66 |
|
---|
| 67 | def clean(self, src):
|
---|
| 68 | if not self.exists( src ):
|
---|
| 69 | return False
|
---|
| 70 | products = self.rpc.productOnClient_getObjects( [], { 'clientId': src } )
|
---|
| 71 | self.rpc.productOnClient_deleteObjects( products )
|
---|
| 72 | if self.debug:
|
---|
| 73 | pprint( self.getProductOnClient( src ) )
|
---|
| 74 | return True
|
---|
| 75 |
|
---|
[1047] | 76 | def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None ):
|
---|
[1028] | 77 |
|
---|
[1047] | 78 | print "create/update", dst, "from template", src + ":",
|
---|
[1046] | 79 | #method host_createOpsiClient id *opsiHostKey *description *notes *hardwareAddress *ipAddress *inventoryNumber *oneTimePassword *created *lastSeen
|
---|
[1047] | 80 | #id=dst
|
---|
[1046] | 81 | opsiHostKey=None
|
---|
| 82 | description=""
|
---|
| 83 | notes="copy of " + src
|
---|
[1047] | 84 | self.rpc.host_createOpsiClient( dst, opsiHostKey, description, notes, hardwareAddress, ipAddress )
|
---|
| 85 | if depot:
|
---|
| 86 | self.rpc.configState_create( "clientconfig.depot.id", dst, depot )
|
---|
| 87 | if self.debug:
|
---|
[1051] | 88 | pprint( self.getProductOnClient( src ) )
|
---|
[1047] | 89 | self.copyProductOnClient( src, dst )
|
---|
| 90 | # TODO:
|
---|
| 91 | # copy product properties:
|
---|
| 92 | # opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
|
---|
| 93 | print "done"
|
---|
[1052] | 94 | return True
|
---|
[1047] | 95 |
|
---|
[1046] | 96 |
|
---|
[1051] | 97 | def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
|
---|
[1047] | 98 | #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
|
---|
| 99 | #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
|
---|
[1051] | 100 | return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
|
---|
[1047] | 101 |
|
---|
| 102 | def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ):
|
---|
| 103 | products = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } )
|
---|
| 104 | for i in products:
|
---|
| 105 | if self.debug:
|
---|
[1051] | 106 | print i['productId']
|
---|
[1047] | 107 | pprint( i )
|
---|
| 108 | i['clientId'] = dst
|
---|
| 109 | self.rpc.productOnClient_createObjects( i )
|
---|
[1051] | 110 | if self.debug:
|
---|
| 111 | pprint( self.getProductOnClient( dst ) )
|
---|
[1047] | 112 |
|
---|
[1028] | 113 |
|
---|
| 114 |
|
---|
[1047] | 115 | if __name__ == '__main__':
|
---|
| 116 | parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.')
|
---|
| 117 | parser.add_argument( '--debug', action='store_true', help="enable debugging output" )
|
---|
| 118 | #parser.add_argument( '--verbose', type=bool, help="add debugging output" )
|
---|
| 119 | parser.add_argument( '--url', help="OPSI Server JSON-RPC url, normally https://<username>:<password>@<OPSI-SERVER>:4447/rpc" )
|
---|
[1028] | 120 |
|
---|
[1050] | 121 | subparsers = parser.add_subparsers(title='subcommands',
|
---|
| 122 | description='valid subcommands',
|
---|
| 123 | help='additional help',
|
---|
| 124 | dest='subcommand' )
|
---|
[1047] | 125 |
|
---|
[1050] | 126 | parser_list = subparsers.add_parser('list', help='list all opsi clients' )
|
---|
| 127 |
|
---|
| 128 | parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' )
|
---|
| 129 | parser_exists.add_argument( 'src', help="source opsi client" )
|
---|
[1051] | 130 |
|
---|
| 131 | parser_info = subparsers.add_parser('info', help='print information about a opsi client' )
|
---|
| 132 | parser_info.add_argument( 'src', help="opsi client" )
|
---|
| 133 |
|
---|
[1050] | 134 | parser_clean = subparsers.add_parser('clean', help='remove all product states from a opsi client' )
|
---|
| 135 | parser_clean.add_argument( 'src', help="source opsi client to clean" )
|
---|
| 136 |
|
---|
| 137 | parser_copy = subparsers.add_parser('copy', help='copy/create a opsi client from a template opsi client')
|
---|
| 138 | parser_copy.add_argument( 'src', help="source/template opsi client" )
|
---|
| 139 | parser_copy.add_argument( 'dst', help="opsi client to be created" )
|
---|
| 140 | parser_copy.add_argument( '--ip', help="IP address of the new opsi client" )
|
---|
| 141 | parser_copy.add_argument( '--mac', help="MAC address of the new opsi client" )
|
---|
| 142 | parser_copy.add_argument( '--depot', help="depot server the new opsi client should be located" )
|
---|
| 143 |
|
---|
[1047] | 144 | args = parser.parse_args()
|
---|
| 145 |
|
---|
[1050] | 146 |
|
---|
[1047] | 147 | urlJsonRpc = UrlJsonRpc
|
---|
| 148 | if args.url:
|
---|
| 149 | urlJsonRpc = args.url
|
---|
| 150 |
|
---|
| 151 | opsi=OpsiRpc( urlJsonRpc, args.debug )
|
---|
[1050] | 152 |
|
---|
[1051] | 153 | result = True
|
---|
| 154 |
|
---|
| 155 | if args.subcommand == "list":
|
---|
| 156 | print( "\n".join( opsi.list() ) )
|
---|
| 157 | elif args.subcommand == "exists":
|
---|
| 158 | result = opsi.exists( args.src )
|
---|
| 159 | elif args.subcommand == "info":
|
---|
| 160 | result = opsi.info( args.src )
|
---|
| 161 | elif args.subcommand == "clean":
|
---|
| 162 | result = opsi.clean( args.src )
|
---|
| 163 | elif args.subcommand == "copy":
|
---|
[1052] | 164 | result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
|
---|
[1050] | 165 | else:
|
---|
| 166 | print "not yet implemented"
|
---|
| 167 |
|
---|
[1051] | 168 | if args.debug: print result
|
---|
| 169 |
|
---|
| 170 | if result:
|
---|
| 171 | exit(0)
|
---|
| 172 | else:
|
---|
| 173 | exit(1)
|
---|