Skip to content

Commit

Permalink
add use_snap_env helper function and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
neoaggelos committed Aug 14, 2023
1 parent feb939d commit 42ff182
Show file tree
Hide file tree
Showing 39 changed files with 188 additions and 270 deletions.
78 changes: 53 additions & 25 deletions microk8s-resources/actions/common/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1152,31 +1152,6 @@ sign_certificate() {
echo "$csr" | ${SNAP}/usr/bin/openssl x509 -req -sha256 -CA "${SNAP_DATA}/certs/ca.crt" -CAkey "${SNAP_DATA}/certs/ca.key" -CAcreateserial -days 3650 -extfile <(echo "${extensions}")
}

# check if this file is run with arguments
if [[ "$0" == "${BASH_SOURCE}" ]] &&
[[ ! -z "$1" ]]
then
# call help
if echo "$*" | grep -q -- 'help'; then
echo "usage: $0 [function]"
echo ""
echo "Run a utility function and return the output."
echo ""
echo "available functions:"
declare -F | gawk '{print "- "$3}'
exit 0
fi

if declare -F "$1" > /dev/null
then
$1 ${@:2}
exit $?
else
echo "Function does not exist: $1" >&2
exit 1
fi
fi

exit_if_low_memory_guard() {
if [ -e ${SNAP_DATA}/var/lock/low-memory-guard.lock ]
then
Expand Down Expand Up @@ -1385,3 +1360,56 @@ increase_sysctl_parameter() {
fi
fi
}

use_snap_env() {
# Configure snap paths for PATH LD_LIBRARY_PATH
export PATH="$SNAP/bin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/usr/sbin:$PATH"
export LD_LIBRARY_PATH="$SNAP_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$SNAPCRAFT_ARCH_TRIPLET:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/ceph:${LD_LIBRARY_PATH:-}"
export OPENSSL_CONF="$SNAP/etc/ssl/openssl.cnf"

# Python configuration
export PYTHONPATH="$SNAP/usr/lib/python3.8:$SNAP/lib/python3.8/site-packages:$SNAP/usr/lib/python3/dist-packages"
export PYTHONNOUSERSITE=false

# NOTE(neoaggelos/2023-08-14):
# we cannot list system locales from snap. instead, we attempt
# well-known locales for Ubuntu/Debian/CentOS and check whether
# they are available on the system.
# if they are, set them for the current shell.
for locale in C.UTF-8 en_US.UTF-8 en_US.utf8; do
if [ -z "$(export LC_ALL=$locale 2>&1)" ]; then
export LC_ALL="${LC_ALL:-$locale}"
export LANG="${LANG:-$locale}"
break
fi
done

# Configure XDG_RUNTIME_DIR
export XDG_RUNTIME_DIR="${SNAP_COMMON}/run"
mkdir -p "${XDG_RUNTIME_DIR}"
}

# check if this file is run with arguments
if [[ "$0" == "${BASH_SOURCE}" ]] &&
[[ ! -z "$1" ]]
then
# call help
if echo "$*" | grep -q -- 'help'; then
echo "usage: $0 [function]"
echo ""
echo "Run a utility function and return the output."
echo ""
echo "available functions:"
declare -F | gawk '{print "- "$3}'
exit 0
fi

if declare -F "$1" > /dev/null
then
$1 ${@:2}
exit $?
else
echo "Function does not exist: $1" >&2
exit 1
fi
fi
11 changes: 3 additions & 8 deletions microk8s-resources/wrappers/apiservice-kicker
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#!/bin/bash
#!/usr/bin/env bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
ARCH="$($SNAP/bin/uname -m)"
export LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export LD_LIBRARY_PATH=$SNAP_LIBRARY_PATH:$LD_LIBRARY_PATH
export LC_ALL="${LC_ALL:-C.UTF-8}"
export LANG="${LANG:-C.UTF-8}"

source $SNAP/actions/common/utils.sh

use_snap_env

if [ -e ${SNAP_DATA}/var/lock/clustered.lock ]
then
echo "api service kicker will not run on a cluster node"
Expand Down
7 changes: 5 additions & 2 deletions microk8s-resources/wrappers/git.wrapper
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/bin/bash
#!/usr/bin/env bash

. $SNAP/actions/common/utils.sh

use_snap_env

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
export GIT_EXEC_PATH="$SNAP/usr/lib/git-core"
export GIT_TEMPLATE_DIR="$SNAP/usr/share/git-core/templates"
export GIT_CONFIG_NOSYSTEM=1
Expand Down
14 changes: 5 additions & 9 deletions microk8s-resources/wrappers/microk8s-add-node.wrapper
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#!/bin/bash
#!/usr/bin/env bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
ARCH="$($SNAP/bin/uname -m)"
export LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export IN_SNAP_LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export PYTHONNOUSERSITE=false

source $SNAP/actions/common/utils.sh

use_snap_env

if [ -e ${SNAP_DATA}/var/lock/clustered.lock ]
then
echo "This MicroK8s deployment is acting as a node in a cluster. Please use the microk8s add-node on the master."
Expand All @@ -18,7 +14,7 @@ fi

if echo "$*" | grep -q -- 'help'; then
# Call add_token.py help
LD_LIBRARY_PATH=$IN_SNAP_LD_LIBRARY_PATH ${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/add_token.py --help
${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/add_token.py --help
exit 0
fi

Expand Down Expand Up @@ -55,4 +51,4 @@ then
fi

# Use python's built-in (3.6+) secrets generator to produce the token.
LD_LIBRARY_PATH=$IN_SNAP_LD_LIBRARY_PATH ${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/add_token.py "${@}"
${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/add_token.py "${@}"
12 changes: 3 additions & 9 deletions microk8s-resources/wrappers/microk8s-addons.wrapper
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#!/bin/bash
#!/usr/bin/env bash

set -eu

source $SNAP/actions/common/utils.sh

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"

ARCH="$($SNAP/bin/uname -m)"
export IN_SNAP_LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export PYTHONNOUSERSITE=false
export LC_ALL="${LC_ALL:-C.UTF-8}"
export LANG="${LANG:-C.UTF-8}"
use_snap_env

exit_if_no_permissions

LD_LIBRARY_PATH=$IN_SNAP_LD_LIBRARY_PATH ${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/addons.py "${@}"
${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/addons.py "${@}"
6 changes: 3 additions & 3 deletions microk8s-resources/wrappers/microk8s-config.wrapper
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/bash
#!/usr/bin/env bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"

source $SNAP/actions/common/utils.sh

use_snap_env

if [ -e ${SNAP_DATA}/var/lock/clustered.lock ]
then
echo "This MicroK8s deployment is acting as a node in a cluster. Please use the microk8s config on the master."
Expand Down
24 changes: 11 additions & 13 deletions microk8s-resources/wrappers/microk8s-ctr.wrapper
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
#!/bin/bash
#!/usr/bin/env bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
ARCH="$($SNAP/bin/uname -m)"
export IN_SNAP_LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"

source $SNAP/actions/common/utils.sh

SNAPSHOTTER=$(snapshotter)
use_snap_env

exit_if_no_permissions

export CONTAINERD_SNAPSHOTTER=$(snapshotter)

if ! [ -e $SNAP_DATA/args/ctr ]
then
echo "Arguments file $SNAP_DATA/args/ctr is missing."
exit 1
fi

declare -a args="($(cat $SNAP_DATA/args/ctr))"
if is_strict
then
"${SNAP}/bin/ctr" "${args[@]}" "$@"
else
declare -a args="($(cat $SNAP_DATA/args/ctr))"
if is_strict
then
LD_LIBRARY_PATH="$IN_SNAP_LD_LIBRARY_PATH" CONTAINERD_SNAPSHOTTER="$SNAPSHOTTER" "${SNAP}/bin/ctr" "${args[@]}" "$@"
else
sudo -E LD_LIBRARY_PATH="$IN_SNAP_LD_LIBRARY_PATH" CONTAINERD_SNAPSHOTTER="$SNAPSHOTTER" "${SNAP}/bin/ctr" "${args[@]}" "$@"
fi
sudo -E "${SNAP}/bin/ctr" "${args[@]}" "$@"
fi
11 changes: 3 additions & 8 deletions microk8s-resources/wrappers/microk8s-dashboard-proxy.wrapper
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#!/bin/bash
#!/usr/bin/env bash
set -eu

ARCH="$($SNAP/bin/uname -m)"
export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
export IN_SNAP_LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export PYTHONNOUSERSITE=false
source $SNAP/actions/common/utils.sh

export LC_ALL="${LC_ALL:-C.UTF-8}"
export LANG="${LANG:-C.UTF-8}"
use_snap_env

exit_if_not_root
exit_if_no_permissions

LD_LIBRARY_PATH=$IN_SNAP_LD_LIBRARY_PATH ${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/dashboard_proxy.py
${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/dashboard_proxy.py
11 changes: 4 additions & 7 deletions microk8s-resources/wrappers/microk8s-dbctl.wrapper
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#!/bin/bash
#!/usr/bin/env bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
ARCH="$($SNAP/bin/uname -m)"
export IN_SNAP_LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export PYTHONNOUSERSITE=false

source $SNAP/actions/common/utils.sh

use_snap_env

exit_if_not_root

exit_if_no_permissions

LD_LIBRARY_PATH=$IN_SNAP_LD_LIBRARY_PATH ${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/dbctl.py "${@}"
${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/dbctl.py "${@}"
13 changes: 4 additions & 9 deletions microk8s-resources/wrappers/microk8s-disable.wrapper
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#!/bin/bash
#!/usr/bin/env bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
ARCH="$($SNAP/bin/uname -m)"
export IN_SNAP_LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export PYTHONNOUSERSITE=false
export LC_ALL="${LC_ALL:-C.UTF-8}"
export LANG="${LANG:-C.UTF-8}"

source $SNAP/actions/common/utils.sh

use_snap_env

# avoid AppArmor denial in strict mode when running under sudo without -H

if is_strict
Expand All @@ -22,4 +17,4 @@ exit_if_not_root

exit_if_no_permissions

LD_LIBRARY_PATH=$IN_SNAP_LD_LIBRARY_PATH ${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/disable.py "${@}"
${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/disable.py "${@}"
14 changes: 4 additions & 10 deletions microk8s-resources/wrappers/microk8s-enable.wrapper
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
#!/bin/bash
#!/usr/bin/env bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
ARCH="$($SNAP/bin/uname -m)"
export IN_SNAP_LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export PYTHONNOUSERSITE=false
export LC_ALL="${LC_ALL:-C.UTF-8}"
export LANG="${LANG:-C.UTF-8}"

source $SNAP/actions/common/utils.sh

# avoid AppArmor denial in strict mode when running under sudo without -H
use_snap_env

# avoid AppArmor denial in strict mode when running under sudo without -H
if is_strict
then
cd "$SNAP"
Expand All @@ -22,4 +16,4 @@ exit_if_not_root

exit_if_no_permissions

LD_LIBRARY_PATH=$IN_SNAP_LD_LIBRARY_PATH ${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/enable.py "${@}"
${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/enable.py "${@}"
5 changes: 3 additions & 2 deletions microk8s-resources/wrappers/microk8s-helm.wrapper
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash
#!/usr/bin/env bash

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
source $SNAP/actions/common/utils.sh

use_snap_env

if [ -e ${SNAP_DATA}/var/lock/clustered.lock ]
then
echo "This MicroK8s deployment is acting as a node in a cluster. Please use the microk8s helm on the master."
Expand Down
5 changes: 3 additions & 2 deletions microk8s-resources/wrappers/microk8s-helm3.wrapper
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash
#!/usr/bin/env bash

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
source $SNAP/actions/common/utils.sh

use_snap_env

if [ -e ${SNAP_DATA}/var/lock/clustered.lock ]
then
echo "This MicroK8s deployment is acting as a node in a cluster. Please use the microk8s helm on the master."
Expand Down
10 changes: 3 additions & 7 deletions microk8s-resources/wrappers/microk8s-images.wrapper
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#!/bin/bash
#!/usr/bin/env bash

set -eu

source $SNAP/actions/common/utils.sh

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"

ARCH="$($SNAP/bin/uname -m)"
export IN_SNAP_LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export PYTHONNOUSERSITE=false
use_snap_env

exit_if_no_permissions

LD_LIBRARY_PATH=$IN_SNAP_LD_LIBRARY_PATH ${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/images.py "${@}"
${SNAP}/usr/bin/python3 ${SNAP}/scripts/wrappers/images.py "${@}"
7 changes: 3 additions & 4 deletions microk8s-resources/wrappers/microk8s-istioctl.wrapper
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/bin/bash
#!/usr/bin/env bash

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
ARCH="$($SNAP/bin/uname -m)"
export LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
. $SNAP/actions/common/utils.sh
use_snap_env

if [ ! -f "${SNAP_DATA}/bin/istioctl" ]; then
echo "Istio not available, try enabling is with 'microk8s enable istio'"
Expand Down
Loading

0 comments on commit 42ff182

Please sign in to comment.