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

Support RockyLinux 9 #110

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
13 changes: 13 additions & 0 deletions .github/scripts/generate_release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# Read stdin
CURRENT_RELEASE=$(cat -)

# Output 1 if there's no existing release, and increment
# release number if releases already exist
if [ "x$CURRENT_RELEASE" == "x" ]; then
echo "1"
else
NEW_RELEASE=$(expr $CURRENT_RELEASE + 1)
echo "$NEW_RELEASE"
fi
63 changes: 63 additions & 0 deletions .github/workflows/build-and-release-rpm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Release Prometheus Slurm Exporter RPM

on:
workflow_dispatch:
inputs:
release-tag:
type: string
description: Tag to release (e.g. 0.20)
required: true
jobs:
build-rocky-rpms:
runs-on: ubuntu-latest
container:
image: rockylinux:9.2

# Required to upload the built RPM to the release
permissions:
contents: write

steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
ref: "${{ github.event.inputs.release-tag }}"
fetch-depth: 0

- name: Install Prerequisites
run: dnf install -y rpm-build rpmdevtools make git go jq curl

# Query GitHub API for assets under the specified release-tag
- name: Generate release number
id: release
run: |
RELEASE=$(curl -fSsL https://api.github.com/repos/${{ github.repository }}/releases |\
jq '.[] | select(.name == "${{ github.event.inputs.release-tag }}") | [.assets[].name | select(test(".*\\.rpm$"))] | length' |\
.github/scripts/generate_release.sh)
echo "::set-output name=release::$RELEASE"

- name: Prepare RPMBUILD Environment
run: |
mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS,tmp} &&
echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros &&
cp README.md ~/rpmbuild/SOURCES &&
cp LICENSE ~/rpmbuild/SOURCES &&
cp lib/systemd/prometheus-slurm-exporter.service ~/rpmbuild/SOURCES &&
cp packages/rpm-ci/*.spec ~/rpmbuild/SPECS

- name: Build RPM
run: |
cd ~/rpmbuild || exit 1
spectool -g -R --define '_release ${{ steps.release.outputs.release }}' --define '_version ${{ github.event.inputs.release-tag }}' SPECS/prometheus-slurm-exporter.spec
rpmbuild -bb --define '_release ${{ steps.release.outputs.release }}' --define '_version ${{ github.event.inputs.release-tag }}' SPECS/prometheus-slurm-exporter.spec

- name: Move RPM
run: |
mkdir -p RPMS
cp ~/rpmbuild/RPMS/*/*.rpm RPMS/

- name: Release RPM
uses: softprops/action-gh-release@v1
with:
tag_name: "${{ github.event.inputs.release-tag }}"
files: "RPMS/*"
4 changes: 3 additions & 1 deletion lib/systemd/prometheus-slurm-exporter.service
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
Description=Prometheus SLURM Exporter

[Service]
ExecStart=/usr/bin/prometheus-slurm-exporter
User=slurm_exporter
Group=slurm_exporter
ExecStart=/usr/bin/prometheus-slurm-exporter --listen-address=0.0.0.0:9341
Restart=always
RestartSec=15

Expand Down
68 changes: 68 additions & 0 deletions packages/rpm-ci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
## GitHub Actions rpmbuild environment

Generally this directory is used by the Release Prometheus Slurm Exporter RPM [workflow](https://github.com/stackhpc/prometheus-slurm-exporter/actions/workflows/build-and-release-rpm.yaml) to
build and release prometheus-slurm-exporter RPMs.

To trigger a build of an RPM for a new version of prometheus-slurm-exporter, run the following commands to sync the upstream repository:

```bash
git clone git@github.com:stackhpc/prometheus-slurm-exporter.git
cd prometheus-slurm-exporter
git remote add upstream https://github.com/vpenso/prometheus-slurm-exporter
git fetch upstream
git fetch upstream --tags
git checkout master
git merge upstream/master
git push origin master
git push origin master --tags
```

Then, the Release Prometheus Slurm Exporter RPM [workflow](https://github.com/stackhpc/prometheus-slurm-exporter/actions/workflows/build-and-release-rpm.yaml) can then be triggered with
with a newly-synced tag.


## Setup a development environment

In order to build your own RPMs you have to setup your own _development environment_ as explained [here](https://wiki.centos.org/HowTos/SetupRpmBuildEnvironment).

### Quick setup

1. Install the ``rpm-build`` and ``rpmdevtools`` packages.

2. Login as **normal user** and create the following structure in the home directory:
```bash
mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
```
3. Create the RPM macros file with the following content:
```bash
echo "%_topdir $HOME/rpmbuild" > ~/.rpmmacros
```

### Prepare the build

4. Clone this repo:
```bash
git clone https://github.com/vpenso/prometheus-slurm-exporter.git
```
5. Get into the source directory and copy the following files under ``~/rpmbuild/SOURCES``:
```bash
cd prometheus-slurm-exporter
cp README.md ~/rpmbuild/SOURCES
cp LICENSE ~/rpmbuild/SOURCES
cp lib/systemd/prometheus-slurm-exporter.service ~/rpmbuild/SOURCES
```
6. Copy the SPEC file in the proper directory:
```bash
cd prometheus-slurm-exporter
cp packages/rpm-ci/*.spec ~/rpmbuild/SPECS
```

### Build the RPM package

8. Build the RPM based on your SPEC file:
```bash
cd $HOME/rpmbuild/SPECS
spectool -g -R --define '_version {prometheus_slurm_exporter_release_version}' prometheus-slurm-exporter.spec
rpmbuild -ba --define '_version {prometheus_slurm_exporter_release_version}' prometheus-slurm-exporter.spec
```
9. The RPM package will be placed under $HOME/rpmbuild/RPMS/x86_64
86 changes: 86 additions & 0 deletions packages/rpm-ci/prometheus-slurm-exporter.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
%define __spec_install_post %{nil}
%define debug_package %{nil}
%define __os_install_post %{_dbpath}/brp-compress

Name: prometheus-slurm-exporter
Version: %{_version}
Release: %{_release}%{?dist}
Summary: Prometheus exporter for SLURM metrics
Group: Monitoring

License: GPL 3.0
URL: https://github.com/stackhpc/prometheus-slurm-exporter

Source0: https://github.com/stackhpc/prometheus-slurm-exporter/archive/refs/tags/%{version}.tar.gz
Source1: %{name}.service
Source2: LICENSE
Source3: README.md
Source4: %{name}

Requires(pre): shadow-utils

Requires(post): systemd
Requires(preun): systemd
Requires(postun): systemd
%{?systemd_requires}
BuildRequires: systemd

BuildRoot: %{_tmppath}/%{name}-%{version}-1-root

%description
A Prometheus exporter for metrics extracted from the Slurm resource scheduling system.

%prep
%setup -q

%build
go build -v -o %{_sourcedir}/%{name}

%install
rm -rf %{buildroot}
mkdir -vp %{buildroot}
mkdir -vp %{buildroot}%{_unitdir}/
mkdir -vp %{buildroot}/usr/bin
mkdir -vp %{buildroot}/usr/share/doc/%{name}-%{version}
mkdir -vp %{buildroot}/var/lib/slurm_exporter
install -m 644 %{SOURCE1} %{buildroot}/usr/lib/systemd/system/%{name}.service
install -m 644 %{SOURCE2} %{buildroot}/usr/share/doc/%{name}-%{version}/LICENSE
install -m 644 %{SOURCE3} %{buildroot}/usr/share/doc/%{name}-%{version}/README.md
install -m 755 %{SOURCE4} %{buildroot}/usr/bin/%{name}

%clean
rm -rf %{buildroot}

%pre
getent group slurm_exporter >/dev/null || groupadd -r slurm_exporter
getent passwd slurm_exporter >/dev/null || \
useradd -r -g slurm_exporter -d /var/lib/slurm_exporter -s /sbin/nologin \
-c "Prometheus Slurm exporter user" slurm_exporter
exit 0

%post
%systemd_post %{name}.service

%preun
%systemd_preun %{name}.service

%postun
%systemd_postun_with_restart %{name}.service

# Only remove the slurm_exporter service user
# when we're uninstalling.
# Need to force because there might still be a
# process running.
if [ $1 -eq 0 ]; then
userdel --force slurm_exporter 2> /dev/null; true
fi

%files
%defattr(-,root,root,-)
%doc LICENSE
%doc README.md
%{_bindir}/%{name}
/usr/share/doc/%{name}-%{version}/
%{_unitdir}/%{name}.service
%attr(755, slurm_exporter, slurm_exporter)/var/lib/slurm_exporter