pull/105/head
Muhammad Atif Ali 2 years ago
parent 9562179318
commit 66c9934dc7

@ -5,77 +5,77 @@ VERSION=${VERSION}
AUTH_PATH=${AUTH_PATH} AUTH_PATH=${AUTH_PATH}
GITHUB_EXTERNAL_AUTH_ID=${GITHUB_EXTERNAL_AUTH_ID} GITHUB_EXTERNAL_AUTH_ID=${GITHUB_EXTERNAL_AUTH_ID}
fetch() { fetch() {
dest="$1" dest="$1"
url="$2" url="$2"
if command -v curl; then if command -v curl; then
curl -sSL --fail "${url}" -o "${dest}" curl -sSL --fail "${url}" -o "${dest}"
elif command -v wget; then elif command -v wget; then
wget -O "${dest}" "${url}" wget -O "${dest}" "${url}"
elif command -v busybox; then elif command -v busybox; then
busybox wget -O "${dest}" "${url}" busybox wget -O "${dest}" "${url}"
else else
printf "curl, wget, or busybox is not installed. Please install curl or wget in your image.\n" printf "curl, wget, or busybox is not installed. Please install curl or wget in your image.\n"
exit 1 exit 1
fi fi
} }
unzip() { unzip() {
if command -v unzip; then if command -v unzip; then
command unzip "$@" command unzip "$@"
elif command -v busybox; then elif command -v busybox; then
busybox unzip "$@" busybox unzip "$@"
else else
printf "unzip or busybox is not installed. Please install unzip in your image.\n" printf "unzip or busybox is not installed. Please install unzip in your image.\n"
exit 1 exit 1
fi fi
} }
# Fetch latest version of Vault if VERSION is 'latest' # Fetch latest version of Vault if VERSION is 'latest'
if [ "${VERSION}" = "latest" ]; then if [ "${VERSION}" = "latest" ]; then
LATEST_VERSION=$(curl -s https://releases.hashicorp.com/vault/ | grep -oP 'vault/\K[0-9]+\.[0-9]+\.[0-9]+' | sort -V | tail -n 1) LATEST_VERSION=$(curl -s https://releases.hashicorp.com/vault/ | grep -oP 'vault/\K[0-9]+\.[0-9]+\.[0-9]+' | sort -V | tail -n 1)
if [ -z "$LATEST_VERSION" ]; then if [ -z "$LATEST_VERSION" ]; then
printf "Failed to determine the latest Vault version.\n" printf "Failed to determine the latest Vault version.\n"
exit 1 exit 1
fi fi
VERSION=$LATEST_VERSION VERSION=$LATEST_VERSION
fi fi
# Check if vault is installed and has the correct version # Check if vault is installed and has the correct version
installation_needed=1 installation_needed=1
if command -v vault &>/dev/null; then if command -v vault &> /dev/null; then
CURRENT_VERSION=$(vault version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+') CURRENT_VERSION=$(vault version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
if [ "$CURRENT_VERSION" = "$VERSION" ]; then if [ "$CURRENT_VERSION" = "$VERSION" ]; then
printf "Vault version %s is already installed and up-to-date.\n\n" "$CURRENT_VERSION" printf "Vault version %s is already installed and up-to-date.\n\n" "$CURRENT_VERSION"
installation_needed=0 installation_needed=0
fi fi
fi fi
if [ $installation_needed -eq 1 ]; then if [ $installation_needed -eq 1 ]; then
# Download and install Vault # Download and install Vault
printf "Installing or updating Vault CLI ...\n\n" printf "Installing or updating Vault CLI ...\n\n"
fetch vault.zip "https://releases.hashicorp.com/vault/${VERSION}/vault_${VERSION}_linux_amd64.zip" fetch vault.zip "https://releases.hashicorp.com/vault/${VERSION}/vault_${VERSION}_linux_amd64.zip"
unzip vault.zip unzip vault.zip
rm vault.zip rm vault.zip
if sudo mv vault /usr/local/bin/vault 2>/dev/null; then if sudo mv vault /usr/local/bin/vault 2> /dev/null; then
printf "Vault installed successfully!\n\n" printf "Vault installed successfully!\n\n"
else else
mkdir -p ~/.local/bin mkdir -p ~/.local/bin
mv vault ~/.local/bin/vault mv vault ~/.local/bin/vault
if [ ! -f ~/.local/bin/vault ]; then if [ ! -f ~/.local/bin/vault ]; then
printf "Failed to move Vault to local bin.\n" printf "Failed to move Vault to local bin.\n"
exit 1 exit 1
fi
printf "Please add ~/.local/bin to your PATH to use vault CLI.\n"
fi fi
printf "Please add ~/.local/bin to your PATH to use vault CLI.\n"
fi
fi fi
# Authenticate with Vault # Authenticate with Vault
printf "🔑 Authenticating with Vault ...\n\n" printf "🔑 Authenticating with Vault ...\n\n"
GITHUB_TOKEN=$(coder external-auth access-token $GITHUB_EXTERNAL_AUTH_ID) GITHUB_TOKEN=$(coder external-auth access-token $GITHUB_EXTERNAL_AUTH_ID)
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
printf "Authentication with Vault failed. Please check your credentials.\n" printf "Authentication with Vault failed. Please check your credentials.\n"
exit 1 exit 1
fi fi
export VAULT_ADDR=$VAULT_ADDR export VAULT_ADDR=$VAULT_ADDR

Loading…
Cancel
Save