| [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"
 | 
|---|
| [1174] | 8 | __copyright__ = "Copyright 2012-2015, dass IT GmbH"
 | 
|---|
| [1055] | 9 | __license__ = "GPL"
 | 
|---|
| [1110] | 10 | __version__ = "1.1"
 | 
|---|
| [1055] | 11 | __email__ = "joerg.steffens@dass-it.de"
 | 
|---|
 | 12 | 
 | 
|---|
| [1051] | 13 | #self.command("opsi-admin -d method host_createOpsiClient "+ \
 | 
|---|
 | 14 |         #computername + " null " + "\\'"+description+"\\'" + \
 | 
|---|
 | 15 |         #" \\'created by dassadmin\\' " + mac_address + " " + \
 | 
|---|
 | 16 |         #ip_address)
 | 
|---|
 | 17 | #self.command("opsi-admin -d method configState_create clientconfig.depot.id " + \
 | 
|---|
| [1174] | 18 |         #computername + " " + depotName)
 | 
|---|
 | 19 | 
 | 
|---|
| [1047] | 20 | import argparse
 | 
|---|
| [1087] | 21 | import jsonrpc
 | 
|---|
| [1110] | 22 | import logging
 | 
|---|
 | 23 | import os
 | 
|---|
| [1174] | 24 | from pprint import pprint, pformat
 | 
|---|
| [1110] | 25 | import time
 | 
|---|
| [1027] | 26 | 
 | 
|---|
| [1063] | 27 | UrlJsonRpc="https://<username>:<password>@opsi:4447/rpc"
 | 
|---|
| [1027] | 28 | 
 | 
|---|
| [1063] | 29 | 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."
 | 
|---|
 | 30 | 
 | 
|---|
| [1087] | 31 | class OpsiRpc:
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 |     UrlJsonRpcDefault="https://opsi:4447/rpc"
 | 
|---|
| [1174] | 34 | 
 | 
|---|
| [1087] | 35 |     ProductAttributesCopy = ['actionRequest','actionResult','installationStatus','packageVersion','productVersion']
 | 
|---|
| [1174] | 36 | 
 | 
|---|
| [1087] | 37 |     def __init__(self, urlJsonRpc = UrlJsonRpcDefault, debug=False ):
 | 
|---|
| [1174] | 38 |         self.logger=logging.getLogger(__name__)
 | 
|---|
| [1087] | 39 |         self.debug=debug
 | 
|---|
 | 40 |         self.urlJsonRpc=urlJsonRpc
 | 
|---|
 | 41 |         self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc)
 | 
|---|
| [1110] | 42 |         self.logger.debug( "initialized: " + self.urlJsonRpc )
 | 
|---|
 | 43 | 
 | 
|---|
| [1174] | 44 | 
 | 
|---|
| [1110] | 45 |     def list(self):
 | 
|---|
 | 46 |         print( "\n".join( self.rpc.getClientIds_list() ) )
 | 
|---|
 | 47 |         return True
 | 
|---|
| [1087] | 48 | 
 | 
|---|
| [1174] | 49 | 
 | 
|---|
| [1110] | 50 |     def getClientsWithProduct( self, product ):
 | 
|---|
 | 51 |         return self.rpc.productOnClient_getObjects( [], { "productId": product, "installationStatus": "installed" } )
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 |     def listClients( self, product ):
 | 
|---|
 | 55 |         if product:
 | 
|---|
 | 56 |             for client in self.getClientsWithProduct( product ):
 | 
|---|
 | 57 |                 print client['clientId']
 | 
|---|
 | 58 |         else:
 | 
|---|
 | 59 |             return self.list()
 | 
|---|
 | 60 |         return True
 | 
|---|
| [1174] | 61 | 
 | 
|---|
| [1087] | 62 |     def exists(self, src):
 | 
|---|
 | 63 |         return len( self.rpc.host_getObjects( [], {"id":src} ) ) == 1
 | 
|---|
| [1174] | 64 | 
 | 
|---|
| [1087] | 65 |     def info(self, src):
 | 
