import repository from arizona
[raven.git] / lib / ravenlib / platform / platform_protogeni.py
1 import os
2 import socket
3
4 SLICENAME_FN = "/etc/slicename"
5
6 def init():
7    if os.path.exists("/etc/emulab") and os.path.exists("/users"):
8        return True
9    else:
10        return False
11
12 def get_slice_names():
13    try:
14       if os.path.isfile(SLICENAME_FN):
15          slicefile = open(SLICENAME_FN)
16          slicename = slicefile.read().strip()
17          slicefile.close()
18       else:
19          return []
20    except (NameError, IOError):
21       return []
22
23    return [slicename]
24    # return ["protogeni_slice"]
25
26 def get_node_names():
27    hostname = socket.gethostname()
28    hostnames = [hostname]
29    parts = hostname.split(".")
30
31    # pc125.emulab.net might look like pc125.slice5827.emulab.net
32    if len(parts)>3 and parts[1].startswith("slice") and parts[2]=="genislices":
33        short_hostname = ".".join([parts[0]] + parts[3:])
34        hostnames.append(short_hostname)
35
36    return hostnames
37
38 def get_group_names():
39    return ["protogeni"]
40
41 def get_ssh_keys(fn):
42    f = open(fn, "r")
43    sshkeys = f.readlines()
44
45    keys = []
46    for key in sshkeys:
47        # remove any keys that are blank
48        key = key.strip()
49        if len(key) == 0:
50            continue
51
52        if key.startswith("#"):
53            continue
54
55        key_dict = {"kind": "ssh", "value": key, "platform": "unix"}
56        keys.append(key_dict)
57
58    return keys
59
60 def get_keys():
61    if not os.path.exists("/users"):
62        return []
63
64    keys = []
65
66    for dir in os.listdir("/users"):
67        if dir == "geniuser":
68            # what is geniuser ?
69            continue
70
71        keyfile = "/users/" + dir + "/.ssh/authorized_keys"
72
73        if not os.path.exists(keyfile):
74            continue
75
76        keys = keys + get_ssh_keys(keyfile)
77
78    return keys
79
80