| [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 | 
|---|
| [1027] | 28 | from pprint import pprint | 
|---|
| [1085] | 29 | from opsirpc import OpsiRpc | 
|---|
| [1027] | 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 |  | 
|---|
| [1047] | 35 | if __name__ == '__main__': | 
|---|
| [1063] | 36 | parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.', epilog=HelpEpilog ) | 
|---|
|  | 37 |  | 
|---|
|  | 38 | parser.add_argument( '--url', required=True, help="OPSI Server JSON-RPC url, in following format: " + UrlJsonRpc ) | 
|---|
|  | 39 |  | 
|---|
| [1047] | 40 | parser.add_argument( '--debug', action='store_true', help="enable debugging output" ) | 
|---|
|  | 41 | #parser.add_argument( '--verbose', type=bool, help="add debugging output" ) | 
|---|
| [1028] | 42 |  | 
|---|
| [1050] | 43 | subparsers = parser.add_subparsers(title='subcommands', | 
|---|
|  | 44 | description='valid subcommands', | 
|---|
|  | 45 | help='additional help', | 
|---|
|  | 46 | dest='subcommand' ) | 
|---|
| [1047] | 47 |  | 
|---|
| [1050] | 48 | parser_list = subparsers.add_parser('list', help='list all opsi clients' ) | 
|---|
|  | 49 |  | 
|---|
|  | 50 | parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' ) | 
|---|
|  | 51 | parser_exists.add_argument( 'src', help="source opsi client" ) | 
|---|
| [1051] | 52 |  | 
|---|
|  | 53 | parser_info = subparsers.add_parser('info', help='print information about a opsi client' ) | 
|---|
|  | 54 | parser_info.add_argument( 'src', help="opsi client" ) | 
|---|
|  | 55 |  | 
|---|
| [1050] | 56 | parser_clean = subparsers.add_parser('clean', help='remove all product states from a opsi client' ) | 
|---|
|  | 57 | parser_clean.add_argument( 'src', help="source opsi client to clean" ) | 
|---|
|  | 58 |  | 
|---|
|  | 59 | parser_copy = subparsers.add_parser('copy', help='copy/create a opsi client from a template opsi client') | 
|---|
|  | 60 | parser_copy.add_argument( 'src', help="source/template opsi client" ) | 
|---|
|  | 61 | parser_copy.add_argument( 'dst', help="opsi client to be created" ) | 
|---|
|  | 62 | parser_copy.add_argument( '--ip', help="IP address of the new opsi client" ) | 
|---|
|  | 63 | parser_copy.add_argument( '--mac', help="MAC address of the new opsi client" ) | 
|---|
|  | 64 | parser_copy.add_argument( '--depot', help="depot server the new opsi client should be located" ) | 
|---|
|  | 65 |  | 
|---|
| [1047] | 66 | args = parser.parse_args() | 
|---|
|  | 67 |  | 
|---|
| [1063] | 68 | opsi=OpsiRpc( args.url, args.debug ) | 
|---|
| [1050] | 69 |  | 
|---|
| [1051] | 70 | result = True | 
|---|
|  | 71 |  | 
|---|
|  | 72 | if args.subcommand == "list": | 
|---|
|  | 73 | print( "\n".join( opsi.list() ) ) | 
|---|
|  | 74 | elif args.subcommand == "exists": | 
|---|
|  | 75 | result = opsi.exists( args.src ) | 
|---|
|  | 76 | elif args.subcommand == "info": | 
|---|
|  | 77 | result = opsi.info( args.src ) | 
|---|
|  | 78 | elif args.subcommand == "clean": | 
|---|
|  | 79 | result = opsi.clean( args.src ) | 
|---|
|  | 80 | elif args.subcommand == "copy": | 
|---|
| [1052] | 81 | result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot ) | 
|---|
| [1050] | 82 | else: | 
|---|
|  | 83 | print "not yet implemented" | 
|---|
|  | 84 |  | 
|---|
| [1051] | 85 | if args.debug: print result | 
|---|
|  | 86 |  | 
|---|
|  | 87 | if result: | 
|---|
|  | 88 | exit(0) | 
|---|
|  | 89 | else: | 
|---|
|  | 90 | exit(1) | 
|---|