#!/usr/bin/env bash if [ "$VERBOSE" == "yes" ]; then set -x; STD=""; fi if [ "$VERBOSE" != "yes" ]; then STD="silent"; fi silent() { "$@" > /dev/null 2>&1; } if [ "$DISABLEIPV6" == "yes" ]; then echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf; $STD sysctl -p; fi YW=$(echo "\033[33m") RD=$(echo "\033[01;31m") BL=$(echo "\033[36m") GN=$(echo "\033[1;92m") CL=$(echo "\033[m") RETRY_NUM=10 RETRY_EVERY=3 CM="${GN}✓${CL}" CROSS="${RD}✗${CL}" BFR="\\r\\033[K" HOLD="-" set -Eeuo pipefail trap 'error_handler $LINENO "$BASH_COMMAND"' ERR function error_handler() { local exit_code="$?" local line_number="$1" local command="$2" local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}" echo -e "\n$error_message\n" } function msg_info() { local msg="$1" echo -ne " ${HOLD} ${YW}${msg}..." } function msg_ok() { local msg="$1" echo -e "${BFR} ${CM} ${GN}${msg}${CL}" } function msg_error() { local msg="$1" echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}" } msg_info "Setting up Container OS " sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen locale-gen >/dev/null while [ "$(hostname -I)" = "" ]; do echo 1>&2 -en "${CROSS}${RD} No Network! " sleep $RETRY_EVERY ((NUM--)) if [ $NUM -eq 0 ]; then echo 1>&2 -e "${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}" exit 1 fi done msg_ok "Set up Container OS" msg_ok "Network Connected: ${BL}$(hostname -I)" set +e if nc -zw1 8.8.8.8 443; then msg_ok "Internet Connected"; else msg_error "Internet NOT Connected" read -r -p "Would you like to continue anyway? " prompt if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}" else echo -e " 🖧 Check Network Settings" exit 1 fi fi RESOLVEDIP=$(nslookup "github.com" | awk -F':' '/^Address: / { matched = 1 } matched { print $2}' | xargs) if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to ${BL}$RESOLVEDIP${CL}"; fi set -e msg_info "Updating Container OS" $STD apt-get update $STD apt-get -y upgrade msg_ok "Updated Container OS" msg_info "Installing Paperless-ngx Dependencies" $STD apt-get install -y --no-install-recommends \ python3 \ python3-pip \ python3-dev \ imagemagick \ fonts-liberation \ optipng \ gnupg \ libpq-dev \ libmagic-dev \ mime-support \ libzbar0 \ poppler-utils \ default-libmysqlclient-dev \ sudo \ mc msg_ok "Installed Paperless-ngx Dependencies" msg_info "Installing OCR Dependencies" $STD apt-get install -y --no-install-recommends \ unpaper \ ghostscript \ icc-profiles-free \ qpdf \ liblept5 \ libxml2 \ pngquant \ zlib1g \ tesseract-ocr \ tesseract-ocr-eng msg_ok "Installed OCR Dependencies" msg_info "Installing Extra Dependencies" $STD apt-get install -y --no-install-recommends \ redis \ postgresql \ build-essential \ python3-setuptools \ python3-wheel msg_ok "Installed Extra Dependencies" msg_info "Installing JBIG2" $STD apt-get install -y --no-install-recommends \ automake \ libtool \ pkg-config \ git \ curl \ libtiff-dev \ libpng-dev \ libleptonica-dev $STD git clone https://github.com/agl/jbig2enc /opt/jbig2enc cd /opt/jbig2enc $STD bash ./autogen.sh $STD bash ./configure $STD make $STD make install rm -rf /opt/jbig2enc msg_ok "Installed JBIG2" msg_info "Installing Paperless-ngx (Patience)" Paperlessngx=$(wget -q https://github.com/paperless-ngx/paperless-ngx/releases/latest -O - | grep "title>Release" | cut -d " " -f 5) cd /opt $STD wget https://github.com/paperless-ngx/paperless-ngx/releases/download/$Paperlessngx/paperless-ngx-$Paperlessngx.tar.xz $STD tar -xf paperless-ngx-$Paperlessngx.tar.xz -C /opt/ mv paperless-ngx paperless rm paperless-ngx-$Paperlessngx.tar.xz cd /opt/paperless ## python 3.10+ doesn't like the '-e', so we remove it from this the requirements file sed -i -e 's|-e git+https://github.com/paperless-ngx/django-q.git|git+https://github.com/paperless-ngx/django-q.git|' /opt/paperless/requirements.txt $STD pip install --upgrade pip $STD pip install -r requirements.txt msg_ok "Installed Paperless-ngx" msg_info "Setting up database" DB_USER=paperless DB_PASS="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)" DB_NAME=paperlessdb $STD sudo -u postgres psql -c "CREATE ROLE $DB_USER WITH LOGIN PASSWORD '$DB_PASS';" $STD sudo -u postgres psql -c "CREATE DATABASE $DB_NAME WITH OWNER $DB_USER TEMPLATE template0;" echo "Paperless-ngx Database User" >>~/paperless.creds echo $DB_USER >>~/paperless.creds echo "Paperless-ngx Database Password" >>~/paperless.creds echo $DB_PASS >>~/paperless.creds echo "Paperless-ngx Database Name" >>~/paperless.creds echo $DB_NAME >>~/paperless.creds mkdir -p {consume,media} sed -i -e 's|#PAPERLESS_DBNAME=paperless|PAPERLESS_DBNAME=paperlessdb|' /opt/paperless/paperless.conf sed -i -e "s|#PAPERLESS_DBPASS=paperless|PAPERLESS_DBPASS=$DB_PASS|" /opt/paperless/paperless.conf SECRET_KEY="$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32)" sed -i -e "s|#PAPERLESS_SECRET_KEY=change-me|PAPERLESS_SECRET_KEY=$SECRET_KEY|" /opt/paperless/paperless.conf cd /opt/paperless/src $STD python3 manage.py migrate msg_ok "Set up database" msg_info "Setting up admin Paperless-ngx User & Password" ## From https://github.com/linuxserver/docker-paperless-ngx/blob/main/root/etc/cont-init.d/99-migrations cat <>~/paperless.creds echo "Paperless-ngx WebUI User" >>~/paperless.creds echo admin >>~/paperless.creds echo "Paperless-ngx WebUI Password" >>~/paperless.creds echo $DB_PASS >>~/paperless.creds msg_ok "Set up admin Paperless-ngx User & Password" msg_info "Creating Services" cat </etc/systemd/system/paperless-scheduler.service [Unit] Description=Paperless Celery beat Requires=redis.service [Service] WorkingDirectory=/opt/paperless/src ExecStart=celery --app paperless beat --loglevel INFO [Install] WantedBy=multi-user.target EOF cat </etc/systemd/system/paperless-task-queue.service [Unit] Description=Paperless Celery Workers Requires=redis.service [Service] WorkingDirectory=/opt/paperless/src ExecStart=celery --app paperless worker --loglevel INFO [Install] WantedBy=multi-user.target EOF cat </etc/systemd/system/paperless-consumer.service [Unit] Description=Paperless consumer Requires=redis.service [Service] WorkingDirectory=/opt/paperless/src ExecStart=python3 manage.py document_consumer [Install] WantedBy=multi-user.target EOF cat </etc/systemd/system/paperless-webserver.service [Unit] Description=Paperless webserver After=network.target Wants=network.target Requires=redis.service [Service] WorkingDirectory=/opt/paperless/src ExecStart=/usr/local/bin/gunicorn -c /opt/paperless/gunicorn.conf.py paperless.asgi:application [Install] WantedBy=multi-user.target EOF sed -i -e 's/rights="none" pattern="PDF"/rights="read|write" pattern="PDF"/' /etc/ImageMagick-6/policy.xml systemctl daemon-reload $STD systemctl enable --now paperless-consumer paperless-webserver paperless-scheduler paperless-task-queue.service msg_ok "Created Services" echo "export TERM='xterm-256color'" >>/root/.bashrc if ! getent shadow root | grep -q "^root:[^\!*]"; then msg_info "Customizing Container" if [ "$PCT_OSTYPE" == "debian" ]; then rm -rf /etc/motd /etc/update-motd.d/10-uname; else chmod -x /etc/update-motd.d/*; fi touch ~/.hushlogin GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf" mkdir -p $(dirname $GETTY_OVERRIDE) cat <$GETTY_OVERRIDE [Service] ExecStart= ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM EOF systemctl daemon-reload systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//') msg_ok "Customized Container" fi if [[ "${SSH_ROOT}" == "yes" ]]; then sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config; systemctl restart sshd; fi msg_info "Cleaning up" $STD apt-get autoremove $STD apt-get autoclean msg_ok "Cleaned"