Skip to content

Commit 2cf1c1a

Browse files
chore: system_name and system_ip_address function changed
1 parent 18dc94a commit 2cf1c1a

File tree

9 files changed

+234
-174
lines changed

9 files changed

+234
-174
lines changed

src/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from flask_sqlalchemy import SQLAlchemy
33

44
from src.logger import logger
5+
from src.helper import get_system_node_name, get_ip_address
6+
# from src.utils import get_ip_address, get_system_node_name
57

68
app = Flask(__name__)
79

@@ -13,6 +15,8 @@
1315
VERSION = "v1.0.4-pre-release"
1416
PROJECT_URL = f"https://github.com/codeperfectplus/{APP_NAME}"
1517
CONTACT_EMAIL = ""
18+
SYSTEM_NAME = get_system_node_name()
19+
SYSTEM_IP_ADDRESS = get_ip_address()
1620

1721
# Configure the SQLite database
1822
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{APP_NAME.lower()}.db'
@@ -28,6 +32,8 @@
2832
version=VERSION,
2933
project_url=PROJECT_URL,
3034
contact_email=CONTACT_EMAIL,
35+
system_name=SYSTEM_NAME,
36+
system_ip_address=SYSTEM_IP_ADDRESS,
3137
)
3238

3339
def get_app_info():
@@ -40,6 +46,8 @@ def get_app_info():
4046
"version": VERSION,
4147
"project_url": PROJECT_URL,
4248
"contact_email": CONTACT_EMAIL,
49+
"system_name": SYSTEM_NAME,
50+
"system_ip_address": SYSTEM_IP_ADDRESS,
4351
}
4452

4553
# Initialize the database

src/helper.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
import subprocess
3+
4+
def get_system_node_name():
5+
"""
6+
Get the system node name.
7+
---
8+
Parameters:
9+
---
10+
Returns:
11+
str: System node name
12+
"""
13+
return os.uname().nodename
14+
15+
def get_ip_address():
16+
try:
17+
# Run the command `hostname -I` to get the IP addresses
18+
result = subprocess.run(['hostname', '-I'], capture_output=True, text=True, check=True)
19+
20+
# Split the output by spaces and get the first IP address
21+
ip_address = result.stdout.split()[0]
22+
23+
return ip_address
24+
except (IndexError, subprocess.CalledProcessError) as e:
25+
print(f"Error occurred: {e}")
26+
return None

src/routes/network_info.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from flask import render_template, blueprints, flash
33
from flask_login import login_required
44
from src.config import app
5-
from src.utils import get_established_connections
5+
from src.utils import get_established_connections, get_ip_address
66
from src.models import PageToggleSettings
77

