Skip to content

Instantly share code, notes, and snippets.

@dreampuf
Created August 7, 2019 15:45
Show Gist options
  • Save dreampuf/5531fd5b131e73fe97d6b871fce60bb0 to your computer and use it in GitHub Desktop.
Save dreampuf/5531fd5b131e73fe97d6b871fce60bb0 to your computer and use it in GitHub Desktop.
Build a Node Exporter RPM package
# Extract Node Exporter RPM package
# Download the official tar package
# https://prometheus.io/download/#node_exporter
VERSION=${VERSION:-0.18.1}
URL=https://github.com/prometheus/node_exporter/releases/download/v${VERSION}/node_exporter-${VERSION}.linux-amd64.tar.gz
FILENAME=${URL##*/}
ROOT_PATH=$PWD/pkg
APP_ROOT_PATH=$ROOT_PATH/opt/node_exporter
ITERATION=${ITERATION:-1}
mkdir -p $APP_ROOT_PATH/script
curl -sLO $URL
tar -C $APP_ROOT_PATH -xvf $FILENAME --strip-components=1
rm $FILENAME
cat > $APP_ROOT_PATH/script/after-install.sh <<'EOF'
#/usr/bin/env bash
/usr/bin/systemctl start node_exporter &>/dev/null || true
/usr/bin/systemctl daemon-reload
EOF
cat > $APP_ROOT_PATH/script/before-remove.sh <<'EOF'
#/usr/bin/env bash
/usr/bin/systemctl stop node_exporter &>/dev/null || true
EOF
cat > $APP_ROOT_PATH/script/after-remove.sh <<'EOF'
#/usr/bin/env bash
/usr/bin/systemctl daemon-reload
EOF
mkdir -p $ROOT_PATH/etc/systemd/system
cat > $ROOT_PATH/etc/systemd/system/node_exporter.service <<'EOF'
[Unit]
Description=Node Exporter
[Service]
Type=simple
Restart=always
PIDFile=/opt/node_exporter/node_exporter.pid
EnvironmentFile=/opt/node_exporter/script/node_exporter.env
ExecStart=/opt/node_exporter/node_exporter $ARG
StartLimitInterval=15
StartLimitBurst=3
[Install]
WantedBy=default.target
EOF
touch $APP_ROOT_PATH/script/node_exporter.env
#chmod 755 $ROOT_PATH/script/after-install.sh $ROOT_PATH/script/before-remove.sh
# And then, package everything into a rpm package
# Besides writing yourself RPM spec, I'd recommend leverage fpm via docker.
docker run -it --rm -v $ROOT_PATH:/opt/pkg -w /opt/pkg dreampuf/fpm -s dir -t rpm -n node_exporter -v $VERSION --iteration $ITERATION --rpm-user root --rpm-group root --prefix / -C /opt/pkg/ -f --after-install opt/node_exporter/script/after-install.sh --before-remove opt/node_exporter/script/before-remove.sh --after-remove opt/node_exporter/script/after-remove.sh --config-files etc/systemd/system/node_exporter.service --config-files opt/node_exporter/script/node_exporter.env
if [ ! -f pkg/node_exporter-$VERSION-$ITERATION.x86_64.rpm ]; then
exit
fi
mv pkg/node_exporter-$VERSION-$ITERATION.x86_64.rpm $PWD/
# Now you have a pefect node_exporter package "$PWD/node_exporter-0.16.0-3.x86_64.rpm"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment