From 04a0e661e0642a0e3538df28cc3141af01515e5e Mon Sep 17 00:00:00 2001 From: Max Asnaashari Date: Wed, 20 Sep 2023 19:52:52 +0000 Subject: [PATCH] snapcraft/hooks: Share common functions among hooks Signed-off-by: Max Asnaashari --- snapcraft/hooks/configure | 42 ++++------------------- snapcraft/hooks/connect-plug-ceph-conf | 26 +++----------- snapcraft/hooks/disconnect-plug-ceph-conf | 26 +++----------- 3 files changed, 14 insertions(+), 80 deletions(-) diff --git a/snapcraft/hooks/configure b/snapcraft/hooks/configure index c9ab24644..bd48e3b77 100755 --- a/snapcraft/hooks/configure +++ b/snapcraft/hooks/configure @@ -1,46 +1,16 @@ -#!/bin/sh -eu +#!/bin/sh + +set -eu # Re-exec outside of apparmor confinement if [ -d /sys/kernel/security/apparmor ] && [ "$(cat /proc/self/attr/current)" != "unconfined" ]; then exec aa-exec -p unconfined -- "$0" "$@" fi -# Utility functions -get_bool() { - value=$(echo "${1:-}" | tr '[:upper:]' '[:lower:]') - - # See if it's true - for yes in "true" "1" "yes" "on"; do - if [ "${value}" = "${yes}" ]; then - echo "true" - return - fi - done - - # See if it's false - for no in "false" "0" "no" "off"; do - if [ "${value}" = "${no}" ]; then - echo "false" - return - fi - done - - # Invalid value (or not set) - return -} - -verify_int () { - value="${1:-}" - - # Verify if the value is a positive integer - if echo "${value}" | grep -Eq '^[0-9]+$'; then - echo "${value}" - return - fi +SNAP_CURRENT="$(realpath "${SNAP}/..")/current" +export "${SNAP_CURRENT}" - # Invalid value (or not set) - return -} +. "${SNAP_CURRENT}/snap/hooks/common" # Don't fail if the mount namespace isn't properly setup yet if [ ! -e /run/snapd-snap.socket ]; then diff --git a/snapcraft/hooks/connect-plug-ceph-conf b/snapcraft/hooks/connect-plug-ceph-conf index df2b58b2c..c717af24a 100755 --- a/snapcraft/hooks/connect-plug-ceph-conf +++ b/snapcraft/hooks/connect-plug-ceph-conf @@ -1,31 +1,13 @@ #!/bin/sh -# Utility functions -get_bool() { - value=$(echo "${1:-}" | tr '[:upper:]' '[:lower:]') +set -eu - # See if it's true - for yes in "true" "1" "yes" "on"; do - if [ "${value}" = "${yes}" ]; then - echo "true" - return - fi - done +SNAP_CURRENT="$(realpath "${SNAP}/..")/current" +export "${SNAP_CURRENT}" - # See if it's false - for no in "false" "0" "no" "off"; do - if [ "${value}" = "${no}" ]; then - echo "false" - return - fi - done - - # Invalid value (or not set) - return -} +. "${SNAP_CURRENT}/snap/hooks/common" ceph_builtin=$(get_bool "$(snapctl get ceph.builtin)") - if ! [ "${ceph_builtin:-"false"}" = "true" ]; then ln -snf ${SNAP_DATA}/microceph/conf/ /etc/ceph fi diff --git a/snapcraft/hooks/disconnect-plug-ceph-conf b/snapcraft/hooks/disconnect-plug-ceph-conf index b169f23d8..b6f6ef9bc 100755 --- a/snapcraft/hooks/disconnect-plug-ceph-conf +++ b/snapcraft/hooks/disconnect-plug-ceph-conf @@ -1,31 +1,13 @@ #!/bin/sh -# Utility functions -get_bool() { - value=$(echo "${1:-}" | tr '[:upper:]' '[:lower:]') +set -eu - # See if it's true - for yes in "true" "1" "yes" "on"; do - if [ "${value}" = "${yes}" ]; then - echo "true" - return - fi - done +SNAP_CURRENT="$(realpath "${SNAP}/..")/current" +export "${SNAP_CURRENT}" - # See if it's false - for no in "false" "0" "no" "off"; do - if [ "${value}" = "${no}" ]; then - echo "false" - return - fi - done - - # Invalid value (or not set) - return -} +. "${SNAP_CURRENT}/snap/hooks/common" ceph_builtin=$(get_bool "$(snapctl get ceph.builtin)") - if [ "${ceph_builtin:-"false"}" = "true" ]; then mkdir -p "${SNAP_COMMON}/ceph" ln -snf "${SNAP_COMMON}/ceph" /etc/ceph