|---|
 | 66 |         if not self.exists( src ):
 | 
|---|
 | 67 |             print "failed: opsi client", src, "does not exist"
 | 
|---|
 | 68 |             return False
 | 
|---|
 | 69 |         print src + ":"
 | 
|---|
 | 70 |         host = self.rpc.host_getHashes( [], {"id":src} )[0]
 | 
|---|
 | 71 |         print "  IP:", host["ipAddress"]
 | 
|---|
 | 72 |         print "  MAC:", host["hardwareAddress"]
 | 
|---|
 | 73 |         print "  inventory:", host["inventoryNumber"]
 | 
|---|
 | 74 |         print "  last seen:", host["lastSeen"]
 | 
|---|
 | 75 |         print "  notes:", host["notes"]
 | 
|---|
| [1174] | 76 | 
 | 
|---|
| [1087] | 77 |         print "  products:"
 | 
|---|
 | 78 |         products = self.getProductOnClient( src, [] )
 | 
|---|
 | 79 |         for i in products:
 | 
|---|
 | 80 |             print "    " + i['productId'] + ":"
 | 
|---|
 | 81 |             print "      " + i['installationStatus'], "(",
 | 
|---|
 | 82 |             if i['actionRequest']:
 | 
|---|
 | 83 |                 print i['actionRequest'],
 | 
|---|
 | 84 |                 if i['actionProgress']:
 | 
|---|
 | 85 |                     print i['actionProgress'],
 | 
|---|
 | 86 |             print ")"
 | 
|---|
 | 87 |             print "      ",
 | 
|---|
 | 88 |             pprint( i, indent=8 )
 | 
|---|
 | 89 |         return True
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |     def clean(self, src):
 | 
|---|
 | 92 |         if not self.exists( src ):
 | 
|---|
 | 93 |             return False
 | 
|---|
 | 94 |         products = self.rpc.productOnClient_getObjects( [], { 'clientId': src } )
 | 
|---|
 | 95 |         self.rpc.productOnClient_deleteObjects( products )
 | 
|---|
| [1088] | 96 | 
 | 
|---|
| [1109] | 97 |         products = self.rpc.productPropertyState_getObjects( [], { 'objectId': src } )
 | 
|---|
| [1088] | 98 |         self.rpc.productPropertyState_deleteObjects( products )
 | 
|---|
| [1174] | 99 | 
 | 
|---|
| [1087] | 100 |         if self.debug:
 | 
|---|
 | 101 |             pprint( self.getProductOnClient( src ) )
 | 
|---|
 | 102 |         return True
 | 
|---|
| [1174] | 103 | 
 | 
|---|
 | 104 |     def getOpsiConfigserverId(self):
 | 
|---|
 | 105 |         # there should always be only one OpsiConfigserver
 | 
|---|
 | 106 |         opsiConfigservers=self.rpc.host_getHashes( [], { "type": "OpsiConfigserver" } )
 | 
|---|
 | 107 |         try:
 | 
|---|
 | 108 |             return opsiConfigservers[0]['id']
 | 
|---|
 | 109 |         except (KeyError,IndexError) as e:
 | 
|---|
 | 110 |             self.logger.error( "failed to retreive OpsiConfigserver" )
 | 
|---|
 | 111 | 
 | 
|---|
| [1087] | 112 |     def createClient(self, name, opsiHostKey, description, notes, hardwareAddress, ipAddress):
 | 
|---|
| [1117] | 113 |         #    self.rpc.host_createOpsiClient( name, opsiHostKey, description, notes, hardwareAddress, ipAddress )
 | 
|---|
 | 114 |         self.updateClient( name, opsiHostKey, description, notes, hardwareAddress, ipAddress )
 | 
|---|
| [1087] | 115 | 
 | 
|---|
| [1175] | 116 |     def deleteClient(self, name):
 | 
|---|
 | 117 |         self.rpc.host_delete( name )
 | 
|---|
 | 118 | 
 | 
