changed the sfa db schema. All records are now stored in 1 table instead of createing...
[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     table = GeniTable()
57     table.create()
58
59     if not level1_auth or level1_auth in ['']:
60         level1_auth = None
61     
62     print "Import: creating top level authorities"
63     if not level1_auth:
64         sfaImporter.create_top_level_auth_records(root_auth)
65         import_auth = root_auth
66     else:
67         if not AuthHierarchy.auth_exists(level1_auth):
68             AuthHierarchy.create_auth(level1_auth)
69         sfaImporter.create_top_level_auth_records(level1_auth)
70         import_auth = level1_auth
71
72     print "Import: adding", import_auth, "to trusted list"
73     authority = AuthHierarchy.get_auth_info(import_auth)
74     TrustedRoots.add_gid(authority.get_gid_object())
75
76     sites = shell.GetSites(plc_auth, {'peer_id': None})
77     # create a fake internet2 site first
78     i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
79                     'login_base': 'internet2', 'site_id': -1}
80     sfaImporter.import_site(import_auth, i2site)
81             
82     for site in sites:
83         sfaImporter.import_site(import_auth, site)
84
85 if __name__ == "__main__":
86     main()