install-scripts/librephotos.sh

202 lines
6.7 KiB
Bash
Raw Permalink Normal View History

2021-02-28 11:27:33 +01:00
#!/bin/bash
#Requirement
# -DEBIAN 11
2021-02-28 23:37:22 +01:00
#
# - Postgres database
2021-03-01 03:12:31 +01:00
# 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;
2021-02-28 11:27:33 +01:00
set -ex
apt install --no-install-recommends -y \
2021-02-28 19:33:58 +01:00
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
2021-02-28 11:27:33 +01:00
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
2021-02-28 19:33:58 +01:00
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
2021-02-28 11:27:33 +01:00
cd /usr/src
2021-03-01 02:57:01 +01:00
git clone https://github.com/tomamplius/librephotos
2021-02-28 11:27:33 +01:00
cd librephotos
2021-02-28 19:33:58 +01:00
pip3 install -r requirements.txt
python3 -m spacy download en_core_web_sm
2021-02-28 11:27:33 +01:00
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
2021-03-01 03:12:31 +01:00
export DB_NAME=librephotos
export DB_USER=librephotos
export DB_PASS=pg_password
2021-02-28 11:27:33 +01:00
export DB_HOST=postgres.net.lgy.fr
export DB_PORT=5432
#redis local ou redis distant
2021-02-28 18:19:01 +01:00
#export REDIS_PATH=
#export REDIS_DB=
2021-02-28 11:27:33 +01:00
# redis distant
export REDIS_HOST=redis.net.lgy.fr
export REDIS_PASS=
export REDIS_PORT=6379
export TIME_ZONE=Europe/Paris
2021-03-01 03:12:31 +01:00
export SECRET_KEY=SecretKeyToBeDefined
2021-02-28 11:27:33 +01:00
export ADMIN_USERNAME=thomas
export ADMIN_EMAIL=thomas@lgy.fr
export ADMIN_PASSWORD=toto
export MAPBOX_API_KEY=
2021-02-28 19:33:58 +01:00
export DEBUG=0
2021-03-01 02:57:01 +01:00
export BASE_DATA=/var/lib/librephotos/
export BASE_LOGS=/var/log/librephotos/
2021-03-01 03:01:04 +01:00
mkdir -p $BASE_LOGS
mkdir -p $BASE_DATA/data_models/
mkdir -p $BASE_DATA/data/nextcloud_media
mkdir -p $BASE_DATA/protected_media/
2021-03-01 02:57:01 +01:00
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
2021-02-28 11:27:33 +01:00
2021-02-28 19:33:58 +01:00
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
2021-02-28 11:27:33 +01:00
echo "Running backend server..."
2021-02-28 19:33:58 +01:00
python3 manage.py rqworker default 2>&1 | tee /logs/rqworker.log &
2021-02-28 11:27:33 +01:00
if [ "$DEBUG" = 1 ]
then
echo "develompent backend starting"
2021-02-28 19:33:58 +01:00
gunicorn --worker-class=gevent --timeout 36000 --reload --bind localhost:8001 --log-level=info ownphotos.wsgi 2>&1 | tee /logs/gunicorn_django.log
2021-02-28 11:27:33 +01:00
else
echo "production backend starting"
2021-02-28 19:33:58 +01:00
gunicorn --worker-class=gevent --timeout 3600 --bind localhost:8001 --log-level=info ownphotos.wsgi 2>&1 | tee /logs/gunicorn_django.log
2021-02-28 11:27:33 +01:00
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;
2021-03-01 02:57:01 +01:00
alias /var/lib/librephotos/protected_media/;
2021-02-28 11:27:33 +01:00
}
# Original Photos
location /original {
internal;
2021-03-01 02:57:01 +01:00
alias /var/lib/librephotos/data/;
2021-02-28 11:27:33 +01:00
}
# Nextcloud Original Photos
location /nextcloud_original {
internal;
2021-03-01 02:57:01 +01:00
alias /var/lib/librephotos/data/nextcloud_media/;
2021-02-28 11:27:33 +01:00
}
}
}
EOF
systemctl restart nginx