2019-11-02 15:42:24 +00:00
|
|
|
import os
|
2020-01-03 13:38:59 +00:00
|
|
|
import argparse
|
2019-11-02 15:42:24 +00:00
|
|
|
|
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
|
|
|
|
|
2020-01-06 07:25:59 +00:00
|
|
|
from uncloud.common.settings import settings
|
2020-01-08 19:40:05 +00:00
|
|
|
from uncloud.common.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()
|
|
|
|
|
2020-01-11 23:32:17 +00:00
|
|
|
DEFAULT_PORT=1234
|
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
arg_parser = argparse.ArgumentParser('metadata', add_help=False)
|
2020-01-11 23:32:17 +00:00
|
|
|
arg_parser.add_argument('--port', '-p', default=DEFAULT_PORT, help='By default bind to port {}'.format(DEFAULT_PORT))
|
2020-01-03 13:38:59 +00:00
|
|
|
|
2019-12-23 07:58:04 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
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-10 10:45:48 +00:00
|
|
|
def main(arguments):
|
|
|
|
port = arguments['port']
|
|
|
|
debug = arguments['debug']
|
2020-01-01 09:59:47 +00:00
|
|
|
app.run(debug=debug, host="::", port=port)
|