Docker deployment Realised

This commit is contained in:
Josako
2024-06-26 12:02:14 +02:00
parent 348bdf2c15
commit 7a1b51dd0c
2494 changed files with 7329 additions and 172135 deletions

View File

@@ -2,8 +2,7 @@
# Usage: ./db_heads.sh -d migrations/public
cd "/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/" || exit 1
source "/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/.venv/bin/activate"
cd "/app/" || exit 1
while getopts d: flag
do
@@ -25,8 +24,9 @@ if [ -z "$directory" ]; then
fi
# Set FLASK_APP environment variable
export FLASK_APP=scripts/run_eveai_app.py # Modify if your Flask app is initiated differently
export PYTHONPATH="$PYTHONPATH:/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/"
PROJECT_DIR="/app"
export FLASK_APP=${PROJECT_DIR}/scripts/run_eveai_app.py # Adjust the path to your Flask app entry point
export PYTHONPATH="$PYTHONPATH:$PROJECT_DIR" # Include the app directory in the Python path
# Run the Flask migration command
flask db heads -d "$directory"

View File

@@ -0,0 +1,87 @@
from sqlalchemy.exc import IntegrityError
from datetime import datetime as dt, timezone as tz
from flask_security import hash_password
from uuid import uuid4
from eveai_app import create_app
from common.extensions import db, security
from common.models.user import User, Tenant, Role, RolesUsers
from common.utils.database import Database
def initialize_data():
app = create_app()
with app.app_context():
# Define Initial Tenant
if Tenant.query.first() is None:
print("No tenants found. Creating initial tenant...")
timestamp = dt.now(tz=tz.utc)
tenant = Tenant(name="Jedi",
website="https://askeveai.com",
created_at=timestamp,
updated_at=timestamp)
db.session.add(tenant)
db.session.commit()
else:
print("Tenants already exist. Skipping tenant creation.")
Database(tenant.id).create_tenant_schema()
# Define Roles
super_user_role = Role.query.filter_by(name="Super User").first()
if super_user_role is None:
super_user_role = Role(name="Super User", description="Users allowed to perform all functions")
db.session.add(super_user_role)
db.session.commit()
tenant_admin_role = Role.query.filter_by(name="Tenant Admin").first()
if tenant_admin_role is None:
tenant_admin_role = Role(name="Tenant Admin", description="Users allowed to manage tenants")
db.session.add(tenant_admin_role)
db.session.commit()
tenant_tester_role = Role.query.filter_by(name="Tenant Tester").first()
if tenant_tester_role is None:
tenant_test_role = Role(name="Tenant Tester", description="Users allowed to test tenants")
db.session.add(tenant_test_role)
db.session.commit()
# Check if any users exist
if User.query.first() is None:
print("No users found. Creating initial user...")
# Ensure tenant exists before creating the user
tenant = Tenant.query.filter_by(name="Jedi").first()
if tenant:
user = User(
user_name="yoda",
email="yoda@flow-it.net",
password=hash_password("Dagobah"),
first_name="Yoda",
last_name="Skywalker",
tenant_id=tenant.id,
created_at=dt.now(tz=tz.utc),
updated_at=dt.now(tz=tz.utc),
fs_uniquifier=str(uuid4()),
active=True,
confirmed_at=dt.now(tz=tz.utc)
)
db.session.add(user)
db.session.commit()
# security.datastore.set_uniquifier()
print("Initial user created.")
# Assign SuperUser role to the new user
user_role = RolesUsers(user_id=user.id, role_id=super_user_role.id)
db.session.add(user_role)
db.session.commit()
print("SuperUser role assigned to the new user.")
else:
print("Failed to find initial tenant for user creation.")
else:
print("Users already exist. Skipping user creation.")
if __name__ == "__main__":
try:
initialize_data()
except IntegrityError:
print("Error: Integrity constraint violation. Initial data already exists.")
except Exception as e:
print(f"An error occurred during initialization: {e}")

View File

