2019-11-02 15:42:24 +00:00
|
|
|
import os
|
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
from flask import Flask, request
|
|
|
|
from flask_restful import Resource, Api
|
2019-12-23 07:58:04 +00:00
|
|
|
from werkzeug.exceptions import HTTPException
|
|
|
|
|
2019-12-31 10:30:02 +00:00
|
|
|
from uncloud.settings import settings
|
|
|
|
from uncloud.shared import shared
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
api = Api(app)
|
|
|
|
|
2019-12-23 07:58:04 +00:00
|
|
|
app.logger.handlers.clear()
|
|
|
|
|
|
|
|
|
|
|
|
@app.errorhandler(Exception)
|
|
|
|
def handle_exception(e):
|
|
|
|
app.logger.error(e)
|
|
|
|
# pass through HTTP errors
|
|
|
|
if isinstance(e, HTTPException):
|
|
|
|
return e
|
|
|
|
|
|
|
|
# now you're handling non-HTTP exceptions only
|
2019-12-30 09:35:07 +00:00
|
|
|
return {"message": "Server Error"}, 500
|
2019-12-23 07:58:04 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
def get_vm_entry(mac_addr):
|
2019-12-30 09:35:07 +00:00
|
|
|
return next(
|
|
|
|
filter(
|
|
|
|
lambda vm: mac_addr in list(zip(*vm.network))[1],
|
|
|
|
shared.vm_pool.vms,
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
# https://stackoverflow.com/questions/37140846/how-to-convert-ipv6-link-local-address-to-mac-address-in-python
|
|
|
|
def ipv62mac(ipv6):
|
|
|
|
# remove subnet info if given
|
2019-12-30 09:35:07 +00:00
|
|
|
subnet_index = ipv6.find("/")
|
2019-10-25 06:42:40 +00:00
|
|
|
if subnet_index != -1:
|
|
|
|
ipv6 = ipv6[:subnet_index]
|
|
|
|
|
2019-12-30 09:35:07 +00:00
|
|
|
ipv6_parts = ipv6.split(":")
|
2019-10-25 06:42:40 +00:00
|
|
|
mac_parts = list()
|
|
|
|
for ipv6_part in ipv6_parts[-4:]:
|
|
|
|
while len(ipv6_part) < 4:
|
2019-12-30 09:35:07 +00:00
|
|
|
ipv6_part = "0" + ipv6_part
|
2019-10-25 06:42:40 +00:00
|
|
|
mac_parts.append(ipv6_part[:2])
|
|
|
|
mac_parts.append(ipv6_part[-2:])
|
|
|
|
|
|
|
|
# modify parts to match MAC value
|
2019-12-30 09:35:07 +00:00
|
|
|
mac_parts[0] = "%02x" % (int(mac_parts[0], 16) ^ 2)
|
2019-10-25 06:42:40 +00:00
|
|
|
del mac_parts[4]
|
|
|
|
del mac_parts[3]
|
2019-12-30 09:35:07 +00:00
|
|
|
return ":".join(mac_parts)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Root(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
data = get_vm_entry(ipv62mac(request.remote_addr))
|
|
|
|
|
|
|
|
if not data:
|
2019-12-30 09:35:07 +00:00
|
|
|
return (
|
|
|
|
{"message": "Metadata for such VM does not exists."},
|
|
|
|
404,
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
2019-12-30 09:35:07 +00:00
|
|
|
etcd_key = os.path.join(
|
|
|
|
settings["etcd"]["user_prefix"],
|
|
|
|
data.value["owner_realm"],
|
|
|
|
data.value["owner"],
|
|
|
|
"key",
|
|
|
|
)
|
|
|
|
etcd_entry = shared.etcd_client.get_prefix(
|
|
|
|
etcd_key, value_in_json=True
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
user_personal_ssh_keys = [key.value for key in etcd_entry]
|
2019-12-30 09:35:07 +00:00
|
|
|
data.value["metadata"]["ssh-keys"] += user_personal_ssh_keys
|
|
|
|
return data.value["metadata"], 200
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def post():
|
2019-12-30 09:35:07 +00:00
|
|
|
return {"message": "Previous Implementation is deprecated."}
|
2019-10-25 06:42:40 +00:00
|
|
|
# data = etcd_client.get("/v1/metadata/{}".format(request.remote_addr), value_in_json=True)
|
|
|
|
# print(data)
|
|
|
|
# if data:
|
|
|
|
# for k in request.json:
|
|
|
|
# if k not in data.value:
|
|
|
|
# data.value[k] = request.json[k]
|
|
|
|
# if k.endswith("-list"):
|
|
|
|
# data.value[k] = [request.json[k]]
|
|
|
|
# else:
|
|
|
|
# if k.endswith("-list"):
|
|
|
|
# data.value[k].append(request.json[k])
|
|
|
|
# else:
|
|
|
|
# data.value[k] = request.json[k]
|
|
|
|
# etcd_client.put("/v1/metadata/{}".format(request.remote_addr),
|
|
|
|
# data.value, value_in_json=True)
|
|
|
|
# else:
|
|
|
|
# data = {}
|
|
|
|
# for k in request.json:
|
|
|
|
# data[k] = request.json[k]
|
|
|
|
# if k.endswith("-list"):
|
|
|
|
# data[k] = [request.json[k]]
|
|
|
|
# etcd_client.put("/v1/metadata/{}".format(request.remote_addr),
|
|
|
|
# data, value_in_json=True)
|
|
|
|
|
|
|
|
|
2019-12-30 09:35:07 +00:00
|
|
|
api.add_resource(Root, "/")
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2020-01-01 09:59:47 +00:00
|
|
|
def main(port=None, debug=False):
|
|
|
|
app.run(debug=debug, host="::", port=port)
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
|
2019-12-30 09:35:07 +00:00
|
|
|
if __name__ == "__main__":
|
2019-11-18 17:39:57 +00:00
|
|
|
main()
|