- Test script for obtaining a node session key
[nodemanager.git] / bootauth.py
1 #!/usr/bin/python
2 #
3 # Test script for obtaining a node session key. Usually, the Boot
4 # Manager obtains it, then writes it to /etc/planetlab/session. To
5 # generate a node key for a node, execute:
6 #
7 # AdmGenerateNodeConfFile(node_id)
8 #
9 # Mark Huang <mlhuang@cs.princeton.edu>
10 # Copyright (C) 2006 The Trustees of Princeton University
11 #
12 # $Id$
13 #
14
15 import os, sys
16 import getopt
17
18 from config import Config
19 from plcapi import PLCAPI
20
21 def main():
22     # Defaults
23     config = None
24     node_id = None
25     key = None
26
27     # Help
28     def usage():
29         print "Usage: %s [OPTION]..." % sys.argv[0]
30         print "Options:"
31         print "     -f, --config=FILE       PLC configuration file (default: /etc/planetlab/plc_config)"
32         print "     -n, --node-id=FILE      Node ID (or file)"
33         print "     -k, --key=FILE          Node key (or file)"
34         print "     --help                  This message"
35         sys.exit(1)
36
37     # Get options
38     try:
39         (opts, argv) = getopt.getopt(sys.argv[1:], "n:k:h",
40                                      ["node=", "nodeid=", "node-id", "node_id",
41                                       "key=",
42                                       "help"])
43     except getopt.GetoptError, err:
44         print "Error: " + err.msg
45         usage()
46
47     for (opt, optval) in opts:
48         if opt == "-f" or opt == "--config" or opt == "--cfg" or opt == "--file":
49             config = optval
50         elif opt == "-n" or opt == "--node" or opt == "--nodeid" or opt == "--node-id" or opt == "--node_id":
51             if os.path.exists(optval):
52                 node_id = file(optval).read().strip()
53             else:
54                 node_id = int(optval)
55         elif opt == "-k" or opt == "--key":
56             if os.path.exists(optval):
57                 key = file(optval).read().strip()
58             else:
59                 key = optval
60         else:
61             usage()
62
63     if config is None:
64         config = Config()
65
66     if node_id is None or \
67        key is None:
68         usage()
69
70     # Authenticate as the Boot Manager would and get a session key
71     plc = PLCAPI(config.plc_api_uri, (node_id, key))
72     session = plc.BootGetNodeDetails()['session']
73
74     plc = PLCAPI(config.plc_api_uri, session)
75     assert session == plc.GetSession()
76
77     print session
78
79 if __name__ == '__main__':
80     main()