11#! /bin/bash
2+ set -euo pipefail
23
34function is_available {
4- command -v $1 > /dev/null 2>&1 || { echo >&2 " $1 is required but it's not installed. Aborting." ; exit 1; }
5+ command -v " $1 " > /dev/null 2>&1 || { echo >&2 " $1 is required but it's not installed. Aborting." ; exit 1; }
56}
67
78function version {
8- echo " $@ " | awk -F. ' { printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
9+ echo " $1 " | awk -F. ' { printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
910}
1011
11- if [ -z " $PATTERN_UTILITY_CONTAINER " ]; then
12+ if [ -z " ${ PATTERN_UTILITY_CONTAINER:- } " ]; then
1213 PATTERN_UTILITY_CONTAINER=" quay.io/validatedpatterns/utility-container"
1314fi
1415# If PATTERN_DISCONNECTED_HOME is set it will be used to populate both PATTERN_UTILITY_CONTAINER
1516# and PATTERN_INSTALL_CHART automatically
16- if [ -n " ${PATTERN_DISCONNECTED_HOME} " ]; then
17+ if [ -n " ${PATTERN_DISCONNECTED_HOME:- } " ]; then
1718 PATTERN_UTILITY_CONTAINER=" ${PATTERN_DISCONNECTED_HOME} /utility-container"
1819 PATTERN_INSTALL_CHART=" oci://${PATTERN_DISCONNECTED_HOME} /pattern-install"
1920 echo " PATTERN_DISCONNECTED_HOME is set to ${PATTERN_DISCONNECTED_HOME} "
@@ -23,10 +24,10 @@ if [ -n "${PATTERN_DISCONNECTED_HOME}" ]; then
2324fi
2425
2526readonly commands=(podman)
26- for cmd in ${commands[@]} ; do is_available " $cmd " ; done
27+ for cmd in " ${commands[@]} " ; do is_available " $cmd " ; done
2728
2829UNSUPPORTED_PODMAN_VERSIONS=" 1.6 1.5"
29- PODMAN_VERSION_STR=$( podman --version)
30+ PODMAN_VERSION_STR=$( podman --version) || { echo " Failed to get podman version " ; exit 1 ; }
3031for i in ${UNSUPPORTED_PODMAN_VERSIONS} ; do
3132 # We add a space
3233 if echo " ${PODMAN_VERSION_STR} " | grep -q -E " \b${i} " ; then
4142PODMAN_VERSION=$( echo " ${PODMAN_VERSION_STR} " | awk ' { print $NF }' )
4243
4344# podman < 4.3.0 do not support keep-id:uid=...
44- if [ $( version " ${PODMAN_VERSION} " ) -lt $( version " 4.3.0" ) ]; then
45- PODMAN_ARGS=" -v ${HOME} :/root"
45+ PODMAN_ARGS=()
46+ if [ " $( version " ${PODMAN_VERSION} " ) " -lt " $( version " 4.3.0" ) " ]; then
47+ PODMAN_ARGS=(-v " ${HOME} :/root" )
4648else
4749 # We do not rely on bash's $UID and $GID because on MacOSX $GID is not set
4850 MYNAME=$( id -n -u)
4951 MYUID=$( id -u)
5052 MYGID=$( id -g)
51- PODMAN_ARGS=" --passwd-entry ${MYNAME} :x:${MYUID} :${MYGID} ::/pattern-home:/bin/bash --user ${MYUID} :${MYGID} --userns keep-id:uid=${MYUID} ,gid=${MYGID} "
52-
53+ PODMAN_ARGS=(--passwd-entry " ${MYNAME} :x:${MYUID} :${MYGID} ::/pattern-home:/bin/bash" --user " ${MYUID} :${MYGID} " --userns " keep-id:uid=${MYUID} ,gid=${MYGID} " )
5354fi
5455
55- if [ -n " $KUBECONFIG " ]; then
56- if [[ ! " ${KUBECONFIG} " =~ ^$HOME * ]]; then
56+ if [ -n " ${KUBECONFIG:- } " ]; then
57+ # Check if KUBECONFIG path starts with HOME directory
58+ if [[ ! " ${KUBECONFIG} " =~ ^" ${HOME} " ]]; then
5759 echo " ${KUBECONFIG} is pointing outside of the HOME folder, this will make it unavailable from the container."
5860 echo " Please move it somewhere inside your $HOME folder, as that is what gets bind-mounted inside the container"
5961 exit 1
6264
6365# Detect if we use podman machine. If we do not then we bind mount local host ssl folders
6466# if we are using podman machine then we do not bind mount anything (for now!)
65- REMOTE_PODMAN=$( podman system connection list | tail -n +2 | wc -l)
66- if [ $REMOTE_PODMAN -eq 0 ]; then # If we are not using podman machine we check the hosts folders
67+ REMOTE_PODMAN=$( podman system connection list | tail -n +2 | wc -l) || REMOTE_PODMAN=0
68+ PKI_HOST_MOUNT_ARGS=()
69+ if [ " ${REMOTE_PODMAN} " -eq 0 ]; then # If we are not using podman machine we check the hosts folders
6770 # We check /etc/pki/tls because on ubuntu /etc/pki/fwupd sometimes
6871 # exists but not /etc/pki/tls and we do not want to bind mount in such a case
6972 # as it would find no certificates at all.
7073 if [ -d /etc/pki/tls ]; then
71- PKI_HOST_MOUNT_ARGS=" -v /etc/pki:/etc/pki:ro"
74+ PKI_HOST_MOUNT_ARGS=( -v /etc/pki:/etc/pki:ro)
7275 elif [ -d /etc/ssl ]; then
73- PKI_HOST_MOUNT_ARGS=" -v /etc/ssl:/etc/ssl:ro"
76+ PKI_HOST_MOUNT_ARGS=( -v /etc/ssl:/etc/ssl:ro)
7477 else
75- PKI_HOST_MOUNT_ARGS=" -v /usr/share/ca-certificates:/usr/share/ca-certificates:ro"
78+ PKI_HOST_MOUNT_ARGS=( -v /usr/share/ca-certificates:/usr/share/ca-certificates:ro)
7679 fi
77- else
78- PKI_HOST_MOUNT_ARGS=" "
80+ fi
81+
82+ # Parse EXTRA_ARGS into an array if set
83+ EXTRA_ARGS_ARRAY=()
84+ if [ -n " ${EXTRA_ARGS:- } " ]; then
85+ # shellcheck disable=SC2206
86+ EXTRA_ARGS_ARRAY=(${EXTRA_ARGS} )
7987fi
8088
8189# Copy Kubeconfig from current environment. The utilities will pick up ~/.kube/config if set so it's not mandatory
@@ -106,12 +114,12 @@ podman run -it --rm --pull=newer \
106114 -e TOKEN_SECRET \
107115 -e UUID_FILE \
108116 -e VALUES_SECRET \
109- ${PKI_HOST_MOUNT_ARGS} \
117+ " ${PKI_HOST_MOUNT_ARGS[@]} " \
110118 -v " $( pwd -P) " :" $( pwd -P) " \
111119 -v " ${HOME} " :" ${HOME} " \
112120 -v " ${HOME} " :/pattern-home \
113- ${PODMAN_ARGS} \
114- ${EXTRA_ARGS} \
121+ " ${PODMAN_ARGS[@]} " \
122+ " ${EXTRA_ARGS_ARRAY[@]} " \
115123 -w " $( pwd -P) " \
116124 " $PATTERN_UTILITY_CONTAINER " \
117- $@
125+ " $@ "
0 commit comments