Format shell scripts with shfmt

This commit is contained in:
Christian Mäder 2021-02-08 12:16:04 +01:00
parent c6df6a040a
commit 04ac3d5f4b
7 changed files with 73 additions and 59 deletions

View File

@ -5,4 +5,4 @@ push_image_to_registry() {
echo "⏫ Pushing '${target_tag}'" echo "⏫ Pushing '${target_tag}'"
$DRY docker push "${target_tag}" $DRY docker push "${target_tag}"
echo "✅ Finished pushing the Docker image '${target_tag}'." echo "✅ Finished pushing the Docker image '${target_tag}'."
} }

View File

@ -45,16 +45,16 @@ _get_image_configuration() {
--silent \ --silent \
--location \ --location \
--header "Authorization: Bearer $token" \ --header "Authorization: Bearer $token" \
"https://registry-1.docker.io/v2/$image/blobs/$digest" \ "https://registry-1.docker.io/v2/$image/blobs/$digest" |
| jq -r ".config.Labels.\"$label\"" jq -r ".config.Labels.\"$label\""
} }
_get_token() { _get_token() {
local image=$1 local image=$1
curl \ curl \
--silent \ --silent \
"https://auth.docker.io/token?scope=repository:$image:pull&service=registry.docker.io" \ "https://auth.docker.io/token?scope=repository:$image:pull&service=registry.docker.io" |
| jq -r '.token' jq -r '.token'
} }
_get_digest() { _get_digest() {
@ -65,8 +65,8 @@ _get_digest() {
--silent \ --silent \
--header "Accept: application/vnd.docker.distribution.manifest.v2+json" \ --header "Accept: application/vnd.docker.distribution.manifest.v2+json" \
--header "Authorization: Bearer $token" \ --header "Authorization: Bearer $token" \
"https://registry-1.docker.io/v2/$image/manifests/$tag" \ "https://registry-1.docker.io/v2/$image/manifests/$tag" |
| jq -r '.config.digest' jq -r '.config.digest'
} }
_get_layers() { _get_layers() {
@ -77,6 +77,6 @@ _get_layers() {
--silent \ --silent \
--header "Accept: application/vnd.docker.distribution.manifest.v2+json" \ --header "Accept: application/vnd.docker.distribution.manifest.v2+json" \
--header "Authorization: Bearer $token" \ --header "Authorization: Bearer $token" \
"https://registry-1.docker.io/v2/$image/manifests/$tag" \ "https://registry-1.docker.io/v2/$image/manifests/$tag" |
| jq -r '.layers[].digest' jq -r '.layers[].digest'
} }

View File

@ -19,7 +19,7 @@ fi
# Checking if PRERELEASE is either unset, 'true' or 'false' # Checking if PRERELEASE is either unset, 'true' or 'false'
### ###
if [ -n "${PRERELEASE}" ] && if [ -n "${PRERELEASE}" ] &&
{ [ "${PRERELEASE}" != "true" ] && [ "${PRERELEASE}" != "false" ]; }; then { [ "${PRERELEASE}" != "true" ] && [ "${PRERELEASE}" != "false" ]; }; then
if [ -z "${DEBUG}" ]; then if [ -z "${DEBUG}" ]; then
echo "⚠️ PRERELEASE must be either unset, 'true' or 'false', but was '${PRERELEASE}'!" echo "⚠️ PRERELEASE must be either unset, 'true' or 'false', but was '${PRERELEASE}'!"
@ -60,9 +60,10 @@ if [ "${PRERELEASE}" == "true" ]; then
# shellcheck disable=SC2003 # shellcheck disable=SC2003
MINOR_UNSTABLE=$(expr match "${VERSION}" 'v[0-9]\+\.\([0-9]\+\)') MINOR_UNSTABLE=$(expr match "${VERSION}" 'v[0-9]\+\.\([0-9]\+\)')
if { [ "${MAJOR_STABLE}" -eq "${MAJOR_UNSTABLE}" ] \ if {
&& [ "${MINOR_STABLE}" -ge "${MINOR_UNSTABLE}" ]; [ "${MAJOR_STABLE}" -eq "${MAJOR_UNSTABLE}" ] &&
} || [ "${MAJOR_STABLE}" -gt "${MAJOR_UNSTABLE}" ]; then [ "${MINOR_STABLE}" -ge "${MINOR_UNSTABLE}" ]
} || [ "${MAJOR_STABLE}" -gt "${MAJOR_UNSTABLE}" ]; then
echo "❎ Latest unstable version '${VERSION}' is not higher than the latest stable version '$STABLE_VERSION'." echo "❎ Latest unstable version '${VERSION}' is not higher than the latest stable version '$STABLE_VERSION'."
if [ -z "$DEBUG" ]; then if [ -z "$DEBUG" ]; then

View File

@ -117,7 +117,7 @@ NETBOX_PATH="${NETBOX_PATH-.netbox}"
### ###
# Fetching the NetBox source # Fetching the NetBox source
### ###
if [ "${2}" != "--push-only" ] && [ -z "${SKIP_GIT}" ] ; then if [ "${2}" != "--push-only" ] && [ -z "${SKIP_GIT}" ]; then
echo "🌐 Checking out '${NETBOX_BRANCH}' of NetBox from the url '${URL}' into '${NETBOX_PATH}'" echo "🌐 Checking out '${NETBOX_BRANCH}' of NetBox from the url '${URL}' into '${NETBOX_PATH}'"
if [ ! -d "${NETBOX_PATH}" ]; then if [ ! -d "${NETBOX_PATH}" ]; then
$DRY git clone -q --depth 10 -b "${NETBOX_BRANCH}" "${URL}" "${NETBOX_PATH}" $DRY git clone -q --depth 10 -b "${NETBOX_BRANCH}" "${URL}" "${NETBOX_PATH}"
@ -174,9 +174,18 @@ PROJECT_VERSION="${PROJECT_VERSION-$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]
# Get the Git information from the netbox directory # Get the Git information from the netbox directory
if [ -d "${NETBOX_PATH}/.git" ]; then if [ -d "${NETBOX_PATH}/.git" ]; then
NETBOX_GIT_REF=$(cd "${NETBOX_PATH}"; git rev-parse HEAD) NETBOX_GIT_REF=$(
NETBOX_GIT_BRANCH=$(cd "${NETBOX_PATH}"; git rev-parse --abbrev-ref HEAD) cd "${NETBOX_PATH}"
NETBOX_GIT_URL=$(cd "${NETBOX_PATH}"; git remote get-url origin) git rev-parse HEAD
)
NETBOX_GIT_BRANCH=$(
cd "${NETBOX_PATH}"
git rev-parse --abbrev-ref HEAD
)
NETBOX_GIT_URL=$(
cd "${NETBOX_PATH}"
git remote get-url origin
)
fi fi
### ###
@ -186,19 +195,22 @@ DOCKER_REGISTRY="${DOCKER_REGISTRY-docker.io}"
DOCKER_ORG="${DOCKER_ORG-netboxcommunity}" DOCKER_ORG="${DOCKER_ORG-netboxcommunity}"
DOCKER_REPO="${DOCKER_REPO-netbox}" DOCKER_REPO="${DOCKER_REPO-netbox}"
case "${NETBOX_BRANCH}" in case "${NETBOX_BRANCH}" in
master) master)
TAG="${TAG-latest}";; TAG="${TAG-latest}"
develop) ;;
TAG="${TAG-snapshot}";; develop)
*) TAG="${TAG-snapshot}"
TAG="${TAG-$NETBOX_BRANCH}";; ;;
*)
TAG="${TAG-$NETBOX_BRANCH}"
;;
esac esac
### ###
# Determine targets to build # Determine targets to build
### ###
DEFAULT_DOCKER_TARGETS=("main" "ldap") DEFAULT_DOCKER_TARGETS=("main" "ldap")
DOCKER_TARGETS=( "${DOCKER_TARGET:-"${DEFAULT_DOCKER_TARGETS[@]}"}") DOCKER_TARGETS=("${DOCKER_TARGET:-"${DEFAULT_DOCKER_TARGETS[@]}"}")
echo "🏭 Building the following targets:" "${DOCKER_TARGETS[@]}" echo "🏭 Building the following targets:" "${DOCKER_TARGETS[@]}"
### ###
@ -216,7 +228,7 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
TARGET_DOCKER_TAG="${TARGET_DOCKER_TAG}-${DOCKER_TARGET}" TARGET_DOCKER_TAG="${TARGET_DOCKER_TAG}-${DOCKER_TARGET}"
fi fi
if [ -n "${GH_ACTION}" ]; then if [ -n "${GH_ACTION}" ]; then
echo "FINAL_DOCKER_TAG=${TARGET_DOCKER_TAG}" >> "$GITHUB_ENV" echo "FINAL_DOCKER_TAG=${TARGET_DOCKER_TAG}" >>"$GITHUB_ENV"
echo "::set-output name=skipped::false" echo "::set-output name=skipped::false"
fi fi
@ -242,7 +254,7 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
### ###
# Proceeding to buils stage, except if `--push-only` is passed # Proceeding to buils stage, except if `--push-only` is passed
### ###
if [ "${2}" != "--push-only" ] ; then if [ "${2}" != "--push-only" ]; then
### ###
# Checking if the build is necessary, # Checking if the build is necessary,
# meaning build only if one of those values changed: # meaning build only if one of those values changed:
@ -259,7 +271,7 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
BUILD_REASON="${BUILD_REASON} interactive" BUILD_REASON="${BUILD_REASON} interactive"
elif [ "$DOCKER_REGISTRY" = "docker.io" ]; then elif [ "$DOCKER_REGISTRY" = "docker.io" ]; then
source ./build-functions/get-public-image-config.sh source ./build-functions/get-public-image-config.sh
IFS=':' read -ra DOCKER_FROM_SPLIT <<< "${DOCKER_FROM}" IFS=':' read -ra DOCKER_FROM_SPLIT <<<"${DOCKER_FROM}"
if ! [[ ${DOCKER_FROM_SPLIT[0]} =~ .*/.* ]]; then if ! [[ ${DOCKER_FROM_SPLIT[0]} =~ .*/.* ]]; then
# Need to use "library/..." for images the have no two part name # Need to use "library/..." for images the have no two part name
DOCKER_FROM_SPLIT[0]="library/${DOCKER_FROM_SPLIT[0]}" DOCKER_FROM_SPLIT[0]="library/${DOCKER_FROM_SPLIT[0]}"
@ -295,8 +307,8 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
-t "${TARGET_DOCKER_TAG}" -t "${TARGET_DOCKER_TAG}"
) )
if [ -n "${TARGET_DOCKER_SHORT_TAG}" ]; then if [ -n "${TARGET_DOCKER_SHORT_TAG}" ]; then
DOCKER_BUILD_ARGS+=( -t "${TARGET_DOCKER_SHORT_TAG}" ) DOCKER_BUILD_ARGS+=(-t "${TARGET_DOCKER_SHORT_TAG}")
DOCKER_BUILD_ARGS+=( -t "${TARGET_DOCKER_LATEST_TAG}" ) DOCKER_BUILD_ARGS+=(-t "${TARGET_DOCKER_LATEST_TAG}")
fi fi
# --label # --label
@ -323,22 +335,22 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
) )
fi fi
if [ -n "${BUILD_REASON}" ]; then if [ -n "${BUILD_REASON}" ]; then
BUILD_REASON=$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' <<< "$BUILD_REASON") BUILD_REASON=$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' <<<"$BUILD_REASON")
DOCKER_BUILD_ARGS+=( --label "BUILD_REASON=${BUILD_REASON}" ) DOCKER_BUILD_ARGS+=(--label "BUILD_REASON=${BUILD_REASON}")
fi fi
# --build-arg # --build-arg
DOCKER_BUILD_ARGS+=( --build-arg "NETBOX_PATH=${NETBOX_PATH}" ) DOCKER_BUILD_ARGS+=(--build-arg "NETBOX_PATH=${NETBOX_PATH}")
if [ -n "${DOCKER_FROM}" ]; then if [ -n "${DOCKER_FROM}" ]; then
DOCKER_BUILD_ARGS+=( --build-arg "FROM=${DOCKER_FROM}" ) DOCKER_BUILD_ARGS+=(--build-arg "FROM=${DOCKER_FROM}")
fi fi
if [ -n "${HTTP_PROXY}" ]; then if [ -n "${HTTP_PROXY}" ]; then
DOCKER_BUILD_ARGS+=( --build-arg "http_proxy=${HTTP_PROXY}" ) DOCKER_BUILD_ARGS+=(--build-arg "http_proxy=${HTTP_PROXY}")
DOCKER_BUILD_ARGS+=( --build-arg "https_proxy=${HTTPS_PROXY}" ) DOCKER_BUILD_ARGS+=(--build-arg "https_proxy=${HTTPS_PROXY}")
fi fi
if [ -n "${NO_PROXY}" ]; then if [ -n "${NO_PROXY}" ]; then
DOCKER_BUILD_ARGS+=( --build-arg "no_proxy=${NO_PROXY}" ) DOCKER_BUILD_ARGS+=(--build-arg "no_proxy=${NO_PROXY}")
fi fi
### ###
@ -360,7 +372,7 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
### ###
# Pushing the docker images if either `--push` or `--push-only` are passed # Pushing the docker images if either `--push` or `--push-only` are passed
### ###
if [ "${2}" == "--push" ] || [ "${2}" == "--push-only" ] ; then if [ "${2}" == "--push" ] || [ "${2}" == "--push-only" ]; then
source ./build-functions/docker-functions.sh source ./build-functions/docker-functions.sh
push_image_to_registry "${TARGET_DOCKER_TAG}" push_image_to_registry "${TARGET_DOCKER_TAG}"

