Papermerge

From BITPlan Wiki
Jump to navigation Jump to search

Links

Issues


API

Script to manage papermerge docker installation

usage

papermerge -h
Usage: /home/wf/bin/papermerge [OPTIONS]
Options:
  -b, --bash             Open a bash shell in the Papermerge container
  -c, --config           View current configuration
  -d, --debug            Enable debug mode
  -dn, --down            Stop Papermerge services
  -f, --force            Force setup even if configuration already exists
  -h, --help             Show this help message
  -l, --logs             View Papermerge logs
  -p, --port PORT        Set the port for Papermerge (default: 8000)
  -s, --setup            Setup Papermerge Docker Compose configuration
  -t, --token            Create a token for the user specified in .env
  -u, --up               Start Papermerge services
  -v, --version          Show version information

Setup and start

create the .env file first as described below. Then:

papermerge -s
✅:Papermerge Docker Compose configuration has been set up in /home/wf/.papermerge
papermerge -u
[+] Running 38/3
 ✔ worker Pulled                                                         108.0s 
 ✔ redis Pulled                                                           89.4s 
 ✔ web Pulled                                                            108.0s 
[+] Running 6/6
 ✔ Network papermerge_default    Created                                   0.1s 
 ✔ Volume "papermerge_data"      Created                                   0.0s 
 ✔ Volume "papermerge_index_db"  Created                                   0.0s 
 ✔ Container papermerge_redis    Started                                  25.4s 
 ✔ Container papermerge_worker   Started                                  25.5s 
 ✔ Container papermerge_web      Started                                   0.7s 
✅:Papermerge services started

You should now be able to access the software via the port as configured in your env

Exploring the environment variables

papermerge -b
env | grep PAPERMERGE | awk -F= '{if($1 ~ /PAPERMERGE__AUTH__PASSWORD|PAPERMERGE__SECURITY__SECRET_KEY/) $2="********"; print $1"="$2} | sort'
# papermerge Script
PAPERMERGE_MEDIA=/bitplan/data/papermerge/media
PAPERMERGE_PORT=8008
# papermerge docker
PAPERMERGE__AUTH__EMAIL=admin@example.com
PAPERMERGE__AUTH__PASSWORD=********
PAPERMERGE__AUTH__USERNAME=admin
PAPERMERGE__DATABASE__URL=sqlite:////db/db.sqlite3
PAPERMERGE__OCR__DEFAULT_LANGUAGE=deu
PAPERMERGE__SECURITY__SECRET_KEY=********
PAPERMERGE__SECURITY__TOKEN_EXPIRE_MINUTES=1051200

.env file

put this in $HOME/.papermerge/.env

# Configuration for Papermerge
# See https://docs.papermerge.io

# Secret key used for cryptographic operations. It should be a strong, unpredictable value.
# https://docs.papermerge.io/3.2/settings/overview/?h=papermerge__security__secret_key
PAPERMERGE__SECURITY__SECRET_KEY=**************************

# Default username for accessing the Papermerge admin interface.
# https://docs.papermerge.io/3.2/sso/oidc/authentik/?h=papermerge__auth__username#step-5-start-papermerge
PAPERMERGE__AUTH__USERNAME=********

# Default password for the admin user. It should be complex and securely stored.
PAPERMERGE__AUTH__PASSWORD=********

# Token expiration time in minutes. Here, it's set to 2 years (1051200 minutes).
# Adjust as necessary for your security requirements.
PAPERMERGE__SECURITY__TOKEN_EXPIRE_MINUTES=1051200

# Directory path where Papermerge will store media files (e.g., uploaded documents).
PAPERMERGE_MEDIA=/bitplan/data/papermerge/media

# Port on which the Papermerge service will be exposed. Ensure it's not conflicting with other services.
PAPERMERGE_PORT=8008

papermerge

For the most current version of the script see https://github.com/WolfgangFahl/scan2wiki/blob/main/scripts/papermerge

#!/bin/bash
# WF 2024-08-24
# Setup and manage Papermerge using Docker Compose
VERSION="0.0.5"
pm_root=$HOME/.papermerge
pm_compose=$pm_root/docker-compose.yml
pm_env=$pm_root/.env

# Color definitions
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m'
orange='\033[38;5;208m'  # This is a close approximation of orange
yellow='\033[0;33m'
endColor='\033[0m'

# Function to display colored messages
color_msg() {
  local l_color="$1"
  local l_msg="$2"
  echo -e "${l_color}$l_msg${endColor}"
}

# Function to display errors
error() {
  local l_msg="$1"
  color_msg $red "Error:" 1>&2
  color_msg $red "\t$l_msg" 1>&2
  exit 1
}

# Function to display warnings
warning() {
  local l_msg="$1"
  color_msg $orange "⚠️:$l_msg"
}

# Function to display negative messages
negative() {
  local l_msg="$1"
  color_msg $red "❌:$l_msg"
}

# Function to display positive messages
positive() {
  local l_msg="$1"
  color_msg $green "✅:$l_msg"
}

