install-scripts/librephotos.sh

202 lines
6.7 KiB
Bash

#!/bin/bash
#Requirement
# -DEBIAN 11
#
# - Postgres database
# CREATE USER librephotos WITH PASSWORD 'pg_password';
# CREATE DATABASE "librephoto" WITH OWNER "librephotos" ENCODING 'UTF8' LC_COLLATE = 'fr_FR.UTF-8' LC_CTYPE = 'fr_FR.UTF-8' TEMPLATE template0;
# GRANT ALL privileges ON DATABASE librephotos TO librephotos;
set -ex
apt install --no-install-recommends -y \
swig libpq-dev postgresql-common curl libopenblas-dev libmagic1 libboost-all-dev libxrender-dev \
liblapack-dev git bzip2 cmake build-essential libsm6 libglib2.0-0 libgl1-mesa-glx gfortran gunicorn \
libheif-dev libssl-dev rustc liblzma-dev python3 python3-pip
mkdir -p /data_models/places365/
mkdir -p /data_models/im2txt/
mkdir -p /root/.cache/torch/hub/checkpoints/
curl -SL https://s3.eu-central-1.amazonaws.com/ownphotos-deploy/places365_model.tar.gz | tar -zxC /data_models/places365/
curl -SL https://s3.eu-central-1.amazonaws.com/ownphotos-deploy/im2txt_model.tar.gz | tar -zxC /data_models/im2txt/
curl -SL https://s3.eu-central-1.amazonaws.com/ownphotos-deploy/im2txt_data.tar.gz | tar -zxC /data_models/im2txt/
curl -SL https://download.pytorch.org/models/resnet152-b121ed2d.pth -o /root/.cache/torch/hub/checkpoints/resnet152-b121ed2d.pth
pip3 install torch==1.7.1+cpu torchvision==0.8.2+cpu -f https://download.pytorch.org/whl/torch_stable.html
pip3 install -v --install-option="--no" --install-option="DLIB_USE_CUDA" --install-option="--no" --install-option="USE_AVX_INSTRUCTIONS" --install-option="--no" --install-option="USE_SSE4_INSTRUCTIONS" dlib
cd /usr/src
git clone https://github.com/tomamplius/librephotos
cd librephotos
pip3 install -r requirements.txt
python3 -m spacy download en_core_web_sm
cat << 'EOF' > /etc/systemd/system/librephotos-backend.service
[Unit]
Description=librephotos-backend
[Service]
ExecStart=/usr/local/bin/librephotos-backend
[Install]
WantedBy=multi-user.target
EOF
systemctl enable librephotos-backend
cat << 'EOF' > /usr/local/bin/librephotos-backend
#! /bin/bash
cd /usr/src/librephotos/
export PYTHONUNBUFFERED=TRUE
export PYTHONUNBUFFERED=TRUE
export BACKEND_HOST=localhost
export DB_BACKEND=postgresql
export DB_NAME=librephotos
export DB_USER=librephotos
export DB_PASS=pg_password
export DB_HOST=postgres.net.lgy.fr
export DB_PORT=5432
#redis local ou redis distant
#export REDIS_PATH=
#export REDIS_DB=
# redis distant
export REDIS_HOST=redis.net.lgy.fr
export REDIS_PASS=
export REDIS_PORT=6379
export TIME_ZONE=Europe/Paris
export SECRET_KEY=SecretKeyToBeDefined
export ADMIN_USERNAME=thomas
export ADMIN_EMAIL=thomas@lgy.fr
export ADMIN_PASSWORD=toto
export MAPBOX_API_KEY=
export DEBUG=0
export BASE_DATA=/var/lib/librephotos/
export BASE_LOGS=/var/log/librephotos/
mkdir -p $BASE_LOGS
mkdir -p $BASE_DATA/data_models/
mkdir -p $BASE_DATA/data/nextcloud_media
mkdir -p $BASE_DATA/protected_media/
python3 image_similarity/main.py 2>&1 | tee $BASE_LOGS/gunicorn_image_similarity.log &
python3 manage.py showmigrations | tee $BASE_LOGS/show_migrate.log
python3 manage.py migrate | tee $BASE_LOGS/command_migrate.log
python3 manage.py showmigrations | tee $BASE_LOGS/show_migrate.log
python3 manage.py build_similarity_index 2>&1 | tee $BASE_LOGS/command_build_similarity_index.log
python3 manage.py clear_cache
python3 manage.py createadmin -u $ADMIN_USERNAME $ADMIN_EMAIL 2>&1 | tee $BASE_LOGS/command_createadmin.log
echo "Running backend server..."
python3 manage.py rqworker default 2>&1 | tee $BASE_LOGS/rqworker.log &
if [ "$DEBUG" = 1 ]
then
echo "develompent backend starting"
gunicorn --worker-class=gevent --timeout 36000 --reload --bind localhost:8001 --log-level=info ownphotos.wsgi 2>&1 | tee $BASE_LOGS/gunicorn_django.log
else
echo "production backend starting"
gunicorn --worker-class=gevent --timeout 3600 --bind localhost:8001 --log-level=info ownphotos.wsgi 2>&1 | tee $BASE_LOGS/gunicorn_django.log
fi
python3 image_similarity/main.py 2>&1 | tee /logs/gunicorn_image_similarity.log &
python3 manage.py showmigrations | tee /logs/show_migrate.log
python3 manage.py migrate | tee /logs/command_migrate.log
python3 manage.py showmigrations | tee /logs/show_migrate.log
python3 manage.py build_similarity_index 2>&1 | tee /logs/command_build_similarity_index.log
python3 manage.py clear_cache
python3 manage.py createadmin -u $ADMIN_USERNAME $ADMIN_EMAIL 2>&1 | tee /logs/command_createadmin.log
echo "Running backend server..."
python3 manage.py rqworker default 2>&1 | tee /logs/rqworker.log &
if [ "$DEBUG" = 1 ]
then
echo "develompent backend starting"
gunicorn --worker-class=gevent --timeout 36000 --reload --bind localhost:8001 --log-level=info ownphotos.wsgi 2>&1 | tee /logs/gunicorn_django.log
else
echo "production backend starting"
gunicorn --worker-class=gevent --timeout 3600 --bind localhost:8001 --log-level=info ownphotos.wsgi 2>&1 | tee /logs/gunicorn_django.log
fi
EOF
chmod +x /usr/local/bin/librephotos-backend
apt-get install -y curl git xsel nodejs git npm --no-install-recommends
mkdir -p /usr/src/librephotos-frontend
git clone https://github.com/LibrePhotos/librephotos-frontend /usr/src/librephotos-frontend
cd /usr/src/librephotos-frontend
npm install
npm run build
npm install serve
cat << EOF > /etc/systemd/system/librephotos-frontend.service
[Unit]
Description=librephotos-frontend
[Service]
ExecStart=/usr/local/bin/librephotos-frontend
[Install]
WantedBy=multi-user.target
EOF
systemctl enable librephotos-frontend
cat << 'EOF' > /usr/local/bin/librephotos-frontend
#!/usr/bin/env bash
cd /usr/src/librephotos-frontend
npm install
npm run start
#/root/node_modules/.bin/serve build -d -l 3000
EOF
chmod +x /usr/local/bin/librephotos-frontend
apt install -y nginx
cat << 'EOF' > /etc/nginx/nginx.conf
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log debug;
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
# React routes are entirely on the App side in the web broswer
# Always proxy to root with the same page request when nginx 404s
error_page 404 /;
proxy_intercept_errors on;
proxy_set_header Host $host;
proxy_pass http://localhost:3000/;
}
location ~ ^/(api|media)/ {
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host backend;
include uwsgi_params;
proxy_pass http://localhost:8001;
}
# Django media
location /protected_media {
internal;
alias /var/lib/librephotos/protected_media/;
}
# Original Photos
location /original {
internal;
alias /var/lib/librephotos/data/;
}
# Nextcloud Original Photos
location /nextcloud_original {
internal;
alias /var/lib/librephotos/data/nextcloud_media/;
}
}
}
EOF
systemctl restart nginx