|---|
| [1117] | 119 |     def updateClient(self, src, opsiHostKey = None, description = None, notes = None, hardwareAddress = None, ipAddress = None, depot = None ):
 | 
|---|
 | 120 |         obj = {
 | 
|---|
 | 121 |           "id" : src,
 | 
|---|
 | 122 |           "type" : "OpsiClient",
 | 
|---|
 | 123 |         }
 | 
|---|
 | 124 |         if opsiHostKey:
 | 
|---|
 | 125 |             obj['opsiHostKey'] = opsiHostKey
 | 
|---|
 | 126 |         if description:
 | 
|---|
 | 127 |             obj['description'] = description
 | 
|---|
 | 128 |         if notes:
 | 
|---|
| [1174] | 129 |             obj['notes'] = notes
 | 
|---|
| [1117] | 130 |         if hardwareAddress:
 | 
|---|
 | 131 |             obj['hardwareAddress'] = hardwareAddress
 | 
|---|
 | 132 |         if ipAddress:
 | 
|---|
 | 133 |             obj['ipAddress'] = ipAddress
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 |         if self.exists( src ):
 | 
|---|
 | 136 |             self.rpc.host_updateObject(obj)
 | 
|---|
 | 137 |         else:
 | 
|---|
 | 138 |             self.rpc.host_insertObject(obj)
 | 
|---|
| [1174] | 139 | 
 | 
|---|
| [1117] | 140 |         if depot:
 | 
|---|
 | 141 |             self.clientSetDepot(src,depot)
 | 
|---|
 | 142 |         return True
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 | 
 | 
|---|
| [1087] | 145 |     def clientSetDepot(self, name, depot):
 | 
|---|
| [1174] | 146 |         self.rpc.configState_create( "clientconfig.depot.id", name, depot )
 | 
|---|
| [1087] | 147 | 
 | 
|---|
| [1088] | 148 |     def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None, description = "", copyProperties = True ):
 | 
|---|
| [1174] | 149 | 
 | 
|---|
| [1087] | 150 |         print "create/update", dst, "from template", src + ":",
 | 
|---|
 | 151 |         obj = {
 | 
|---|
 | 152 |           "id" : dst,
 | 
|---|
 | 153 |           "type" : "OpsiClient",
 | 
|---|
 | 154 |           "notes" : "copy of " + src,
 | 
|---|
 | 155 |           "description" : description,
 | 
|---|
 | 156 |           #"inventoryNumber" : "",
 | 
|---|
 | 157 |         }
 | 
|---|
 | 158 |         if hardwareAddress:
 | 
|---|
 | 159 |             obj['hardwareAddress'] = hardwareAddress
 | 
|---|
 | 160 |         if ipAddress:
 | 
|---|
 | 161 |             obj['ipAddress'] = ipAddress
 | 
|---|
 | 162 | 
 | 
|---|
 | 163 |         if self.exists( dst ):
 | 
|---|
 | 164 |             self.rpc.host_updateObject(obj)
 | 
|---|
 | 165 |         else:
 | 
|---|
 | 166 |             self.rpc.host_insertObject(obj)
 | 
|---|
| [1174] | 167 | 
 | 
|---|
| [1087] | 168 |         if depot:
 | 
|---|
 | 169 |             self.clientSetDepot(dst,depot)
 | 
|---|
| [1174] | 170 | 
 | 
|---|
| [1087] | 171 |         if self.debug:
 | 
|---|
 | 172 |             pprint( self.getProductOnClient( src ) )
 | 
|---|
 | 173 |         self.copyProductOnClient( src, dst )
 | 
|---|
| [1088] | 174 |         if copyProperties:
 | 
|---|
 | 175 |             if self.debug:
 | 
|---|
 | 176 |                 print "copy product properties"
 | 
|---|
| [1174] | 177 |             if not depot:
 | 
|---|
 | 178 |                 # get default Properties from Master Depot Server (OpsiConfigserver)
 | 
|---|
 | 179 |                 depot = self.getOpsiConfigserverId()
 | 
