Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding deploy #8

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions boot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

# to be run as swarms user
set -e
set -x
export ROOT=""
export HOME="${ROOT}/home/swarms"
unset CONDA_EXE
unset CONDA_PYTHON_EXE
export PATH="${ROOT}/var/swarms/agent_workspace/.venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

if [ ! -f "${ROOT}/var/swarms/agent_workspace/.venv/" ];
then
virtualenv "${ROOT}/var/swarms/agent_workspace/.venv/"
fi
ls "${ROOT}/var/swarms/agent_workspace/"
. "${ROOT}/var/swarms/agent_workspace/.venv/bin/activate"

pip install fastapi uvicorn termcolor
# these are tried to be installed by the app on boot
pip install sniffio pydantic-core httpcore exceptiongroup annotated-types pydantic anyio httpx ollama
pip install -e "${ROOT}/opt/swarms/"
cd "${ROOT}/var/swarms/"
#pip install -e "${ROOT}/opt/swarms-memory"
#pip install "fastapi[standard]"
#pip install "loguru"
#pip install "hunter" # for tracing
pip install pydantic==2.8.2
pip install pathos || echo oops
pip freeze
# launch as systemd
# python /opt/swarms/api/main.py
17 changes: 17 additions & 0 deletions boot_fast.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# to be run as swarms user
set -e
set -x
export ROOT=""
export HOME="${ROOT}/home/swarms"
unset CONDA_EXE
unset CONDA_PYTHON_EXE
export PATH="${ROOT}/var/swarms/agent_workspace/.venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

ls "${ROOT}/var/swarms/agent_workspace/"
. "${ROOT}/var/swarms/agent_workspace/.venv/bin/activate"

pip install -e "${ROOT}/opt/swarms/"
cd "${ROOT}/var/swarms/"
pip install -e "${ROOT}/opt/swarms-memory"
159 changes: 159 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/bin/bash
# review and improve
. ./.env # for secrets
set -e # stop on any error
set -x
export BRANCH="main"
export ROOT="/mnt/data1/swarmsdeploy"
export ROOT="" # empty
export WORKSOURCE="${ROOT}/opt/swarms/api"
export GIT=https://github.com/jmikedupont2/SwarmDeploy.git

if [ ! -d "${ROOT}/opt/swarms/install/" ]; then
mkdir -p "${ROOT}/opt/swarms/install"
fi

if [ ! -f "${ROOT}/opt/swarms/install/apt.txt" ]; then
apt update
apt install --allow-change-held-packages -y git python3-virtualenv nginx
apt install --allow-change-held-packages -y expect
apt install --allow-change-held-packages -y jq netcat-traditional # missing packages
snap install aws-cli --classic
echo 1 >"${ROOT}/opt/swarms/install/apt.txt"
fi

if [ ! -f "${ROOT}/opt/swarms/install/setup.txt" ]; then
adduser --disabled-password --gecos "" swarms --home "${ROOT}/home/swarms" || echo ignore
git config --global --add safe.directory "${ROOT}/opt/swarms"
git config --global --add safe.directory "${ROOT}/opt/swarms-memory"
# we should have done this
if [ ! -d "${ROOT}/opt/swarms/" ];
then
git clone https://github.com/jmikedupont2/swarms "${ROOT}/opt/swarms/"
fi
cd "${ROOT}/opt/swarms/" || exit 1 # "we need swarms"
git checkout --force $BRANCH
git pull
git log -2 --patch | head -1000
if [ ! -d "${ROOT}/opt/swarms-memory/" ];
then
# git clone https://github.com/The-Swarm-Corporation/swarms-memory "${ROOT}/opt/swarms-memory"
fi
# where the swarms will run
mkdir -p "${ROOT}/var/swarms/agent_workspace/"
mkdir -p "${ROOT}/home/swarms"
chown -R swarms:swarms "${ROOT}/var/swarms/agent_workspace" "${ROOT}/home/swarms"


# copy the run file from git
cp "${WORKSOURCE}/boot.sh" "${ROOT}/var/swarms/agent_workspace/boot.sh"
mkdir -p "${ROOT}/var/swarms/logs"
chmod +x "${ROOT}/var/swarms/agent_workspace/boot.sh"
chown -R swarms:swarms "${ROOT}/var/swarms/" "${ROOT}/home/swarms" "${ROOT}/opt/swarms"

echo 1 >"${ROOT}/opt/swarms/install/setup.txt"
fi

if [ ! -f "${ROOT}/opt/swarms/install/boot.txt" ]; then
# user install but do not start
su -c "bash -e -x ${ROOT}/var/swarms/agent_workspace/boot.sh" swarms
echo 1 >"${ROOT}/opt/swarms/install/boot.txt"
fi


## pull

if [ ! -f "${ROOT}/opt/swarms/install/pull.txt" ]; then
cd "${ROOT}/opt/swarms/" || exit 1 # "we need swarms"
# git fetch local
# git stash
git checkout --force $BRANCH
git pull # $BRANCH
echo 1 >"${ROOT}/opt/swarms/install/pull.txt"
fi

if [ ! -f "${ROOT}/opt/swarms/install/config.txt" ]; then
mkdir -p "${ROOT}/var/run/swarms/secrets/"
mkdir -p "${ROOT}/home/swarms/.cache/huggingface/hub"
# aws ssm get-parameter --name "swarms_openai_key" > /root/openaikey.txt
export OPENAI_KEY=`aws ssm get-parameter --name "swarms_openai_key" | jq .Parameter.Value -r `
echo "OPENAI_KEY=${OPENAI_KEY}" > "${ROOT}/var/run/swarms/secrets/env"

