source: opsi/server/dass-opsi-tools/usr/bin/opsi-client.py @ 1050

Last change on this file since 1050 was 1050, checked in by joergs, 9 years ago

added subcommand

  • Property svn:executable set to *
File size: 5.1 KB
Line 
1#!/usr/bin/env python
2
3# Skript erstellen, dass ein OPSI-Rechner-Eintrag kopiert.
4# D.h. die Produkte, Anforderung.
5# Ggf. optional Stand und ggf. Versionsnummer
6# Ggf. optional ProductProperties
7
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)                       
15                       
16import argparse
17import jsonrpc
18from pprint import pprint
19
20UrlJsonRpc="https://joergs:linuxlinux@opsi4.joergs.dass-it:4447/rpc"
21
22class OpsiRpc:
23   
24    ProductAttributesCopy = ['actionRequest','actionResult','installationStatus','packageVersion','productVersion']
25   
26    def __init__(self, urlJsonRpc, debug=False ):
27        self.debug=debug
28        self.urlJsonRpc=urlJsonRpc
29        self.rpc=jsonrpc.ServiceProxy(self.urlJsonRpc)
30       
31    def dump(self):
32        print self.urlJsonRpc
33        print self.rpc.getClientIds_list()
34       
35    def copyClient( self, src, dst, ipAddress = None, hardwareAddress = None, depot = None ):
36   
37        print "create/update", dst, "from template", src + ":",
38        #method host_createOpsiClient id *opsiHostKey *description *notes *hardwareAddress *ipAddress *inventoryNumber *oneTimePassword *created *lastSeen
39        #id=dst
40        opsiHostKey=None
41        description=""
42        notes="copy of " + src
43        self.rpc.host_createOpsiClient( dst, opsiHostKey, description, notes, hardwareAddress, ipAddress )
44        if depot:
45            self.rpc.configState_create( "clientconfig.depot.id", dst, depot )       
46        if self.debug:
47            self.productOnClient( src )
48        self.copyProductOnClient( src, dst )
49        # TODO:
50        #  copy product properties:
51        #  opsiCallClientBaculaProperties=[ "method", "getProductProperties_hash", "bacula" ]
52        print "done"
53
54       
55    def productOnClient( self, client, attributes = ProductAttributesCopy ):
56        #pprint( self.rpc.productOnClient_getObjects( [], { 'clientId': client } ) )
57        #pprint( self.rpc.productOnClient_getHashes( attributes, { 'clientId': client } ) )
58        pprint( self.rpc.productOnClient_getHashes( [], { 'clientId': client } ) )
59       
60    def copyProductOnClient( self, src, dst, attributes = ProductAttributesCopy ):
61        products = self.rpc.productOnClient_getHashes( attributes, { 'clientId': src } )
62        for i in products:
63            if self.debug:
64                pprint( i )
65                print i['productId']
66            i['clientId'] = dst
67            self.rpc.productOnClient_createObjects( i )
68            if self.debug:
69                self.productOnClient( dst )
70       
71
72
73if __name__ == '__main__':
74    parser = argparse.ArgumentParser(description='Command line tool for OPSI configuration.')
75    parser.add_argument( '--debug', action='store_true', help="enable debugging output" )
76    #parser.add_argument( '--verbose', type=bool, help="add debugging output" )
77    parser.add_argument( '--url', help="OPSI Server JSON-RPC url, normally https://<username>:<password>@<OPSI-SERVER>:4447/rpc" )
78
79    subparsers = parser.add_subparsers(title='subcommands',
80        description='valid subcommands',
81        help='additional help',
82        dest='subcommand' )
83
84    parser_list = subparsers.add_parser('list', help='list all opsi clients' )
85
86    parser_exists = subparsers.add_parser('exists', help='check, if a opsi clients exists' )
87    parser_exists.add_argument( 'src', help="source opsi client" )
88   
89    parser_clean = subparsers.add_parser('clean', help='remove all product states from a opsi client' )
90    parser_clean.add_argument( 'src', help="source opsi client to clean" )
91               
92    parser_copy = subparsers.add_parser('copy', help='copy/create a opsi client from a template opsi client')   
93    parser_copy.add_argument( 'src', help="source/template opsi client" )
94    parser_copy.add_argument( 'dst', help="opsi client to be created" )
95    parser_copy.add_argument( '--ip', help="IP address of the new opsi client" )
96    parser_copy.add_argument( '--mac', help="MAC address of the new opsi client" )
97    parser_copy.add_argument( '--depot', help="depot server the new opsi client should be located" )
98
99    #>>> parser_bar.set_defaults(func=bar)
100    #>>> # parse the args and call whatever function was selected
101    #>>> args = parser.parse_args('bar XYZYX'.split())
102    #>>> args.func(args)   
103   
104   
105    args = parser.parse_args()
106   
107   
108    urlJsonRpc = UrlJsonRpc
109    if args.url:
110        urlJsonRpc = args.url
111   
112    opsi=OpsiRpc( urlJsonRpc, args.debug )
113   
114    if args.subcommand == "copy":
115        opsi.copyClient( args.src, args.dst, args.ip, args.mac, args.depot )
116    else:
117        print "not yet implemented"
118
119    if args.debug: print "end"
120   
Note: See TracBrowser for help on using the repository browser.