/home/alex/dev/podman/unifi.sh (1)

From RaySoft
#!/bin/bash -
# ------------------------------------------------------------------------------
# unifi.sh
# ========
#
# Project   Replace Docker by Podman (UniFi Controller)
# Scope     Linux
# Copyright (C) 2024 by RaySoft, Zurich, Switzerland
# License   GNU General Public License (GPL) 2.0
#           https://www.gnu.org/licenses/gpl2.txt
#
# ------------------------------------------------------------------------------

set -o 'errexit' -o 'noglob' -o 'nounset' -o 'pipefail' # -o 'xtrace'

# ------------------------------------------------------------------------------

POD_NAME='unifi'

CONFIG_DIR='config'

STORAGE_PATH='/var/lib/containers/volumes'

# ------------------------------------------------------------------------------

# Define paths for the storage directories
config_storage_path="${STORAGE_PATH}/${POD_NAME}/${CONFIG_DIR////-}"

# Create storage directories
for dir in "${config_storage_path}"; do
  [[ ! -d "${dir}" ]] && sudo mkdir -p "${dir}"
done

# Set permissions for the main storage directorie
sudo find "${STORAGE_PATH}" \
           '(' -type 'd' -execdir chmod --changes 0755 '{}' '+' ')' \
       -or '(' -type 'f' -execdir chmod --changes 0644 '{}' '+' ')'

# Creating a dedicated user account
if ! id 'ubnt' >'/dev/null' 2>&1; then
  sudo useradd --shell='/bin/false' --system 'ubnt'
fi

# Get the UID and GID of the dedicated user account
uid="$(id -u 'ubnt')"
gid="$(id -g 'ubnt')"

# Create a new pod
sudo podman pod create \
  --name="${POD_NAME}" \
  --publish='3478:3478/udp' \
  --publish='8080:8080/tcp' \
  --publish='8443:8443/tcp' \
  --publish='10001:10001/udp' \
  --share='net'

# Create a new container in the pod
sudo podman run \
  --detach \
  --env='MEM_LIMIT=1024M' \
  --env="PGID=${gid}" \
  --env="PUID=${uid}" \
  --label 'io.containers.autoupdate=registry' \
  --name="${POD_NAME}-main" \
  --pod="${POD_NAME}" \
  --restart='always' \
  --volume="${config_storage_path}/:/${CONFIG_DIR}/:z" \
  'docker.io/linuxserver/unifi-controller:latest'

# Change to the script's directory
cd "${0%/*}"

# Generate the start scripts (one for the pod and one for each container)
sudo podman generate systemd --files --name --new "${POD_NAME}"

# Copy the start scripts
find . -maxdepth 1 -type 'f' -name "*${POD_NAME}*.service" \
  -execdir sudo cp --target-directory='/etc/systemd/system' '{}' '+'

# Reload the start scripts
sudo systemctl daemon-reload

# Stop the pod and its containers
sudo podman pod stop "${POD_NAME}"

# Remove the pod and its containers
sudo podman pod rm "${POD_NAME}"

# Enable and start the pod and its containers
sudo systemctl enable --now "pod-${POD_NAME}.service"

# Verify if the pod and its containers are running
sudo systemctl status "pod-${POD_NAME}.service"

# ------------------------------------------------------------------------------

exit 0

Usage

See UniFi Controller Howto