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

Update setup.sh #653

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
185 changes: 133 additions & 52 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,70 +1,151 @@
#!/bin/bash

#!/bin/sh
#
# This script is used to install the required packages and download
# the latest version of GatewayD from GitHub and install the plugins.
# If you run ./setup.sh -d or ./setup.sh --docker, it prepare Docker compose related files and configs.
# Written in a POSIX-compatible way (sh, dash, bash, etc.).

set -eu #exit on any error and treat unset variables as errors


usage() {
cat <<EOF
Usage: $0 [OPTIONS]

If no option is provided, the script installs GatewayD by default.

OPTIONS:
-d, --docker Download docker compose config files
-h, --help Show this help message
EOF
}

install_gatewayd() {
# Set the architecture to amd64 if it is not set
if [ -z "${ARCH}" ]; then
architecture=$(uname -m)
if [[ "${architecture}" = "x86_64" ]]; then
echo "Setting architecture to amd64"
ARCH=amd64 && export ARCH
elif [[ "${architecture}" = "aarch64" ]] || [[ "${architecture}" = "arm64" ]]; then
echo "Setting architecture to arm64"
ARCH=arm64 && export ARCH
if [ -z "${ARCH:-}" ]; then
architecture="$(uname -m)"
if [ "$architecture" = "x86_64" ]; then
echo "Setting architecture to amd64"
ARCH=amd64
export ARCH
elif [ "$architecture" = "aarch64" ] || [ "$architecture" = "arm64" ]; then
echo "Setting architecture to arm64"
ARCH=arm64
export ARCH
fi
fi
fi
echo "Using architecture: ${ARCH}"
echo "Using architecture: ${ARCH}"

# Install the required packages
apk add --no-cache curl git
# Install the required packages
apk add --no-cache curl git

# Get the latest released version of GatewayD from GitHub
[ -z "${GATEWAYD_VERSION}" ] && GATEWAYD_VERSION=$(git ls-remote --tags --sort=v:refname "https://github.com/gatewayd-io/gatewayd" | cut -d/ -f3- | tail -n1) && export GATEWAYD_VERSION
# Get the latest released version of GatewayD from GitHub
if [ -z "${GATEWAYD_VERSION:-}" ]; then
GATEWAYD_VERSION="$(
git ls-remote --tags --sort=v:refname "https://github.com/gatewayd-io/gatewayd" \
| cut -d/ -f3- \
| tail -n1
)"
export GATEWAYD_VERSION
fi
# Check if the GatewayD version is set
if [ -z "${GATEWAYD_VERSION:-}" ]; then
echo "Failed to set GatewayD version. Set GATEWAYD_VERSION manually if needed."
exit 126
fi

# Check if the GatewayD version is set
if [ -z "${GATEWAYD_VERSION}" ]; then
echo "Failed to set GatewayD version. Consider setting the GATEWAYD_VERSION environment variable manually."
exit 126
fi
echo "Installing GatewayD ${GATEWAYD_VERSION}"

echo "Installing GatewayD ${GATEWAYD_VERSION}"
# Change the directory to the gatewayd-files directory to download the GatewayD archive
# and install the plugins. This will fail exit code 127 (file or directory not found).

# Set the environment variables if they are not set
[ -z "${GATEWAYD_FILES}" ] && GATEWAYD_FILES=/gatewayd-files && export GATEWAYD_FILES
if [ -z "${GATEWAYD_FILES:-}" ]; then
GATEWAYD_FILES="/gatewayd-files"
export GATEWAYD_FILES
fi
[ -d "$GATEWAYD_FILES" ] || mkdir -p "$GATEWAYD_FILES"

# Create the directory to store the gatewayd files
[ -d "${GATEWAYD_FILES}" ] || mkdir "${GATEWAYD_FILES}"
# Move into that directory
cd "$GATEWAYD_FILES" || exit 127

# Change the directory to the gatewayd-files directory to download the GatewayD archive
# and install the plugins. This will fail exit code 127 (file or directory not found).
cd "${GATEWAYD_FILES}" || exit 127
# Download the GatewayD archive if it doesn't exist
tarball="gatewayd-linux-${ARCH}-${GATEWAYD_VERSION}.tar.gz"
if [ ! -f "$tarball" ]; then
curl -L "https://github.com/gatewayd-io/gatewayd/releases/download/${GATEWAYD_VERSION}/${tarball}" \
-o "$tarball"
fi