|---|
 | 180 |             self.copyProductPropertyState( src, dst, depot )
 | 
|---|
| [1087] | 181 |         print "done"
 | 
|---|
 | 182 |         return True
 | 
|---|
 | 183 | 
 | 
|---|
 | 184 |     def getProductOnClient( self, client, attributes = ProductAttributesCopy ):
 | 
|---|
 | 185 |         return self.rpc.productOnClient_getHashes( [], { 'clientId': client } )
 | 
|---|
| [1110] | 186 | 
 | 
|---|
| [1087] | 187 |     def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ):
 | 
|---|
| [1088] | 188 |         products_src = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } )
 | 
|---|
 | 189 |         products_dst = []
 | 
|---|
 | 190 |         for i in products_src:
 | 
|---|
| [1087] | 191 |             if self.debug: 
 | 
|---|
 | 192 |                 print i['productId']
 | 
|---|
 | 193 |                 pprint( i )
 | 
|---|
 | 194 |             i['clientId'] = dst
 | 
|---|
| [1088] | 195 |             products_dst.append(i)
 | 
|---|
 | 196 |         self.rpc.productOnClient_createObjects( products_dst )
 | 
|---|
| [1087] | 197 |         if self.debug:
 | 
|---|
 | 198 |             pprint( self.getProductOnClient( dst ) )
 | 
|---|
| [1088] | 199 | 
 | 
|---|
| [1174] | 200 | 
 | 
|---|
| [1088] | 201 |     def getProductPropertyState( self, client, attributes = [] ):
 | 
|---|
 | 202 |         return self.rpc.productPropertyState_getHashes( [], { 'objectId': client } )
 | 
|---|
| [1174] | 203 | 
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 |     def copyProductPropertyState( self, src, dst, default = None, attributes = [] ):
 | 
|---|
 | 206 |         if default:
 | 
|---|
 | 207 |             productProperties_default = self.getProductPropertyState( default, attributes )
 | 
|---|
 | 208 |         else:
 | 
|---|
 | 209 |             productProperties_default = []
 | 
|---|
| [1088] | 210 |         productProperties_src = self.getProductPropertyState( src, attributes )
 | 
|---|
 | 211 |         productProperties_dst = []
 | 
|---|
 | 212 |         for i in productProperties_src:
 | 
|---|
| [1174] | 213 |             use_default=False
 | 
|---|
 | 214 |             for j in productProperties_default:
 | 
|---|
 | 215 |                 if i['productId'] == j['productId'] and i["propertyId"] == j["propertyId"]:
 | 
|---|
 | 216 |                     if i['values'] == j['values']:
 | 
|---|
 | 217 |                         use_default=True
 | 
|---|
 | 218 |             if self.debug:
 | 
|---|
 | 219 |                 print i['productId'], "-", i["propertyId"] + ": ", pformat(i["values"]),
 | 
|---|
 | 220 |                 if use_default:
 | 
|---|
 | 221 |                     print "(use default)"
 | 
|---|
 | 222 |                 else:
 | 
|---|
 | 223 |                     print "(set)"
 | 
|---|
 | 224 |             if not use_default:
 | 
|---|
 | 225 |                 i['objectId'] = dst
 | 
|---|
 | 226 |                 productProperties_dst.append(i)
 | 
|---|
| [1088] | 227 |         self.rpc.productPropertyState_createObjects( productProperties_dst )
 | 
|---|
 | 228 |         if self.debug:
 | 
|---|
 | 229 |             pprint( self.getProductPropertyState( dst ) )
 | 
|---|
| [1110] | 230 | 
 | 
|---|
 | 231 | 
 | 
|---|
 | 232 |     def getClientProductProperty( self, client, product ):
 | 
|---|
 | 233 |         return self.rpc.getProductProperties_hash( product, [ client ] )
 | 
|---|
 | 234 | 
 | 
|---|
| [1118] | 235 |     def setProductPropertiesOnClient( self, dst, product, properties ):
 | 
|---|
 | 236 |         self.rpc.setProductProperties(product,properties,dst)
 | 
