Setting tag planetlab-umts-tools-0.7-1
[planetlab-umts-tools.git] / utils / add_to_nodegroup.py
1 #!/usr/bin/python
2
3 # adds a node to a nodegroup - to be called with an argument (the ip or hostname of the plc)
4
5 import sys
6 import xmlrpclib
7
8 import sys
9 import getpass
10
11
12 sys.path.append('/usr/share/plc_api')
13
14
15 if (len(sys.argv)<2):
16         print('I need the first argument (the pi or hostname address of the plc)')
17         exit(1)
18
19 plc_ip=sys.argv[1]
20
21
22 user = raw_input('Insert your user:')
23 password= getpass.getpass('Insert your password:')
24 #password = raw_input('Insert your password:')
25 hostname = raw_input('Insert the node hostname:')
26 nodegroup=raw_input('Insert the group:')
27
28
29 auth = {}
30
31 auth['AuthMethod'] = 'password'
32 auth['Username'] = user
33 auth['AuthString'] = password
34
35
36 plc = xmlrpclib.ServerProxy('https://'+plc_ip+'/PLCAPI/', allow_none=True)
37
38         
39 authorized = plc.AuthCheck(auth)
40
41 if authorized:
42         print 'We are authorized!'
43         
44 node_fields = {'nodegroups' : [nodegroup] }
45
46 nodes = plc.GetNodes(auth, hostname, ['node_id'])
47
48 if len(nodes)>1:
49         print 'I found  %d node with that hostname' % len(nodes)
50         exit(1)
51         
52 if len(nodes)==0:
53         print 'I haven\'t found any nodes with that hostname'
54         exit(1)
55
56 node_id=nodes[0]['node_id']
57
58 if plc.UpdateNode (auth, node_id, node_fields) != 1:
59         print "Couldn't set the nodegroup - error in UpdateNode api";
60 else:
61         print 'Successfully added node to nodegroup!'
62