Compare commits

..

14 Commits

Author SHA1 Message Date
Saifeddine ALOUI
b9e23cd036 Rename pyproject.toml to setup.sh 2025-07-30 20:17:01 +02:00
Saifeddine ALOUI
57f24d7df6 Update pyproject.toml 2025-07-08 10:37:31 +02:00
Saifeddine ALOUI
23e52908a6 Update pyproject.toml 2025-07-08 09:44:39 +02:00
Saifeddine ALOUI
be3ba31d8f Update setup_service.sh 2025-07-08 09:43:50 +02:00
Saifeddine ALOUI
0177d25556 Update setup_service.sh 2025-07-08 09:40:49 +02:00
Saifeddine ALOUI
380691edbe Update setup_service.sh 2025-07-08 09:37:00 +02:00
Saifeddine ALOUI
bf0b4b5154 Update pyproject.toml 2025-07-08 09:36:44 +02:00
Saifeddine ALOUI
e139ee4d12 Update README.md 2025-06-25 12:03:08 +02:00
Saifeddine ALOUI
3f117c4274 Update setup_service.sh 2025-06-25 11:59:51 +02:00
Saifeddine ALOUI
57a22fa7a6 Update setup_service.sh 2025-06-25 11:58:18 +02:00
Saifeddine ALOUI
58ad66a6cb Update setup_service.sh 2025-06-25 11:49:55 +02:00
Saifeddine ALOUI
8bda441976 Update setup_service.sh 2025-06-25 11:36:01 +02:00
Saifeddine ALOUI
d46415c98e Update setup_service.sh 2025-06-25 11:32:36 +02:00
Saifeddine ALOUI
b210352741 Update setup_service.sh 2025-06-25 11:29:45 +02:00
4 changed files with 408 additions and 80 deletions

View File

