[GH-ISSUE #627] script to manage docker installation #490

Closed
opened 2026-02-25 21:32:02 +03:00 by kerem · 1 comment
Owner

Originally created by @WolfgangFahl on GitHub (Aug 24, 2024).
Original GitHub issue: https://github.com/ciur/papermerge/issues/627

Originally assigned to: @ciur on GitHub.

at https://wiki.bitplan.com/index.php/Papermerge#Script_to_manage_papermerge_docker_installation you'll find a script to manage the installation of papermerge. https://github.com/WolfgangFahl/scan2wiki/blob/main/scripts/papermerge has the current version and https://github.com/WolfgangFahl/scan2wiki/issues/19 is the relevant issue in my scan2wiki project

it uses a .env file 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

And the current script is:

#!/bin/bash
# WF 2024-08-24
# Setup and manage Papermerge using Docker Compose
VERSION="0.0.4"
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_USER variable from .env file
  source "$pm_env"
  if [ -z "$PAPERMERGE_USER" ]; then
    error "PAPERMERGE_USER not found in .env file. Please specify the username."
  fi

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

# 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
      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

# Set PAPERMERGE_PORT in .env file if provided
if [ -n "$PAPERMERGE_PORT" ]; then
  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
fi

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

which makes life easier for me to manage a papermerge installation - and might be helpful for others too with the idea to hide the technical details of docker and papermerge that might change over time.

Originally created by @WolfgangFahl on GitHub (Aug 24, 2024). Original GitHub issue: https://github.com/ciur/papermerge/issues/627 Originally assigned to: @ciur on GitHub. at https://wiki.bitplan.com/index.php/Papermerge#Script_to_manage_papermerge_docker_installation you'll find a script to manage the installation of papermerge. https://github.com/WolfgangFahl/scan2wiki/blob/main/scripts/papermerge has the current version and https://github.com/WolfgangFahl/scan2wiki/issues/19 is the relevant issue in my scan2wiki project it uses a .env file in $HOME/.papermerge/.env: ```bash # 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 ``` And the current script is: ```bash #!/bin/bash # WF 2024-08-24 # Setup and manage Papermerge using Docker Compose VERSION="0.0.4" 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_USER variable from .env file source "$pm_env" if [ -z "$PAPERMERGE_USER" ]; then error "PAPERMERGE_USER not found in .env file. Please specify the username." fi docker exec papermerge_web create_token.sh "$PAPERMERGE_USER" positive "Token created for user $PAPERMERGE_USER" } # 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 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 # Set PAPERMERGE_PORT in .env file if provided if [ -n "$PAPERMERGE_PORT" ]; then 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 fi # If no action was performed, show usage if [ "$action_performed" = false ]; then usage fi ``` which makes life easier for me to manage a papermerge installation - and might be helpful for others too with the idea to hide the technical details of docker and papermerge that might change over time.
kerem 2026-02-25 21:32:02 +03:00
Author
Owner

@ciur commented on GitHub (Aug 25, 2024):

Thank you for sharing your script!

<!-- gh-comment-id:2308967696 --> @ciur commented on GitHub (Aug 25, 2024): Thank you for sharing your script!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/papermerge#490
No description provided.