particle-os-modules/modules/brew/brew-nofile-limits-logic.sh
Thomas Mecattaf a7e074fe36
feat: Add Brew module (#239)
* feat(brew): add new Brew module for Homebrew installation and management

* feat(brew): add new Brew module for Homebrew installation and management

* feat(brew): part 2 of adding new Brew module for Homebrew installation and management

* feat(brew): part 2.2 of adding new Brew module for Homebrew installation and management

* feat(brew): part 3 of adding new Brew module for Homebrew installation and management

* docs: Add kebab-cases for module entries in README

* docs: Don't add whitespaces in module.yml

* chore: Use arrays for package list & add quotes + brackets to some strings

to make it more safe.

* feat(brew): systemd services and timers written dynamically

* feat(brew): systemd services and timers written dynamically 2

* feat(brew): clean up fish completion file name

* chore: Remove duplicate brew completions file

* feat(brew): tweak in brew.sh 3

* chore: Rename shell completion to completions pt. 1

* chore: Rename shell completion to completions pt. 2

* chore: Rename shell completion to completions pt. 3

* feat(brew): Nofile Limits flag, update brew.sh module.yml and README.md

* feat(brew): Brew analytics opt-out flag

* Brew Analytics Enabled by default in docs

* docs: `build-time` instead of `build time`

* docs: Improve `gcc` package log explanation

* fix(brew): Fetching YAML entries due to miss of transition from `_` to `-`

* chore(brew): Improve disabling analytics write to `environment`

* chore(brew): Add script for modifying nofile limit

* chore(brew): Forgot to source the nofile script

* chore(brew): Disable option of installing packages pt.1

* chore(brew): Disable option of installing packages pt.2

* chore(brew): Disable option of installing packages pt.3

* docs(brew): Remove installing packages as a feature

* chore(brew): Add more logs

* chore(brew): Fix typos in logs

* fix(brew): Use `awk` instead of `grep` which fails for stupid reason

Without any error logs either.

* fix(brew): Update & upgrade permutation

* docs(brew): Fix upgrade & update permutation

* chore(brew): Make small tweaks to brew-analytics

If environment is empty, than don't make the extra whiteline

* docs(brew): Slight order adjustment in README inside "Features"

* docs(brew): Add link to Homebrew

* docs(brew): Add note about uninstalling brew

* docs(brew): Include explanation on why manual uninstallation is necessary

Also give big thanks to M2

* docs(brew): Further explain on why the manual uninstallation is needed

* docs(brew): Add link to files module documentation

* docs(brew): Be more detailed about how the module works

* docs(brew): Some small fixes & restore credits

* docs(brew): Rename Brew to Linuxbrew

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>

* docs(brew): Change features wording

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>

* docs(brew): Reword update & upgrade wording

* docs(brew): Move update & upgrade service labels above timers

* docs(brew): Change brew documentation wording

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>

* docs(brew): Syntax highlighting for bash script

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>

* docs(brew): Improve uninstallation intro wording

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>

* docs(brew): Move brew-analytics above warning

* docs(brew): Update update/upgrade ordering pt. 1

* docs(brew): Update update/upgrade naming pt. 2

* docs(brew): Update update/upgrade order pt. 3

* chore(brew): Use `#!/usr/bin/env fish` instead of `#!/usr/bin/fish`

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>

* chore(brew): Use `#!/usr/bin/env bash` instead of `#!/bin/sh`

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>

* chore(brew): Set shellcheck from sh to bash

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>

* fix(brew): Convert SH-ism to bash in bash completion

* chore(brew): Use double brackets in bash shell completion

* chore(brew): Make rc in brackets

* chore(brew): Use more double brackets

* chore(brew): Add more double brackets pt. 2

* chore(brew): Make bash shell completion spacing better

* chore(brew): Revert to `bash-completions` completion

* docs(brew): Add comment about why bash completion uses sh

* fix(brew): Apply brew path clash fix

* docs(brew): Explain brew path clash fix

* docs(brew): Add file location of brew path fix

* docs(brew): Reword exporting brew path

* docs(brew): Reword again

* docs(brew): Reword...

* chore(brew): Copy bash completion to `/usr/etc` instead of `/etc`

* chore(brew): Make `/usr/etc/profile.d` if it doesn't exist

* docs(brew): Fix typo in README

* docs(brew): Use caution card instead of warning string

* docs(brew): Brew path reword

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>

* chore(brew): Implement code comment fix

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>

* docs(brew): Try to improve `nofile-limits` section

* docs(brew): Use serverfault link instead

* docs(brew): Thread, not article

* docs(brew): Improve Configuration section

* docs(brew): Than vs then

Love to mess this one

* docs(brew): Uninstall script wording fix

---------

Co-authored-by: fiftydinar <65243233+fiftydinar@users.noreply.github.com>
Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>
2024-06-03 09:25:45 +00:00

234 lines
11 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
# Script used to read nofile limits from the base image, since issuing easy systemctl status commands don't work in build-time.
# Takes into account config priorities & current config settings regarding nofile limits
# (if nofile limit is already applied with appropriate values in the base image, than this tweak is not applied)
# Modifies limits nofile value & systemd DefaultLimitNOFILE value
DESIRED_SOFT_LIMIT=4096
DESIRED_HARD_LIMIT=524288
BREW_LIMITS_D_CONFIG="/usr/etc/security/limits.d/zz1-brew-limits.conf"
BREW_SYSTEMD_SYSTEM_CONFIG="/usr/lib/systemd/system.conf.d/zz1-brew-limits.conf"
BREW_SYSTEMD_USER_CONFIG="/usr/lib/systemd/user.conf.d/zz1-brew-limits.conf"
# SSH/TTY nofile limit (security ulimit config)
# From least to most preferred
SSH_TTY_LIMIT_ORDER=(
"/usr/etc/security/limits.conf"
"/usr/etc/security/limits.d/"
"/etc/security/limits.conf"
"/etc/security/limits.d/"
)
updated_ssh_array=()
for path in "${SSH_TTY_LIMIT_ORDER[@]}"; do
if [ -e "${path}" ]; then
updated_ssh_array+=("${path}")
fi
done
# Update the original array with the existing paths
SSH_TTY_LIMIT_ORDER=("${updated_ssh_array[@]}")
# Soft SSH/TTY nofile limit
SSH_TTY_SOFT_INFO=$(find "${SSH_TTY_LIMIT_ORDER[@]}" -type f -name "*.conf" -exec awk -v OFS='\t' '/soft\s+nofile/ && !/^#/ {sub(/.*nofile/, ""); gsub(/^[ \t]+/, ""); print FILENAME, $0}' {} + | tail -n 1)
if [[ -n "${SSH_TTY_SOFT_INFO}" ]]; then
CURRENT_SSH_TTY_SOFT_VALUE=$(echo "${SSH_TTY_SOFT_INFO}" | awk '{print $2}')
else
CURRENT_SSH_TTY_SOFT_VALUE=0
fi
# Hard SSH/TTY nofile limit
SSH_TTY_HARD_INFO=$(find "${SSH_TTY_LIMIT_ORDER[@]}" -type f -name "*.conf" -exec awk -v OFS='\t' '/hard\s+nofile/ && !/^#/ {sub(/.*nofile/, ""); gsub(/^[ \t]+/, ""); print FILENAME, $0}' {} + | tail -n 1)
if [[ -n "${SSH_TTY_HARD_INFO}" ]]; then
CURRENT_SSH_TTY_HARD_VALUE=$(echo "${SSH_TTY_HARD_INFO}" | awk '{print $2}')
else
CURRENT_SSH_TTY_HARD_VALUE=0
fi
# SystemD nofile limit
# SystemD system soft & hard nofile limit
# From least to most preferred
SYSTEMD_SYSTEM_LIMIT_ORDER=(
"/usr/lib/systemd/system.conf"
"/usr/lib/systemd/system.conf.d/"
"/usr/etc/systemd/system.conf"
"/usr/etc/systemd/system.conf.d/"
"/etc/systemd/system.conf"
"/etc/systemd/system.conf.d/"
)
updated_systemd_system_array=()
for path in "${SYSTEMD_SYSTEM_LIMIT_ORDER[@]}"; do
if [ -e "${path}" ]; then
updated_systemd_system_array+=("${path}")
fi
done
# Update the original array with the existing paths
SYSTEMD_SYSTEM_LIMIT_ORDER=("${updated_systemd_system_array[@]}")
# SystemD system soft & hard nofile limit
SYSTEMD_SYSTEM_SOFT_INFO=$(find "${SYSTEMD_SYSTEM_LIMIT_ORDER[@]}" -type f -name "*.conf" -exec awk -F'[:=]' '/^[^#]*DefaultLimitNOFILE/ {print FILENAME, $2}' {} + | tail -n 1)
if [[ -n "${SYSTEMD_SYSTEM_SOFT_INFO}" ]]; then
CURRENT_SYSTEMD_SYSTEM_SOFT_VALUE=$(echo "${SYSTEMD_SYSTEM_SOFT_INFO}" | awk '{print $2}')
else
CURRENT_SYSTEMD_SYSTEM_SOFT_VALUE=0
fi
SYSTEMD_SYSTEM_HARD_INFO=$(find "${SYSTEMD_SYSTEM_LIMIT_ORDER[@]}" -type f -name "*.conf" -exec awk -F'[:=]' '/^[^#]*DefaultLimitNOFILE/ {print FILENAME, $3}' {} + | tail -n 1)
if [[ -n "${SYSTEMD_SYSTEM_HARD_INFO}" ]]; then
CURRENT_SYSTEMD_SYSTEM_HARD_VALUE=$(echo "${SYSTEMD_SYSTEM_HARD_INFO}" | awk '{print $2}')
else
CURRENT_SYSTEMD_SYSTEM_HARD_VALUE=0
fi
# SystemD user soft & hard nofile limit
SYSTEMD_USER_LIMIT_ORDER=(
"/usr/lib/systemd/user.conf"
"/usr/lib/systemd/user.conf.d/"
"/usr/etc/systemd/user.conf"
"/usr/etc/systemd/user.conf.d/"
"/etc/systemd/user.conf"
"/etc/systemd/user.conf.d/"
)
updated_systemd_user_array=()
for path in "${SYSTEMD_USER_LIMIT_ORDER[@]}"; do
if [ -e "${path}" ]; then
updated_systemd_user_array+=("${path}")
fi
done
# Update the original array with the existing paths
SYSTEMD_USER_LIMIT_ORDER=("${updated_systemd_user_array[@]}")
SYSTEMD_USER_SOFT_INFO=$(find "${SYSTEMD_USER_LIMIT_ORDER[@]}" -type f -name "*.conf" -exec awk -F'[:=]' '/^[^#]*DefaultLimitNOFILE/ {print FILENAME, $2}' {} + | tail -n 1)
if [[ -n "${SYSTEMD_USER_SOFT_INFO}" ]]; then
CURRENT_SYSTEMD_USER_SOFT_VALUE=$(echo "${SYSTEMD_USER_SOFT_INFO}" | awk '{print $2}')
else
CURRENT_SYSTEMD_USER_SOFT_VALUE=0
fi
SYSTEMD_USER_HARD_INFO=$(find "${SYSTEMD_USER_LIMIT_ORDER[@]}" -type f -name "*.conf" -exec awk -F'[:=]' '/^[^#]*DefaultLimitNOFILE/ {print FILENAME, $3}' {} + | tail -n 1)
if [[ -n "${SYSTEMD_USER_HARD_INFO}" ]]; then
CURRENT_SYSTEMD_USER_HARD_VALUE=$(echo "${SYSTEMD_USER_HARD_INFO}" | awk '{print $2}')
else
CURRENT_SYSTEMD_USER_HARD_VALUE=0
fi
# Check current state
echo "Current nofile limit values:"
check_and_print() {
if [[ "${1}" -eq 0 ]]; then
echo "UNSET"
else
echo "${1}"
fi
}
echo "SSH/TTY soft nofile limit: $(check_and_print ${CURRENT_SSH_TTY_SOFT_VALUE})"
echo "SSH/TTY hard nofile limit: $(check_and_print ${CURRENT_SSH_TTY_HARD_VALUE})"
echo "SystemD system soft nofile limit: $(check_and_print ${CURRENT_SYSTEMD_SYSTEM_SOFT_VALUE})"
echo "SystemD system hard nofile limit: $(check_and_print ${CURRENT_SYSTEMD_SYSTEM_HARD_VALUE})"
echo "SystemD user soft nofile limit: $(check_and_print ${CURRENT_SYSTEMD_USER_SOFT_VALUE})"
echo "SystemD user hard nofile limit: $(check_and_print ${CURRENT_SYSTEMD_USER_HARD_VALUE})"
# Write nofile limit values
# zz1- prefix is used for config, to assure that nofile limit is going to be applied, as it's high in lexical order.
# If config is higher in lexical order, that means that it's preferred over other ones.
# Downstreams can just go on with their own modifications in zz2-, zz3-, zz4-, zz5-, etc. increments
# if they wish to experiment with nofile limits differently
# Write SSH/TTY nolimit values
if [[ "${CURRENT_SSH_TTY_SOFT_VALUE}" -lt "${DESIRED_SOFT_LIMIT}" ]] || [[ "${CURRENT_SSH_TTY_HARD_VALUE}" -lt "${DESIRED_HARD_LIMIT}" ]]; then
if [[ ! -d "/usr/etc/security/limits.d/" ]]; then
mkdir -p "/usr/etc/security/limits.d/"
fi
echo "# This file sets the resource limits for users logged in via PAM,
# more specifically, users logged in via SSH or tty (console).
# Limits related to terminals in Wayland/Xorg sessions depend on a
# change to /etc/systemd/user.conf.
# This does not affect resource limits of the system services.
# This file overrides defaults set in /etc/security/limits.conf
" > "${BREW_LIMITS_D_CONFIG}"
fi
if [[ "${CURRENT_SSH_TTY_SOFT_VALUE}" -lt "${DESIRED_SOFT_LIMIT}" ]] && [[ "${CURRENT_SSH_TTY_HARD_VALUE}" -ge "${DESIRED_HARD_LIMIT}" ]]; then
echo "Writing increased SSH/TTY soft nofile limit value"
echo "* soft nofile ${DESIRED_SOFT_LIMIT}" >> "${BREW_LIMITS_D_CONFIG}"
elif [[ "${CURRENT_SSH_TTY_SOFT_VALUE}" -ge "${DESIRED_SOFT_LIMIT}" ]]; then
echo "Required SSH/TTY soft nofile limit value is already satisfied!"
fi
if [[ "${CURRENT_SSH_TTY_SOFT_VALUE}" -ge "${DESIRED_SOFT_LIMIT}" ]] && [[ "${CURRENT_SSH_TTY_HARD_VALUE}" -lt "${DESIRED_HARD_LIMIT}" ]]; then
echo "Writing increased SSH/TTY hard nofile limit value"
echo "* hard nofile ${DESIRED_HARD_LIMIT}" >> "${BREW_LIMITS_D_CONFIG}"
elif [[ "${CURRENT_SSH_TTY_HARD_VALUE}" -ge "${DESIRED_HARD_LIMIT}" ]]; then
echo "Required SSH/TTY hard nofile limit value is already satisfied!"
fi
if [[ "${CURRENT_SSH_TTY_SOFT_VALUE}" -ge "${DESIRED_SOFT_LIMIT}" ]] && [[ "${CURRENT_SSH_TTY_HARD_VALUE}" -lt "${DESIRED_HARD_LIMIT}" ]]; then
echo "Writing increased SSH/TTY soft & hard nofile limit value"
echo "* soft nofile ${DESIRED_SOFT_LIMIT}" >> "${BREW_LIMITS_D_CONFIG}"
echo "* hard nofile ${DESIRED_HARD_LIMIT}" >> "${BREW_LIMITS_D_CONFIG}"
fi
# Write SystemD nolimit values
# Writing SystemD system nolimit values
if [[ "${CURRENT_SYSTEMD_SYSTEM_SOFT_VALUE}" -lt "${DESIRED_SOFT_LIMIT}" ]] || [[ "${CURRENT_SYSTEMD_SYSTEM_HARD_VALUE}" -lt "${DESIRED_HARD_LIMIT}" ]]; then
if [[ ! -d "/usr/lib/systemd/system.conf.d/" ]]; then
mkdir -p "/usr/lib/systemd/system.conf.d/"
fi
fi
if [[ "${CURRENT_SYSTEMD_SYSTEM_SOFT_VALUE}" -lt "${DESIRED_SOFT_LIMIT}" ]] && [[ "${CURRENT_SYSTEMD_SYSTEM_HARD_VALUE}" -ge "${DESIRED_HARD_LIMIT}" ]]; then
echo "Writing increased SystemD system soft nofile limit value"
echo "[Manager]
DefaultLimitNOFILE=${DESIRED_SOFT_LIMIT}:${CURRENT_SYSTEMD_SYSTEM_HARD_VALUE}" > "${BREW_SYSTEMD_SYSTEM_CONFIG}"
elif [[ "${CURRENT_SYSTEMD_SYSTEM_SOFT_VALUE}" -ge "${DESIRED_SOFT_LIMIT}" ]]; then
echo "Required SystemD system soft nofile limit value is already satisfied!"
fi
if [[ "${CURRENT_SYSTEMD_SYSTEM_SOFT_VALUE}" -ge "${DESIRED_SOFT_LIMIT}" ]] && [[ "${CURRENT_SYSTEMD_SYSTEM_HARD_VALUE}" -lt "${DESIRED_HARD_LIMIT}" ]]; then
echo "Writing increased SystemD system hard nofile limit value"
echo "[Manager]
DefaultLimitNOFILE=${CURRENT_SYSTEMD_SYSTEM_SOFT_VALUE}:${DESIRED_HARD_LIMIT}" > "${BREW_SYSTEMD_SYSTEM_CONFIG}"
elif [[ "${CURRENT_SYSTEMD_SYSTEM_HARD_VALUE}" -ge "${DESIRED_HARD_LIMIT}" ]]; then
echo "Required SystemD system hard nofile limit value is already satisfied!"
fi
if [[ "${CURRENT_SYSTEMD_SYSTEM_SOFT_VALUE}" -lt "${DESIRED_SOFT_LIMIT}" ]] && [[ "${CURRENT_SYSTEMD_SYSTEM_HARD_VALUE}" -lt "${DESIRED_HARD_LIMIT}" ]]; then
echo "Writing increased SystemD system soft & hard nofile limit value"
echo "[Manager]
DefaultLimitNOFILE=${DESIRED_SOFT_LIMIT}:${DESIRED_HARD_LIMIT}" > "${BREW_SYSTEMD_SYSTEM_CONFIG}"
fi
# Writing SystemD user nolimit values
if [[ "${CURRENT_SYSTEMD_USER_SOFT_VALUE}" -lt "${DESIRED_SOFT_LIMIT}" ]] || [[ "${CURRENT_SYSTEMD_USER_HARD_VALUE}" -lt "${DESIRED_HARD_LIMIT}" ]]; then
if [[ ! -d "/usr/lib/systemd/user.conf.d/" ]]; then
mkdir -p "/usr/lib/systemd/user.conf.d/"
fi
fi
if [[ "${CURRENT_SYSTEMD_USER_SOFT_VALUE}" -lt "${DESIRED_SOFT_LIMIT}" ]] && [[ "${CURRENT_SYSTEMD_USER_HARD_VALUE}" -ge "${DESIRED_HARD_LIMIT}" ]]; then
echo "Writing increased SystemD user soft nofile limit value"
echo "[Manager]
DefaultLimitNOFILE=${DESIRED_SOFT_LIMIT}:${CURRENT_SYSTEMD_USER_HARD_VALUE}" > "${BREW_SYSTEMD_USER_CONFIG}"
elif [[ "${CURRENT_SYSTEMD_USER_SOFT_VALUE}" -ge "${DESIRED_SOFT_LIMIT}" ]]; then
echo "Required SystemD user soft nofile limit value is already satisfied!"
fi
if [[ "${CURRENT_SYSTEMD_USER_SOFT_VALUE}" -ge "${DESIRED_SOFT_LIMIT}" ]] && [[ "${CURRENT_SYSTEMD_USER_HARD_VALUE}" -lt "${DESIRED_HARD_LIMIT}" ]]; then
echo "Writing increased SystemD user hard nofile limit value"
echo "[Manager]
DefaultLimitNOFILE=${CURRENT_SYSTEMD_USER_SOFT_VALUE}:${DESIRED_HARD_LIMIT}" > "${BREW_SYSTEMD_USER_CONFIG}"
elif [[ "${CURRENT_SYSTEMD_USER_HARD_VALUE}" -ge "${DESIRED_HARD_LIMIT}" ]]; then
echo "Required SystemD user hard nofile limit value is already satisfied!"
fi
if [[ "${CURRENT_SYSTEMD_USER_SOFT_VALUE}" -lt "${DESIRED_SOFT_LIMIT}" ]] && [[ "${CURRENT_SYSTEMD_USER_HARD_VALUE}" -lt "${DESIRED_HARD_LIMIT}" ]]; then
echo "Writing increased SystemD user soft & hard nofile limit value"
echo "[Manager]
DefaultLimitNOFILE=${DESIRED_SOFT_LIMIT}:${DESIRED_HARD_LIMIT}" > "${BREW_SYSTEMD_USER_CONFIG}"
fi