checking in initial version of component interface
[sfa.git] / component / component.py
1 import tempfile
2 import os
3
4 import sys
5
6 from cert import *
7 from gid import *
8 from geniserver import *
9 from excep import *
10 from trustedroot import *
11 from misc import *
12 from record import *
13
14 import accounts
15
16
17 class ComponentManager(GeniServer):
18     def __init__(self, ip, port, key_file, cert_file):
19         GeniServer.__init__(self, ip, port, key_file, cert_file)
20
21     def register_functions(self):
22         GeniServer.register_functions(self)
23         self.server.register_function(self.stop_slice)
24         self.server.register_function(self.start_slice)
25         self.server.register_function(self.reset_slice)
26         self.server.register_function(self.delete_slice)
27
28     def stop_slice(self, cred_str):
29         self.decode_authentication(cred_str, "stopslice")
30         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
31         print "stopslice:", slicename
32         accounts.get(slicename).start()
33
34     def start_slice(self, cred_str):
35         self.decode_authentication(cred_str, "startslice")
36         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
37         print "startslice:", slicename
38         accounts.get(slicename).start()
39
40     def reset_slice(self, cred_str):
41         self.decode_authentication(cred_str, "resetslice")
42         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
43         print "resetslice:", slicename
44         accounts.get(slicename).stop()
45         accounts.get(slicename).ensure_destroyed()
46         accounts.get(slicename).ensure_created()
47
48     def delete_slice(self, cred_str):
49         self.decode_authentication(cred_str, "deleteslice")
50         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
51         print "deleteslice:", slicename
52         accounts.get(slicename).ensure_destroyed()
53
54
55 if __name__ == "__main__":
56     global TrustedRoots
57
58     key_file = "component.key"
59     cert_file = "component.cert"
60
61     # if no key is specified, then make one up
62     if (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
63         key = Keypair(create=True)
64         key.save_to_file(key_file)
65
66         cert = Certificate(subject="component")
67         cert.set_issuer(key=key, subject="component")
68         cert.set_pubkey(key)
69         cert.sign()
70         cert.save_to_file(cert_file)
71
72     TrustedRoots = TrustedRootList()
73
74     s = ComponentManager("", 12345, key_file, cert_file)
75     s.trusted_cert_list = TrustedRoots.get_list()
76     s.run()
77