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