View File

@ -18,7 +18,7 @@ CUR_DB_WAIT_TIME=0
while ! ./manage.py migrate 2>&1 && [ "${CUR_DB_WAIT_TIME}" -lt "${MAX_DB_WAIT_TIME}" ]; do while ! ./manage.py migrate 2>&1 && [ "${CUR_DB_WAIT_TIME}" -lt "${MAX_DB_WAIT_TIME}" ]; do
echo "⏳ Waiting on DB... (${CUR_DB_WAIT_TIME}s / ${MAX_DB_WAIT_TIME}s)" echo "⏳ Waiting on DB... (${CUR_DB_WAIT_TIME}s / ${MAX_DB_WAIT_TIME}s)"
sleep "${DB_WAIT_TIMEOUT}" sleep "${DB_WAIT_TIMEOUT}"
CUR_DB_WAIT_TIME=$(( CUR_DB_WAIT_TIME + DB_WAIT_TIMEOUT )) CUR_DB_WAIT_TIME=$((CUR_DB_WAIT_TIME + DB_WAIT_TIMEOUT))
done done
if [ "${CUR_DB_WAIT_TIME}" -ge "${MAX_DB_WAIT_TIME}" ]; then if [ "${CUR_DB_WAIT_TIME}" -ge "${MAX_DB_WAIT_TIME}" ]; then
echo "❌ Waited ${MAX_DB_WAIT_TIME}s or more for the DB to become ready." echo "❌ Waited ${MAX_DB_WAIT_TIME}s or more for the DB to become ready."
@ -36,17 +36,17 @@ else
SUPERUSER_EMAIL='admin@example.com' SUPERUSER_EMAIL='admin@example.com'
fi fi
if [ -f "/run/secrets/superuser_password" ]; then if [ -f "/run/secrets/superuser_password" ]; then
SUPERUSER_PASSWORD="$(< /run/secrets/superuser_password)" SUPERUSER_PASSWORD="$(</run/secrets/superuser_password)"
elif [ -z ${SUPERUSER_PASSWORD+x} ]; then elif [ -z ${SUPERUSER_PASSWORD+x} ]; then
SUPERUSER_PASSWORD='admin' SUPERUSER_PASSWORD='admin'
fi fi
if [ -f "/run/secrets/superuser_api_token" ]; then if [ -f "/run/secrets/superuser_api_token" ]; then
SUPERUSER_API_TOKEN="$(< /run/secrets/superuser_api_token)" SUPERUSER_API_TOKEN="$(</run/secrets/superuser_api_token)"
elif [ -z ${SUPERUSER_API_TOKEN+x} ]; then elif [ -z ${SUPERUSER_API_TOKEN+x} ]; then
SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567' SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567'
fi fi
./manage.py shell --interface python << END ./manage.py shell --interface python <<END
from django.contrib.auth.models import User from django.contrib.auth.models import User
from users.models import Token from users.models import Token
if not User.objects.filter(username='${SUPERUSER_NAME}'): if not User.objects.filter(username='${SUPERUSER_NAME}'):

