Skip to content

Commit

Permalink
ImageServer Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sunami09 committed Feb 3, 2025
1 parent 7730a91 commit 913c8b7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
11 changes: 9 additions & 2 deletions autogole-api/packaging/files/etc/rtmon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
sleep_timer: 30
# Work dir for temp files (api directory). Default /srv
workdir: '/srv/'

image_dir: '/srv/images'
# Grafana settings (for the API)
grafana_host: 'https://autogole-grafana.nrp-nautilus.io'
grafana_api_key: 'REPLACE_ME'
grafana_api_key:
baseImageURL: 'http://localhost:8000/images'
grafana_folder: 'Real Time Mon'
grafana_dev:
image_dir:
image_host:
image_flask_port:
image_debug:

# FOR DEVELOPMEENT ONLY
# Enable grafana_dev parameter (can be any string, and will be used as name to create directory inside Grafana)
# Additionally - all dashboards will have this added to name.
Expand Down
39 changes: 32 additions & 7 deletions autogole-api/packaging/imageserver.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
from flask import Flask, send_from_directory, render_template_string, abort
import os
import yaml

app = Flask(__name__)

IMAGE_DIRECTORY = '/srv/images'
def findImageDirectory():
config_file = "/etc/rtmon.yaml"
if not os.path.isfile(config_file):
raise Exception(f"Config file {config_file} does not exist.")
try:
with open(config_file, "r", encoding="utf-8") as fd:
content = fd.read()
except Exception as ex:
raise Exception(f"Error reading the config file: {ex}")

try:
config = yaml.safe_load(content)
except Exception as ex:
raise Exception(f"Error parsing YAML: {ex}")
if not isinstance(config, dict):
raise Exception("Config file does not contain a valid YAML dictionary.")
image_dir = config.get("image_dir", '/srv/images')
image_host = config.get("image_host", '0.0.0.0')
image_port = config.get("image_port", 8000)
image_debug = config.get("image_debug", True)

return [image_dir, image_host, image_port, image_debug]

IMAGE_CONFIG = findImageDirectory()
@app.route('/')
def home():
return "<h1>Hello, this is the root page!</h1>"

@app.route('/images')
def list_images():
if not os.path.exists(IMAGE_DIRECTORY):
if not os.path.exists(IMAGE_CONFIG[0]):
return "<h1>Image directory not found.</h1>", 404

images = [f for f in os.listdir(IMAGE_DIRECTORY) if os.path.isfile(os.path.join(IMAGE_DIRECTORY, f))]
images = [f for f in os.listdir(IMAGE_CONFIG[0]) if os.path.isfile(os.path.join(IMAGE_CONFIG[0], f))]

html = """
<h1>Network Topology</h1>
Expand All @@ -27,10 +51,11 @@ def list_images():

@app.route('/images/<filename>')
def serve_image(filename):
if not os.path.isfile(os.path.join(IMAGE_DIRECTORY, filename)):
path = os.path.abspath(os.path.join(IMAGE_CONFIG[0], filename))

if not path.startswith(IMAGE_CONFIG[0]) or not os.path.isfile(path):
abort(404)
return send_from_directory(IMAGE_DIRECTORY, filename)
return send_from_directory(IMAGE_CONFIG[0], filename)

if __name__ == "__main__":
port = 8000
app.run(host='0.0.0.0', port=port, debug=True)
app.run(IMAGE_CONFIG[1], IMAGE_CONFIG[2], debug = IMAGE_CONFIG[3])
6 changes: 4 additions & 2 deletions autogole-api/src/python/RTMonLibs/DiagramWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ def d_addSwitch(self, item):
break
if ipLabel2:
break
ip_node = Custom(ipLabel + "\n" + ipLabel2, self.BGP_ICON_PATH)
switch1 >> Edge() << ip_node
ipNode = Custom(ipLabel, self.BGP_ICON_PATH)
switch1 >> Edge() << ipNode
ipNode2 = Custom(ipLabel2, self.BGP_ICON_PATH)
ipNode >> Edge() << ipNode2
return switch1


Expand Down

0 comments on commit 913c8b7

Please sign in to comment.