if [ -f "$tarball" ]; then
echo "GatewayD archive downloaded successfully."
else
echo "GatewayD archive download failed."
exit 1
fi

#Extract and set permissions
echo "Extracting GatewayD archive..."
tar zxvf gatewayd-linux-"${ARCH}"-"${GATEWAYD_VERSION}".tar.gz -C "${GATEWAYD_FILES}"
# Set execute permission for the GatewayD binary
echo "Setting execute permission for the GatewayD binary..."
chmod +x gatewayd

# Install the GatewayD plugins
# If the installation fails, the script will exit with exit code 126 (command invoke error).
echo "Installing GatewayD plugins..."
"${GATEWAYD_FILES}/gatewayd" plugin install \
--skip-path-slip-verification \
--output-dir "${GATEWAYD_FILES}" \
--plugin-config "${GATEWAYD_FILES}/gatewayd_plugins.yaml" \
--cleanup=false \
--update \
--overwrite-config || exit 126

# Replace the default Redis URL with the Redis container URL
if [ -z "${REDIS_URL:-}" ]; then
REDIS_URL="redis://redis:6379/0"
export REDIS_URL
fi
echo "Setting Redis URL to ${REDIS_URL}"
sed -i "s#redis://localhost:6379/0#${REDIS_URL}#" "${GATEWAYD_FILES}/gatewayd_plugins.yaml"

echo "GatewayD ${GATEWAYD_VERSION} and plugins installed successfully!"
}

# Download the GatewayD archive if it doesn't exist
[ -f "${GATEWAYD_FILES}"/gatewayd-linux-"${ARCH}"-"${GATEWAYD_VERSION}".tar.gz ] || curl -L https://github.com/gatewayd-io/gatewayd/releases/download/"${GATEWAYD_VERSION}"/gatewayd-linux-"${ARCH}"-"${GATEWAYD_VERSION}".tar.gz -o gatewayd-linux-"${ARCH}"-"${GATEWAYD_VERSION}".tar.gz
if [ -f "${GATEWAYD_FILES}"/gatewayd-linux-"${ARCH}"-"${GATEWAYD_VERSION}".tar.gz ]; then
echo "GatewayD archive downloaded successfully."
else
echo "GatewayD archive download failed."
exit 1
fi

# Extract the GatewayD archive
echo "Extracting GatewayD archive..."
tar zxvf gatewayd-linux-"${ARCH}"-"${GATEWAYD_VERSION}".tar.gz -C "${GATEWAYD_FILES}"
# Docker compose config download

# Set execute permission for the GatewayD binary
echo "Setting execute permission for the GatewayD binary..."
chmod +x gatewayd
docker_mode() {
curl -L https://raw.githubusercontent.com/gatewayd-io/gatewayd/main/docker-compose.yaml -o docker-compose.yaml
mkdir -p observability-configs && cd observability-configs
curl -L https://raw.githubusercontent.com/gatewayd-io/gatewayd/main/observability-configs/grafana-datasources.yaml -o datasources.yaml
curl -L https://raw.githubusercontent.com/gatewayd-io/gatewayd/main/observability-configs/prometheus.yaml -o prometheus.yaml
curl -L https://raw.githubusercontent.com/gatewayd-io/gatewayd/main/observability-configs/tempo.yaml -o tempo.yaml
}

# Install the GatewayD plugins
# If the installation fails, the script will exit with exit code 126 (command invoke error).
echo "Installing GatewayD plugins..."
"${GATEWAYD_FILES}"/gatewayd plugin install --skip-path-slip-verification --output-dir "${GATEWAYD_FILES}" --plugin-config "${GATEWAYD_FILES}"/gatewayd_plugins.yaml --cleanup=false --update --overwrite-config || exit 126

# Replace the default Redis URL with the Redis container URL
[ -z "${REDIS_URL}" ] && REDIS_URL="redis://redis:6379/0" && export REDIS_URL
echo "Setting Redis URL to ${REDIS_URL}"
sed -i "s#redis://localhost:6379/0#${REDIS_URL}#" "${GATEWAYD_FILES}"/gatewayd_plugins.yaml
# Main logic: Parse arguments

if [ $# -eq 0 ]; then
# No arguments: Run the installation by default
install_gatewayd
exit 0
fi

echo "GatewayD ${GATEWAYD_VERSION} and plugins installed successfully. Exiting..."
# If there are arguments, parse them
while [ $# -gt 0 ]; do
case "$1" in
-d|--docker)
docker_mode
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1"
usage
exit 1
;;
esac
done
Loading