Skip to content

Commit

Permalink
hooks/conf_regen: Fix some shellcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Salamandar committed Dec 21, 2024
1 parent e1a1452 commit f841bca
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
34 changes: 20 additions & 14 deletions hooks/conf_regen/34-mysql
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

set -e
. /usr/share/yunohost/helpers
# Exit hook on subcommand error or unset variable
set -Eeuo pipefail

# Source YNH helpers
# shellcheck source=../../helpers/helpers
source /usr/share/yunohost/helpers

if ! dpkg --list | grep -q 'ii *mariadb-server '; then
echo 'mysql/mariadb is not installed, skipping'
exit 0
fi

do_pre_regen() {
pending_dir=$1

#cd /usr/share/yunohost/conf/mysql

# Nothing to do
:
}

do_post_regen() {
Expand All @@ -44,12 +45,14 @@ do_post_regen() {
MYSQL_PKG="$(dpkg --list | sed -ne 's/^ii \(mariadb-server-[[:digit:].]\+\) .*$/\1/p')"
dpkg-reconfigure -freadline -u "$MYSQL_PKG" 2>&1

systemctl -q is-active mariadb.service \
|| systemctl start mariadb

if ! systemctl -q is-active mariadb.service; then
systemctl start mariadb
fi
sleep 5

echo "" | mysql && echo "Can't connect to mysql using unix_socket auth ... something went wrong during initial configuration of mysql !?" >&2
if ! echo "" | mysql; then
echo "Can't connect to mysql using unix_socket auth ... something went wrong during initial configuration of mysql !?" >&2
fi
fi

# mysql is supposed to be an alias to mariadb... but in some weird case is not
Expand All @@ -60,11 +63,14 @@ do_post_regen() {
systemctl disable mysql -q
systemctl disable mariadb -q
systemctl enable mariadb -q
systemctl is-active mariadb -q || systemctl start mariadb
if ! systemctl is-active mariadb -q; then
systemctl start mariadb
fi
fi

[[ -z "$regen_conf_files" ]] \
|| systemctl restart mysql
if [[ -n "$regen_conf_files" ]]; then
systemctl restart mysql
fi
}

do_$1_regen ${@:2}
"do_$1_regen" "${@:2}"
34 changes: 20 additions & 14 deletions hooks/conf_regen/35-postgresql
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

set -e
. /usr/share/yunohost/helpers
# Exit hook on subcommand error or unset variable
set -Eeuo pipefail

# Source YNH helpers
# shellcheck source=../../helpers/helpers
source /usr/share/yunohost/helpers

if ! dpkg --list | grep -q "ii *postgresql-$PSQL_VERSION "; then
echo 'postgresql is not installed, skipping'
Expand All @@ -31,7 +35,8 @@ if [ ! -e "/etc/postgresql/$PSQL_VERSION" ]; then
fi

do_pre_regen() {
return 0
# Nothing to do
:
}

do_post_regen() {
Expand All @@ -41,21 +46,22 @@ do_post_regen() {
# (N.B. : to check the active state, we check the cluster state because
# postgresql could be flagged as active even though the cluster is in
# failed state because of how the service is configured..)
systemctl is-active postgresql@$PSQL_VERSION-main -q || ynh_systemd_action --service_name=postgresql --action=restart
systemctl is-enabled postgresql -q || systemctl enable postgresql --quiet
if ! systemctl is-active "postgresql@$PSQL_VERSION-main" -q; then
ynh_systemd_action --service_name=postgresql --action=restart
fi
if ! systemctl is-enabled postgresql -q; then
systemctl enable postgresql --quiet
fi

# If this is the very first time, we define the root password
# and configure a few things
if [ ! -f "$PSQL_ROOT_PWD_FILE" ] || [ -z "$(cat $PSQL_ROOT_PWD_FILE)" ]; then
ynh_string_random > $PSQL_ROOT_PWD_FILE
if [ ! -f "$PSQL_ROOT_PWD_FILE" ] || [ ! -s "$PSQL_ROOT_PWD_FILE" ]; then
ynh_string_random > "$PSQL_ROOT_PWD_FILE"
fi
chown root:postgres "$PSQL_ROOT_PWD_FILE"
chmod 440 "$PSQL_ROOT_PWD_FILE"

[ ! -e $PSQL_ROOT_PWD_FILE ] || {
chown root:postgres $PSQL_ROOT_PWD_FILE
chmod 440 $PSQL_ROOT_PWD_FILE
}

sudo --login --user=postgres psql -c"ALTER user postgres WITH PASSWORD '$(cat $PSQL_ROOT_PWD_FILE)'" postgres
sudo --login --user=postgres psql -c"ALTER user postgres WITH PASSWORD '$(cat "$PSQL_ROOT_PWD_FILE")'" postgres

# force all user to connect to local databases using hashed passwords
# https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html#EXAMPLE-PG-HBA.CONF
Expand All @@ -67,4 +73,4 @@ do_post_regen() {
ynh_systemd_action --service_name=postgresql --action=reload
}

do_$1_regen ${@:2}
"do_$1_regen" "${@:2}"
9 changes: 6 additions & 3 deletions hooks/conf_regen/37-mdns
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ do_pre_regen() {
mkdir -p "${pending_dir}/etc/systemd/system/"
cp yunomdns.service "${pending_dir}/etc/systemd/system/"

getent passwd mdns &> /dev/null || useradd --no-create-home --shell /usr/sbin/nologin --system --user-group mdns
if ! getent passwd mdns &>/dev/null; then
useradd --no-create-home --shell /usr/sbin/nologin --system --user-group mdns
fi

mkdir -p "$pending_dir/etc/yunohost"
_generate_config > "$pending_dir/etc/yunohost/mdns.yml"
Expand All @@ -75,8 +77,9 @@ do_post_regen() {
sleep 2
fi

[[ -z "$regen_conf_files" ]] \
|| systemctl restart yunomdns
if [[ -n "$regen_conf_files" ]]; then
systemctl restart yunomdns
fi
}

"do_$1_regen" "${@:2}"
5 changes: 3 additions & 2 deletions hooks/conf_regen/52-fail2ban
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ do_post_regen() {
chmod 644 /etc/fail2ban/jail.d/*.conf
fi

[[ -z "$regen_conf_files" ]] \
|| systemctl reload fail2ban
if [[ -n "$regen_conf_files" ]]; then
systemctl reload fail2ban
fi
}

"do_$1_regen" "${@:2}"

0 comments on commit f841bca

Please sign in to comment.