@@ -1,15 +1,46 @@
#!/usr/bin/env bash
#!/bin/bash
cd "/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/" || exit 1
source "/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/.venv/bin/activate"
cd "/app" || exit 1
export PYTHONPATH="$PYTHONPATH:/app/"
export PYTHONPATH="$PYTHONPATH:/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/"
# Wait for the database to be ready
echo "Waiting for database to be ready"
until pg_isready -h db -p 5432; do
echo "Postgres is unavailable - sleeping"
sleep 2
done
echo "Postgres is up - executing commands"
export PGPASSWORD=Skywalker!
# Check if the database exists and initialize if not
if ! psql -U luke -h db -d eveai -c '\dt' | grep -q 'No relations found'; then
echo "Database eveai does not exist or is empty. Initializing..."
psql -U luke -h db -d postgres -c "CREATE DATABASE eveai;"
psql -U luke -h db -d eveai -c "CREATE EXTENSION IF NOT EXISTS vector;"
fi
echo "Applying migrations to the public and tenant schema..."
# Set FLASK_APP environment variables
PROJECT_DIR="/app"
export FLASK_APP=${PROJECT_DIR}/scripts/run_eveai_app.py # Adjust the path to your Flask app entry point
export PYTHONPATH="$PYTHONPATH:$PROJECT_DIR" # Include the app directory in the Python path
# Run Alembic upgrade for the public schema
echo "Applying migrations to the public schema..."
flask db upgrade -d "${PROJECT_DIR}/migrations/public"
# Run Alembic upgrade for the tenant schema
echo "Applying migrations to the tenant schema..."
flask db upgrade -d "${PROJECT_DIR}/migrations/tenant"
# Set flask environment variables
export FLASK_ENV=development # Use 'production' as appropriate
export FLASK_DEBUG=1 # Use 0 for production
#export FLASK_ENV=development # Use 'production' as appropriate
#export FLASK_DEBUG=1 # Use 0 for production
# Initialize initial data (tenant and user)
echo "Initializing initial tenant and user..."
python ${PROJECT_DIR}/scripts/initialize_data.py # Adjust the path to your initialization script
# Start Flask app
gunicorn -w 4 -k gevent -b 0.0.0.0:5001 scripts.run_eveai_app:app
deactivate

View File

@@ -1,15 +1,11 @@
#!/usr/bin/env bash
#!/bin/bash
cd "/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/" || exit 1
source "/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/.venv/bin/activate"
export PYTHONPATH="$PYTHONPATH:/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/"
cd "/app/" || exit 1
export PYTHONPATH="$PYTHONPATH:/app/"
# Set flask environment variables
export FLASK_ENV=development # Use 'production' as appropriate
export FLASK_DEBUG=1 # Use 0 for production
#export FLASK_ENV=development # Use 'production' as appropriate
#export FLASK_DEBUG=1 # Use 0 for production
# Start Flask app
gunicorn -w 4 -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -b 0.0.0.0:5002 scripts.run_eveai_chat:app
deactivate

View File

@@ -1,13 +1,10 @@
#!/usr/bin/env bash
cd "/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/" || exit 1
source "/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/.venv/bin/activate"
export PYTHONPATH="$PYTHONPATH:/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/"
cd "/app/" || exit 1
export PYTHONPATH="$PYTHONPATH:/app/"
# Start a worker for the 'llm_interactions' queue with auto-scaling
celery -A eveai_chat_workers.celery worker --loglevel=info -Q llm_interactions --autoscale=2,8 --hostname=interactions_worker@%h &
# Wait for all background processes to finish
wait
deactivate

View File

@@ -1,15 +1,13 @@
#!/usr/bin/env bash
#!/bin/bash
cd "/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/" || exit 1
source "/Volumes/OWC4M2_1/Dropbox/Josako's Dev/Josako/EveAI/Development/eveAI/.venv/bin/activate"
cd "/app/" || exit 1
export PYTHONPATH="$PYTHONPATH:/app/"
# Start a worker for the 'embeddings' queue with higher concurrency
celery -A eveai_workers.celery worker --loglevel=info -Q embeddings --autoscale=1,4 --hostname=embeddings_worker@%h &
celery -A eveai_workers.celery worker --loglevel=info -Q embeddings --autoscale=2,8 --hostname=embeddings_worker@%h &
# Start a worker for the 'llm_interactions' queue with auto-scaling - not necessary, in eveai_chat_workers
# celery -A eveai_workers.celery worker --loglevel=info - Q llm_interactions --autoscale=2,8 --hostname=interactions_worker@%h &
# Wait for all background processes to finish
wait
deactivate