init commit

This commit is contained in:
t0is 2025-02-28 12:02:44 +01:00
commit 6f774c7ae0
5 changed files with 156 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.env
.venv
software
sys02.lib

36
Dockerfile Normal file
View File

@ -0,0 +1,36 @@
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Install git
RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
# Set work directory
WORKDIR /app
# Copy requirements and install Python packages
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Build-time arguments for git configuration and remote repository URL.
# These can be passed in from the .env file or as build arguments.
ARG GIT_USER
ARG GIT_EMAIL
ARG GIT_REMOTE
# Configure git with the provided user credentials
RUN git config --global user.name "${GIT_USER}" && \
git config --global user.email "${GIT_EMAIL}"
# Clone the repository using the provided remote URL (includes credentials if needed)
# This will clone the repo into the current working directory (/app)
RUN mkdir software
RUN git clone ${GIT_REMOTE} software
# Copy the rest of the application code into the container (if not already cloned)
COPY . .
# Expose port 5000 for the Flask app
EXPOSE 9776
# Start the Flask app
CMD ["gunicorn", "--bind", "0.0.0.0:9776", "app:app"]

61
app.py Normal file
View File

@ -0,0 +1,61 @@
import os
import shutil
from flask import Flask, request, render_template, jsonify
import subprocess
from dotenv import load_dotenv
# Load environment variables from .env (if available)
load_dotenv()
app = Flask(__name__, static_folder='.', template_folder='.')
# Define target directories (relative to the scripts folder)
TARGET_DIRS = [
os.path.join("software", "."),
os.path.join("software", "VAT_LATEST", "Data"),
os.path.join("software", "VAT_LATEST_DUREMOTE", "Data"),
os.path.join("software", "GENERAL8.0.5.6", "Data")
]
@app.route('/')
def index():
# Render the HTML file; ensure index.html is in the same folder as app.py
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload():
if 'file' not in request.files:
return jsonify({'error': 'No file provided'}), 400
file = request.files['file']
commit_msg = request.form.get('commit_msg', '')
# Check file name
if file.filename != "sys02.lib":
return jsonify({'error': 'Uploaded file must be named sys02.lib'}), 400
try:
# Read file content into memory so we can write to multiple locations
file_data = file.read()
for target in TARGET_DIRS:
# Ensure the target directory exists
os.makedirs(target, exist_ok=True)
filepath = os.path.join(target, file.filename)
with open(filepath, 'wb') as f:
f.write(file_data)
# Run git commands: stage changes, commit, and push.
# (Make sure that the working directory is already a git repository.)
os.chdir("software")
subprocess.check_call(["git", "add", "."])
subprocess.check_call(["git", "commit", "-m", commit_msg])
subprocess.check_call(["git", "push"])
return jsonify({'message': 'File uploaded and git commit/push successful.'})
except subprocess.CalledProcessError as e:
return jsonify({'error': 'Git command failed', 'details': str(e)}), 500
except Exception as e:
return jsonify({'error': 'An error occurred', 'details': str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5001, debug=True)

52
index.html Normal file
View File

@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>File Upload and Git Commit</title>
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 flex items-center justify-center min-h-screen">
<div class="bg-white p-8 rounded shadow-md w-full max-w-md">
<h1 class="text-2xl font-bold mb-6">Upload sys02.lib</h1>
<form id="uploadForm">
<div class="mb-4">
<label class="block text-gray-700">Select file (must be sys02.lib):</label>
<input type="file" name="file" class="mt-1 p-2 border border-gray-300 rounded w-full" required>
</div>
<div class="mb-4">
<label class="block text-gray-700">Commit Message (cislo noveho protokolu nebo jiny popis):</label>
<input type="text" name="commit_msg" class="mt-1 p-2 border border-gray-300 rounded w-full" placeholder="Enter commit message" required>
</div>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">Submit</button>
</form>
<div id="result" class="mt-4"></div>
</div>
<script>
$(document).ready(function () {
$('#uploadForm').on('submit', function (e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
url: '/upload',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (response) {
$('#result').html('<p class="text-green-600">' + response.message + '</p>');
},
error: function (xhr) {
var error = (xhr.responseJSON && xhr.responseJSON.error) ? xhr.responseJSON.error : 'An error occurred';
$('#result').html('<p class="text-red-600">' + error + '</p>');
}
});
});
});
</script>
</body>
</html>

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
Flask
python-dotenv
gunicorn