|---|
 | 237 | 
 | 
|---|
 | 238 |     def setProductPropertyOnClient( self, dst, product, prop, value):
 | 
|---|
 | 239 |         self.rpc.setProductProperty(product,prop,value,dst)
 | 
|---|
 | 240 | 
 | 
|---|
 | 241 | 
 | 
|---|
| [1110] | 242 |     def write_client_conf( self, fd, client, properties ):
 | 
|---|
 | 243 |         #Client {
 | 
|---|
 | 244 |         #Name = ting-fd
 | 
|---|
 | 245 |         #Address = ting.dass-it
 | 
|---|
 | 246 |         #FDPort = 9102
 | 
|---|
 | 247 |         #Catalog = MyCatalog
 | 
|---|
 | 248 |         #Password = "D5w2V5w6B8a9H5Z"
 | 
|---|
 | 249 |         #File Retention = 6 months
 | 
|---|
 | 250 |         #Job Retention = 6 months
 | 
|---|
 | 251 |         #AutoPrune = yes
 | 
|---|
 | 252 |         #}
 | 
|---|
 | 253 |         params = [ "FDPort", "FileRetention", "JobRetention", "AutoPrune" ]
 | 
|---|
 | 254 |         fd.write( "Client {\n" )
 | 
|---|
 | 255 |         fd.write( '  Name     = "' + properties['filedaemon_full_name'] + '"' + "\n" )
 | 
|---|
 | 256 |         fd.write( '  Address  = "' + client['clientId'] + '"' + "\n" )
 | 
|---|
 | 257 |         # ipAddress: method host_getObjects [] '{"id":client['clientId']}'
 | 
|---|
 | 258 |         #print "  # Address =", ipAddress
 | 
|---|
 | 259 |         fd.write( '  Password = "' + properties['filedaemon_full_password'] + '"' + "\n" )
 | 
|---|
 | 260 |         try:
 | 
|---|
 | 261 |             catalog = properties['catalog']
 | 
|---|
 | 262 |         except KeyError:
 | 
|---|
 | 263 |             catalog = "MyCatalog"
 | 
|---|
 | 264 |         fd.write( '  Catalog  = "' + catalog + '"' + "\n" )
 | 
|---|
 | 265 |         for i in params:
 | 
|---|
 | 266 |             try:
 | 
|---|
 | 267 |                 fd.write( '  ' + i + ' = "' + properties[i.lower()] + '"' + "\n" )
 | 
|---|
 | 268 |             except KeyError:
 | 
|---|
 | 269 |                 fd.write( '  # ' + i + " = \n" )
 | 
|---|
 | 270 |         fd.write( "}\n")
 | 
|---|
 | 271 |         fd.write( "\n" )
 | 
|---|
| [1087] | 272 | 
 | 
|---|
 | 273 | 
 | 
|---|
| [1110] | 274 |     def write_job_conf( self, fd, client, properties ):
 | 
|---|
 | 275 |         #Job {
 | 
|---|
 | 276 |         #FileSet = "tingfileset"
 | 
|---|
 | 277 |         #Name = "ting"
 | 
|---|
 | 278 |         #Client = ting-fd
 | 
|---|
 | 279 |         #JobDefs = "LaptopJob"
 | 
|---|
 | 280 |         ## Write Bootstrap = "/var/lib/bacula/ting.bsr"
 | 
|---|
 | 281 |         #}
 | 
|---|
 | 282 |         params = [ "Fileset", "JobDefs" ]
 | 
|---|
 | 283 |         fd.write( "Job {" + "\n" )
 | 
|---|
 | 284 |         fd.write( '  Name    = "' + client['clientId'] + '-job"' + "\n" )
 | 
|---|
 | 285 |         fd.write( '  Client  = "' + properties['filedaemon_full_name'] + '"' + "\n" )
 | 
|---|
 | 286 |         for i in params:
 | 
|---|
 | 287 |             fd.write( "  " )
 | 
|---|
 | 288 |             try:
 | 
