Skip to content

Commit

Permalink
snapcraft/hooks: Share common functions among hooks
Browse files Browse the repository at this point in the history
Signed-off-by: Max Asnaashari <[email protected]>
  • Loading branch information
masnax committed Sep 20, 2023
1 parent 440402d commit c21945e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 84 deletions.
38 changes: 38 additions & 0 deletions snapcraft/hooks/common
Original file line number Diff line number Diff line change
@@ -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
}
39 changes: 3 additions & 36 deletions snapcraft/hooks/configure
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 3 additions & 24 deletions snapcraft/hooks/connect-plug-ceph-conf
Original file line number Diff line number Diff line change
@@ -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
27 changes: 3 additions & 24 deletions snapcraft/hooks/disconnect-plug-ceph-conf
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit c21945e

Please sign in to comment.