From 4f663bb7a59cd2cc6e12b22cbc226dbafa9aaf74 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 28 Feb 2024 11:44:32 +0500 Subject: [PATCH] remove other than hooks --- installer.sh | 68 ----------------------------- logs/a_error.log | 1 - logs/b_error.log | 1 - readme.md | 22 ---------- src/app.py | 59 ------------------------- src/config.json | 111 ----------------------------------------------- 6 files changed, 262 deletions(-) delete mode 100644 installer.sh delete mode 100644 logs/a_error.log delete mode 100644 logs/b_error.log delete mode 100644 readme.md delete mode 100644 src/app.py delete mode 100644 src/config.json diff --git a/installer.sh b/installer.sh deleted file mode 100644 index e854f69..0000000 --- a/installer.sh +++ /dev/null @@ -1,68 +0,0 @@ -#!/bin/bash - -# Define variables for easier modifications and readability -GIT_REPO="https://git.maxprint.io/MaxApex/ticket_ai_flask_app.git" -APP_DIR="/opt/ticket-ai" -VENV_DIR="$APP_DIR/venv" -SERVICE_FILE="/etc/systemd/system/ticket-ai.service" -PYTHON_BIN="$VENV_DIR/bin/python3" -ACTIVATE_SCRIPT="$VENV_DIR/bin/activate" -IPTABLES_RULES=("83.136.253.122") # Add IPs as needed - -# Ensure the script is run as root -if [ "$(id -u)" != "0" ]; then - echo "This script must be run as root" 1>&2 - exit 1 -fi - -# Installing git -yum install -y git || { echo "Failed to install git. Exiting."; exit 1; } - -# Clone the repository if it doesn't exist -if [ ! -d "$APP_DIR" ]; then - git clone $GIT_REPO $APP_DIR || { echo "Failed to clone repository. Exiting."; exit 1; } -else - echo "$APP_DIR already exists. Skipping clone." -fi - -# Navigate to the repo directory -cd $APP_DIR || { echo "Failed to navigate to $APP_DIR. Exiting."; exit 1; } - -# Create virtual environment if it doesn't exist -if [ ! -d "$VENV_DIR" ]; then - python3 -m venv $VENV_DIR -else - echo "$VENV_DIR already exists. Skipping virtual environment creation." -fi - -# Activate virtual environment and install dependencies -source $ACTIVATE_SCRIPT -pip install Flask flask_jwt_extended || { echo "Failed to install Flask or flask_jwt_extended. Exiting."; exit 1; } - -# Create systemd service file -cat < $SERVICE_FILE -[Unit] -Description=Ticket AI - -[Service] -Type=simple -ExecStart=$PYTHON_BIN $APP_DIR/src/app.py - -[Install] -WantedBy=multi-user.target -EOF - -# Reload systemd to recognize the new service and start it -systemctl daemon-reload -systemctl start ticket-ai.service || { echo "Failed to start ticket-ai.service. Exiting."; exit 1; } - -# Add firewall rules to accept traffic -for IP in "${IPTABLES_RULES[@]}"; do - iptables -A INPUT -p tcp -m tcp -s $IP --dport 5000 -j ACCEPT -done - -# Save iptables rules and restart the service to apply changes -service iptables save -service iptables restart || { echo "Failed to restart iptables. Exiting."; exit 1; } - -echo "Setup completed successfully." diff --git a/logs/a_error.log b/logs/a_error.log deleted file mode 100644 index 37e6b4f..0000000 --- a/logs/a_error.log +++ /dev/null @@ -1 +0,0 @@ -this is a error log from file 'A' \ No newline at end of file diff --git a/logs/b_error.log b/logs/b_error.log deleted file mode 100644 index f2917c1..0000000 --- a/logs/b_error.log +++ /dev/null @@ -1 +0,0 @@ -this is a error log from file 'B' \ No newline at end of file diff --git a/readme.md b/readme.md deleted file mode 100644 index 7520a42..0000000 --- a/readme.md +++ /dev/null @@ -1,22 +0,0 @@ -# install packages -pip install Flask -pip install flask_jwt_extended - -# run application -python app.py - -# call api -http://127.0.0.1:5000/get_logs?issue_type=Demo - -# sample response -{ - "/var/log/db.log": "Last 50 lines of db.log...", - "/var/log/db_error.log": "Last 30 lines of db_error.log...", - "top": "Output of the top command..." -} - -# run installation script -./install.sh - -# run application -./run.sh diff --git a/src/app.py b/src/app.py deleted file mode 100644 index 489f4a7..0000000 --- a/src/app.py +++ /dev/null @@ -1,59 +0,0 @@ -from flask import Flask, request, jsonify -import json -import subprocess -from flask_jwt_extended import JWTManager, create_access_token -import os -import datetime - -app = Flask(__name__) - -# Setup the Flask-JWT-Extended extension -app.config["JWT_SECRET_KEY"] = os.environ.get("JWT_SECRET_KEY", "default-secret-key") -jwt = JWTManager(app) - -def split_directory_and_file(path): - directory, file_with_wildcard = os.path.split(path) - file_name = file_with_wildcard.split('*')[0] # Get the part before the wildcard - return directory, file_name - -def read_log_from_dir(dir_path, pattern, lines): - ret_var = "" - three_hours_ago = datetime.datetime.now() - datetime.timedelta(hours=3) - log_files = [f for f in os.listdir(dir_path) if pattern in f and datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(dir_path, f))) > three_hours_ago] - for file_name in log_files: - file_path = os.path.join(dir_path, file_name) - ret_var += f"{file_name}\n{subprocess.check_output(['tail', '-n', str(lines), file_path]).decode('utf-8')}\n\n" - return ret_var - -def read_log(log_file, lines): - return subprocess.check_output(['tail', '-n', str(lines), log_file]).decode('utf-8') - -def read_top(): - return subprocess.check_output(['top', '-b', '-n', '1']).decode('utf-8') - -@app.route('/login', methods=['POST']) -def login(): - username = request.json.get("username") - password = request.json.get("password") - if username != "admin" or password != "password": - return jsonify({"msg": "Bad username or password"}), 401 - access_token = create_access_token(identity=username) - return jsonify(access_token=access_token) - -@app.route('/get_logs', methods=['GET']) -def get_logs(): - issue_type = request.args.get("issue_type") - try: - with open('/opt/ticket-ai/src/config.json') as config_file: - config = json.load(config_file) - if issue_type not in config: - return jsonify({"error": "Invalid issue type"}) - issue_config = config[issue_type] - response = {log.get('log_file').split('*')[0]: read_log_from_dir(*split_directory_and_file(log.get('log_file')), log['lines']) if '*' in log.get('log_file') else read_log(log['log_file'], log['lines']) for log in issue_config['logs']} - response.update({comm.get('tag'): os.popen(comm.get('comm')).read() for comm in issue_config['commands']}) - except Exception as e: - return jsonify({"error": str(e)}) - return jsonify(response) - -if __name__ == '__main__': - app.run(debug=True, host="0.0.0.0") diff --git a/src/config.json b/src/config.json deleted file mode 100644 index 776b388..0000000 --- a/src/config.json +++ /dev/null @@ -1,111 +0,0 @@ -{ - "--Select Issue Type--": { - "commands": [], - "logs": [] - }, - "Password Reset Request": { - "commands": [], - "logs": [] - }, - "Domain Mapping": { - "commands": [], - "logs": [] - }, - "Wallet/Reverse Proxy Required": { - "commands": [], - "logs": [] - }, - "Others": { - "commands": [], - "logs": [] - }, - "Email Problem": { - "commands": [], - "logs": [ - { - "log_file": "/var/log/messages", - "lines": 50 - } - ] - }, - "Jasper Reports": { - "commands": [ - { - "comm": "top -b -n 1", - "tag": "top" - } - ], - "logs": [ - { - "log_file": "/opt/tomcat/logs/catalina.out", - "lines": 50 - }, - { - "log_file": "/var/log/messages", - "lines": 50 - } - ] - }, - "Server Unavailable": { - "commands": [], - "logs": [ - { - "log_file": "/opt/tomcat/logs/catalina.out", - "lines": 50 - }, - { - "log_file": "/opt/oracle/diag/rdbms/xe/XE/trace/alert_XE.log", - "lines": 50 - }, - { - "log_file": "/var/log/messages", - "lines": 50 - } - ] - }, - "Demo1": { - "commands": [], - "logs": [ - { - "log_file": "/home/arehman/Documents/Projects/Python/Maxapex/ticket_ai_flask_app/logs/*_error.log", - "lines": 50 - } - ] - }, - "Demo2": { - "commands": [], - "logs": [ - { - "log_file": "/home/arehman/Documents/Projects/Python/Maxapex/ticket_ai_flask_app/logs/a_error.log", - "lines": 50 - } - ] - }, - "Demo3": { - "commands": [ - { - "comm": "top -b -n 1", - "tag": "top" - } - ], - "logs": [] - }, - "Demo": { - "commands": [ - { - "comm": "top -b -n 1", - "tag": "top" - } - ], - "logs": [ - { - "log_file": "/home/arehman/Documents/Projects/Python/Maxapex/ticket_ai_flask_app/logs/a_error.log", - "lines": 50 - }, - { - "log_file": "/home/arehman/Documents/Projects/Python/Maxapex/ticket_ai_flask_app/logs/*_error.log", - "lines": 50 - } - ] - } -} \ No newline at end of file