Skip to content

Commit

Permalink
Lint bash scripts with custom shebangs (home-assistant#3824)
Browse files Browse the repository at this point in the history
* Lint bash scripts with custom shebangs

Many scripts in this repository have this shebang:

    #!/usr/bin/with-contenv bashio

However, this doesn't match the shebang regex in action-shellcheck
and therefore those files wouldn't get included in the linting check.

* Fix or ignore current shellcheck violations

* shellcheck: specify bash shell type for scripts with unrecognized shebangs

* Convert shutdown script from CRLF line endings to LF

* shellcheck: silence remaining violations for now

* Apply suggestions from code review

* mariadb: avoid adding extra newlines

Co-authored-by: Stefan Agner <[email protected]>

* Fix GitHub syntax highlighting for bash scripts

---------

Co-authored-by: Stefan Agner <[email protected]>
  • Loading branch information
mislav and agners authored Dec 12, 2024
1 parent 21ac292 commit c9a0c15
Show file tree
Hide file tree
Showing 126 changed files with 306 additions and 18 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,21 @@ jobs:

- name: Run linter
uses: ludeeus/[email protected]
with:
# Names of scripts with custom shebangs that the action would not
# discover on its own:
additional_files: >-
check_certificate_renewal
cpcd-config-up
discovery
finish
lock-tables-for-backup
matter-server-discovery
reboot
run
shutdown
universal-silabs-flasher-up
unlock-tables-for-backup
vlc-discovery
ignore_paths: >-
google_assistant
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Take down the S6 supervision tree when service fails
# s6-overlay docs: https://github.com/just-containers/s6-overlay
# ==============================================================================
# shellcheck disable=SC2155
readonly exit_code_container=$(</run/s6-linux-init-container-results/exitcode)
readonly exit_code_service="${1}"
readonly exit_code_signal="${2}"
Expand All @@ -14,7 +16,7 @@ bashio::log.info \

if [[ "${exit_code_service}" -eq 256 ]]; then
if [[ "${exit_code_container}" -eq 0 ]]; then
echo $((128 + $exit_code_signal)) > /run/s6-linux-init-container-results/exitcode
echo $((128 + exit_code_signal)) > /run/s6-linux-init-container-results/exitcode
fi
[[ "${exit_code_signal}" -eq 15 ]] && exec /run/s6/basedir/bin/halt
elif [[ "${exit_code_service}" -ne 0 ]]; then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Sends discovery information to Home Assistant.
Expand Down
1 change: 1 addition & 0 deletions cec_scan/rootfs/etc/services.d/cec-scan/finish
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bashio
# vim: ft=bash
# ==============================================================================
# Take down the S6 supervision tree when service is done
# ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions cec_scan/rootfs/etc/services.d/cec-scan/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start CEC scan service
# ==============================================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Take down the S6 supervision tree when service fails
# s6-overlay docs: https://github.com/just-containers/s6-overlay
# ==============================================================================
# shellcheck disable=SC2155
readonly exit_code_container=$(</run/s6-linux-init-container-results/exitcode)
readonly exit_code_service="${1}"
readonly exit_code_signal="${2}"
Expand All @@ -14,7 +16,7 @@ bashio::log.info \

if [[ "${exit_code_service}" -eq 256 ]]; then
if [[ "${exit_code_container}" -eq 0 ]]; then
echo $((128 + $exit_code_signal)) > /run/s6-linux-init-container-results/exitcode
echo $((128 + exit_code_signal)) > /run/s6-linux-init-container-results/exitcode
fi
[[ "${exit_code_signal}" -eq 15 ]] && exec /run/s6/basedir/bin/halt
elif [[ "${exit_code_service}" -ne 0 ]]; then
Expand Down
2 changes: 2 additions & 0 deletions configurator/rootfs/etc/s6-overlay/s6-rc.d/configurator/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start Configurator service
# ==============================================================================
Expand Down
1 change: 1 addition & 0 deletions deconz/rootfs/etc/cont-init.d/firmware.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
# ==============================================================================
# Manage deCONZ firmware
# ==============================================================================
Expand Down
1 change: 1 addition & 0 deletions deconz/rootfs/etc/cont-init.d/nginx.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
# ==============================================================================
# Configure NGINX for use with deCONZ
# ==============================================================================
Expand Down
1 change: 1 addition & 0 deletions deconz/rootfs/etc/cont-init.d/novnc.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
# ==============================================================================
# Setup noVNC
# ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions deconz/rootfs/etc/services.d/deconz/discovery
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Send deCONZ discovery information to Home Assistant
# ==============================================================================
Expand Down
1 change: 1 addition & 0 deletions deconz/rootfs/etc/services.d/deconz/finish
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bashio
# vim: ft=bash
# ==============================================================================
# Take down the S6 supervision tree when service fails
# s6-overlay docs: https://github.com/just-containers/s6-overlay
Expand Down
2 changes: 2 additions & 0 deletions deconz/rootfs/etc/services.d/deconz/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start deCONZ service
# ==============================================================================
Expand Down
1 change: 1 addition & 0 deletions deconz/rootfs/etc/services.d/nginx/finish
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bashio
# vim: ft=bash
# ==============================================================================
# Take down the S6 supervision tree when service fails
# s6-overlay docs: https://github.com/just-containers/s6-overlay
Expand Down
2 changes: 2 additions & 0 deletions deconz/rootfs/etc/services.d/nginx/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start NGINX service
# ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions deconz/rootfs/etc/services.d/otau-ikea/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Download available firmware update for IKEA
# ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions deconz/rootfs/etc/services.d/otau-ledvance/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Download available firmware update for OSRAM Ledvance
# ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions deconz/rootfs/etc/services.d/websockify/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start ozw-admin
# ==============================================================================
Expand Down
1 change: 1 addition & 0 deletions dhcp_server/data/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
set -e

CONFIG="/etc/dhcpd.conf"
Expand Down
1 change: 1 addition & 0 deletions dnsmasq/rootfs/etc/cont-init.d/config.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
# ==============================================================================
# DNSMASQ config
# ==============================================================================
Expand Down
1 change: 1 addition & 0 deletions dnsmasq/rootfs/etc/services.d/dnsmasq/finish
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bashio
# vim: ft=bash
# ==============================================================================
# Take down the S6 supervision tree when service fails
# s6-overlay docs: https://github.com/just-containers/s6-overlay
Expand Down
2 changes: 2 additions & 0 deletions dnsmasq/rootfs/etc/services.d/dnsmasq/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
CONFIG="/etc/dnsmasq.conf"

# Run dnsmasq
Expand Down
4 changes: 3 additions & 1 deletion duckdns/rootfs/etc/s6-overlay/s6-rc.d/duckdns/finish
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Home Assistant Community Add-on: chrony
# Take down the S6 supervision tree when chrony fails
# ==============================================================================
# shellcheck disable=SC2155
readonly exit_code_container=$(</run/s6-linux-init-container-results/exitcode)
readonly exit_code_service="${1}"
readonly exit_code_signal="${2}"
Expand All @@ -15,7 +17,7 @@ bashio::log.info \

if [[ "${exit_code_service}" -eq 256 ]]; then
if [[ "${exit_code_container}" -eq 0 ]]; then
echo $((128 + $exit_code_signal)) > /run/s6-linux-init-container-results/exitcode
echo $((128 + exit_code_signal)) > /run/s6-linux-init-container-results/exitcode
fi
[[ "${exit_code_signal}" -eq 15 ]] && exec /run/s6/basedir/bin/halt
elif [[ "${exit_code_service}" -ne 0 ]]; then
Expand Down
2 changes: 2 additions & 0 deletions duckdns/rootfs/etc/s6-overlay/s6-rc.d/duckdns/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash

CERT_DIR=/data/letsencrypt
WORK_DIR=/data/workdir
Expand Down
1 change: 1 addition & 0 deletions letsencrypt/rootfs/etc/cont-init.d/file-structure.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
# ==============================================================================
# Init folder & structures
# ==============================================================================
Expand Down
1 change: 1 addition & 0 deletions letsencrypt/rootfs/etc/services.d/lets-encrypt/finish
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bashio
# vim: ft=bash
# ==============================================================================
# Take down the S6 supervision tree when service is done
# ==============================================================================
Expand Down
3 changes: 3 additions & 0 deletions letsencrypt/rootfs/etc/services.d/lets-encrypt/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start sshd service if enabled
# ==============================================================================
Expand Down Expand Up @@ -310,6 +312,7 @@ else
bashio::log.info "Detecting existing certificate type for ${DOMAIN_ARR[1]}"
readarray -t CBCERTS < <(certbot certificates --non-interactive --cert-name "${DOMAIN_ARR[1]}" --config-dir "$CERT_DIR" --work-dir "$WORK_DIR")
for output in "${CBCERTS[@]}"; do
# shellcheck disable=SC2076
if [[ $output =~ "No certificates found." ]]; then
bashio::log.info "No certificate found - using 'ecdsa' key type."
KEY_ARGUMENTS+=("--key-type" "ecdsa")
Expand Down
4 changes: 3 additions & 1 deletion mariadb/rootfs/etc/s6-overlay/s6-rc.d/mariadb-core/finish
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Take down the S6 supervision tree when daemon fails
# ==============================================================================
# shellcheck disable=SC2155
readonly exit_code_container=$(</run/s6-linux-init-container-results/exitcode)
readonly exit_code_service="${1}"
readonly exit_code_signal="${2}"
Expand All @@ -14,7 +16,7 @@ bashio::log.info \

if [[ "${exit_code_service}" -eq 256 ]]; then
if [[ "${exit_code_container}" -eq 0 ]]; then
echo $((128 + $exit_code_signal)) > /run/s6-linux-init-container-results/exitcode
echo $((128 + exit_code_signal)) > /run/s6-linux-init-container-results/exitcode
fi
[[ "${exit_code_signal}" -eq 15 ]] && exec /run/s6/basedir/bin/halt
elif [[ "${exit_code_service}" -ne 0 ]]; then
Expand Down
1 change: 1 addition & 0 deletions mariadb/rootfs/etc/s6-overlay/s6-rc.d/mariadb-core/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start MariaDB service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# MariaDB client (to lock tables for backups) stopped
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start MariaDB client (to lock tables for backups)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Unlock tables locked for backups
# ==============================================================================

declare MARIADB_LOCK_CORE_PID
MARIADB_LOCK_CORE_PID=$(s6-svstat -o pid "/run/service/mariadb-lock-core")
# shellcheck disable=SC2086
if bashio::var.equals ${MARIADB_LOCK_CORE_PID} -1; then
bashio::log.error "Invalid mariadb-lock-core process id ${MARIADB_LOCK_CORE_PID}"
bashio::exit.nok
fi

# File descriptor &4 is used as stdin for mysql, because &0 is closed after the mariadb-lock-core service is started
# shellcheck disable=SC2086
echo "UNLOCK TABLES;" > /proc/${MARIADB_LOCK_CORE_PID}/fd/4

bashio::log.info "MariaDB tables unlocked"
3 changes: 3 additions & 0 deletions mariadb/rootfs/etc/s6-overlay/s6-rc.d/mariadb-lock-post/run
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Lock tables for backups
# ==============================================================================

declare MARIADB_LOCK_CORE_PID
MARIADB_LOCK_CORE_PID=$(s6-svstat -o pid "/run/service/mariadb-lock-core")
# shellcheck disable=SC2086
if bashio::var.equals ${MARIADB_LOCK_CORE_PID} -1; then
bashio::log.error "Invalid mariadb-lock-core process id ${MARIADB_LOCK_CORE_PID}"
bashio::exit.nok
fi

# File descriptor &4 is used as stdin for mysql, because &0 is closed after the mariadb-lock-core service is started
# shellcheck disable=SC2086
echo "FLUSH TABLES WITH READ LOCK;" > /proc/${MARIADB_LOCK_CORE_PID}/fd/4

bashio::log.info "MariaDB tables locked"
1 change: 1 addition & 0 deletions mariadb/rootfs/etc/s6-overlay/s6-rc.d/mariadb-post/finish
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Post-start finalization of MariaDB service
Expand Down
3 changes: 3 additions & 0 deletions mariadb/rootfs/etc/s6-overlay/s6-rc.d/mariadb-post/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Post-start initialization of MariaDB service
Expand All @@ -8,7 +9,9 @@ function execute_without_error_messages() {
local current_log_level="${__BASHIO_LOG_LEVELS[${__BASHIO_LOG_LEVEL}]}"
bashio::log.level fatal
local exit_code=0
# shellcheck disable=SC2068
$@ || exit_code=$?
# shellcheck disable=SC2086
bashio::log.level ${current_log_level}
return ${exit_code}
}
Expand Down
5 changes: 3 additions & 2 deletions mariadb/rootfs/etc/s6-overlay/s6-rc.d/mariadb-pre/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Pre-start initialization of MariaDB service
Expand All @@ -11,8 +12,8 @@ if ! bashio::fs.directory_exists "${MARIADB_DATA}"; then
fi

# Save variables
printf "${MARIADB_DATA}" > /var/run/s6/container_environment/MARIADB_DATA
printf "${NEW_INSTALL}" > /var/run/s6/container_environment/NEW_INSTALL
echo -n "${MARIADB_DATA}" > /var/run/s6/container_environment/MARIADB_DATA
echo -n "${NEW_INSTALL}" > /var/run/s6/container_environment/NEW_INSTALL

# Redirect log output
mkdir -p "${MARIADB_DATA}"
Expand Down
1 change: 1 addition & 0 deletions mariadb/rootfs/usr/bin/lock-tables-for-backup
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Lock tables for backup
Expand Down
1 change: 1 addition & 0 deletions mariadb/rootfs/usr/bin/unlock-tables-for-backup
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Unlock tables for backup
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bashio
# vim: ft=bash
# ==============================================================================
# s6-overlay docs: https://github.com/just-containers/s6-overlay
# ==============================================================================
Expand Down
Loading

0 comments on commit c9a0c15

Please sign in to comment.