2019-12-14 15:55:30 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# MetadataAPIInstance.py - Class implementing an EC2-compatible cloud-init Metadata server
|
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2023-12-29 11:16:59 -05:00
|
|
|
# Copyright (C) 2018-2024 Joshua M. Boniface <joshua@boniface.me>
|
2019-12-14 15:55:30 -05:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2021-03-25 16:57:17 -04:00
|
|
|
# the Free Software Foundation, version 3.
|
2019-12-14 15:55:30 -05:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
import gevent.pywsgi
|
|
|
|
import flask
|
|
|
|
import sys
|
|
|
|
import psycopg2
|
2020-08-11 11:46:41 -04:00
|
|
|
|
|
|
|
from threading import Thread
|
2019-12-14 15:55:30 -05:00
|
|
|
from psycopg2.extras import RealDictCursor
|
|
|
|
|
2020-02-08 18:48:59 -05:00
|
|
|
import daemon_lib.vm as pvc_vm
|
|
|
|
import daemon_lib.network as pvc_network
|
2019-12-14 15:55:30 -05:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2019-12-14 15:55:30 -05:00
|
|
|
class MetadataAPIInstance(object):
|
|
|
|
mdapi = flask.Flask(__name__)
|
|
|
|
|
|
|
|
# Initialization function
|
2021-05-31 19:55:09 -04:00
|
|
|
def __init__(self, zkhandler, config, logger):
|
|
|
|
self.zkhandler = zkhandler
|
2019-12-14 15:55:30 -05:00
|
|
|
self.config = config
|
|
|
|
self.logger = logger
|
|
|
|
self.thread = None
|
|
|
|
self.md_http_server = None
|
2019-12-15 00:08:18 -05:00
|
|
|
self.add_routes()
|
2019-12-14 15:55:30 -05:00
|
|
|
|
|
|
|
# Add flask routes inside our instance
|
|
|
|
def add_routes(self):
|
2021-11-06 03:02:43 -04:00
|
|
|
@self.mdapi.route("/", methods=["GET"])
|
2019-12-14 15:55:30 -05:00
|
|
|
def api_root():
|
2021-11-06 03:02:43 -04:00
|
|
|
return (
|
|
|
|
flask.jsonify({"message": "PVC Provisioner Metadata API version 1"}),
|
|
|
|
209,
|
|
|
|
)
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
@self.mdapi.route("/<version>/meta-data/", methods=["GET"])
|
2019-12-14 15:55:30 -05:00
|
|
|
def api_metadata_root(version):
|
|
|
|
metadata = """instance-id\nname\nprofile"""
|
|
|
|
return metadata, 200
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
@self.mdapi.route("/<version>/meta-data/instance-id", methods=["GET"])
|
2019-12-14 15:55:30 -05:00
|
|
|
def api_metadata_instanceid(version):
|
2021-11-06 03:02:43 -04:00
|
|
|
source_address = flask.request.__dict__["environ"]["REMOTE_ADDR"]
|
2019-12-14 15:55:30 -05:00
|
|
|
vm_details = self.get_vm_details(source_address)
|
2021-11-06 03:02:43 -04:00
|
|
|
instance_id = vm_details.get("uuid", None)
|
2019-12-14 15:55:30 -05:00
|
|
|
return instance_id, 200
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
@self.mdapi.route("/<version>/meta-data/name", methods=["GET"])
|
2019-12-14 15:55:30 -05:00
|
|
|
def api_metadata_hostname(version):
|
2021-11-06 03:02:43 -04:00
|
|
|
source_address = flask.request.__dict__["environ"]["REMOTE_ADDR"]
|
2019-12-14 15:55:30 -05:00
|
|
|
vm_details = self.get_vm_details(source_address)
|
2021-11-06 03:02:43 -04:00
|
|
|
vm_name = vm_details.get("name", None)
|
2019-12-14 15:55:30 -05:00
|
|
|
return vm_name, 200
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
@self.mdapi.route("/<version>/meta-data/profile", methods=["GET"])
|
2019-12-14 15:55:30 -05:00
|
|
|
def api_metadata_profile(version):
|
2021-11-06 03:02:43 -04:00
|
|
|
source_address = flask.request.__dict__["environ"]["REMOTE_ADDR"]
|
2019-12-14 15:55:30 -05:00
|
|
|
vm_details = self.get_vm_details(source_address)
|
2021-11-06 03:02:43 -04:00
|
|
|
vm_profile = vm_details.get("profile", None)
|
2019-12-14 15:55:30 -05:00
|
|
|
return vm_profile, 200
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
@self.mdapi.route("/<version>/user-data", methods=["GET"])
|
2019-12-14 15:55:30 -05:00
|
|
|
def api_userdata(version):
|
2021-11-06 03:02:43 -04:00
|
|
|
source_address = flask.request.__dict__["environ"]["REMOTE_ADDR"]
|
2019-12-14 15:55:30 -05:00
|
|
|
vm_details = self.get_vm_details(source_address)
|
2021-11-06 03:02:43 -04:00
|
|
|
vm_profile = vm_details.get("profile", None)
|
2019-12-14 15:55:30 -05:00
|
|
|
# Get the userdata
|
2020-01-13 09:21:23 -05:00
|
|
|
if vm_profile:
|
|
|
|
userdata = self.get_profile_userdata(vm_profile)
|
2021-11-06 03:02:43 -04:00
|
|
|
self.logger.out(
|
|
|
|
"Returning userdata for profile {}".format(vm_profile),
|
|
|
|
state="i",
|
|
|
|
prefix="Metadata API",
|
|
|
|
)
|
2020-01-13 09:21:23 -05:00
|
|
|
else:
|
|
|
|
userdata = None
|
2019-12-14 15:55:30 -05:00
|
|
|
return flask.Response(userdata)
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2019-12-14 15:55:30 -05:00
|
|
|
def launch_wsgi(self):
|
|
|
|
try:
|
|
|
|
self.md_http_server = gevent.pywsgi.WSGIServer(
|
2021-11-06 03:02:43 -04:00
|
|
|
("169.254.169.254", 80),
|
2019-12-14 15:55:30 -05:00
|
|
|
self.mdapi,
|
|
|
|
log=sys.stdout,
|
2021-11-06 03:02:43 -04:00
|
|
|
error_log=sys.stdout,
|
2019-12-14 15:55:30 -05:00
|
|
|
)
|
|
|
|
self.md_http_server.serve_forever()
|
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
self.logger.out("Error starting Metadata API: {}".format(e), state="e")
|
2019-12-14 15:55:30 -05:00
|
|
|
|
|
|
|
# WSGI start/stop
|
|
|
|
def start(self):
|
|
|
|
# Launch Metadata API
|
2021-11-06 03:02:43 -04:00
|
|
|
self.logger.out("Starting Metadata API at 169.254.169.254:80", state="i")
|
2020-08-11 11:46:41 -04:00
|
|
|
self.thread = Thread(target=self.launch_wsgi)
|
2019-12-14 15:55:30 -05:00
|
|
|
self.thread.start()
|
2021-11-06 03:02:43 -04:00
|
|
|
self.logger.out("Successfully started Metadata API thread", state="o")
|
2019-12-14 15:55:30 -05:00
|
|
|
|
|
|
|
def stop(self):
|
2019-12-15 00:08:18 -05:00
|
|
|
if not self.md_http_server:
|
|
|
|
return
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
self.logger.out("Stopping Metadata API at 169.254.169.254:80", state="i")
|
2019-12-15 00:08:18 -05:00
|
|
|
try:
|
|
|
|
self.md_http_server.stop()
|
|
|
|
self.md_http_server.close()
|
|
|
|
self.md_http_server = None
|
2021-11-06 03:02:43 -04:00
|
|
|
self.logger.out("Successfully stopped Metadata API", state="o")
|
2019-12-15 00:08:18 -05:00
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
self.logger.out("Error stopping Metadata API: {}".format(e), state="e")
|
2019-12-14 15:55:30 -05:00
|
|
|
|
|
|
|
# Helper functions
|
|
|
|
def open_database(self):
|
|
|
|
conn = psycopg2.connect(
|
2023-11-29 23:35:12 -05:00
|
|
|
host=self.config["api_postgresql_host"],
|
|
|
|
port=self.config["api_postgresql_port"],
|
|
|
|
dbname=self.config["api_postgresql_dbname"],
|
|
|
|
user=self.config["api_postgresql_user"],
|
|
|
|
password=self.config["api_postgresql_password"],
|
2019-12-14 15:55:30 -05:00
|
|
|
)
|
|
|
|
cur = conn.cursor(cursor_factory=RealDictCursor)
|
|
|
|
return conn, cur
|
|
|
|
|
|
|
|
def close_database(self, conn, cur):
|
|
|
|
cur.close()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
# Obtain a list of templates
|
|
|
|
def get_profile_userdata(self, vm_profile):
|
2020-11-06 19:44:14 -05:00
|
|
|
query = """SELECT userdata.userdata FROM profile
|
2020-01-04 15:10:37 -05:00
|
|
|
JOIN userdata ON profile.userdata = userdata.id
|
2019-12-14 15:55:30 -05:00
|
|
|
WHERE profile.name = %s;
|
|
|
|
"""
|
|
|
|
args = (vm_profile,)
|
|
|
|
|
|
|
|
conn, cur = self.open_database()
|
|
|
|
cur.execute(query, args)
|
|
|
|
data_raw = cur.fetchone()
|
|
|
|
self.close_database(conn, cur)
|
2021-06-13 16:43:25 -04:00
|
|
|
if data_raw is not None:
|
2021-11-06 03:02:43 -04:00
|
|
|
data = data_raw.get("userdata", None)
|
2021-06-13 16:43:25 -04:00
|
|
|
return data
|
|
|
|
else:
|
|
|
|
return None
|
2019-12-14 15:55:30 -05:00
|
|
|
|
|
|
|
# VM details function
|
|
|
|
def get_vm_details(self, source_address):
|
|
|
|
# Start connection to Zookeeper
|
2021-05-31 19:55:09 -04:00
|
|
|
_discard, networks = pvc_network.get_list(self.zkhandler, None)
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2019-12-14 15:55:30 -05:00
|
|
|
# Figure out which server this is via the DHCP address
|
|
|
|
host_information = dict()
|
2021-11-06 03:02:43 -04:00
|
|
|
networks_managed = (x for x in networks if x.get("type") == "managed")
|
2019-12-14 15:55:30 -05:00
|
|
|
for network in networks_managed:
|
2021-11-06 03:02:43 -04:00
|
|
|
network_leases = pvc_network.getNetworkDHCPLeases(
|
|
|
|
self.zkhandler, network.get("vni")
|
|
|
|
)
|
2019-12-14 15:55:30 -05:00
|
|
|
for network_lease in network_leases:
|
2021-11-06 03:02:43 -04:00
|
|
|
information = pvc_network.getDHCPLeaseInformation(
|
|
|
|
self.zkhandler, network.get("vni"), network_lease
|
|
|
|
)
|
2019-12-14 15:55:30 -05:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
if information.get("ip4_address", None) == source_address:
|
2019-12-14 15:55:30 -05:00
|
|
|
host_information = information
|
2020-11-06 18:55:10 -05:00
|
|
|
except Exception:
|
2019-12-14 15:55:30 -05:00
|
|
|
pass
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2019-12-14 15:55:30 -05:00
|
|
|
# Get our real information on the host; now we can start querying about it
|
2021-11-06 03:02:43 -04:00
|
|
|
client_macaddr = host_information.get("mac_address", None)
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2019-12-14 15:55:30 -05:00
|
|
|
# Find the VM with that MAC address - we can't assume that the hostname is actually right
|
2021-07-14 00:51:48 -04:00
|
|
|
_discard, vm_list = pvc_vm.get_list(self.zkhandler, None, None, None, None)
|
2019-12-14 15:55:30 -05:00
|
|
|
vm_details = dict()
|
|
|
|
for vm in vm_list:
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
for network in vm.get("networks"):
|
|
|
|
if network.get("mac", None) == client_macaddr:
|
2019-12-14 15:55:30 -05:00
|
|
|
vm_details = vm
|
2020-11-06 18:55:10 -05:00
|
|
|
except Exception:
|
2019-12-14 15:55:30 -05:00
|
|
|
pass
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2019-12-14 15:55:30 -05:00
|
|
|
return vm_details
|