updated prod deployment to gunicorn

This commit is contained in:
Adrian Gunnar Lauterer 2024-05-31 18:06:33 +02:00
parent 6e61d54c68
commit 8606c6c392
7 changed files with 133 additions and 40 deletions

View File

@ -10,20 +10,73 @@
pkgs = import nixpkgs { system = "x86_64-linux"; }; pkgs = import nixpkgs { system = "x86_64-linux"; };
lib = pkgs.lib; lib = pkgs.lib;
types = lib.types; types = lib.types;
deps = [
pkgs.python3
pkgs.python3Packages.flask
pkgs.python3Packages.flask-socketio
pkgs.python3Packages.requests
pkgs.python3Packages.gunicorn
pkgs.python3Packages.eventlet
];
in in
{ {
packages.x86_64-linux.default = pkgs.python3Packages.buildPythonApplication rec { packages.x86_64-linux.default = pkgs.python3Packages.buildPythonPackage rec {
pname = "ozai-webui"; pname = "ozai-webui";
version = "0.1.1"; version = "0.1.1";
propagatedBuildInputs = [ pkgs.python3Packages.flask pkgs.python3Packages.flask-socketio pkgs.python3Packages.requests ]; #propagatedBuildInputs = deps;
dependencies = deps;
src = ./src; src = ./src;
doCheck = false; doCheck = false;
postInstall = '' postInstall = ''
mkdir -p $out/share/ozai-webui mkdir -p $out/share/ozai_webui/static
cp -r static $out/share/ozai-webui/static mkdir -p $out/share/ozai_webui/templates
cp -r templates $out/share/ozai-webui/templates install -Dm444 ${src}/static/* $out/share/ozai_webui/static/
mv $out/bin/main.py $out/bin/ozai-webui install -Dm444 ${src}/templates/* $out/share/ozai_webui/templates/
'';
};
packages.x86_64-linux.ozai-webui-run = pkgs.stdenv.mkDerivation rec {
pname = "ozai-webui-run";
version = "0.1.1";
src = ./src;
buildInputs = [
pkgs.bash
self.packages.x86_64-linux.default
pkgs.bash
pkgs.python3
pkgs.python3Packages.flask
pkgs.python3Packages.flask-socketio
pkgs.python3Packages.requests
pkgs.python3Packages.gunicorn
pkgs.python3Packages.eventlet
];
#propagatedBuildInputs
nativeBuildInputs = [
pkgs.makeWrapper
pkgs.python3
pkgs.python3Packages.flask
pkgs.python3Packages.flask-socketio
pkgs.python3Packages.requests
pkgs.python3Packages.gunicorn
pkgs.python3Packages.eventlet
];
installPhase = ''
mkdir -p $out/bin
find .
cp $src/run.sh $out/bin/run
chmod +x $out/bin/run
wrapProgram $out/bin/run \
--prefix PATH : ${lib.makeBinPath [ pkgs.bash pkgs.python3 pkgs.python3Packages.gunicorn ]}
'';
};
devShells.x86_64-linux.default = pkgs.mkShell {
buildInputs = deps;
shellHook = ''
OZAI_WEBUI_STATIC_FOLDER=result/share/ozai-webui/static
OZAI_WEBUI_TEMPLATE_FOLDER=result/share/ozai-webui/templates
''; '';
}; };
@ -45,11 +98,6 @@
default = "http://localhost:8000/api/"; default = "http://localhost:8000/api/";
description = "The URL of the Ozai server"; description = "The URL of the Ozai server";
}; };
secretKey = lib.mkOption {
type = types.str;
default = "secret_key";
description = "The secret key for the Flask app";
};
}; };
config = lib.mkIf config.services.ozai-webui.enable { config = lib.mkIf config.services.ozai-webui.enable {
@ -57,8 +105,16 @@
description = "Ozai WebUI server"; description = "Ozai WebUI server";
after = [ "network.target" ]; after = [ "network.target" ];
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
environment = {
OZAI_URL= "${config.services.ozai-webui.ozaiUrl}";
OZAI_WEBUI_HOST= "${config.services.ozai-webui.host}";
OZAI_WEBUI_PORT= toString(config.services.ozai-webui.port);
OZAI_WEBUI_STATIC_FOLDER= "${self.packages.x86_64-linux.default}/share/ozai-webui/static";
OZAI_WEBUI_TEMPLATE_FOLDER="${self.packages.x86_64-linux.default}/share/ozai-webui/templates";
};
serviceConfig = { serviceConfig = {
ExecStart = "${self.packages.x86_64-linux.default}/bin/ozai-webui --port ${toString config.services.ozai-webui.port} --host '${config.services.ozai-webui.host}' --ozai_url '${config.services.ozai-webui.ozaiUrl}' --secret_key '${config.services.ozai-webui.secretKey}' --static_folder '${self.packages.x86_64-linux.default}/share/ozai-webui/static' --template_folder '${self.packages.x86_64-linux.default}/share/ozai-webui/templates'"; ExecStart = "${self.packages.x86_64-linux.ozai-webui-run}/bin/run ${config.services.ozai-webui.host} ${toString(config.services.ozai-webui.port)} ${self.packages.x86_64-linux.default}/bin/ozai_webui";
Restart = "always"; Restart = "always";
}; };
}; };

1
result Symbolic link
View File

@ -0,0 +1 @@
/nix/store/mfa98r1faj74qf8c4nnz7ailnad7m0fr-ozai-webui-run-0.1.1

1
result-1 Symbolic link
View File

@ -0,0 +1 @@
/nix/store/ppbdq7fcx3111dis424vlf1lz20x8hvv-python3.11-ozai-webui-0.1.1

View File

@ -2,7 +2,7 @@
from flask import Flask, render_template, request from flask import Flask, render_template, request
from flask_socketio import SocketIO, join_room from flask_socketio import SocketIO, join_room
import requests import requests
import argparse #import argparse
import os import os
app = Flask(__name__) app = Flask(__name__)
@ -29,31 +29,39 @@ if os.getenv('OZAI_WEBUI_STATIC_FOLDER') is not None:
if os.getenv('OZAI_WEBUI_TEMPLATE_FOLDER') is not None: if os.getenv('OZAI_WEBUI_TEMPLATE_FOLDER') is not None:
template_folder = os.getenv('TEMPLATE_FOLDER') template_folder = os.getenv('TEMPLATE_FOLDER')
parser = argparse.ArgumentParser(description="Run the Ozai WebUI server")
parser.add_argument('-H', '--host', type=str, default=ozai_webui_host, help='The host to run the server on') print(f"using host { ozai_webui_host }")
parser.add_argument('-P', '--port', type=int, default=ozai_webui_port, help='The port to run the server on') print(f"using port { ozai_webui_port }")
parser.add_argument('-O', '--ozai_url', type=str, default=ozai_url, help='The URL of the Ozai server') print(f"using ozai url { ozai_url }")
parser.add_argument('-S', '--secret_key', type=str, default=app.config['SECRET_KEY'], help='The secret key for the Flask app') print(f"using template folder { template_folder }")
parser.add_argument('--static_folder', type=str, default=static_folder, help='The location of the static folder') print(f"using static folder { static_folder }")
parser.add_argument('--template_folder', type=str, default=template_folder, help='The location of the template folder')
args = parser.parse_args() #parser = argparse.ArgumentParser(description="Run the Ozai WebUI server")
#
if args.host: #parser.add_argument('-H', '--host', type=str, default=ozai_webui_host, help='The host to run the server on')
ozai_webui_host = args.host #parser.add_argument('-P', '--port', type=int, default=ozai_webui_port, help='The port to run the server on')
if args.port: #parser.add_argument('-O', '--ozai_url', type=str, default=ozai_url, help='The URL of the Ozai server')
ozai_webui_port = args.port #parser.add_argument('-S', '--secret_key', type=str, default=app.config['SECRET_KEY'], help='The secret key for the Flask app')
if args.ozai_url: #parser.add_argument('--static_folder', type=str, default=static_folder, help='The location of the static folder')
ozai_url = args.ozai_url #parser.add_argument('--template_folder', type=str, default=template_folder, help='The location of the template folder')
if args.secret_key: #
app.config['SECRET_KEY'] = args.secret_key #
if args.static_folder: #args = parser.parse_args()
app.static_folder = args.static_folder #
if args.template_folder: #if args.host:
app.template_folder = args.template_folder # ozai_webui_host = args.host
#if args.port:
# ozai_webui_port = args.port
#if args.ozai_url:
# ozai_url = args.ozai_url
#if args.secret_key:
# app.config['SECRET_KEY'] = args.secret_key
#if args.static_folder:
# app.static_folder = args.static_folder
#if args.template_folder:
# app.template_folder = args.template_folder
#
#home page #home page
@app.route('/') @app.route('/')
@ -210,7 +218,9 @@ def ws_message(data):
#send message to the player that the move was invalid #send message to the player that the move was invalid
socketio.emit('move_status', response.text, room=game_id) socketio.emit('move_status', response.text, room=game_id)
def main ():
if __name__ == '__main__':
# app.run(debug=True, host='0.0.0.0', port=5000,ssl_context='adhoc') # app.run(debug=True, host='0.0.0.0', port=5000,ssl_context='adhoc')
socketio.run(app, debug=False, host=ozai_webui_host, port=ozai_webui_port, allow_unsafe_werkzeug=True) socketio.run(app, debug=False, host=ozai_webui_host, port=ozai_webui_port, allow_unsafe_werkzeug=True)
if __name__ == '__main__':
main()

1
src/result Symbolic link
View File

@ -0,0 +1 @@
/nix/store/ldv1rcfs9phhh7hh2wzcvn2600dvgw4y-python3.11-ozai-webui-0.1.1

15
src/run.sh Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
# Default values for host, port, and package path
DEFAULT_HOST="0.0.0.0"
DEFAULT_PORT="8000"
DEFAULT_PACKAGE_PATH="ozai_webui"
# Get the host, port, and package path from command line arguments, or use defaults
HOST="${1:-$DEFAULT_HOST}"
PORT="${2:-$DEFAULT_PORT}"
PACKAGE_PATH="${3:-$DEFAULT_PACKAGE_PATH}"
# Run Gunicorn with the specified host, port, and package path
gunicorn -w 1 --chdir "$PACKAGE_PATH" --bind "$HOST:$PORT" __init__:main

View File

@ -6,6 +6,8 @@ requires = (
"flask", "flask",
"flask-socketio", "flask-socketio",
"requests", "requests",
"gunicorn",
"eventlet",
) )
@ -17,9 +19,16 @@ setup(
name='ozai-webui', name='ozai-webui',
version='0.1.1', version='0.1.1',
author = "Adrian Gunnar Lauterer", author = "Adrian Gunnar Lauterer",
packages=find_packages(), packages=find_packages("."),
scripts=["main.py"], #scripts=["ozai_webui.py"],
#scripts={"ozai_webui:main": "ozai_webui"},
entry_points = {
"console_scripts": [
"ozai_webui = ozai_webui:main"
]
},
install_requires=requires, install_requires=requires,
#package_dir= {'':'src'},
package_data={ package_data={
'': ['templates/*.html', 'static/*.*'], '': ['templates/*.html', 'static/*.*'],
}, },