1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | # -*- coding: utf-8 -*- |
---|
4 | |
---|
5 | """ospi-client: performs operation for opsi clients on opsi server via JSON-RPC.""" |
---|
6 | |
---|
7 | __author__ = "Joerg Steffens" |
---|
8 | __copyright__ = "Copyright 2012, dass IT GmbH" |
---|
9 | __license__ = "GPL" |
---|
10 | __version__ = "1.0" |
---|
11 | __email__ = "joerg.steffens@dass-it.de" |
---|
12 | |
---|
13 | # |
---|
14 | # Skript, dass ein OPSI-Rechner-Eintrag kopiert. |
---|
15 | # D.h. die Produkte, Anforderung. |
---|
16 | # Ggf. optional Stand und ggf. Versionsnummer |
---|
17 | # Ggf. optional ProductProperties |
---|
18 | # |
---|
19 | |
---|
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) |
---|
26 | |
---|
27 | import argparse |
---|
28 | from pprint import pprint |
---|
29 | from opsirpc import OpsiRpc |
---|
30 | |
---|
31 | UrlJsonRpc="https://<username>:<password>@opsi:4447/rpc" |
---|
32 | |
---|
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 | |
---|
35 | if __name__ == '__main__': |
---|
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 | |
---|
40 | parser.add_argument( '--debug', action='store_true', help="enable debugging output" ) |
---|
41 | #parser.add_argument( '--verbose', type=bool, help="add debugging output" ) |
---|
42 | |
---|
43 | subparsers = parser.add_subparsers(title='subcommands', |
---|
44 | description='valid subcommands', |
---|
45 | help='additional help', |
---|
46 | dest='subcommand' ) |
---|
47 | |
---|
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" ) |
---|
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 | |
---|
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 | |
---|
66 | args = parser.parse_args() |
---|
67 | |
---|
68 | opsi=OpsiRpc( args.url, args.debug ) |
---|
69 | |
---|
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": |
---|
81 | result = opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot ) |
---|
82 | else: |
---|
83 | print "not yet implemented" |
---|
84 | |
---|
85 | if args.debug: print result |
---|
86 | |
---|
87 | if result: |
---|
88 | exit(0) |
---|
89 | else: |
---|
90 | exit(1) |
---|