88
network_info_bp = blueprints.Blueprint("network_stats", __name__)
@@ -15,7 +15,8 @@ def network_stats():
1515
flash("You do not have permission to view this page.", "danger")
1616
return render_template("error/403.html")
1717
net_io = psutil.net_io_counters()
18-
ipv4_ip, ipv6_ip = get_established_connections()
18+
_, ipv6_ip = get_established_connections()
19+
ipv4_ip = get_ip_address()
1920
system_info = {
2021
"network_sent": round(net_io.bytes_sent / (1024**2), 2), # In MB
2122
"network_received": round(net_io.bytes_recv / (1024**2), 2), # In MB

src/scripts/dashboard.sh

Lines changed: 148 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -62,154 +62,159 @@ CONDA_ENV_NAME=$APP_NAME
6262
GIT_REMOTE_URL="https://github.com/codeperfectplus/SystemDashboard" # Set this if you want to add a remote
6363
ENV_FILE="/home/$(whoami)/.bashrc"
6464

65-
# Function to fetch the value of an environment variable from a file
66-
fetch_env_variable() {
67-
var_name=$1 # The name of the environment variable to fetch
68-
# Check if the environment file exists
69-
if [ ! -f "$ENV_FILE" ]; then
70-
echo "Error: Environment file '$ENV_FILE' not found."
71-
return 1
72-
fi
73-
74-
# Fetch the value of the environment variable
75-
var_value=$(grep -E "^${var_name}=" "$ENV_FILE" | sed -E "s/^${var_name}=(.*)/\1/")
76-
77-
# Check if the variable was found and has a value
78-
if [ -z "$var_value" ]; then
79-
echo "Error: Variable '$var_name' not found in '$ENV_FILE'."
80-
return 1
81-
fi
82-
83-
# Print the value of the environment variable
84-
echo "$var_value"
85-
}
86-
87-
auto_update=$(fetch_env_variable "sg_auto_update")
88-
# Fetch from bashrc for auto-update
89-
echo "Auto update for $APP_NAME is set to $auto_update"
90-
91-
# Ensure log directory exists
92-
LOG_DIR="$(dirname "$LOG_FILE")"
93-
mkdir -p "$LOG_DIR"
94-
95-
# Check for Miniconda3 and Anaconda3
96-
CONDA_PATHS=("/home/$USERNAME/miniconda3" "/home/$USERNAME/anaconda3")
97-
CONDA_FOUND=false
98-
99-
for CONDA_PATH in "${CONDA_PATHS[@]}"; do
100-
if [ -d "$CONDA_PATH" ]; then
101-
CONDA_FOUND=true
102-
CONDA_EXECUTABLE="$CONDA_PATH/bin/conda"
103-
CONDA_SETUP_SCRIPT="$CONDA_PATH/etc/profile.d/conda.sh"
104-
break
105-
fi
106-
done
107-
108-
if [ "$CONDA_FOUND" = false ]; then
109-
log_message "Neither Miniconda3 nor Anaconda3 found. Ensure Conda is installed."
110-
exit 1
111-
fi
112-
113-
# Check if Conda setup script exists
114-
if [ ! -f "$CONDA_SETUP_SCRIPT" ]; then
115-
log_message "Conda setup script not found at $CONDA_SETUP_SCRIPT."
116-
exit 1
117-
fi
118-
119-
# Initialize Conda
120-
source "$CONDA_SETUP_SCRIPT"
121-
122-
# Check if the Conda environment exists and create it if not
123-
if ! conda info --envs | awk '{print $1}' | grep -q "^$CONDA_ENV_NAME$"; then
124-
log_message "Conda environment '$CONDA_ENV_NAME' not found. Creating it..."
125-
conda create -n "$CONDA_ENV_NAME" python=3.10 -y
126-
127-
log_message "Activating Conda environment '$CONDA_ENV_NAME' and installing requirements."
128-
conda run -n "$CONDA_ENV_NAME" pip install -r "$REQUIREMENTS_FILE"
129-
else
130-
log_message "Activating existing Conda environment '$CONDA_ENV_NAME'."
131-
fi
132-
13365
# Export Flask environment variables
13466
export FLASK_APP="$FLASK_APP_PATH"
13567
export FLASK_ENV=production
136-
137-
fetch_latest_changes() {
138-
local project_dir="$1"
139-
local git_remote_url="${2-}" # Optional remote URL if needed
68+
export FLASK_RUN_PORT="$FLASK_PORT"
69+
export FLASK_RUN_HOST=$(hostname -I | cut -d' ' -f1)
70+
71+
echo "Flask app path: $FLASK_APP"
72+
echo "Flask environment: $FLASK_ENV"
73+
echo "Flask run host: $FLASK_RUN_HOST"
74+
echo "Flask run port: $FLASK_RUN_PORT"
75+
76+
# # Function to fetch the value of an environment variable from a file
77+
# fetch_env_variable() {
78+
# var_name=$1 # The name of the environment variable to fetch
79+
# # Check if the environment file exists
80+
# if [ ! -f "$ENV_FILE" ]; then
81+
# echo "Error: Environment file '$ENV_FILE' not found."
82+
# return 1
83+
# fi
84+
85+
# # Fetch the value of the environment variable
86+
# var_value=$(grep -E "^${var_name}=" "$ENV_FILE" | sed -E "s/^${var_name}=(.*)/\1/")
87+
88+
# # Check if the variable was found and has a value
89+
# if [ -z "$var_value" ]; then
90+
# echo "Error: Variable '$var_name' not found in '$ENV_FILE'."
91+
# return 1
92+
# fi
93+
94+
# # Print the value of the environment variable
95+
# echo "$var_value"
96+
# }
97+
98+
# auto_update=$(fetch_env_variable "sg_auto_update")
99+
# # Fetch from bashrc for auto-update
100+
# echo "Auto update for $APP_NAME is set to $auto_update"
101+
102+
# # Ensure log directory exists
103+
# LOG_DIR="$(dirname "$LOG_FILE")"
104+
# mkdir -p "$LOG_DIR"
105+
106+
# # Check for Miniconda3 and Anaconda3
107+
# CONDA_PATHS=("/home/$USERNAME/miniconda3" "/home/$USERNAME/anaconda3")
108+
# CONDA_FOUND=false
109+
110+
# for CONDA_PATH in "${CONDA_PATHS[@]}"; do
111+
# if [ -d "$CONDA_PATH" ]; then
112+
# CONDA_FOUND=true
113+
# CONDA_EXECUTABLE="$CONDA_PATH/bin/conda"
114+
# CONDA_SETUP_SCRIPT="$CONDA_PATH/etc/profile.d/conda.sh"
115+
# source "$CONDA_SETUP_SCRIPT" &> /dev/null
116+
# break
117+
# fi
118+
# done
119+
120+
# if [ "$CONDA_FOUND" = false ]; then
121+
# log_message "Neither Miniconda3 nor Anaconda3 found. Ensure Conda is installed."
122+
# exit 1
123+
# fi
124+
125+
# # Check if Conda setup script exists
126+
# if [ ! -f "$CONDA_SETUP_SCRIPT" ]; then
127+
# log_message "Conda setup script not found at $CONDA_SETUP_SCRIPT."
128+
# exit 1
129+
# fi
130+
131+
# # Check if the Conda environment exists and create it if not
132+
# if ! conda info --envs | awk '{print $1}' | grep -q "^$CONDA_ENV_NAME$"; then
133+
# log_message "Conda environment '$CONDA_ENV_NAME' not found. Creating it..."
134+
# conda create -n "$CONDA_ENV_NAME" python=3.10 -y
135+
136+
# log_message "Activating Conda environment '$CONDA_ENV_NAME' and installing requirements."
137+
# conda run -n "$CONDA_ENV_NAME" pip install -r "$REQUIREMENTS_FILE"
138+
# else
139+
# log_message "Activating existing Conda environment '$CONDA_ENV_NAME'."
140+
# fi
141+
142+
# fetch_latest_changes() {
143+
# local project_dir="$1"
144+
# local git_remote_url="${2-}" # Optional remote URL if needed
140145

141-
# Check if the project directory is set and exists
142-
if [[ -z "$project_dir" || ! -d "$project_dir" ]]; then
143-
log_message "ERROR" "Invalid project directory specified."
144-
return 1
145-
fi
146+
# # Check if the project directory is set and exists
147+
# if [[ -z "$project_dir" || ! -d "$project_dir" ]]; then
148+
# log_message "ERROR" "Invalid project directory specified."
149+
# return 1
150+
# fi
146151

147-
# Check if Git is installed
148-
if ! command -v git &> /dev/null; then
149-
log_message "ERROR" "Git is not installed. Please install Git and try again."
150-
return 1
151-
fi
152-
153-
# Check if the directory is a Git repository
154-
if [ -d "$project_dir/.git" ]; then
155-
log_message "INFO" "Repository found at $project_dir. Checking the current branch and for latest changes..."
152+
# # Check if Git is installed
153+
# if ! command -v git &> /dev/null; then
154+
# log_message "ERROR" "Git is not installed. Please install Git and try again."
155+
# return 1
156+
# fi
157+
158+
# # Check if the directory is a Git repository
159+
# if [ -d "$project_dir/.git" ]; then
160+
# log_message "INFO" "Repository found at $project_dir. Checking the current branch and for latest changes..."
156161

157-
# Navigate to the project directory
158-
pushd "$project_dir" > /dev/null
162+
# # Navigate to the project directory
163+
# pushd "$project_dir" > /dev/null
159164

160-
# Get the current branch name
161-
branch=$(git symbolic-ref --short HEAD 2>/dev/null)
162-
log_message "INFO" "Current branch is '$branch'."
165+
# # Get the current branch name
166+
# branch=$(git symbolic-ref --short HEAD 2>/dev/null)
167+
# log_message "INFO" "Current branch is '$branch'."
163168

164-
# Check if there are untracked files
165-
if git status --porcelain | grep '^[?]'; then
166-
# Check if the repository has any commits
167-
if [ $(git rev-list --count HEAD 2>/dev/null) -gt 0 ]; then
168-
# Repository has commits, proceed with stashing
169-
log_message "INFO" "Stashing untracked files..."
170-
if git stash -u; then
171-
log_message "INFO" "Untracked files stashed successfully."
172-
stash_applied=true
173-
else
174-
log_message "ERROR" "Failed to stash untracked files."
175-
popd > /dev/null
176-
return 1
177-
fi
178-
else
179-
log_message "ERROR" "Repository does not have any commits. Cannot stash untracked files."
180-
log_message "INFO" "Manual intervention required to handle untracked files."
181-
popd > /dev/null
182-
return 1
183-
fi
184-
fi
185-
186-
if git pull origin "$branch"; then
187-
log_message "INFO" "Successfully pulled the latest changes from branch '$branch'."
188-
else
189-
log_message "ERROR" "Failed to pull the latest changes. Check your network connection or repository settings."
190-
popd > /dev/null
191-
return 1
192-
fi
193-
194-
# Apply stashed changes if any
195-
if [ "$stash_applied" = true ]; then
196-
log_message "INFO" "Applying stashed changes..."
197-
git stash pop
198-
fi
199-
200-
# Return to the original directory
201-
popd > /dev/null
202-
fi
203-
}
204-
205-
# Check if Flask app is running
206-
if ! pgrep -f "flask run --host=0.0.0.0 --port=$FLASK_PORT" > /dev/null; then
207-
log_message "Flask app is not running. Checking repository and starting it..."
208-
[ "$auto_update" = true ] &&
209-
fetch_latest_changes $PROJECT_DIR $GIT_REMOTE_URL
210-
log_message "Starting Flask app..."
211-
# Ensure environment activation and `flask` command
212-
bash -c "source $CONDA_SETUP_SCRIPT && conda activate $CONDA_ENV_NAME && flask run --host=0.0.0.0 --port=$FLASK_PORT" &>> "$LOG_FILE" &
213-
else
214-
log_message "Flask app is already running."
215-
fi
169+
# # Check if there are untracked files
170+
# if git status --porcelain | grep '^[?]'; then
171+
# # Check if the repository has any commits
172+
# if [ $(git rev-list --count HEAD 2>/dev/null) -gt 0 ]; then
173+
# # Repository has commits, proceed with stashing
174+
# log_message "INFO" "Stashing untracked files..."
175+
# if git stash -u; then
176+
# log_message "INFO" "Untracked files stashed successfully."
177+
# stash_applied=true
178+
# else
179+
# log_message "ERROR" "Failed to stash untracked files."
180+
# popd > /dev/null
181+
# return 1
182+
# fi
183+
# else
184+
# log_message "ERROR" "Repository does not have any commits. Cannot stash untracked files."
185+
# log_message "INFO" "Manual intervention required to handle untracked files."
186+
# popd > /dev/null
187+
# return 1
188+
# fi
189+
# fi
190+
191+
# if git pull origin "$branch"; then
192+
# log_message "INFO" "Successfully pulled the latest changes from branch '$branch'."
193+
# else
194+
# log_message "ERROR" "Failed to pull the latest changes. Check your network connection or repository settings."
195+
# popd > /dev/null
196+
# return 1
197+
# fi
198+
199+
# # Apply stashed changes if any
200+
# if [ "$stash_applied" = true ]; then
201+
# log_message "INFO" "Applying stashed changes..."
202+
# git stash pop
203+
# fi
204+
205+
# # Return to the original directory
206+
# popd > /dev/null
207+
# fi
208+
# }
209+
210+
# # Check if Flask app is running
211+
# if ! pgrep -f "flask run --host=0.0.0.0 --port=$FLASK_PORT" > /dev/null; then
212+
# log_message "Flask app is not running. Checking repository and starting it..."
213+
# [ "$auto_update" = true ] &&
214+
# fetch_latest_changes $PROJECT_DIR $GIT_REMOTE_URL
215+
# log_message "Starting Flask app..."
216+
# # Ensure environment activation and `flask` command
217+
# bash -c "source $CONDA_SETUP_SCRIPT && conda activate $CONDA_ENV_NAME && flask run --host=0.0.0.0 --port=$FLASK_PORT" &>> "$LOG_FILE" &
218+
# else
219+
# log_message "Flask app is already running."
220+
# fi
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
<div class="col-md-6 col-lg-4 mb-4"><div class="card bg-network"><div class="card-body"><h5 class="card-title">IPV4 Connections <i class="fas fa-network-wired"></i></h5><p class="card-text fs-4">{{ system_info['ipv4_ip']}}</p></div></div></div>
1+
<div class="col-md-6 col-lg-4 mb-4">
2+
<div class="card bg-network">
3+
<div class="card-body">
4+
<h5 class="card-title">IPV4 Connections <i class="fas fa-network-wired"></i></h5>
5+
<p class="card-text fs-4">{{ system_info['ipv4_ip']}}</p>
6+
</div>
7+
</div>
8+
</div>

0 commit comments

Comments
 (0)