Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snapcraft/hooks: Share common functions among hooks #154

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 6 additions & 36 deletions snapcraft/hooks/configure
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 4 additions & 22 deletions snapcraft/hooks/connect-plug-ceph-conf
Original file line number Diff line number Diff line change
@@ -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
26 changes: 4 additions & 22 deletions snapcraft/hooks/disconnect-plug-ceph-conf
Original file line number Diff line number Diff line change
@@ -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
Expand Down