|---|
 | 289 |                 if not properties[i.lower()]:
 | 
|---|
 | 290 |                     fd.write( "# " )
 | 
|---|
 | 291 |                 fd.write( i + ' = "' + properties[i.lower()] + '"' + "\n" )
 | 
|---|
 | 292 |             except KeyError:
 | 
|---|
 | 293 |                 fd.write( "# " + i + " = " + "\n" )
 | 
|---|
 | 294 |         fd.write( "}" + "\n" )
 | 
|---|
 | 295 |         fd.write( "\n" )
 | 
|---|
 | 296 | 
 | 
|---|
 | 297 | 
 | 
|---|
 | 298 |     def write_config_file_header( self, fd ):
 | 
|---|
| [1174] | 299 |         try:
 | 
|---|
| [1110] | 300 |             fd.write( "#\n" )
 | 
|---|
| [1114] | 301 |             fd.write( "# automatically generated at {0}\n".format( time.asctime() ) )
 | 
|---|
| [1110] | 302 |             fd.write( "#\n\n" )
 | 
|---|
 | 303 |         except BaseException as e:        
 | 
|---|
 | 304 |             self.logger.exception( "failed to create files" )
 | 
|---|
 | 305 |             return False
 | 
|---|
 | 306 |         return True
 | 
|---|
 | 307 | 
 | 
|---|
| [1174] | 308 | 
 | 
|---|
| [1110] | 309 |     def createBaculaConfigFiles( self ):
 | 
|---|
 | 310 |         clientsWithBacula=self.getClientsWithProduct( "bacula" )
 | 
|---|
 | 311 |         if clientsWithBacula:
 | 
|---|
 | 312 |             try:
 | 
|---|
 | 313 |                 file_opsi_clients = open('opsi-clients-generated.conf', 'w')
 | 
|---|
 | 314 |                 self.write_config_file_header( file_opsi_clients )
 | 
|---|
| [1174] | 315 | 
 | 
|---|
| [1110] | 316 |                 file_opsi_jobs = open('opsi-jobs-generated.conf', 'w')
 | 
|---|
 | 317 |                 self.write_config_file_header( file_opsi_jobs )
 | 
|---|
| [1174] | 318 |             except BaseException as e:
 | 
|---|
| [1110] | 319 |                 self.logger.exception( "failed to create files" )
 | 
|---|
 | 320 |                 return False
 | 
|---|
 | 321 |             for client in clientsWithBacula:
 | 
|---|
| [1174] | 322 |                 clientId = client['clientId']
 | 
|---|
| [1110] | 323 |                 try:
 | 
|---|
 | 324 |                     clientBaculaProperties=self.getClientProductProperty( clientId, "bacula" )
 | 
|---|
 | 325 |                 except ValueError as e:
 | 
|---|
 | 326 |                     self.logger.warn( "%s: no valid information found: %s" %(clientId, e) )
 | 
|---|
 | 327 |                 else:
 | 
|---|
 | 328 |                     if clientBaculaProperties:
 | 
|---|
 | 329 |                         #pprint( clientBaculaProperties )
 | 
|---|
 | 330 |                         self.write_client_conf( file_opsi_clients, client, clientBaculaProperties )
 | 
|---|
 | 331 |                         self.write_job_conf( file_opsi_jobs, client, clientBaculaProperties )
 | 
|---|
 | 332 |                         self.logger.info( "%s: OK" % clientId )
 | 
|---|
 | 333 |                     else:
 | 
|---|
 | 334 |                         self.logger.warn( "%s: failed: no product properties defined" %(clientId) )
 | 
|---|
 | 335 |             return True
 | 
|---|
 | 336 | 
 | 
|---|
 | 337 | 
 | 
|---|
 | 338 | 
 | 
|---|
| [1047] | 339 | if __name__ == '__main__':
 | 
|---|
| [1110] | 340 |     logging.basicConfig(format='%(message)s')
 | 
|---|
 | 341 |     logger = logging.getLogger(__name__)
 | 