View File

@ -22,17 +22,18 @@ load_configuration() {
# this curl call will get a reply once unit is fully launched # this curl call will get a reply once unit is fully launched
curl --silent --output /dev/null --request GET --unix-socket $UNIT_SOCKET http://localhost/ curl --silent --output /dev/null --request GET --unix-socket $UNIT_SOCKET http://localhost/
echo "⚙️ Applying configuration from $UNIT_CONFIG"; echo "⚙️ Applying configuration from $UNIT_CONFIG"
RESP_CODE=$(curl \ RESP_CODE=$(
--silent \ curl \
--output /dev/null \ --silent \
--write-out '%{http_code}' \ --output /dev/null \
--request PUT \ --write-out '%{http_code}' \
--data-binary "@${UNIT_CONFIG}" \ --request PUT \
--unix-socket $UNIT_SOCKET \ --data-binary "@${UNIT_CONFIG}" \
http://localhost/config --unix-socket $UNIT_SOCKET \
) http://localhost/config
)
if [ "$RESP_CODE" != "200" ]; then if [ "$RESP_CODE" != "200" ]; then
echo "⚠️ Could no load Unit configuration" echo "⚠️ Could no load Unit configuration"
kill "$(cat /opt/unit/unit.pid)" kill "$(cat /opt/unit/unit.pid)"
@ -45,9 +46,9 @@ load_configuration() {
load_configuration & load_configuration &
exec unitd \ exec unitd \
--no-daemon \ --no-daemon \
--control unix:$UNIT_SOCKET \ --control unix:$UNIT_SOCKET \
--pid /opt/unit/unit.pid \ --pid /opt/unit/unit.pid \
--log /dev/stdout \ --log /dev/stdout \
--state /opt/unit/state/ \ --state /opt/unit/state/ \
--tmp /opt/unit/tmp/ --tmp /opt/unit/tmp/

View File

@ -28,7 +28,7 @@ if [ -z "${IMAGE}" ]; then
echo "⚠️ No image defined" echo "⚠️ No image defined"
if [ -z "${DEBUG}" ]; then if [ -z "${DEBUG}" ]; then
exit 1; exit 1
else else
echo "⚠️ Would 'exit 1' here, but DEBUG is '${DEBUG}'." echo "⚠️ Would 'exit 1' here, but DEBUG is '${DEBUG}'."
fi fi
@ -49,7 +49,7 @@ test_setup() {
( (
cd initializers cd initializers
for script in *.yml; do for script in *.yml; do
sed -E 's/^# //' "${script}" > "../${INITIALIZERS_DIR}/${script}" sed -E 's/^# //' "${script}" >"../${INITIALIZERS_DIR}/${script}"
done done
) )
} }