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
|
|
|
|
#
|
2020-01-08 19:38:02 -05:00
|
|
|
# Copyright (C) 2018-2020 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
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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
|
2019-12-18 11:56:04 -05:00
|
|
|
import time
|
2019-12-14 15:55:30 -05:00
|
|
|
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
|
|
|
|
|
|
|
class MetadataAPIInstance(object):
|
|
|
|
mdapi = flask.Flask(__name__)
|
|
|
|
|
|
|
|
# Initialization function
|
|
|
|
def __init__(self, zk_conn, config, logger):
|
|
|
|
self.zk_conn = zk_conn
|
|
|
|
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):
|
|
|
|
@self.mdapi.route('/', methods=['GET'])
|
|
|
|
def api_root():
|
|
|
|
return flask.jsonify({"message": "PVC Provisioner Metadata API version 1"}), 209
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2019-12-14 15:55:30 -05:00
|
|
|
@self.mdapi.route('/<version>/meta-data/', methods=['GET'])
|
|
|
|
def api_metadata_root(version):
|
|
|
|
metadata = """instance-id\nname\nprofile"""
|
|
|
|
return metadata, 200
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2019-12-14 15:55:30 -05:00
|
|
|
@self.mdapi.route('/<version>/meta-data/instance-id', methods=['GET'])
|
|
|
|
def api_metadata_instanceid(version):
|
|
|
|
source_address = flask.request.__dict__['environ']['REMOTE_ADDR']
|
|
|
|
vm_details = self.get_vm_details(source_address)
|
2020-01-13 09:21:23 -05: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
|
|
|
|
2019-12-14 15:55:30 -05:00
|
|
|
@self.mdapi.route('/<version>/meta-data/name', methods=['GET'])
|
|
|
|
def api_metadata_hostname(version):
|
|
|
|
source_address = flask.request.__dict__['environ']['REMOTE_ADDR']
|
|
|
|
vm_details = self.get_vm_details(source_address)
|
2020-01-13 09:21:23 -05: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
|
|
|
|
2019-12-14 15:55:30 -05:00
|
|
|
@self.mdapi.route('/<version>/meta-data/profile', methods=['GET'])
|
|
|
|
def api_metadata_profile(version):
|
|
|
|
source_address = flask.request.__dict__['environ']['REMOTE_ADDR']
|
|
|
|
vm_details = self.get_vm_details(source_address)
|
2020-01-13 09:21:23 -05: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
|
|
|
|
2019-12-14 15:55:30 -05:00
|
|
|
@self.mdapi.route('/<version>/user-data', methods=['GET'])
|
|
|
|
def api_userdata(version):
|
|
|
|
source_address = flask.request.__dict__['environ']['REMOTE_ADDR']
|
|
|
|
vm_details = self.get_vm_details(source_address)
|
2020-01-13 09:21:23 -05: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)
|
|
|
|
self.logger.out("Returning userdata for profile {}".format(vm_profile), state='i', prefix='Metadata API')
|
|
|
|
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(
|
|
|
|
('169.254.169.254', 80),
|
|
|
|
self.mdapi,
|
|
|
|
log=sys.stdout,
|
|
|
|
error_log=sys.stdout
|
|
|
|
)
|
|
|
|
self.md_http_server.serve_forever()
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.out('Error starting Metadata API: {}'.format(e), state='e')
|
|
|
|
|
|
|
|
# WSGI start/stop
|
|
|
|
def start(self):
|
|
|
|
# Launch Metadata API
|
|
|
|
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()
|
|
|
|
self.logger.out('Successfully started Metadata API thread', state='o')
|
|
|
|
|
|
|
|
def stop(self):
|
2019-12-15 00:08:18 -05:00
|
|
|
if not self.md_http_server:
|
|
|
|
return
|
|
|
|
|
2019-12-14 15:55:30 -05: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()
|
2019-12-18 11:56:04 -05:00
|
|
|
time.sleep(0.1)
|
2019-12-15 00:08:18 -05:00
|
|
|
self.md_http_server.close()
|
2019-12-18 11:56:04 -05:00
|
|
|
time.sleep(0.1)
|
2019-12-15 00:08:18 -05:00
|
|
|
self.md_http_server = None
|
|
|
|
self.logger.out('Successfully stopped Metadata API', state='o')
|
|
|
|
except Exception as e:
|
|
|
|
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(
|
|
|
|
host=self.config['metadata_postgresql_host'],
|
|
|
|
port=self.config['metadata_postgresql_port'],
|
|
|
|
dbname=self.config['metadata_postgresql_dbname'],
|
|
|
|
user=self.config['metadata_postgresql_user'],
|
|
|
|
password=self.config['metadata_postgresql_password']
|
|
|
|
)
|
|
|
|
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-01-04 15:17:27 -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)
|
2020-01-13 09:21:23 -05:00
|
|
|
data = data_raw.get('userdata', None)
|
2019-12-14 15:55:30 -05:00
|
|
|
return data
|
|
|
|
|
|
|
|
# VM details function
|
|
|
|
def get_vm_details(self, source_address):
|
|
|
|
# Start connection to Zookeeper
|
|
|
|
_discard, networks = pvc_network.get_list(self.zk_conn, 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()
|
2020-01-13 09:21:23 -05: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:
|
2020-01-13 09:21:23 -05:00
|
|
|
network_leases = pvc_network.getNetworkDHCPLeases(self.zk_conn, network.get('vni'))
|
2019-12-14 15:55:30 -05:00
|
|
|
for network_lease in network_leases:
|
2020-01-13 09:21:23 -05:00
|
|
|
information = pvc_network.getDHCPLeaseInformation(self.zk_conn, network.get('vni'), network_lease)
|
2019-12-14 15:55:30 -05:00
|
|
|
try:
|
2020-01-13 09:21:23 -05: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
|
2020-01-06 16:34:39 -05:00
|
|
|
client_hostname = host_information.get('hostname', None)
|
|
|
|
client_macaddr = host_information.get('mac_address', None)
|
|
|
|
client_ipaddr = host_information.get('ip4_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
|
|
|
|
_discard, vm_list = pvc_vm.get_list(self.zk_conn, None, None, None)
|
|
|
|
vm_name = None
|
|
|
|
vm_details = dict()
|
|
|
|
for vm in vm_list:
|
|
|
|
try:
|
2020-01-13 09:21:23 -05:00
|
|
|
for network in vm.get('networks'):
|
|
|
|
if network.get('mac', None) == client_macaddr:
|
|
|
|
vm_name = vm.get('name')
|
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
|
2020-11-06 19:05:48 -05:00
|
|
|
|