From 3b14fbf0b3eb1bb56c76be592f01871f13697bdc Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 23 Feb 2024 15:29:26 +0500 Subject: [PATCH] first update --- .gitignore | 3 ++ Dockerfile | 29 ---------------- docker-compose.yml | 10 ------ install.sh | 82 +++++++--------------------------------------- logs/a_error.log | 1 + logs/b_error.log | 1 + logs/error.log | 1 - readme.md | 31 +++--------------- requirements.txt | 12 ------- run.sh | 4 +++ src/app.py | 14 +++++--- src/config.json | 28 ++++++++++++++-- 12 files changed, 59 insertions(+), 157 deletions(-) create mode 100644 .gitignore delete mode 100644 Dockerfile delete mode 100644 docker-compose.yml create mode 100644 logs/a_error.log create mode 100644 logs/b_error.log delete mode 100644 logs/error.log delete mode 100644 requirements.txt create mode 100644 run.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..37a6672 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vscode/ +playground/ +venv/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index c3a0681..0000000 --- a/Dockerfile +++ /dev/null @@ -1,29 +0,0 @@ -# docker build --rm -t docker.io/mavenarh/ticket_ai:latest . - -# Use the official Python image as a base -FROM python:3.9-slim - -# Set environment variables -#ENV PYTHONDONTWRITEBYTECODE 1 -#ENV PYTHONUNBUFFERED 1 - -# Set the working directory in the container -WORKDIR /app - -# Copy the dependencies file to the working directory -#COPY requirements.txt . - -# Install any dependencies -#RUN pip install --no-cache-dir -r requirements.txt - -RUN pip install Flask -RUN pip install flask_jwt_extended - -# Copy the content of the local src directory to the working directory -COPY src/ . - -# Expose the port the app runs on -EXPOSE 5000 - -# Run the application -CMD ["python", "app.py"] diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index a388a62..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,10 +0,0 @@ -version: '3' - -services: - flask: - image: ticket_ai docker.io/mavenarh/ticket_ai:latest - ports: - - "5000:5000" - volumes: - - ./src/config.json:/app/config.json - - ./logs:/app/logs diff --git a/install.sh b/install.sh index cb818c4..d419579 100644 --- a/install.sh +++ b/install.sh @@ -1,73 +1,13 @@ #!/bin/bash -# Update System Packages -sudo yum update -y - -# Install Python3 and Pip -sudo yum install -y python3 python3-pip - -# Install Flask -pip3 install Flask - -# Create a directory for the Flask application -mkdir -p ~/flask_app -cd ~/flask_app - -# Create the Flask application file (app.py) -cat > app.py << 'EOF' -from flask import Flask, request, jsonify -import json -import subprocess - -app = Flask(__name__) - -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('/get_logs', methods=['GET']) -def get_logs(): - issue_type = request.args.get('issue_type') - response = {} - try: - with open('config.json') as config_file: - config = json.load(config_file) - if issue_type in config: - issue_config = config[issue_type] - for log in issue_config['logs']: - log_output = read_log(log['log_file'], log['lines']) - response[log['log_file']] = log_output - if issue_config.get('include_top'): - response['top'] = read_top() - else: - response = {"error": "Invalid issue type"} - except Exception as e: - response = {"error": str(e)} - return jsonify(response) - -if __name__ == '__main__': - app.run(debug=True, host='0.0.0.0') -EOF - -# Create the configuration file (config.json) -cat > config.json << 'EOF' -{ - "database_issue": { - "include_top": true, - "logs": [ - { "log_file": "/var/log/db.log", "lines": 50 }, - { "log_file": "/var/log/db_error.log", "lines": 30 } - ] - }, - "network_issue": { - "include_top": false, - "logs": [ - { "log_file": "/var/log/network.log", "lines": 100 } - ] - } -} -EOF - -echo "Installation complete. Please modify config.json as needed." +if ! command -v python3 &> /dev/null +then + echo "Error: python3 could not be found" + exit +else + python3 -m venv venv + source venv/bin/activate + pip install Flask + pip install flask_jwt_extended + chmod +x run.sh +fi diff --git a/logs/a_error.log b/logs/a_error.log new file mode 100644 index 0000000..37e6b4f --- /dev/null +++ b/logs/a_error.log @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000..f2917c1 --- /dev/null +++ b/logs/b_error.log @@ -0,0 +1 @@ +this is a error log from file 'B' \ No newline at end of file diff --git a/logs/error.log b/logs/error.log deleted file mode 100644 index dec0b63..0000000 --- a/logs/error.log +++ /dev/null @@ -1 +0,0 @@ -this is a error log \ No newline at end of file diff --git a/readme.md b/readme.md index 96ab048..7520a42 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ pip install flask_jwt_extended python app.py # call api -http://127.0.0.1:5000/get_logs?issue_type=database_issue +http://127.0.0.1:5000/get_logs?issue_type=Demo # sample response { @@ -16,30 +16,7 @@ http://127.0.0.1:5000/get_logs?issue_type=database_issue } # run installation script -chmod +x install_flask_app.sh -./install_flask_app.sh -cd ~/flask_app -python3 app.py +./install.sh -# install on centos -yum install python36-devel -yum install python36-setuptools -yum install python36-virtualenv - -python3 -m pip install --upgrade pip - -python3 -m virtualenv env - -pip install Flask -pip install flask_jwt_extended - -# usage -curl http://127.0.0.1:5000/get_logs?issue_type=Demo - -# run docker image -docker run -d \ - -p 5000:5000 \ - -v $(pwd)/src/config.json:/app/config.json \ - -v $(pwd)/logs:/app/logs \ - --name ticket_ai \ - docker.io/mavenarh/ticket_ai:latest +# run application +./run.sh diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 6ff241a..0000000 --- a/requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -click==8.0.4 -dataclasses==0.8 -Flask==2.0.3 -Flask-JWT-Extended==4.4.2 -importlib-metadata==4.8.3 -itsdangerous==2.0.1 -Jinja2==3.0.3 -MarkupSafe==2.0.1 -PyJWT==2.4.0 -typing_extensions==4.1.1 -Werkzeug==2.0.3 -zipp==3.6.0 diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..45260b1 --- /dev/null +++ b/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +source venv/bin/activate +python3 src/app.py diff --git a/src/app.py b/src/app.py index 175c9bc..563939d 100644 --- a/src/app.py +++ b/src/app.py @@ -78,7 +78,7 @@ def get_logs(): issue_type = request.args.get("issue_type") response = {} try: - with open('config.json') as config_file: + with open('./src/config.json') as config_file: config = json.load(config_file) if issue_type in config: issue_config = config[issue_type] @@ -86,17 +86,23 @@ def get_logs(): # logs for log in issue_config['logs']: + directory, file_name = split_directory_and_file(log.get('log_file')) + if '*' in log.get('log_file'): - directory, file_name = split_directory_and_file(log.get('log_file')) + # directory, file_name = split_directory_and_file(log.get('log_file')) log_output = read_log_from_dir(directory, file_name, log['lines']) else: log_output = read_log(log['log_file'], log['lines']) - response[log['log_file']] = log_output + # response[log['log_file']] = log_output + response[file_name] = log_output # commands for comm in issue_config['commands']: - response[comm.get('tag')] = subprocess.check_output([comm.get('comm')]).decode('utf-8') + # response[comm.get('tag')] = subprocess.check_output([comm.get('comm')]).decode('utf-8') + response[comm.get('tag')] = os.popen(comm.get('comm')).read() + # response[comm.get('tag')] = subprocess.run(comm.get('comm'), shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + # response[comm.get('tag')] = read_top() else: response = {"error": "Invalid issue type"} diff --git a/src/config.json b/src/config.json index a3d9c75..655ba61 100644 --- a/src/config.json +++ b/src/config.json @@ -40,23 +40,45 @@ { "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": [ - { "comm": "top", "tag": "top" } + ], "logs": [ - { "log_file": "/app/logs/*error_log", "lines": 50 } + { "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": "/app/logs/*error_log", "lines": 50 } + { "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 } ] } }