import repository from arizona
[raven.git] / apps / mprepo / repo_xmlrpc_server.py
1 #!/usr/bin/python
2
3 """arizonaconfig
4    options=[["-C",   "--configfile",    "configfile",    "store",      "string", "/usr/local/mprepo/etc/mprepo.conf",        "FILE", "use a different config file (/usr/local/cherryrepo/etc/repo.conf is the default)"],
5             ["",     "--daemon",        "daemon",        "store_true", None,     False,                                      None, "daemonize"],
6
7              ]
8
9    includes=[]
10 """
11 import tempfile
12 import os
13 import sys
14 import base64
15 from xmlrpclib import ServerProxy
16
17 from sfa.trust.certificate import *
18 from sfa.trust.gid import *
19 from sfa.trust.trustedroot import *
20 from sfa.util.faults import *
21 from sfa.util.misc import *
22
23 from geniserverbase import *
24
25 import arizonaconfig
26 import repobackend
27 import repoclassify
28 import repoplc
29 import repoconfig
30 import repouid
31
32 LOG_DIR = "/usr/local/mprepo/var/log"
33
34 class RepositoryManager(GeniServerBase):
35
36     def __init__(self, ip, port, key_file, cert_file):
37         GeniServerBase.__init__(self, ip, port, key_file, cert_file)
38
39     ##
40     # Register the server RPCs for the component
41
42     def register_functions(self):
43         GeniServerBase.register_functions(self)
44         self.server.register_function(self.enable_stork)
45         self.server.register_function(self.disable_stork)
46         self.server.register_function(self.upload_file)
47         self.server.register_function(self.upload_legacy)
48
49     def enable_stork(self, cred_str):
50         self.decode_authentication(cred_str, None)
51         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
52         # XXX: Finish
53
54     def disable_stork(self, cred_str):
55         self.decode_authentication(cred_str, None)
56         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
57         # XXX: Finish
58
59     def upload_base(self, file_name, file_bytes, slice):
60         file_bytes = base64.b64decode(file_bytes)
61
62         fdt, tmpfile_name = tempfile.mkstemp()
63         os.write(fdt, file_bytes)
64         os.close(fdt)
65
66         file_type = repoclassify.getFiletype(file_name)
67
68         desiredname = os.path.basename(file_name.replace("\\","/"))
69
70         file = {"srcname": tmpfile_name,
71                 "type": file_type,
72                 "desiredname": desiredname}
73
74         if slice:
75            file['slice'] = slice
76
77         repobackend.process_uploads([file])
78
79         return True
80
81     def upload_file(self, cred_str, file_name, file_bytes):
82         self.decode_authentication(cred_str, None)
83         return self.upload_base(file_name, file_bytes, None)
84
85     def upload_legacy(self, cred_str, file_name, file_bytes):
86         self.decode_authentication(cred_str, None)
87         slicename = hrn_to_pl_slicename(self.object_gid.get_hrn())
88
89         # XXX: Verify that credential is a slice credential??
90
91         return self.upload_base(file_name, file_bytes, slicename)
92
93 def daemon():
94     """Daemonize the current process.\r
95        XXX code from princeton plc.py """\r
96     if os.fork() != 0: os._exit(0)\r
97     os.setsid()\r
98     if os.fork() != 0: os._exit(0)\r
99     os.umask(0)\r
100     devnull = os.open(os.devnull, os.O_RDWR)\r
101     os.dup2(devnull, 0)\r
102     # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull\r
103     crashlog = os.open(os.path.join(LOG_DIR, "repo_xmlrpc_server.demon"), os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)\r
104     os.dup2(crashlog, 1)\r
105     os.dup2(crashlog, 2)
106
107 if __name__ == "__main__":
108     global TrustedRoots
109
110     arizonaconfig.init_options("repo_xmlrpc_server.py", configfile_optvar="configfile", alt_paths=["/var/www/html/src"])
111
112     if arizonaconfig.get_option("daemon"):
113         daemon()
114         open("/var/run/repo_xmlrpc_server.pid","w").write(str(os.getpid()))
115
116     repouid.switchuid()
117
118     repoconfig.init()
119
120     key_file = "component.key"
121     cert_file = "component.cert"
122
123     # if no key is specified, then make one up
124     if (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
125         key = Keypair(create=True)
126         key.save_to_file(key_file)
127
128         cert = Certificate(subject="storkrepo")
129         cert.set_issuer(key=key, subject="storkrepo")
130         cert.set_pubkey(key)
131         cert.sign()
132         cert.save_to_file(cert_file)
133
134     TrustedRoots = TrustedRootList(dir = repoconfig.repo_trusted_roots_dir)
135
136     s = RepositoryManager("", 12348, key_file, cert_file)
137     s.trusted_cert_list = TrustedRoots.get_list()
138     s.run()
139