Skip to content
Open
Changes from all commits
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
29 changes: 22 additions & 7 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ set -e
# Options
sort_dir="/opt/sort"
venv_dir="$sort_dir/venv"
pip="$venv_dir/bin/pip"
python_version="python$(cat .python-version | xargs)"
python="$venv_dir/bin/python"
pip="$venv_dir/bin/python -m pip"
env_file="$sort_dir/.env"
node_version=20
django_media_root="/srv/www/sort/uploads/"
Expand All @@ -29,18 +29,30 @@ sudo locale-gen en_GB
sudo locale-gen en_GB.UTF-8
sudo update-locale

# Create Python virtual environment
# Install OS packages (don't ask for user input)
echo "Installing Ubuntu packages..."
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get upgrade -y -qq -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"
apt-get install -y -qq -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" "$python_version" "$python_version-venv" curl

# Create program files directory
mkdir --parents "$sort_dir"
apt update -qq
apt upgrade --yes -qq
apt install --upgrade --yes -qq "$python_version" "$python_version-venv" curl
python3 -m venv "$venv_dir"

# Create Python virtual environment
echo "Installing Python packages..."
# If the virtual environment doesn't already exist, make a new one
if [ ! -f "$venv_dir/pyvenv.cfg" ]; then
python3 -m venv "$venv_dir"
fi

# Install the SORT Django app package
$pip install --quiet -r requirements.txt
$python -m pip install --upgrade pip
$pip install --quiet --upgrade -r requirements.txt
cp --recursive ./* "$sort_dir/"

# Create gunicorn group if it doesn't exist
echo "Creating system user..."
if ! getent group gunicorn > /dev/null 2>&1; then
groupadd --system gunicorn
fi
Expand All @@ -62,6 +74,7 @@ curl -fsSL "https://deb.nodesource.com/setup_$node_version.x" -o nodesource_setu
sudo -E bash nodesource_setup.sh
apt-get install -y --allow-downgrades nodejs="$node_version.*"
node --version
npm config set fund false

# Upgrade npm to latest stable version
# https://github.com/RSE-Sheffield/SORT/issues/494
Expand All @@ -70,6 +83,7 @@ npm install -g npm@latest
npm --version

# Install JavaScript package
echo "Installing front-end packages..."
# (Use a sub-shell to avoid changing directory.)
(cd "$sort_dir" && npm ci && npm run build)

Expand Down Expand Up @@ -225,5 +239,6 @@ echo "Applying Django migrations..."
# shellcheck source=/opt/sort/.env
(cd "$sort_dir" && set -a && source "$env_file" && set +a && $python manage.py migrate)

# Ensure updated code is loaded
echo "Restarting web application service..."
systemctl restart gunicorn.service