Files
cosmoserver/files/api/Cosmos_Settings.py
2026-03-29 09:39:43 -07:00

107 lines
3.5 KiB
Python

import yaml
from urllib.parse import urlparse
import secrets, string
#######################################################################
### Settings Handler Functions
#######################################################################
# default application setting variables
app_settings = {
"noisy_test" : False,
"debug_output" : True,
"log_output" : True,
"secure_api" : True,
"push_redis" : False,
"run_background" : True,
"cosmostat_server": False,
"cosmostat_server_reporter": False,
"update_frequency": 1,
"custom_api_port": "5000",
"cosmostat_server_api": "http://10.200.27.20:5000/",
"REAL_API_KEY": ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(256)),
"disable_local_api": False,
"local_api_address": "http://10.200.27.20:5000/",
"cosmostat_server_ip": "10.200.27.20"
}
with open('cosmostat_settings.yaml', 'r') as f:
print("Loading cosmostat_settings file")
cosmostat_settings = yaml.safe_load(f)
print("...Done")
# initialize system variables from settings file
print("Checking for system var overrides")
for setting in app_settings:
if setting in cosmostat_settings:
cosmos_setting = cosmostat_settings[setting]
if app_settings["debug_output"]:
print(f"{setting}: {cosmos_setting}")
app_settings[setting] = cosmos_setting
print("...Done")
# this returns the docker gateway from the settings
def cosmostat_bind_ip() -> str:
return cosmostat_settings["docker_gateway"]
# this returns the jenkins user that ran the pipeline
def jenkins_user_settings() -> str:
return cosmostat_settings["jenkins_user"]
# this returns the ansible_hostname from setup
def jenkins_hostname_settings() -> str:
return cosmostat_settings["ansible_hostname"]
# this returns the inventory_generation_timestamp
def jenkins_inventory_generation_timestamp_settings() -> str:
return cosmostat_settings["inventory_generation_timestamp"]
def run_cosmostat_server():
return cosmostat_settings["cosmostat_server"]
def run_cosmostat_reporter():
result = False
if not cosmostat_settings["cosmostat_server"] and cosmostat_settings["cosmostat_server_reporter"]:
result = True
return result
def service_gateway_ip():
result = "0.0.0.0"
if cosmostat_settings["secure_api"] and not cosmostat_settings["cosmostat_server"]:
result = cosmostat_bind_ip()
return result
def redis_gateway_ip():
if cosmostat_settings["secure_api"]:
return cosmostat_bind_ip()
else:
return "0.0.0.0"
def cosmostat_server_api():
return cosmostat_settings["cosmostat_server_api"]
def service_api_port():
return cosmostat_settings["custom_api_port"]
def log_data(log_output:str, log_level = "noisy_test"):
log_levels = [
"noisy_test",
"debug_output",
"log_output",
]
if log_level in log_levels:
if cosmostat_settings[log_level]:
print(log_output)
else:
print(f"Warning - {log_level} not valid log level")
if app_settings["cosmostat_server"]:
app_settings["cosmostat_server_reporter"] = False
log_data(log_output = "Warning - server and reporter cannot run concurrently, server is prioritized.", log_level = cosmostat_settings["log_output"])
#for level_check in log_levels:
# if log_level == level_check:
# print(f"log_level: {log_level} - level_check - {level_check}")
# print(log_output)