2020-02-08 19:52:15 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-02-08 20:27:45 -05:00
|
|
|
# Daemon.py - PVC HTTP API daemon
|
2020-02-08 19:52:15 -05:00
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2022-10-06 11:55:27 -04:00
|
|
|
# Copyright (C) 2018-2022 Joshua M. Boniface <joshua@boniface.me>
|
2020-02-08 19:52:15 -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.
|
2020-02-08 19:52:15 -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/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2021-05-28 23:33:36 -04:00
|
|
|
import os
|
|
|
|
import yaml
|
|
|
|
|
2022-08-23 10:58:47 -04:00
|
|
|
from ssl import SSLContext, TLSVersion
|
|
|
|
|
2021-05-28 23:33:36 -04:00
|
|
|
from distutils.util import strtobool as dustrtobool
|
|
|
|
|
2021-05-30 00:09:39 -04:00
|
|
|
# Daemon version
|
2023-08-23 01:56:57 -04:00
|
|
|
version = "0.9.65"
|
2021-05-28 23:33:36 -04:00
|
|
|
|
2021-05-30 00:09:39 -04:00
|
|
|
# API version
|
|
|
|
API_VERSION = 1.0
|
|
|
|
|
2021-05-28 23:33:36 -04:00
|
|
|
|
|
|
|
##########################################################
|
|
|
|
# Helper Functions
|
|
|
|
##########################################################
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
|
2021-05-28 23:33:36 -04:00
|
|
|
def strtobool(stringv):
|
|
|
|
if stringv is None:
|
|
|
|
return False
|
|
|
|
if isinstance(stringv, bool):
|
|
|
|
return bool(stringv)
|
|
|
|
try:
|
|
|
|
return bool(dustrtobool(stringv))
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################
|
|
|
|
# Configuration Parsing
|
|
|
|
##########################################################
|
|
|
|
|
|
|
|
# Parse the configuration file
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
pvcapid_config_file = os.environ["PVC_CONFIG_FILE"]
|
2021-05-28 23:33:36 -04:00
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
print(
|
|
|
|
'Error: The "PVC_CONFIG_FILE" environment variable must be set before starting pvcapid.'
|
|
|
|
)
|
2021-05-28 23:33:36 -04:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
print('Loading configuration from file "{}"'.format(pvcapid_config_file))
|
|
|
|
|
|
|
|
# Read in the config
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
with open(pvcapid_config_file, "r") as cfgfile:
|
2021-05-28 23:33:36 -04:00
|
|
|
o_config = yaml.load(cfgfile, Loader=yaml.BaseLoader)
|
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
print("ERROR: Failed to parse configuration file: {}".format(e))
|
2021-05-28 23:33:36 -04:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Create the config object
|
|
|
|
config = {
|
2021-11-06 03:02:43 -04:00
|
|
|
"debug": strtobool(o_config["pvc"]["debug"]),
|
|
|
|
"coordinators": o_config["pvc"]["coordinators"],
|
|
|
|
"listen_address": o_config["pvc"]["api"]["listen_address"],
|
|
|
|
"listen_port": int(o_config["pvc"]["api"]["listen_port"]),
|
|
|
|
"auth_enabled": strtobool(o_config["pvc"]["api"]["authentication"]["enabled"]),
|
|
|
|
"auth_secret_key": o_config["pvc"]["api"]["authentication"]["secret_key"],
|
|
|
|
"auth_tokens": o_config["pvc"]["api"]["authentication"]["tokens"],
|
|
|
|
"ssl_enabled": strtobool(o_config["pvc"]["api"]["ssl"]["enabled"]),
|
|
|
|
"ssl_key_file": o_config["pvc"]["api"]["ssl"]["key_file"],
|
|
|
|
"ssl_cert_file": o_config["pvc"]["api"]["ssl"]["cert_file"],
|
|
|
|
"database_host": o_config["pvc"]["provisioner"]["database"]["host"],
|
|
|
|
"database_port": int(o_config["pvc"]["provisioner"]["database"]["port"]),
|
|
|
|
"database_name": o_config["pvc"]["provisioner"]["database"]["name"],
|
|
|
|
"database_user": o_config["pvc"]["provisioner"]["database"]["user"],
|
|
|
|
"database_password": o_config["pvc"]["provisioner"]["database"]["pass"],
|
|
|
|
"queue_host": o_config["pvc"]["provisioner"]["queue"]["host"],
|
|
|
|
"queue_port": o_config["pvc"]["provisioner"]["queue"]["port"],
|
|
|
|
"queue_path": o_config["pvc"]["provisioner"]["queue"]["path"],
|
|
|
|
"storage_hosts": o_config["pvc"]["provisioner"]["ceph_cluster"][
|
|
|
|
"storage_hosts"
|
|
|
|
],
|
|
|
|
"storage_domain": o_config["pvc"]["provisioner"]["ceph_cluster"][
|
|
|
|
"storage_domain"
|
|
|
|
],
|
|
|
|
"ceph_monitor_port": o_config["pvc"]["provisioner"]["ceph_cluster"][
|
|
|
|
"ceph_monitor_port"
|
|
|
|
],
|
|
|
|
"ceph_storage_secret_uuid": o_config["pvc"]["provisioner"]["ceph_cluster"][
|
|
|
|
"ceph_storage_secret_uuid"
|
|
|
|
],
|
2021-05-28 23:33:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Use coordinators as storage hosts if not explicitly specified
|
2021-11-06 03:02:43 -04:00
|
|
|
if not config["storage_hosts"]:
|
|
|
|
config["storage_hosts"] = config["coordinators"]
|
2021-05-28 23:33:36 -04:00
|
|
|
|
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
print("ERROR: Failed to load configuration: {}".format(e))
|
2021-05-28 23:33:36 -04:00
|
|
|
exit(1)
|
|
|
|
|
2020-02-08 19:52:15 -05:00
|
|
|
|
|
|
|
##########################################################
|
|
|
|
# Entrypoint
|
|
|
|
##########################################################
|
2020-10-26 01:39:55 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
|
2021-05-30 00:18:04 -04:00
|
|
|
def entrypoint():
|
|
|
|
import pvcapid.flaskapi as pvc_api # noqa: E402
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if config["ssl_enabled"]:
|
2022-08-23 10:58:47 -04:00
|
|
|
context = SSLContext()
|
|
|
|
context.minimum_version = TLSVersion.TLSv1
|
|
|
|
context.get_ca_certs()
|
|
|
|
context.load_cert_chain(config["ssl_cert_file"], keyfile=config["ssl_key_file"])
|
2021-05-30 00:18:04 -04:00
|
|
|
else:
|
|
|
|
context = None
|
|
|
|
|
|
|
|
# Print our startup messages
|
2021-11-06 03:02:43 -04:00
|
|
|
print("")
|
|
|
|
print("|----------------------------------------------------------|")
|
|
|
|
print("| |")
|
|
|
|
print("| ███████████ ▜█▙ ▟█▛ █████ █ █ █ |")
|
|
|
|
print("| ██ ▜█▙ ▟█▛ ██ |")
|
|
|
|
print("| ███████████ ▜█▙ ▟█▛ ██ |")
|
|
|
|
print("| ██ ▜█▙▟█▛ ███████████ |")
|
|
|
|
print("| |")
|
|
|
|
print("|----------------------------------------------------------|")
|
|
|
|
print("| Parallel Virtual Cluster API daemon v{0: <19} |".format(version))
|
|
|
|
print("| Debug: {0: <49} |".format(str(config["debug"])))
|
|
|
|
print("| API version: v{0: <42} |".format(API_VERSION))
|
|
|
|
print(
|
|
|
|
"| Listen: {0: <48} |".format(
|
|
|
|
"{}:{}".format(config["listen_address"], config["listen_port"])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print("| SSL: {0: <51} |".format(str(config["ssl_enabled"])))
|
|
|
|
print("| Authentication: {0: <40} |".format(str(config["auth_enabled"])))
|
|
|
|
print("|----------------------------------------------------------|")
|
|
|
|
print("")
|
|
|
|
|
|
|
|
pvc_api.app.run(
|
|
|
|
config["listen_address"],
|
|
|
|
config["listen_port"],
|
|
|
|
threaded=True,
|
|
|
|
ssl_context=context,
|
|
|
|
)
|