|---|
 | 342 |     logger.setLevel(logging.INFO)
 | 
|---|
 | 343 |     
 | 
|---|
| [1063] | 344 |     parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.', epilog=HelpEpilog )
 | 
|---|
 | 345 | 
 | 
|---|
| [1047] | 346 |     parser.add_argument( '--debug', action='store_true', help="enable debugging output" )
 | 
|---|
| [1028] | 347 | 
 | 
|---|
| [1110] | 348 |     parser_url = parser.add_mutually_exclusive_group(required=True)
 | 
|---|
 | 349 |     parser_url.add_argument( '--url', help="OPSI Server JSON-RPC url, in following format: " + UrlJsonRpc )
 | 
|---|
 | 350 |     
 | 
|---|
 | 351 |     parser_url.add_argument( '--server', help="OPSI Server (instead of URL)" )
 | 
|---|
 | 352 |     username_default=os.getlogin()
 | 
|---|
 | 353 |     
 | 
|---|
 | 354 |     parser.add_argument( '--username', help="username (instead of URL), default: " + username_default, default=username_default )
 | 
|---|
 | 355 |     parser.add_argument( '--password', help="password (instead of URL)" )
 | 
|---|
 | 356 |     
 | 
|---|
| [1050] | 357 |     subparsers = parser.add_subparsers(title='subcommands',
 | 
|---|
 | 358 |         description='valid subcommands',
 | 
|---|
 | 359 |         help='additional help',
 | 
|---|
 | 360 |         dest='subcommand' )
 | 
|---|
| [1047] | 361 | 
 | 
|---|
| [1050] | 362 |     parser_clean = subparsers.add_parser('clean', help='remove all product states from a opsi client' )
 | 
|---|
 | 363 |     parser_clean.add_argument( 'src', help="source opsi client to clean" )
 | 
|---|
| [1174] | 364 | 
 | 
|---|
 | 365 |     parser_copy = subparsers.add_parser('copy', help='copy/create a opsi client from a template opsi client')
 | 
|---|
| [1050] | 366 |     parser_copy.add_argument( 'src', help="source/template opsi client" )
 | 
|---|
 | 367 |     parser_copy.add_argument( 'dst', help="opsi client to be created" )
 | 
|---|
 | 368 |     parser_copy.add_argument( '--ip', help="IP address of the new opsi client" )
 | 
|---|
 | 369 |     parser_copy.add_argument( '--mac', help="MAC address of the new opsi client" )
 | 
|---|
 | 370 |     parser_copy.add_argument( '--depot', help="depot server the new opsi client should be located" )
 | 
|---|
| [1088] | 371 |     #parser_copy.add_argument( '--no-properties', action='store_false', help="don't copy product properties" )
 | 
|---|
| [1174] | 372 | 
 | 
|---|
| [1110] | 373 |     parser_createBaculaConfigFiles = subparsers.add_parser('createBaculaConfigFiles', help='create Bacula config files for all clients that have bacula installed')
 | 
|---|
 | 374 | 
 | 
|---|
 | 375 |     parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' )
 | 
|---|
 | 376 |     parser_exists.add_argument( 'src', help="source opsi client" )
 | 
|---|
 | 377 |     #parser_list = subparsers.add_parser('list', help='list all opsi clients' )
 | 
|---|
| [1174] | 378 | 
 | 
|---|
| [1110] | 379 |     parser_listClients = subparsers.add_parser('listClients', help='list opsi clients')
 | 
|---|
 | 380 |     parser_listClients.add_argument( '--product', help="only list clients, that have product installed" )
 | 
|---|
| [1174] | 381 | 
 | 
|---|
| [1110] | 382 |     parser_info = subparsers.add_parser('info', help='print information about a opsi client' )
 | 
|---|
 | 383 |     parser_info.add_argument( 'src', help="opsi client" )
 | 
|---|
| [1174] | 384 | 
 | 
|---|
| [1117] | 385 |     parser_update = subparsers.add_parser('update', help='update/create a opsi client')
 | 
