improved start sequence, fixed user update
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -6,3 +6,5 @@ __pycache__/
|
||||
instance/
|
||||
.vscode/
|
||||
.DS_Store
|
||||
docker-compose.yml
|
||||
|
||||
|
||||
@@ -68,49 +68,62 @@ def add_user(mac_address, description, vlan_id):
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
def update_user_description(mac_address, description):
|
||||
"""Update the description field of a user identified by MAC address."""
|
||||
def update_user(mac_address, description, vlan_id):
|
||||
"""Update both description and VLAN ID for a given MAC address."""
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("UPDATE users SET description = %s WHERE mac_address = %s", (description, mac_address.lower()))
|
||||
cursor.execute(
|
||||
"UPDATE users SET description = %s, vlan_id = %s WHERE mac_address = %s",
|
||||
(description, vlan_id, mac_address.lower())
|
||||
)
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
# def update_user_description(mac_address, description):
|
||||
# """Update the description field of a user identified by MAC address."""
|
||||
# conn = get_connection()
|
||||
# cursor = conn.cursor()
|
||||
# cursor.execute("UPDATE users SET description = %s WHERE mac_address = %s", (description, mac_address.lower()))
|
||||
# conn.commit()
|
||||
# cursor.close()
|
||||
# conn.close()
|
||||
|
||||
# Note: This function seems redundant with update_user_description. Included as per instructions.
|
||||
def update_description(mac_address, description):
|
||||
"""Update the description for a given MAC address in the users table."""
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"UPDATE users SET description = %s WHERE mac_address = %s",
|
||||
(description, mac_address.lower())
|
||||
)
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
# def update_description(mac_address, description):
|
||||
# """Update the description for a given MAC address in the users table."""
|
||||
# conn = get_connection()
|
||||
# cursor = conn.cursor()
|
||||
# cursor.execute(
|
||||
# "UPDATE users SET description = %s WHERE mac_address = %s",
|
||||
# (description, mac_address.lower())
|
||||
# )
|
||||
# conn.commit()
|
||||
# cursor.close()
|
||||
# conn.close()
|
||||
|
||||
def update_user_vlan(mac_address, vlan_id):
|
||||
"""Update the VLAN ID for a given MAC address in the users table."""
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("UPDATE users SET vlan_id = %s WHERE mac_address = %s", (vlan_id, mac_address.lower()))
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
# def update_user_vlan(mac_address, vlan_id):
|
||||
# """Update the VLAN ID for a given MAC address in the users table."""
|
||||
# conn = get_connection()
|
||||
# cursor = conn.cursor()
|
||||
# cursor.execute("UPDATE users SET vlan_id = %s WHERE mac_address = %s", (vlan_id, mac_address.lower()))
|
||||
# conn.commit()
|
||||
# cursor.close()
|
||||
# conn.close()
|
||||
|
||||
# Note: This function seems redundant with update_user_vlan. Included as per instructions.
|
||||
def update_vlan(mac_address, vlan_id):
|
||||
"""Update the VLAN ID for a given MAC address in the users table."""
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"UPDATE users SET vlan_id = %s WHERE mac_address = %s",
|
||||
(vlan_id, mac_address.lower())
|
||||
)
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
# # Note: This function seems redundant with update_user_vlan. Included as per instructions.
|
||||
# def update_vlan(mac_address, vlan_id):
|
||||
# """Update the VLAN ID for a given MAC address in the users table."""
|
||||
# conn = get_connection()
|
||||
# cursor = conn.cursor()
|
||||
# cursor.execute(
|
||||
# "UPDATE users SET vlan_id = %s WHERE mac_address = %s",
|
||||
# (vlan_id, mac_address.lower())
|
||||
# )
|
||||
# conn.commit()
|
||||
# cursor.close()
|
||||
# conn.close()
|
||||
|
||||
def delete_user(mac_address):
|
||||
"""Remove a user from the database by their MAC address."""
|
||||
|
||||
@@ -31,31 +31,28 @@
|
||||
<tr>
|
||||
<td>{{ entry.mac_address }}</td>
|
||||
|
||||
<!-- Form spans Description and Actions columns -->
|
||||
<form method="POST" action="{{ url_for('user.update_description_route') }}">
|
||||
<td>
|
||||
<form method="POST" action="{{ url_for('user.update_user_route') }}">
|
||||
<input type="hidden" name="mac_address" value="{{ entry.mac_address }}">
|
||||
|
||||
<td>
|
||||
<input type="text" name="description" value="{{ entry.description or '' }}">
|
||||
</td>
|
||||
|
||||
<td>{{ entry.vendor or "..." }}</td>
|
||||
|
||||
<td>
|
||||
<form method="POST" action="{{ url_for('user.update_vlan_route') }}" class="inline-form">
|
||||
<input type="hidden" name="mac_address" value="{{ entry.mac_address }}">
|
||||
<select name="group_id" onchange="this.form.submit()">
|
||||
<select name="group_id">
|
||||
{% for group in available_groups %}
|
||||
<option value="{{ group.vlan_id }}" {% if group.vlan_id == entry.vlan_id %}selected{% endif %}>
|
||||
VLAN {{ group.vlan_id }}{% if group.description %} - {{ group.description }}{% endif %}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</form>
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<button type="submit" title="Save">💾</button>
|
||||
</form> <!-- Closing the description form here -->
|
||||
</form>
|
||||
|
||||
<form method="POST" action="{{ url_for('user.delete') }}" style="display:inline;">
|
||||
<input type="hidden" name="mac_address" value="{{ entry.mac_address }}">
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, flash
|
||||
from db_interface import get_all_users, get_all_groups, add_user, update_description, update_vlan, delete_user, refresh_vendors, get_user_by_mac
|
||||
from db_interface import (
|
||||
get_all_users,
|
||||
get_all_groups,
|
||||
add_user,
|
||||
update_user,
|
||||
delete_user,
|
||||
refresh_vendors,
|
||||
get_user_by_mac
|
||||
)
|
||||
|
||||
user = Blueprint('user', __name__, url_prefix='/user')
|
||||
|
||||
@@ -19,21 +27,28 @@ def add():
|
||||
add_user(mac, desc, group_id)
|
||||
return redirect(url_for('user.user_list'))
|
||||
|
||||
|
||||
@user.route('/update_description', methods=['POST'])
|
||||
def update_description_route():
|
||||
@user.route('/update_user', methods=['POST'])
|
||||
def update_user_route():
|
||||
mac = request.form['mac_address']
|
||||
desc = request.form.get('description', '')
|
||||
update_description(mac, desc)
|
||||
vlan_id = request.form['group_id']
|
||||
update_user(mac, desc, vlan_id)
|
||||
return redirect(url_for('user.user_list'))
|
||||
|
||||
# @user.route('/update_description', methods=['POST'])
|
||||
# def update_description_route():
|
||||
# mac = request.form['mac_address']
|
||||
# desc = request.form.get('description', '')
|
||||
# update_description(mac, desc)
|
||||
# return redirect(url_for('user.user_list'))
|
||||
|
||||
@user.route('/update_vlan', methods=['POST'])
|
||||
def update_vlan_route():
|
||||
mac = request.form['mac_address']
|
||||
group_id = request.form['group_id']
|
||||
update_vlan(mac, group_id)
|
||||
return redirect(url_for('user.user_list'))
|
||||
|
||||
# @user.route('/update_vlan', methods=['POST'])
|
||||
# def update_vlan_route():
|
||||
# mac = request.form['mac_address']
|
||||
# group_id = request.form['group_id']
|
||||
# update_vlan(mac, group_id)
|
||||
# return redirect(url_for('user.user_list'))
|
||||
|
||||
|
||||
@user.route('/delete', methods=['POST'])
|
||||
|
||||
@@ -18,5 +18,7 @@ COPY . .
|
||||
# Expose RADIUS port (UDP)
|
||||
EXPOSE 1812/udp
|
||||
|
||||
COPY wait-for-db.py .
|
||||
|
||||
# Run the RADIUS service
|
||||
CMD ["python", "main.py"]
|
||||
CMD ["sh", "-c", "python wait-for-db.py && python main.py"]
|
||||
|
||||
36
radius/wait-for-db.py
Normal file
36
radius/wait-for-db.py
Normal file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
import time
|
||||
import os
|
||||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
|
||||
host = os.getenv("DB_HOST", "db")
|
||||
port = int(os.getenv("DB_PORT", "3306"))
|
||||
user = os.getenv("DB_USER")
|
||||
password = os.getenv("DB_PASSWORD")
|
||||
database = os.getenv("DB_NAME")
|
||||
|
||||
timeout = 60 # seconds
|
||||
start_time = time.time()
|
||||
|
||||
print(f"⏳ Waiting for DB at {host}:{port} to be ready...")
|
||||
|
||||
while True:
|
||||
try:
|
||||
conn = mysql.connector.connect(
|
||||
host=host,
|
||||
port=port,
|
||||
user=user,
|
||||
password=password,
|
||||
database=database
|
||||
)
|
||||
if conn.is_connected():
|
||||
print("✅ Database is ready!")
|
||||
conn.close()
|
||||
break
|
||||
except Error as e:
|
||||
print(f"🛑 DB not ready yet: {e}")
|
||||
time.sleep(2)
|
||||
if time.time() - start_time > timeout:
|
||||
print("❌ Timeout waiting for the database.")
|
||||
exit(1)
|
||||
Reference in New Issue
Block a user