updated prod deployment to gunicorn

This commit is contained in:
Adrian Gunnar Lauterer 2024-05-31 18:06:33 +02:00
parent 6e61d54c68
commit 759791c39e
4 changed files with 68 additions and 38 deletions

View File

@ -10,20 +10,34 @@
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;
dependencies = [
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.buildPythonApplication 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 = dependencies;
src = ./src; src = ./src;
doCheck = false; doCheck = false;
postInstall = '' postInstall = ''
mkdir -p $out/share/ozai-webui install -Dm444 ${src}/static/* $out/share/ozai-webui/static/
cp -r static $out/share/ozai-webui/static install -Dm444 ${src}/templates/* $out/share/ozai-webui/templates/
cp -r templates $out/share/ozai-webui/templates install -Dm755 ${src}/main.py $out/bin/main.py
mv $out/bin/main.py $out/bin/ozai-webui install -Dm755 ${src}/run.sh $out/bin/ozai-webui
'';
};
devShells.x86_64-linux.default = pkgs.mkShell {
buildInputs = dependencies;
shellHook = ''
''; '';
}; };
@ -45,21 +59,35 @@
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 {
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;
};
systemd.services.ozai-webui = { systemd.services.ozai-webui = {
description = "Ozai WebUI server"; description = "Ozai WebUI server";
after = [ "network.target" ]; after = [ "network.target" ];
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
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.default}/bin/ozai-webui";
Restart = "always"; Restart = "always";
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}"
];
User = "ozai-webui";
Group = "ozai-webui";
AmbientCapabilities = "CAP_NET_BIND_SERVICE";
NoNewPrivileges = true;
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
PrivateDevices = true;
}; };
}; };
}; };

1
result Symbolic link
View File

@ -0,0 +1 @@
/nix/store/c14ps8575ncarch0fvr36xm3v586qw5n-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,31 @@ 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 = 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') #parser.add_argument('-H', '--host', type=str, default=ozai_webui_host, help='The host to run the server on')
parser.add_argument('-P', '--port', type=int, default=ozai_webui_port, help='The port to run the server on') #parser.add_argument('-P', '--port', type=int, default=ozai_webui_port, help='The port to run the server on')
parser.add_argument('-O', '--ozai_url', type=str, default=ozai_url, help='The URL of the Ozai server') #parser.add_argument('-O', '--ozai_url', type=str, default=ozai_url, help='The URL of the Ozai server')
parser.add_argument('-S', '--secret_key', type=str, default=app.config['SECRET_KEY'], help='The secret key for the Flask app') #parser.add_argument('-S', '--secret_key', type=str, default=app.config['SECRET_KEY'], help='The secret key for the Flask app')
parser.add_argument('--static_folder', type=str, default=static_folder, help='The location of the static folder') #parser.add_argument('--static_folder', type=str, default=static_folder, help='The location of the static folder')
parser.add_argument('--template_folder', type=str, default=template_folder, help='The location of the template folder') #parser.add_argument('--template_folder', type=str, default=template_folder, help='The location of the template folder')
#
#
args = parser.parse_args() #args = parser.parse_args()
#
if args.host: #if args.host:
ozai_webui_host = args.host # ozai_webui_host = args.host
if args.port: #if args.port:
ozai_webui_port = args.port # ozai_webui_port = args.port
if args.ozai_url: #if args.ozai_url:
ozai_url = args.ozai_url # ozai_url = args.ozai_url
if args.secret_key: #if args.secret_key:
app.config['SECRET_KEY'] = args.secret_key # app.config['SECRET_KEY'] = args.secret_key
if args.static_folder: #if args.static_folder:
app.static_folder = args.static_folder # app.static_folder = args.static_folder
if args.template_folder: #if args.template_folder:
app.template_folder = args.template_folder # app.template_folder = args.template_folder
#
#home page #home page
@app.route('/') @app.route('/')

1
src/run.sh Normal file
View File

@ -0,0 +1 @@
gunicorn --worker-class eventlet -w 1 main:app