|---|
 | 386 |     parser_update.add_argument( 'src', help="opsi client to be created" )
 | 
|---|
 | 387 |     parser_update.add_argument( '--ip', help="IP address of the new opsi client" )
 | 
|---|
 | 388 |     parser_update.add_argument( '--mac', help="MAC address of the new opsi client" )
 | 
|---|
 | 389 |     parser_update.add_argument( '--description', help="IP address of the new opsi client" )
 | 
|---|
 | 390 |     parser_update.add_argument( '--notes', help="MAC address of the new opsi client" )    
 | 
|---|
 | 391 |     parser_update.add_argument( '--depot', help="depot server the new opsi client should be located" )
 | 
|---|
 | 392 |     
 | 
|---|
| [1047] | 393 |     args = parser.parse_args()
 | 
|---|
 | 394 |     
 | 
|---|
| [1110] | 395 |     if args.debug:
 | 
|---|
 | 396 |         logger.setLevel(logging.DEBUG)
 | 
|---|
 | 397 | 
 | 
|---|
 | 398 |     url=args.url
 | 
|---|
 | 399 |     if (not url):
 | 
|---|
 | 400 |         if args.server:
 | 
|---|
 | 401 |             account=""
 | 
|---|
 | 402 |             if args.username and args.password:
 | 
|---|
 | 403 |                 account=args.username + ":" + args.password + "@"
 | 
|---|
 | 404 |             elif args.username:
 | 
|---|
 | 405 |                 account=args.username + "@"
 | 
|---|
 | 406 |             url="https://" + account + args.server + ":4447/rpc"
 | 
|---|
 | 407 |         else:
 | 
|---|
 | 408 |             parser.error( "argument --url is required" )
 | 
|---|
| [1174] | 409 | 
 | 
|---|
| [1110] | 410 |     opsi=OpsiRpc( url, args.debug )
 | 
|---|
| [1174] | 411 | 
 | 
|---|
| [1051] | 412 |     result = True
 | 
|---|
 | 413 | 
 | 
|---|
| [1174] | 414 |     try:
 | 
|---|
 | 415 |         if args.subcommand == "clean":
 | 
|---|
 | 416 |             result = opsi.clean( args.src )
 | 
|---|
 | 417 |         elif args.subcommand == "copy": 
 | 
|---|
 | 418 |             result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
 | 
|---|
 | 419 |         elif args.subcommand == "createBaculaConfigFiles": 
 | 
|---|
 | 420 |             result = opsi.createBaculaConfigFiles()
 | 
|---|
 | 421 |         elif args.subcommand == "exists":
 | 
|---|
 | 422 |             result = opsi.exists( args.src )
 | 
|---|
 | 423 |         elif args.subcommand == "list":
 | 
|---|
 | 424 |             result = opsi.list()
 | 
|---|
 | 425 |         elif args.subcommand == "listClients":
 | 
|---|
 | 426 |             result = opsi.listClients( args.product )
 | 
|---|
 | 427 |         elif args.subcommand == "info":
 | 
|---|
 | 428 |             result = opsi.info( args.src )
 | 
|---|
 | 429 |         elif args.subcommand == "update": 
 | 
|---|
 | 430 |             result = opsi.updateClient( args.src, None, args.description, args.notes, args.mac, args.ip, args.depot )
 | 
|---|
 | 431 |         else:
 | 
|---|
 | 432 |             print "not yet implemented"
 | 
|---|
 | 433 |     except IOError as e:
 | 
|---|
 | 434 |         result = False
 | 
|---|
 | 435 |         # connection refused
 | 
|---|
 | 436 |         print "failed:", e
 | 
|---|
| [1050] | 437 | 
 | 
|---|
| [1174] | 438 |     if args.debug: print result
 | 
|---|
 | 439 | 
 | 
|---|
| [1051] | 440 |     if result:
 | 
|---|
 | 441 |         exit(0)
 | 
|---|
 | 442 |     else:
 | 
|---|
 | 443 |         exit(1)
 | 
|---|