Ec2MetaServer.py 6.25 KB
Newer Older
Srikanth Raju's avatar
Srikanth Raju committed
1
2
from BaseHTTPServer import BaseHTTPRequestHandler
import urlparse
Srikanth Raju's avatar
Srikanth Raju committed
3
import traceback
Srikanth Raju's avatar
Srikanth Raju committed
4
5
6
7
8
9
10
11
12
13
14
import os
import mysql.connector


class Ec2MetaHandler(BaseHTTPRequestHandler):

    def __init__(self, req, ca, huh):
        self.cnx = mysql.connector.connect(user='tmcd',
                              database='tbdb', unix_socket='/tmp/mysql.sock')
        BaseHTTPRequestHandler.__init__(self,req,ca,huh)

Srikanth Raju's avatar
Srikanth Raju committed
15
16
17



Srikanth Raju's avatar
Srikanth Raju committed
18
19
20
21
22
23
24
25
    def do_GET(self):
        parsed_path = urlparse.urlparse(self.path)

        only_path = parsed_path.path
        folders=[]
        while 1:
            only_path,folder=os.path.split(only_path)

Srikanth Raju's avatar
Srikanth Raju committed
26
27
28
29
            if folder != "":
                folders.append(folder)
            if only_path=="/":
                break;
Srikanth Raju's avatar
Srikanth Raju committed
30

Srikanth Raju's avatar
Srikanth Raju committed
31
32
        if len(folders) > 0:
            folders.pop() #Ignore version
Srikanth Raju's avatar
Srikanth Raju committed
33
34
        folders.reverse()

Srikanth Raju's avatar
Srikanth Raju committed
35
36
37
38
39
        try:
            message = self.handle_req(folders, self.metas)
            message = message + "\n"
        except Exception as e:
            print traceback.format_exc()
Srikanth Raju's avatar
Srikanth Raju committed
40
41
42
43
44
45
46
47
48
            self.send_response(404)
            self.end_headers()
            return

        self.send_response(200)
        self.end_headers()
        self.wfile.write(message)
        return

Srikanth Raju's avatar
Srikanth Raju committed
49
50
51
52
    def listmetas(self, metas):
        message = "\n".join(map(lambda x: x + "/" if (x == "public-keys" or not(callable(metas[x])))  else x,
                        metas.keys()));
        return message
Srikanth Raju's avatar
Srikanth Raju committed
53

Srikanth Raju's avatar
Srikanth Raju committed
54
55
56
57
58
59
60
    def handle_req(self, arg, metas):
        if callable(metas):
            return metas(self, arg)
        elif len(arg) == 0:
            return self.listmetas(metas);
        else:
            return self.handle_req(arg[1:], metas[arg[0]])
Srikanth Raju's avatar
Srikanth Raju committed
61

Srikanth Raju's avatar
Srikanth Raju committed
62
63
64
    def do_userdata(self):
        #TODO
        return "\n";
Srikanth Raju's avatar
Srikanth Raju committed
65

Srikanth Raju's avatar
Srikanth Raju committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    def doamiid(self, arg):
        cursor = self.cnx.cursor()
        ip = self.client_address[0]
        cursor.execute("select osname from os_info "
            "join nodes on os_info.osid = nodes.osid "
            "join interfaces on nodes.node_id=interfaces.node_id "
            "where interfaces.ip=%s", (ip,));
        if cursor.with_rows:
            ami_id = cursor.fetchone()
            ami_id = ami_id[0]
        else:
            ami_id = ""
        cursor.close()
        return ami_id;
Srikanth Raju's avatar
Srikanth Raju committed
80
81
82
83
84
85
86
87
88

    def dolocal_hostname(self, args):
        cursor = self.cnx.cursor()
        ip = self.client_address[0]
        cursor.execute("select vname,eid,pid from reserved join interfaces on interfaces.node_id=reserved.node_id"
            " where interfaces.ip=%s",(ip,))
        if cursor.with_rows:
            node_id = cursor.fetchone()
        else:
Srikanth Raju's avatar
Srikanth Raju committed
89
            cursor.close()
Srikanth Raju's avatar
Srikanth Raju committed
90
91
92
93
94
            return ""

        cursor.close()
        return node_id[0] + "." + node_id[1] + "." + node_id[2] + "." + "emulab.net"

Srikanth Raju's avatar
Srikanth Raju committed
95
96
97
98
99
100
101
102
103
    def doavail(self, args):
        #TODO
        return "emulab"


    def domacs(self, args):
        #TODO
        return "324AF"