# Function to display usage information
usage() {
  echo "Usage: $0 [OPTIONS]"
  echo "Options:"
  echo "  -b, --bash             Open a bash shell in the Papermerge container"
  echo "  -c, --config           View current configuration"
  echo "  -d, --debug            Enable debug mode"
  echo "  -dn, --down            Stop Papermerge services"
  echo "  -f, --force            Force setup even if configuration already exists"
  echo "  -h, --help             Show this help message"
  echo "  -l, --logs             View Papermerge logs"
  echo "  -p, --port PORT        Set the port for Papermerge (default: 8000)"
  echo "  -s, --setup            Setup Papermerge Docker Compose configuration"
  echo "  -t, --token            Create a token for the user specified in .env"
  echo "  -u, --up               Start Papermerge services"
  echo "  -v, --version          Show version information"
  exit 1
}

# Function to setup Papermerge
setup_papermerge() {
  local force=$1

  if [ -f "$pm_compose" ] && [ "$force" != "true" ]; then
    warning "Papermerge configuration already exists."
    warning "Use -f or --force option to override the existing setup."
    return
  fi

  if [ ! -d "$pm_root" ]; then
    color_msg $blue "Creating $pm_root"
    mkdir -p "$pm_root"
  fi

  cat << EOF > "$pm_compose"
x-backend: &common
  platform: linux/x86_64
  image: papermerge/papermerge:3.0.3
  env_file: .env
  volumes:
    - data:/db
    - index_db:/core_app/index_db
    - \${PAPERMERGE_MEDIA:-./media}:/core_app/media

services:
  web:
    <<: *common
    container_name: papermerge_web
    ports:
     - "\${PAPERMERGE_PORT:-12000}:80"
    depends_on:
      - redis

  worker:
    <<: *common
    container_name: papermerge_worker
    command: worker

  redis:
    image: redis:6
    container_name: papermerge_redis

volumes:
  data:
  index_db:
EOF

  if [ ! -f "$pm_env" ]; then
    warning ".env file not found. Please create $pm_env with the necessary environment variables."
  else
    positive "Papermerge Docker Compose configuration has been set up in $pm_root"
  fi
}

# Function to start Papermerge services
start_services() {
  if [ ! -f "$pm_env" ]; then
    error ".env file not found. Please create $pm_env with the necessary environment variables."
  fi
  cd "$pm_root" && docker compose up -d
  positive "Papermerge services started"
}

# Function to stop Papermerge services
stop_services() {
  cd "$pm_root" && docker compose down
  positive "Papermerge services stopped"
}

# Function to view Papermerge logs
view_logs() {
  cd "$pm_root" && docker compose logs -f
}

# Function to view current configuration
view_config() {
  if [ -f "$pm_compose" ]; then
    cat "$pm_compose"
  else
    negative "docker-compose.yml not found. Please run setup first."
  fi
}

# Function to open a bash shell in the Papermerge container
open_bash() {
  docker exec -it papermerge_web /bin/bash
}

# Function to create a token for the user specified in .env
create_token() {
  if [ ! -f "$pm_env" ]; then
    error ".env file not found. Please create $pm_env with the necessary environment variables."
  fi

  # Read the PAPERMERGE__AUTH__USERNAME variable from .env file
  source "$pm_env"
  if [ -z "$PAPERMERGE__AUTH__USERNAME" ]; then
    error "PAPERMERGE__AUTH__USERNAME not found in .env file. Please specify the username."
  fi

  docker exec papermerge_web create_token.sh "$PAPERMERGE__AUTH__USERNAME"
  positive "Token created for user $PAPERMERGE__AUTH__USERNAME"
}

# Initialize flags
action_performed=false
force_setup=false

# Parse command line arguments
while [[ $# -gt 0 ]]; do
  case $1 in
    -b|--bash)
      open_bash
      action_performed=true
      ;;
    -c|--config)
      view_config
      action_performed=true
      ;;
    -d|--debug)
      set -x
      ;;
    -dn|--down)
      stop_services
      action_performed=true
      ;;
    -f|--force)
      force_setup=true
      ;;
    -h|--help)
      usage
      ;;
    -l|--logs)
      view_logs
      action_performed=true
      ;;
    -p|--port)
      if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
        PAPERMERGE_PORT=$2
        shift
        if [ -f "$pm_env" ]; then
          sed -i '/^PAPERMERGE_PORT=/d' "$pm_env"
        fi
        echo "PAPERMERGE_PORT=$PAPERMERGE_PORT" >> "$pm_env"
        positive "Papermerge port set to $PAPERMERGE_PORT"
        action_performed=true
      else
        error "Error: Argument for $1 is missing"
      fi
      ;;
    -s|--setup)
      setup_papermerge $force_setup
      action_performed=true
      ;;
    -t|--token)
      create_token
      action_performed=true
      ;;
    -u|--up)
      start_services
      action_performed=true
      ;;
    -v|--version)
      echo "Version: $VERSION"
      action_performed=true
      ;;
    *)
      error "Unknown option: $1"
      ;;
  esac
  shift
done


# If no action was performed, show usage
if [ "$action_performed" = false ]; then
  usage
fi