@@ -192,14 +192,45 @@ The repository includes a script called `setup_service.sh` to set up Ollama Prox
sudo journalctl -u ollama-proxy-server -f
```
### Managing Users with `ops` Command
After setting up the service, you can add more users using the new `ops` command:
### 🔐 Managing Users with the `ops` Command
Once the `ops` CLI tool is installed, you can easily manage access by adding authorized users. Each user is stored in `/etc/ops/authorized_users.txt` in the format `username:password`.
#### Add a User with a Specific Password
To add a user with a password you choose:
```bash
sudo ops add_user username:password
```
#### 🎲 Add a User with a Random Password
If you omit the password, a secure 12-character password will be generated automatically:
```bash
sudo ops add_user username
```
Example output:
```
Generated password: x8D1qf7rZa2L
User 'username' added successfully with password 'x8D1qf7rZa2L'.
```
#### 🔐 Where Users Are Stored
All users are stored in:
```
/etc/ops/authorized_users.txt
```
The file is protected with appropriate permissions (`ops:ops`) to prevent unauthorized access.
## Contributing
Contributions are welcome! Please follow these steps:

View File

@@ -1,43 +0,0 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "ollama_proxy_server"
version = "7.1.0"
description = "A fastapi server for petals decentralized text generation"
readme = { file = "README.md", content-type = "text/markdown" }
authors = [
{ name = "ParisNeo", email = "parisneoai@gmail.com" },
]
dependencies = [
"ascii-colors>=0.11.3",
"certifi==2024.7.4",
"charset-normalizer==3.3.2",
"configparser==6.0.1",
"idna==3.6",
"queues==0.6.3",
"requests==2.31.0",
"urllib3==2.2.1"
]
requires-python = ">=3.11"
keywords = ["fastapi", "petals"]
classifiers = [
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
]
[project.urls]
Homepage = "https://github.com/ParisNeo/ollama_proxy_server"
[tool.setuptools.package-data]
"*" = ["*"] # Include all package data
[project.scripts]
ollama_proxy_server = "ollama_proxy_server.main:main"
ollama_proxy_add_user = "ollama_proxy_server.add_user:main"
[project.optional-dependencies]
dev = [
]

266
setup.sh Normal file
View File

@@ -0,0 +1,266 @@
#!/bin/bash
# Configuration with parameters
SERVICE_NAME="ollama-proxy-server"
USER="ops"
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <working_directory>"
exit 1
fi
WORKING_DIR=$1
LOG_DIR="$WORKING_DIR/logs"
SCRIPT_PATH="$WORKING_DIR/ollama-proxy-server/main.py"
CONFIG_FILE="/etc/ops/config.ini"
AUTHORIZED_USERS_FILE="/etc/ops/authorized_users.txt"
# Default port and log path; these can be customized by the user
DEFAULT_PORT=11534
DEFAULT_LOG_PATH="$LOG_DIR/server.log"
echo "Setting up Ollama Proxy Server..."
# Create dedicated user if it doesn't exist already
if ! id "$USER" &>/dev/null; then
echo "Creating user $USER..."
sudo useradd -r -s /bin/false "$USER"
fi
# Ensure the working directory is writable by the dedicated user
mkdir -p "$WORKING_DIR"
cp -r * "$WORKING_DIR/"
# Set permissions for logs and reports directories
echo "Setting up directories and files..."
mkdir -p "$LOG_DIR"
mkdir -p "$WORKING_DIR/reports"
# Create systemd service file
echo "Creating systemd service..."
read -p "Enter the port number (default: $DEFAULT_PORT): " PORT
PORT=${PORT:-$DEFAULT_PORT}
read -p "Enter the log path (default: $DEFAULT_LOG_PATH): " LOG_PATH
LOG_PATH=${LOG_PATH:-$DEFAULT_LOG_PATH}
sudo tee /etc/systemd/system/$SERVICE_NAME.service > /dev/null << EOF
[Unit]
Description=Ollama Proxy Server
After=network.target
Wants=network.target
[Service]
Type=simple
User=$USER
Group=$USER
WorkingDirectory=$WORKING_DIR
ExecStart=/bin/bash $WORKING_DIR/run.sh --log_path $LOG_PATH --port $PORT --config $CONFIG_FILE --users_list $AUTHORIZED_USERS_FILE
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
# Environment
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Environment=PYTHONUNBUFFERED=1
# Security settings
NoNewPrivileges=true
PrivateTmp=true
ProtectHome=true
ProtectSystem=strict
ReadWritePaths=$WORKING_DIR $LOG_DIR
[Install]
WantedBy=multi-user.target
EOF
# Install Python dependencies with proper permissions and environment variables preserved
echo "Installing Python dependencies..."
python3 -m venv $WORKING_DIR/venv
# Activate the virtual environment and install dependencies as user without --user flag
echo "Activating virtualenv and installing Python packages..."
source $WORKING_DIR/venv/bin/activate && pip install --no-cache-dir $WORKING_DIR
# Create logrotate config
echo "Setting up log rotation..."
sudo tee /etc/logrotate.d/$SERVICE_NAME > /dev/null << EOF
$LOG_DIR/*.log {
daily
rotate 15
compress
delaycompress
missingok
notifempty
create 644 $USER $USER
postrotate
systemctl reload-or-restart $SERVICE_NAME
endscript
}
EOF
# Create and populate config.ini and authorized_users.txt files
echo "Creating configuration files..."
sudo mkdir -p /etc/ops
sudo tee $CONFIG_FILE > /dev/null << EOF
[DefaultServer]
url = http://localhost:11434
max_parallel_connections = 4
queue_size = 100
# Additional servers can be added here with similar format
EOF
echo "Adding authorized users to the list. Type 'done' when finished."
while true; do
read -p "Enter user:password or type 'done': " input
if [ "$input" == "done" ]; then
break
fi
echo "$input" | sudo tee -a $AUTHORIZED_USERS_FILE > /dev/null
done
echo "You can add more users to the authorized_users.txt file if needed."
# Create ops command script
echo "Creating 'ops' command..."
sudo cat << 'EOF' | sudo tee /usr/local/bin/ops > /dev/null
#!/bin/bash
# Define usage function to display help message
usage() {
echo "Usage: $0 [help | add_user username[:password] | add_server server_name url [max_parallel_connections queue_size] | edit_server server_name parameter value | list_servers]"
exit 1
}
if [ "$#" -lt 1 ]; then
usage
fi
COMMAND="$1"
shift
case $COMMAND in
help)
echo "ops command usage:"
echo ""
echo " ops help Display this help message"
echo " ops add_user username[:password] Add a user with optional password generation"
echo " ops add_server name url [max queue] Add a new server configuration"
echo " ops edit_server name param value Edit a server's parameter"
echo " ops list_servers List all configured servers"
;;
add_user)
USER_PAIR="$1"
IFS=':' read -r USER PASSWORD <<< "$USER_PAIR"
if [ -z "$PASSWORD" ]; then
PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 12)
echo "Generated password: $PASSWORD"
fi
AUTHORIZED_USERS_FILE="/etc/ops/authorized_users.txt"
sudo mkdir -p /etc/ops
[ ! -f "$AUTHORIZED_USERS_FILE" ] && sudo touch "$AUTHORIZED_USERS_FILE"
echo "$USER:$PASSWORD" | sudo tee -a "$AUTHORIZED_USERS_FILE" > /dev/null
sudo chown ops:ops "$AUTHORIZED_USERS_FILE"
echo "User '$USER' added successfully with password '$PASSWORD'."
;;
add_server)
[ "$#" -lt 2 ] && usage
SERVER_NAME="$1"
URL="$2"
MAX_PARALLEL_CONNECTIONS="${3:-4}"
QUEUE_SIZE="${4:-100}"
CONFIG_FILE="/etc/ops/config.ini"
{
echo "[$SERVER_NAME]"
echo "url = $URL"
echo "max_parallel_connections = $MAX_PARALLEL_CONNECTIONS"
echo "queue_size = $QUEUE_SIZE"
} | sudo tee -a "$CONFIG_FILE" > /dev/null
echo "Server '$SERVER_NAME' added successfully."
;;
edit_server)
[ "$#" -ne 3 ] && usage
SERVER_NAME="$1"
PARAMETER="$2"
VALUE="$3"
CONFIG_FILE="/etc/ops/config.ini"
TMP_FILE=$(mktemp)
sudo awk -v section="[$SERVER_NAME]" -v param="$PARAMETER" -v value="$VALUE" '
$0 == section { in_section = 1; print; next }
/^\[.*\]/ { in_section = 0 }
in_section && $0 ~ "^" param "[[:space:]]*=" {
print param " = " value
next
}
{ print }
' "$CONFIG_FILE" > "$TMP_FILE" && sudo mv "$TMP_FILE" "$CONFIG_FILE"
echo "Parameter '$PARAMETER' for server '$SERVER_NAME' updated to '$VALUE'."
;;
list_servers)
CONFIG_FILE="/etc/ops/config.ini"
echo "Listing servers and their configuration:"
sudo awk '
/^\[.*\]/ {
if (in_section) print "";
print substr($0, 2, length($0) - 2) ":";
in_section = 1;
next
}
NF && in_section {
print " " $0
}
' "$CONFIG_FILE"
;;
*)
usage
;;
esac
EOF
# Make ops command executable
sudo chmod +x /usr/local/bin/ops
sudo chown -R "$USER:$USER" "$LOG_DIR"
sudo chown -R "$USER:$USER" "$WORKING_DIR"
sudo chown -R "$USER:$USER" $WORKING_DIR/venv
sudo chown $USER:$USER $CONFIG_FILE
sudo chown $USER:$USER $AUTHORIZED_USERS_FILE
# Reload systemd and enable service
echo "Enabling service..."
sudo systemctl daemon-reload
sudo systemctl enable "$SERVICE_NAME"
echo "Service setup complete!"
echo ""
echo "Commands:"
echo " Start: sudo systemctl start $SERVICE_NAME"
echo " Stop: sudo systemctl stop $SERVICE_NAME"
echo " Status: sudo journalctl -u $SERVICE_NAME -f"
echo " Logs: sudo journalctl -u $SERVICE_NAME -f"
echo " Reports: ls $WORKING_DIR/reports/"
echo ""
echo "How to use the new 'ops' command:"
echo " To display help, run: ops help"
echo " To add a user, run: ops add_user username[:password]"
echo " To add a server, run: ops add_server server_name url [max_parallel_connections queue_size]"
echo " To edit a server setting, run: ops edit_server server_name parameter value"
echo " To list all servers and their parameters, run: ops list_servers"

View File

@@ -28,15 +28,13 @@ if ! id "$USER" &>/dev/null; then
fi
# Ensure the working directory is writable by the dedicated user
sudo mkdir -p "$WORKING_DIR"
sudo cp -r * "$WORKING_DIR/"
sudo chown -R "$USER:$USER" "$WORKING_DIR"
mkdir -p "$WORKING_DIR"
cp -r * "$WORKING_DIR/"
# Set permissions for logs and reports directories
echo "Setting up directories and files..."
sudo mkdir -p "$LOG_DIR"
sudo mkdir -p "$WORKING_DIR/reports"
sudo chown -R "$USER:$USER" "$LOG_DIR"
mkdir -p "$LOG_DIR"
mkdir -p "$WORKING_DIR/reports"
# Create systemd service file
echo "Creating systemd service..."
@@ -81,14 +79,13 @@ EOF
# Install Python dependencies with proper permissions and environment variables preserved
echo "Installing Python dependencies..."
sudo -u "$USER" python3 -m venv $WORKING_DIR/venv
sudo chown -R "$USER:$USER" $WORKING_DIR/venv
python3 -m venv $WORKING_DIR/venv
# Activate the virtual environment and install dependencies as user without --user flag
echo "Activating virtualenv and installing Python packages..."
sudo -H -u "$USER" bash << EOF
source $WORKING_DIR/venv/bin/activate && pip install --no-cache-dir $WORKING_DIR
EOF
# Create logrotate config
echo "Setting up log rotation..."
@@ -113,8 +110,12 @@ sudo mkdir -p /etc/ops
sudo tee $CONFIG_FILE > /dev/null << EOF
[DefaultServer]
url = http://localhost:11434
max_parallel_connections = 4
queue_size = 100
# Additional servers can be added here with similar format
EOF
sudo chown $USER:$USER $CONFIG_FILE
echo "Adding authorized users to the list. Type 'done' when finished."
while true; do
@@ -123,56 +124,125 @@ while true; do
break
fi
echo "$input" | sudo tee -a $AUTHORIZED_USERS_FILE > /dev/null
sudo chown $USER:$USER $AUTHORIZED_USERS_FILE
done
echo "You can add more users to the authorized_users.txt file if needed."
# Create ops command script
echo "Creating 'ops' command..."
sudo tee /usr/local/bin/ops > /dev/null << 'EOF'
sudo cat << 'EOF' | sudo tee /usr/local/bin/ops > /dev/null
#!/bin/bash
# Define usage function to display help message
usage() {
echo "Usage: $0 add_user username:password"
echo "Usage: $0 [help | add_user username[:password] | add_server server_name url [max_parallel_connections queue_size] | edit_server server_name parameter value | list_servers]"
exit 1
}
# Check if exactly one argument is provided and it's 'add_user'
if [ "$#" -ne 2 ] || [ "$1" != "add_user" ]; then
if [ "$#" -lt 1 ]; then
usage
fi
USER_PAIR="$2"
COMMAND="$1"
shift
# Extract the user and password from the input
IFS=':' read -r USER PASSWORD <<< "$USER_PAIR"
if [ -z "$USER" ] || [ -z "$PASSWORD" ]; then
echo "Invalid username:password format."
usage
fi
case $COMMAND in
help)
echo "ops command usage:"
echo ""
echo " ops help Display this help message"
echo " ops add_user username[:password] Add a user with optional password generation"
echo " ops add_server name url [max queue] Add a new server configuration"
echo " ops edit_server name param value Edit a server's parameter"
echo " ops list_servers List all configured servers"
;;
add_user)
USER_PAIR="$1"
IFS=':' read -r USER PASSWORD <<< "$USER_PAIR"
AUTHORIZED_USERS_FILE="/etc/ops/authorized_users.txt"
if [ -z "$PASSWORD" ]; then
PASSWORD=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 12)
echo "Generated password: $PASSWORD"
fi
# Check if the authorized_users file exists, create it otherwise
sudo mkdir -p /etc/ops
if [ ! -f "$AUTHORIZED_USERS_FILE" ]; then
sudo touch $AUTHORIZED_USERS_FILE
fi
AUTHORIZED_USERS_FILE="/etc/ops/authorized_users.txt"
sudo mkdir -p /etc/ops
[ ! -f "$AUTHORIZED_USERS_FILE" ] && sudo touch "$AUTHORIZED_USERS_FILE"
# Append the new user:password pair to the file
echo "$USER:$PASSWORD" | sudo tee -a $AUTHORIZED_USERS_FILE > /dev/null
echo "$USER:$PASSWORD" | sudo tee -a "$AUTHORIZED_USERS_FILE" > /dev/null
sudo chown ops:ops "$AUTHORIZED_USERS_FILE"
echo "User '$USER' added successfully with password '$PASSWORD'."
;;
add_server)
[ "$#" -lt 2 ] && usage
# Ensure correct permissions for the file
sudo chown ops:ops $AUTHORIZED_USERS_FILE
SERVER_NAME="$1"
URL="$2"
MAX_PARALLEL_CONNECTIONS="${3:-4}"
QUEUE_SIZE="${4:-100}"
CONFIG_FILE="/etc/ops/config.ini"
echo "User '$USER' added successfully."
{
echo "[$SERVER_NAME]"
echo "url = $URL"
echo "max_parallel_connections = $MAX_PARALLEL_CONNECTIONS"
echo "queue_size = $QUEUE_SIZE"
} | sudo tee -a "$CONFIG_FILE" > /dev/null
echo "Server '$SERVER_NAME' added successfully."
;;
edit_server)
[ "$#" -ne 3 ] && usage
SERVER_NAME="$1"
PARAMETER="$2"
VALUE="$3"
CONFIG_FILE="/etc/ops/config.ini"
TMP_FILE=$(mktemp)
sudo awk -v section="[$SERVER_NAME]" -v param="$PARAMETER" -v value="$VALUE" '
$0 == section { in_section = 1; print; next }
/^\[.*\]/ { in_section = 0 }
in_section && $0 ~ "^" param "[[:space:]]*=" {
print param " = " value
next
}
{ print }
' "$CONFIG_FILE" > "$TMP_FILE" && sudo mv "$TMP_FILE" "$CONFIG_FILE"
echo "Parameter '$PARAMETER' for server '$SERVER_NAME' updated to '$VALUE'."
;;
list_servers)
CONFIG_FILE="/etc/ops/config.ini"
echo "Listing servers and their configuration:"
sudo awk '
/^\[.*\]/ {
if (in_section) print "";
print substr($0, 2, length($0) - 2) ":";
in_section = 1;
next
}
NF && in_section {
print " " $0
}
' "$CONFIG_FILE"
;;
*)
usage
;;
esac
EOF
# Make ops command executable
sudo chmod +x /usr/local/bin/ops
sudo chown -R "$USER:$USER" "$LOG_DIR"
sudo chown -R "$USER:$USER" "$WORKING_DIR"
sudo chown -R "$USER:$USER" $WORKING_DIR/venv
sudo chown $USER:$USER $CONFIG_FILE
sudo chown $USER:$USER $AUTHORIZED_USERS_FILE
# Reload systemd and enable service
echo "Enabling service..."
sudo systemctl daemon-reload
@@ -189,4 +259,8 @@ echo " Reports: ls $WORKING_DIR/reports/"
echo ""
echo "How to use the new 'ops' command:"
echo " To add a user, run: ops add_user username:password"
echo " To display help, run: ops help"
echo " To add a user, run: ops add_user username[:password]"
echo " To add a server, run: ops add_server server_name url [max_parallel_connections queue_size]"
echo " To edit a server setting, run: ops edit_server server_name parameter value"
echo " To list all servers and their parameters, run: ops list_servers"