Srikanth Raju's avatar
Srikanth Raju committed
104
105
106
107
108
109
110
111
    def domac(self, args):
        cursor = self.cnx.cursor()
        ip = self.client_address[0]
        cursor.execute("select mac from interfaces"
            " where interfaces.ip=%s",(ip,))
        if cursor.with_rows:
            mac = cursor.fetchone()
        else:
Srikanth Raju's avatar
Srikanth Raju committed
112
            cursor.close()
Srikanth Raju's avatar
Srikanth Raju committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
            return ""

        cursor.close()
        split = [mac[0][i:i+2] for i in range(0, len(mac[0]),2)]
        return ":".join(split)

    def doinstance_id(self, args):
        cursor = self.cnx.cursor()
        ip = self.client_address[0]
        cursor.execute("select uuid from interfaces"
            " where interfaces.ip=%s",(ip,))
        if cursor.with_rows:
            uuid = cursor.fetchone()
        else:
Srikanth Raju's avatar
Srikanth Raju committed
127
            cursor.close()
Srikanth Raju's avatar
Srikanth Raju committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
            return ""

        cursor.close()
        return uuid[0]


    def dopublic_keys(self, args):
        if len(args) == 0:
            #Throw out all the users. Hope the stuff don't change between queries
            cursor = self.cnx.cursor()
            ip = self.client_address[0]
            cursor.execute("select user_pubkeys.uid,user_pubkeys.idx from user_pubkeys "
                "join group_membership on group_membership.uid = user_pubkeys.uid "
                "join experiments on experiments.pid=group_membership.pid AND experiments.gid=group_membership.gid "
                "join reserved on reserved.exptidx=experiments.idx "
                "join interfaces on reserved.node_id=interfaces.node_id "
                "where interfaces.ip=%s;", (ip,));

            list = ""
            ctr = 0
            if cursor.with_rows:
                for (user,uid) in cursor:
                    list = list + str(ctr) + "=" + str(user) + str(uid) + "\n"
                    ctr = ctr+1
            else:
Srikanth Raju's avatar
Srikanth Raju committed
153
                cursor.close()
Srikanth Raju's avatar
Srikanth Raju committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
                return ""

            cursor.close()
            return list
        elif len(args) == 1:
            #TODO: Verify ig idx is within limits
            return "openssh-key"
        elif len(args) == 2:
            val = int(args[0])
            cursor = self.cnx.cursor()
            ip = self.client_address[0]
            cursor.execute("select user_pubkeys.pubkey from user_pubkeys "
                "join group_membership on group_membership.uid = user_pubkeys.uid "
                "join experiments on experiments.pid=group_membership.pid AND experiments.gid=group_membership.gid "
                "join reserved on reserved.exptidx=experiments.idx "
                "join interfaces on reserved.node_id=interfaces.node_id "
                "where interfaces.ip=%s limit " + str(val) +", 1;", (ip,));

            if cursor.with_rows:
                key = cursor.fetchone()
            else:
Srikanth Raju's avatar
Srikanth Raju committed
175
                cursor.close()
Srikanth Raju's avatar
Srikanth Raju committed
176
177
178
179
180
                return ""

            cursor.close()
            return key[0]

Srikanth Raju's avatar
Srikanth Raju committed
181
    metas = {
Srikanth Raju's avatar
Srikanth Raju committed
182
183
184
185
186
187
188
189
190
191
        "meta-data" : {
            "placement" : {"availability-zone" : doavail},
            "ami-id": doamiid,
            "local-hostname" : dolocal_hostname,
            "public-hostname":dolocal_hostname,
            "network": {"interfaces": {"macs" : domacs}},
            "mac":domac,
            "instance-id":doinstance_id,
            "public-keys": dopublic_keys },
        "user-data" : do_userdata
Srikanth Raju's avatar
Srikanth Raju committed
192
    }
Srikanth Raju's avatar
Srikanth Raju committed
193
194
195
196


if __name__ == '__main__':
    from BaseHTTPServer import HTTPServer
Srikanth Raju's avatar
Srikanth Raju committed
197
198
    import socket
    server = HTTPServer((socket.gethostbyname(socket.gethostname()), 8787), Ec2MetaHandler)
Srikanth Raju's avatar
Srikanth Raju committed
199
200
    print 'Starting server, use <Ctrl-C> to stop'
    server.serve_forever()
Srikanth Raju's avatar
Srikanth Raju committed
201

Srikanth Raju's avatar
Srikanth Raju committed
202