## append new homedir
echo "HF_HOME=${ROOT}/home/swarms/.cache/huggingface/hub" >> "${ROOT}/var/run/swarms/secrets/env"
echo "HOME=${ROOT}/home/swarms" >> "${ROOT}/var/run/swarms/secrets/env"
# attempt to move the workspace
echo 'WORKSPACE_DIR=${STATE_DIRECTORY}' >> "${ROOT}/var/run/swarms/secrets/env"
#EnvironmentFile=ROOT/var/run/swarms/secrets/env
#ExecStart=ROOT/var/run/uvicorn/env/bin/uvicorn \
# --uds ROOT/run/uvicorn/uvicorn-swarms-api.sock \
echo 1 >"${ROOT}/opt/swarms/install/config.txt"
fi

if [ ! -f "${ROOT}/opt/swarms/install/nginx.txt" ]; then
mkdir -p ${ROOT}/var/log/nginx/swarms/
fi


# create sock
mkdir -p ${ROOT}/run/uvicorn/
chown -R swarms:swarms ${ROOT}/run/uvicorn

# reconfigure
# now we setup the service and replace root in the files
#echo cat "${WORKSOURCE}/nginx/site.conf" \| sed -e "s!ROOT!${ROOT}!g"
sed -e "s!ROOT!${ROOT}!g" > /etc/nginx/sites-enabled/default < "${WORKSOURCE}/nginx/site.conf"
#cat /etc/nginx/sites-enabled/default

# ROOT/var/run/swarms/uvicorn-swarms-api.sock;
# access_log ROOT/var/log/nginx/swarms/access.log;
# error_log ROOT/var/log/nginx/swarms/error.log;
#echo cat "${WORKSOURCE}/systemd/uvicorn.service" \| sed -e "s!ROOT!/${ROOT}/!g"
#cat "${WORKSOURCE}/systemd/uvicorn.service"
sed -e "s!ROOT!${ROOT}!g" > /etc/systemd/system/swarms-uvicorn.service < "${WORKSOURCE}/systemd/uvicorn.service"
grep . -h -n /etc/systemd/system/swarms-uvicorn.service

# if [ -f ${ROOT}/etc/systemd/system/swarms-uvicorn.service ];
# then
# cp ${ROOT}/etc/systemd/system/swarms-uvicorn.service /etc/systemd/system/swarms-uvicorn.service
# else
# # allow for editing as non root
# mkdir -p ${ROOT}/etc/systemd/system/
# cp /etc/systemd/system/swarms-uvicorn.service ${ROOT}/etc/systemd/system/swarms-uvicorn.service
# fi

#
#chown -R mdupont:mdupont ${ROOT}/etc/systemd/system/
#/run/uvicorn/
# triage
chown -R swarms:swarms ${ROOT}/var/run/swarms/
# Dec 12 10:55:50 mdupont-G470 unbuffer[3921723]: OSError: [Errno 30] Read-only file system:
#cat /etc/systemd/system/swarms-uvicorn.service

# now fix the perms
mkdir -p ${ROOT}/opt/swarms/api/agent_workspace/try_except_wrapper/
chown -R swarms:swarms ${ROOT}/opt/swarms/api/

# always reload
systemctl daemon-reload
# systemctl start swarms-uvicorn || systemctl status swarms-uvicorn.service && journalctl -xeu swarms-uvicorn.service
systemctl start swarms-uvicorn || journalctl -xeu swarms-uvicorn.service
# systemctl status swarms-uvicorn.service
# journalctl -xeu swarms-uvicorn.serviceo
systemctl enable swarms-uvicorn || journalctl -xeu swarms-uvicorn.service
systemctl enable nginx
systemctl start nginx

journalctl -xeu swarms-uvicorn.service | tail -200 || echo oops
systemctl status swarms-uvicorn.service || echo oops2

# now after swarms is up, we restart nginx
HOST="localhost"
PORT=5474
while ! nc -z $HOST $PORT; do
sleep 1
echo -n "."
done
echo "Port $PORT is now open!"

systemctl restart nginx
14 changes: 14 additions & 0 deletions nginx/site.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# from https://github.com/neamaddin/debian-fastapi-server
server {
listen [::]:80;
listen 80;
server_name swarms;
access_log ROOT/var/log/nginx/swarms/access.log;
error_log ROOT/var/log/nginx/swarms/error.log;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;

location / {
proxy_pass http://127.0.0.1:5474;
}
}
35 changes: 35 additions & 0 deletions systemd/uvicorn.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# derived from https://github.com/encode/uvicorn/issues/678
[Unit]
Description=swarms
After=network.target

[Service]
Type=simple
User=swarms
Group=swarms
DynamicUser=true
WorkingDirectory=ROOT/opt/swarms/api/
ReadWritePaths=ROOT/opt/swarms/api/agent_workspace/
StateDirectory=swarms_state
PrivateTmp=true
ProtectHome=true
EnvironmentFile=ROOT/var/run/swarms/secrets/env

# for debugging use
#ExecStart=/usr/bin/unbuffer ROOT/var/run/uvicorn/env/bin/uvicorn

# this runs and is modifiable by the agent, so this can be destabilized
# FIXME: containerize the run(chroot jail?)
ExecStart=/usr/bin/unbuffer ROOT/var/swarms/agent_workspace/.venv/bin/uvicorn \
--proxy-headers \
--forwarded-allow-ips='*' \
--workers=4 \
--port=5474 \
main:create_app

ExecReload=/bin/kill -HUP ${MAINPID}
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target
Loading