import repository from arizona
[raven.git] / apps / mprepo / repogeni.py
1 import os
2 import signal
3 import sys
4
5 import arizonaconfig
6 import arizonageneral
7 import arizonareport
8 import arizonatemplate
9
10 import repoconfig
11
12 from sfa.trust.certificate import Keypair, Certificate
13 from sfa.trust.gid import GID
14 from sfa.trust.credential import Credential
15 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
16
17 REPO_HRN="plc.arizona.smbaker"
18 #REG_URL="http://198.0.0.132:12345/"
19 REG_URL="http://www.planet-lab.org:12345/"
20
21 def get_self_credential(registry, kind, hrn):
22     key = registry.repo_key
23     cert = registry.repo_cert
24     cert_string = cert.save_to_string(save_parents=True)
25     cred_str = registry.GetSelfCredential(cert_string, hrn, kind)
26     return Credential(string=cred_str)
27
28 def resolve(registry, hrn):
29     return registry.Resolve(hrn, registry.repo_cred.save_to_string(save_parents=True))
30
31 def init_connection():
32     repo_hrn = REPO_HRN
33     reg_url = REG_URL
34
35     leaf = repo_hrn.split(".")[-1]
36
37     key_file = os.path.join(repoconfig.repo_key_dir, leaf + ".pkey")
38     cert_file = os.path.join(repoconfig.repo_key_dir, leaf + ".cert")
39
40     key = Keypair(filename = key_file)
41     if not os.path.exists(cert_file):
42         cert = Certificate(subject="storkrepo")
43         cert.set_issuer(key=key, subject="storkrepo")
44         cert.set_pubkey(key)
45         cert.sign()
46         cert.save_to_file(cert_file)
47     else:
48         cert = Certificate(filename = cert_file)
49
50     registry = xmlrpcprotocol.get_server(reg_url, key_file, cert_file)
51     registry.repo_key = key
52     registry.repo_cert = cert
53     registry.repo_hrn = repo_hrn
54     registry.repo_cred = get_self_credential(registry, "user", repo_hrn)
55
56     return registry
57
58 def get_record(kind, hrn):
59     registry = init_connection()
60     records = resolve(registry, hrn)
61
62     if not records:
63        return None;
64
65     return records[0]
66
67 def get_slice_hrns(hrn):
68     user_record = get_record("user", hrn)
69     return user_record.get("slices", [])
70
71 # -- the items below are entrypoints from the frontent --
72
73 def get_gid(hrn):
74     user_record = get_record("user", hrn)
75     return GID(string = user_record.get("gid", None))
76
77 def get_slices(hrn):
78     slice_names = []
79     slice_hrns = get_slice_hrns(hrn)
80     dicts = []
81     for hrn in slice_hrns:
82         parts = hrn.split(".")
83         planetlab_slice_name = parts[-2] + "_" + parts[-1]
84
85         dict = {"name": planetlab_slice_name, "displayname": hrn}
86         dicts.append(dict)
87     return dicts
88