cosmostat active host inventory file api

This commit is contained in:
2026-04-04 17:47:32 -07:00
parent be95ab7593
commit a89703c420
26 changed files with 1243 additions and 261 deletions

View File

@ -3,10 +3,10 @@
### cosmostat service handler
#######################################################################
from flask import Flask, jsonify, request, Response
from flask import Flask, jsonify, request, Response, abort
from flask_apscheduler import APScheduler
from typing import Dict, Union
import json, time, redis, yaml
import json, time, redis, yaml, datetime
import secrets, string
import requests
from requests import RequestException, Response
@ -63,6 +63,8 @@ def get_server_redis_data():
"data_timestamp": client.data_timestamp,
"uuid": client.uuid,
"short_id": client.name,
"active_ip": client.active_ip,
"is_server": client.is_server,
"redis_data": client.redis_data
}
result.append(this_client_key)
@ -168,7 +170,7 @@ def get_php_summary():
"info_strings": component.get_properties_strings(return_simple = True)
}
system_components.append(this_component)
this_primary_ip = cosmostat_client.primary_ip
if run_cosmostat_server():
client_uuid = cosmostat_server.get_uuid_from_hostname(cosmostat_client.name)
data_timestamp = cosmostat_server.get_system(client_uuid)
@ -178,11 +180,13 @@ def get_php_summary():
"component_name": "Data Timestamp",
"info_strings": f"Data is {data_timestamp} seconds old"
}
this_primary_ip = cosmostat_settings["cosmostat_server_ip"]
system_components.append(component_age)
result = [{
"system_properties": system_properties,
"system_components": system_components
"system_components": system_components,
"active_interface": this_primary_ip
}]
return result
@ -217,6 +221,7 @@ def create_client():
result = {}
# check the request and return payload dict {} if all good
payload = client_submit_check(request = request, dict_name = "client_properties")
# if the client does not exist, create it
if not cosmostat_server.check_uuid(payload["uuid"]):
result = run_create_client(payload)
else:
@ -266,6 +271,20 @@ def get_server_redis():
result = {"message": "server not running on this endpoint"}
return jsonify(result)
# api to get server redis data
@app.route('/client_ip_summary', methods=['GET'])
def client_ip_summary():
result = []
if run_cosmostat_server():
result = get_client_ip_summary()
else:
result = {"message": "server not running on this endpoint"}
return jsonify(result)
# return inventory file of all clients for update
@app.route("/client_inventory", methods=["GET"])
def client_inventory():
return build_inventory()
#######################################################################
### Server Flask Helpers
@ -295,6 +314,7 @@ def run_update_client(this_client):
# create client on server
def run_create_client(this_client):
if public_api_check(this_client):
#this_client["active_interface"] = cosmostat_client.primary_ip
timestamp_update = cosmostat_server.add_system(system_dictionary = this_client)
update_status = f'created client {this_client["short_id"]}'
return {
@ -331,6 +351,7 @@ def client_submit_check(request, dict_name: str):
missing = required_keys - payload.keys()
if missing:
raise ValueError(f"Missing required keys: {', '.join(sorted(missing))}")
return payload
# generate cosmostat server summary
@ -366,14 +387,92 @@ def get_client_details():
result = {"message": "no clients reporting"}
return result
# get client IP summary
def get_client_ip_summary(return_list = False):
result = []
for client in cosmostat_server.systems:
device_info = {
"small_id": client.name,
"uuid": client.uuid,
"hostname": client.hostname,
"lan_ip": client.active_ip,
}
if client.hostname != cosmostat_server.hostname:
if return_list:
result.append(client.active_ip)
else:
result.append(device_info)
return result
# build client ansible inventory file
def build_inventory():
all_ips = get_client_ip_summary(return_list = True)
cosmos_subnets = [
"172.25.1.0/24",
"172.20.0.0/16",
"172.19.10.0/24",
"10.200.26.0/24",
"10.200.27.0/24",
"192.168.60.0/24",
]
ips = []
bad_ips = []
# build list of reachable IPs
for ip in all_ips:
for subnet in cosmos_subnets:
if is_ip_in_subnets(ip, subnet) and cosmostat_server.get_vpn_ip(ip, just_check = True):
ips.append(ip)
# list of unreachable IPs
for ip in all_ips:
if ip not in ips:
bad_ips.append(ip)
# add any VPN IPs for bad IPs to the main IP list
for ip in bad_ips:
ips.append(cosmostat_server.get_vpn_ip(ip))
hosts = {ip: {"ansible_host": ip} for ip in ips}
inventory = {
"all": {
"hosts": hosts,
"vars": {
"refresh_only": "true",
"ansible_connection": "ssh",
"ansible_ssh_private_key_file": "/var/jenkins_home/jenkins_key",
"ansible_python_interpreter": "/usr/bin/python3",
"jenkins_user": 'automate',
"jenkins_group": 'Jenkins-Admin',
"subnet_group_check": 'Jenkins-AllSubnets',
"SERVER_SUBNET_GROUP": 'Jenkins-AllSubnets',
"inventory_generation_timestamp": f"{datetime.datetime.now().isoformat()}",
"playbook_file": '/var/jenkins_home/ansible/playbooks/cosmostat.yaml',
"quick_refresh": "true",
"noisy_test": "false",
"debug_output": "false",
"push_redis": "false",
"run_background": "true",
"log_output": "true",
"public_dashboard": "false",
"custom_port": "80",
"custom_api_port": "5000",
"cosmostat_server_reporter": "true",
"cosmostat_server": "false",
"secure_api": "false",
"disable_local_dashboard": "true",
"REAL_API_KEY": f"{cosmostat_settings['REAL_API_KEY']}"
},
}
}
return yaml.safe_dump(
inventory,
default_flow_style=False,
sort_keys=False,
)
#######################################################################
### Cosmostat Client Subroutines
#######################################################################
# since the API isn't running
# def local_client_update():
# Cosmostat Client Reporter
# Cosmostat Client Reporter Handler
def client_update():
api_url = f"{cosmostat_server_api()}update_client"
payload = get_client_payload(get_client_redis_data(human_readable = False), "redis_data")
@ -385,6 +484,7 @@ def client_update():
if not result or not result.get('client_updated'):
log_data(log_output = f"Client not updated, initializing", log_level = "log_output")
result = client_api_initialize()
# this result does not matter and is not used anywhere
return result
# Cosmostat Client Initializer
@ -393,6 +493,9 @@ def client_api_initialize():
# generate payload
payload = get_client_payload(get_php_summary(), "client_properties")
# execute API call
payload["active_interface"] = cosmostat_client.primary_ip
payload["is_server"] = False
result = client_submission_handler(api_url, payload)
return result
@ -410,7 +513,7 @@ def client_submission_handler(api_url: str, payload: dict):
try:
result = response.json()
except ValueError as exc:
log_data(log_output = "Server responded with non-JSON payload: {response.text!r}", log_level = "log_output")
log_data(log_output = f"Server responded with non-JSON payload: {response.text!r}", log_level = "log_output")
return result
def get_client_payload(system_dictionary: dict, dictionary_name: str):
@ -427,7 +530,6 @@ def get_client_payload(system_dictionary: dict, dictionary_name: str):
}
return payload
#######################################################################
#######################################################################
### Main Subroutine
@ -452,7 +554,7 @@ if __name__ == '__main__':
# instantiate and return the Cosmoserver System object
def new_cosmostat_server():
new_server = CosmostatServer(cosmostat_client.uuid)
new_server = CosmostatServer(name = cosmostat_client.uuid, hostname = jenkins_hostname_settings())
log_data(log_output = f"New Cosmostat serverobject name: {new_server.name}", log_level = "log_output")
return new_server
@ -473,7 +575,7 @@ if __name__ == '__main__':
cosmostat_client.update_system_state()
# publish to redis if the web dashboard is active locally
if app_settings["push_redis"] and not app_settings["disable_local_api"]:
if app_settings["push_redis"] and not app_settings["disable_local_dashboard"]:
update_redis_server()
# report data to the server if configured
@ -489,7 +591,7 @@ if __name__ == '__main__':
run_update_client(get_client_payload(get_client_redis_data(human_readable = False), "redis_data"))
log_data(log_output = f"{this_client}", log_level = "noisy_test")
time.sleep(0.5)
time.sleep(0.2)
######################################
# instantiate client
@ -514,15 +616,18 @@ if __name__ == '__main__':
log_data(log_output = f"Cosmostat Server Start", log_level = "log_output")
cosmostat_server = new_cosmostat_server()
this_client = get_client_payload(get_php_summary(), "client_properties")
this_client["active_interface"] = cosmostat_settings["cosmostat_server_ip"]
this_client["is_server"] = True
timestamp_update = cosmostat_server.add_system(system_dictionary = this_client)
# moving this here so all the bits exist
client_update()
# if not a server, update client on the server
else:
client_update()
######################################
# send initial stats update to redis
######################################
if app_settings["push_redis"] and not app_settings["disable_local_api"]:
if app_settings["push_redis"] and not app_settings["disable_local_dashboard"]:
log_data(log_output = f"Initial Redis Push", log_level = "log_output")
update_redis_server()
@ -530,7 +635,7 @@ if __name__ == '__main__':
# Flask scheduler for scanner
######################################
if app_settings["run_background"] and not app_settings["disable_local_api"]:
if app_settings["run_background"] and not app_settings["disable_local_dashboard"]:
log_data(log_output = f"Background Function Initializing", log_level = "log_output")
log_data(log_output = "Loading flask background subroutine...", log_level = "log_output")
@ -549,7 +654,7 @@ if __name__ == '__main__':
# Flask API
######################################
log_data(log_output = f"gateway: {service_gateway_ip()} - port: {service_api_port()}", log_level = "log_output")
if not app_settings["disable_local_api"]:
if not app_settings["disable_local_dashboard"]:
log_data(log_output = f"Main API Start", log_level = "log_output")
app.run(debug=False, host=service_gateway_ip(), port=service_api_port())
else: