From c21945ed672f8ee615da8890ce29e992ddb93301 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/common | 38 ++++++++++++++++++++++ snapcraft/hooks/configure | 39 ++--------------------- snapcraft/hooks/connect-plug-ceph-conf | 27 ++-------------- snapcraft/hooks/disconnect-plug-ceph-conf | 27 ++-------------- 4 files changed, 47 insertions(+), 84 deletions(-) create mode 100755 snapcraft/hooks/common diff --git a/snapcraft/hooks/common b/snapcraft/hooks/common new file mode 100755 index 000000000..e7a5de417 --- /dev/null +++ b/snapcraft/hooks/common @@ -0,0 +1,38 @@ +#!/bin/sh -eu + +# 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 + + # Invalid value (or not set) + return +} diff --git a/snapcraft/hooks/configure b/snapcraft/hooks/configure index c9ab24644..60fde44e9 100755 --- a/snapcraft/hooks/configure +++ b/snapcraft/hooks/configure @@ -5,42 +5,9 @@ if [ -d /sys/kernel/security/apparmor ] && [ "$(cat /proc/self/attr/current)" != 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 - - # Invalid value (or not set) - return -} +# shellcheck disable=SC2155 +export SNAP_CURRENT="$(realpath "${SNAP}/..")/current" +. "${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..91478f41b 100755 --- a/snapcraft/hooks/connect-plug-ceph-conf +++ b/snapcraft/hooks/connect-plug-ceph-conf @@ -1,31 +1,10 @@ #!/bin/sh -# 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 -} +# shellcheck disable=SC2155 +export SNAP_CURRENT="$(realpath "${SNAP}/..")/current" +. "${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..033260919 100755 --- a/snapcraft/hooks/disconnect-plug-ceph-conf +++ b/snapcraft/hooks/disconnect-plug-ceph-conf @@ -1,31 +1,10 @@ #!/bin/sh -# 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 -} +# shellcheck disable=SC2155 +export SNAP_CURRENT="$(realpath "${SNAP}/..")/current" +. "${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