From 1fc01f84f59b79f9f58d9316b48f2366e9a8b391 Mon Sep 17 00:00:00 2001 From: mike dupont Date: Wed, 18 Dec 2024 20:06:01 -0500 Subject: [PATCH 1/7] adding deploy --- boot.sh | 32 ++++++++ boot_fast.sh | 17 +++++ install.sh | 159 ++++++++++++++++++++++++++++++++++++++++ nginx/site.conf | 14 ++++ systemd/uvicorn.service | 35 +++++++++ 5 files changed, 257 insertions(+) create mode 100644 boot.sh create mode 100644 boot_fast.sh create mode 100644 install.sh create mode 100644 nginx/site.conf create mode 100644 systemd/uvicorn.service diff --git a/boot.sh b/boot.sh new file mode 100644 index 0000000..e2de333 --- /dev/null +++ b/boot.sh @@ -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 diff --git a/boot_fast.sh b/boot_fast.sh new file mode 100644 index 0000000..995eb8b --- /dev/null +++ b/boot_fast.sh @@ -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" diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..bd84f6a --- /dev/null +++ b/install.sh @@ -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 diff --git a/nginx/site.conf b/nginx/site.conf new file mode 100644 index 0000000..5ec4d84 --- /dev/null +++ b/nginx/site.conf @@ -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; + } +} diff --git a/systemd/uvicorn.service b/systemd/uvicorn.service new file mode 100644 index 0000000..7618cf6 --- /dev/null +++ b/systemd/uvicorn.service @@ -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 \ No newline at end of file From 436ba5000f6fbb64415057b0c183fb05afb2b9c0 Mon Sep 17 00:00:00 2001 From: mike dupont Date: Wed, 18 Dec 2024 21:47:58 -0500 Subject: [PATCH 2/7] bugfix: empty if in bash is an error --- install.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index bd84f6a..4fdb700 100644 --- a/install.sh +++ b/install.sh @@ -35,10 +35,10 @@ if [ ! -f "${ROOT}/opt/swarms/install/setup.txt" ]; then git checkout --force $BRANCH git pull git log -2 --patch | head -1000 - if [ ! -d "${ROOT}/opt/swarms-memory/" ]; - then +# if [ ! -d "${ROOT}/opt/swarms-memory/" ]; +# then # git clone https://github.com/The-Swarm-Corporation/swarms-memory "${ROOT}/opt/swarms-memory" - fi +# fi # where the swarms will run mkdir -p "${ROOT}/var/swarms/agent_workspace/" mkdir -p "${ROOT}/home/swarms" From fbbe514d1c3a00328b5c50520c40c0112fe17b65 Mon Sep 17 00:00:00 2001 From: mike dupont Date: Wed, 18 Dec 2024 21:49:18 -0500 Subject: [PATCH 3/7] bugfix: update root path to remove api --- install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.sh b/install.sh index 4fdb700..f4af12a 100644 --- a/install.sh +++ b/install.sh @@ -6,7 +6,7 @@ set -x export BRANCH="main" export ROOT="/mnt/data1/swarmsdeploy" export ROOT="" # empty -export WORKSOURCE="${ROOT}/opt/swarms/api" +export WORKSOURCE="${ROOT}/opt/swarms" export GIT=https://github.com/jmikedupont2/SwarmDeploy.git if [ ! -d "${ROOT}/opt/swarms/install/" ]; then From 3848badc64a4dd4472e76763d4ab601c84f560f4 Mon Sep 17 00:00:00 2001 From: mike dupont Date: Wed, 18 Dec 2024 21:59:04 -0500 Subject: [PATCH 4/7] fix the service name --- systemd/uvicorn.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/systemd/uvicorn.service b/systemd/uvicorn.service index 7618cf6..569aa71 100644 --- a/systemd/uvicorn.service +++ b/systemd/uvicorn.service @@ -25,7 +25,7 @@ ExecStart=/usr/bin/unbuffer ROOT/var/swarms/agent_workspace/.venv/bin/uvicorn \ --forwarded-allow-ips='*' \ --workers=4 \ --port=5474 \ - main:create_app + api:app ExecReload=/bin/kill -HUP ${MAINPID} RestartSec=10 From 0591644d9bf807a3e77af76f79f2a1a955549e1b Mon Sep 17 00:00:00 2001 From: mike dupont Date: Wed, 18 Dec 2024 22:00:50 -0500 Subject: [PATCH 5/7] adding a restart --- install.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/install.sh b/install.sh index f4af12a..26b3bc1 100644 --- a/install.sh +++ b/install.sh @@ -150,6 +150,9 @@ systemctl status swarms-uvicorn.service || echo oops2 # now after swarms is up, we restart nginx HOST="localhost" PORT=5474 + +systemctl restart swarms-uvicorn + while ! nc -z $HOST $PORT; do sleep 1 echo -n "." From 896681275fd4a3d4d12027cf823b7f89b199a046 Mon Sep 17 00:00:00 2001 From: mike dupont Date: Wed, 18 Dec 2024 22:06:02 -0500 Subject: [PATCH 6/7] moving to port 8000 --- install.sh | 2 +- systemd/uvicorn.service | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/install.sh b/install.sh index 26b3bc1..d402c9a 100644 --- a/install.sh +++ b/install.sh @@ -149,7 +149,7 @@ systemctl status swarms-uvicorn.service || echo oops2 # now after swarms is up, we restart nginx HOST="localhost" -PORT=5474 +PORT=8000 systemctl restart swarms-uvicorn diff --git a/systemd/uvicorn.service b/systemd/uvicorn.service index 569aa71..5909b42 100644 --- a/systemd/uvicorn.service +++ b/systemd/uvicorn.service @@ -24,7 +24,7 @@ ExecStart=/usr/bin/unbuffer ROOT/var/swarms/agent_workspace/.venv/bin/uvicorn \ --proxy-headers \ --forwarded-allow-ips='*' \ --workers=4 \ - --port=5474 \ + --port=8000 \ api:app ExecReload=/bin/kill -HUP ${MAINPID} From 4db3af3bc4ec8e35a78a33f6a9bd16912286c35e Mon Sep 17 00:00:00 2001 From: mike dupont Date: Wed, 18 Dec 2024 22:07:16 -0500 Subject: [PATCH 7/7] changing prompt --- api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api.py b/api.py index b6cc887..2f3b9c8 100644 --- a/api.py +++ b/api.py @@ -7,7 +7,7 @@ # Define the payload payload = { - "task": "What are the ways vcs can fuck founders over in the term sheet? What should they look for?", # Replace with your task + "task": "What are the ways vcs and founders interact in the term sheet? What should they look for?", # Replace with your task } # Make the POST request @@ -15,4 +15,4 @@ # Print the response print("Status Code:", response.status_code) -print("Response JSON:", response.json()) \ No newline at end of file +print("Response JSON:", response.json())