Skip to content

Instantly share code, notes, and snippets.

@zulhfreelancer
Created April 4, 2024 04:07
Show Gist options
  • Select an option

  • Save zulhfreelancer/4ec567b87a1e2a77850601a868bb8766 to your computer and use it in GitHub Desktop.

Select an option

Save zulhfreelancer/4ec567b87a1e2a77850601a868bb8766 to your computer and use it in GitHub Desktop.
How to install promtool by Prometheus?

How to install promtool by Prometheus?

# Fetch latest version
TAG=$(curl -s https://api.github.com/repos/prometheus/prometheus/releases | jq -r '.[] | .tag_name' | head -n 1)
echo $TAG

# Remove the `v` prefix
VERSION=$(echo $TAG | sed 's/v//')
echo $VERSION

# Check CPU architecture
ARCH=$(uname -s | tr A-Z a-z)-$(uname -m | sed 's/x86_64/amd64/')
echo $ARCH

# Download the release
curl -L -O https://github.com/prometheus/prometheus/releases/download/$TAG/prometheus-$VERSION.$ARCH.tar.gz

# Extract the file
tar -xvzf prometheus-$VERSION.$ARCH.tar.gz

# Move promtool
mv prometheus-$VERSION.$ARCH/promtool /usr/local/bin

# Verify if promtool is installed
promtool --version

# Delete downloaded files
rm -rf prometheus-$VERSION.$ARCH
rm -rf prometheus-$VERSION.$ARCH.tar.gz
@Rovanion

Copy link
Copy Markdown

You can extract a specific file from a tar-archive and place it into a specific folder, though the magic is not obvious:

tar -xvzf prometheus-$VERSION.$ARCH.tar.gz \
  --directory /usr/local/bin \
  prometheus-$VERSION.$ARCH/promtool \
  --strip-components=1
  1. --directory changes the directory the command operates on, this is position dependant and must therefore be placed after the path to the archive.
  2. prometheus-$VERSION.$ARCH/promtool is the path inside the archive to the file we want to extract.
  3. --strip-components=1 removes prometheus-$VERSION.$ARCH from file path that is placed in /usr/local/bin.

@Rovanion

Copy link
Copy Markdown

This is the script I used in my project:

#!/bin/bash                                                                                                                                                                                   
# This scripts is intended to run from a Makefile which sets environment it variables uses.                                                                                                   
set -u
## Any command failing should exit the script.                                                                                                                                                
set -e
set -o pipefail

prometheus_folder=prometheus-${PROMTOOL_VERSION}.linux-${PROMTOOL_ARCH}
prometheus_tgz=${prometheus_folder}.tar.gz
url=https://github.com/prometheus/prometheus/releases/download/v${PROMTOOL_VERSION}/${prometheus_tgz}
output_dir=external

mkdir -p "$output_dir/bin"
if ! [ -f "external/$prometheus_tgz" ]; then
  echo Downloading from "$url"
  # --fail makes curl return non-zero on 404,                                                                                                                                                 
  # --location makes it follow redirects, and                                                                                                                                                 
  # --remote-name outputs to a file named the same as the last part of the URL.                                                                                                               
  curl --silent --show-error --fail --location --remote-name --output-dir "$output_dir" "$url"
fi

tar -xvf "${output_dir}/${prometheus_tgz}" --directory "${output_dir}/bin" "${prometheus_folder}/promtool" --strip-components=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment