fix some bugs regarding sub authority implementaiton
[sfa.git] / sfa / plc / sfa-import-plc.py
1 #!/usr/bin/python
2 #
3 ### $Id$
4 ### $URL$
5 #
6 ##
7 # Import PLC records into the Geni database. It is indended that this tool be
8 # run once to create Geni records that reflect the current state of the
9 # planetlab database.
10 #
11 # The import tool assumes that the existing PLC hierarchy should all be part
12 # of "planetlab.us" (see the root_auth and level1_auth variables below).
13 #
14 # Public keys are extracted from the users' SSH keys automatically and used to
15 # create GIDs. This is relatively experimental as a custom tool had to be
16 # written to perform conversion from SSH to OpenSSL format. It only supports
17 # RSA keys at this time, not DSA keys.
18 ##
19
20 import getopt
21 import sys
22 import tempfile
23
24 from sfa.util.record import *
25 from sfa.util.genitable import GeniTable
26 from sfa.util.misc import *
27 from sfa.util.config import Config
28 from sfa.util.report import trace, error
29
30 from sfa.trust.certificate import convert_public_key, Keypair
31 from sfa.trust.trustedroot import *
32 from sfa.trust.hierarchy import *
33 from sfa.trust.gid import create_uuid
34 from sfa.plc.sfaImport import *
35
36
37
38 def process_options():
39    global hrn
40
41    (options, args) = getopt.getopt(sys.argv[1:], '', [])
42    for opt in options:
43        name = opt[0]
44        val = opt[1]
45
46 def main():
47     process_options()
48     config = Config()
49     root_auth = config.SFA_REGISTRY_ROOT_AUTH
50     level1_auth = config.SFA_REGISTRY_LEVEL1_AUTH
51     sfaImporter = sfaImport()
52     shell = sfaImporter.shell
53     plc_auth = sfaImporter.plc_auth 
54     AuthHierarchy = sfaImporter.AuthHierarchy
55     TrustedRoots = sfaImporter.TrustedRoots
56     
57     if not level1_auth or level1_auth in ['']:
58         level1_auth = None
59     
60     print "Import: creating top level authorities"
61     if not level1_auth:
62         sfaImporter.create_top_level_auth_records(root_auth)
63         import_auth = root_auth
64     else:
65         if not AuthHierarchy.auth_exists(level1_auth):
66             AuthHierarchy.create_auth(level1_auth)
67         sfaImporter.create_top_level_auth_records(level1_auth)
68         import_auth = level1_auth
69
70     print "Import: adding", import_auth, "to trusted list"
71     authority = AuthHierarchy.get_auth_info(import_auth)
72     TrustedRoots.add_gid(authority.get_gid_object())
73
74     sites = shell.GetSites(plc_auth, {'peer_id': None})
75     # create a fake internet2 site first
76     i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
77                     'login_base': 'internet2', 'site_id': -1}
78     sfaImporter.import_site(import_auth, i2site)
79     
80     for site in sites:
81         sfaImporter.import_site(import_auth, site)
82
83 if __name__ == "__main__":
84     main()