Added ReCreate. Also added try catch to api eval of rpc method.
[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: bootauth.py,v 1.2 2006/11/18 18:16:40 mlhuang Exp $
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:], "f:n:k:h",
40                                      ["config=", "cfg=", "file=",
41                                       "node=", "nodeid=", "node-id", "node_id",
42                                       "key=",
43                                       "help"])
44     except getopt.GetoptError, err:
45         print "Error: " + err.msg
46         usage()
47
48     for (opt, optval) in opts:
49         if opt == "-f" or opt == "--config" or opt == "--cfg" or opt == "--file":
50             config = Config(optval)
51         elif opt == "-n" or opt == "--node" or opt == "--nodeid" or opt == "--node-id" or opt == "--node_id":
52             if os.path.exists(optval):
53                 node_id = file(optval).read().strip()
54             else:
55                 node_id = int(optval)
56         elif opt == "-k" or opt == "--key":
57             if os.path.exists(optval):
58                 key = file(optval).read().strip()
59             else:
60                 key = optval
61         else:
62             usage()
63
64     if config is None:
65         config = Config()
66
67     if node_id is None or \
68        key is None:
69         usage()
70
71     # Authenticate as the Boot Manager would and get a session key
72     plc = PLCAPI(config.plc_api_uri, config.cacert, (node_id, key))
73     session = plc.BootGetNodeDetails()['session']
74
75     plc = PLCAPI(config.plc_api_uri, config.cacert, session)
76     assert session == plc.GetSession()
77
78     print session
79
80 if __name__ == '__main__':
81     main()