From b72f43d24fffa17d4f91a67ab306463211e92c76 Mon Sep 17 00:00:00 2001 From: Gabriele Iannetti Date: Fri, 2 Dec 2022 14:19:17 +0100 Subject: [PATCH] Refactor rpm build process * Move rpm build files to contrib/rpm/ directory structure * Update top level README.md for referencing to contrib/rpm/README.md for rpm build --- README.md | 23 +-------- contrib/rpm/README.md | 25 ++++++++++ contrib/rpm/build.sh | 49 +++++++++++++++++++ .../rpm/config/prometheus-ipmi-exporter.yml | 0 .../rpm/docker/Dockerfile-centos7 | 7 ++- .../rpm}/prometheus-ipmi-exporter.spec | 0 .../rpm/sudoers}/prometheus-ipmi-exporter | 0 .../systemd}/prometheus-ipmi-exporter.service | 0 rpm/build.sh | 23 --------- 9 files changed, 79 insertions(+), 48 deletions(-) create mode 100644 contrib/rpm/README.md create mode 100755 contrib/rpm/build.sh rename prometheus-ipmi-exporter.yml => contrib/rpm/config/prometheus-ipmi-exporter.yml (100%) rename docker/RPM-Dockerfile => contrib/rpm/docker/Dockerfile-centos7 (80%) rename {rpm => contrib/rpm}/prometheus-ipmi-exporter.spec (100%) rename {sudoers => contrib/rpm/sudoers}/prometheus-ipmi-exporter (100%) rename {systemd => contrib/rpm/systemd}/prometheus-ipmi-exporter.service (100%) delete mode 100755 rpm/build.sh diff --git a/README.md b/README.md index e3725a8..0cea882 100644 --- a/README.md +++ b/README.md @@ -45,28 +45,9 @@ You can build a Docker container with the included `docker` make target: This will not even require Go tooling on the host. See the included [docker compose example](docker-compose.yml) for how to use the resulting container. -### Building RPM Package +### Building a RPM Package -The RPM package build targets to run the exporter locally with sudo permissions to expose most metrics. - -#### Directly on CentOS with rpmbuild - -Build script is located in `rpm/build.sh`. - -The RPM package will be available under `$HOME/rpmbuild/`. - -#### Docker Build Container - -The build container uses CentOS7. - -```bash -# from repo base dir run - -sudo docker build -t rpm_dock -f docker/RPM-Dockerfile . -sudo docker run -v $PWD:/rpm -it rpm_dock -``` - -The RPM package will be available under `build/`. +See [how to build a RPM package](contrib/rpm/README.md). ## Running diff --git a/contrib/rpm/README.md b/contrib/rpm/README.md new file mode 100644 index 0000000..7680600 --- /dev/null +++ b/contrib/rpm/README.md @@ -0,0 +1,25 @@ +# Building a RPM Package + +The RPM package build targets to run the exporter locally as Prometheus user with sudo permissions to expose most metrics. + +For building a RPM package a build script and [Docker](https://www.docker.com/) build container are available. + +NOTE: +> The build script and the Docker build image must be executed from the project base directory! + +## CentOS with rpmbuild + +A Build script is located in `contrib/rpm/build.sh` to be executed on a CentOS-based host with rpmbuild tool. + +The RPM package will be available under `$HOME/rpmbuild/`. + +## Docker Build Container + +A Docker build container is provided for CentOS7. + +```bash +sudo docker build -t centos7_rpmbuild_ipmi_exporter -f contrib/rpm/docker/Dockerfile-centos7 . +sudo docker run -v $PWD/contrib/rpm/build:/outdir -it centos7_rpmbuild_ipmi_exporter +``` + +The RPM package will be available under `contrib/rpm/build/`. diff --git a/contrib/rpm/build.sh b/contrib/rpm/build.sh new file mode 100755 index 0000000..096ec9a --- /dev/null +++ b/contrib/rpm/build.sh @@ -0,0 +1,49 @@ +#!/bin/bash +set -e + +function do_checks { + + info_msg="The build script must be executed from the projects base directory!" + + if [ -z "$VERSION" ]; then + echo "ERROR: Build failed! VERSION file not found" >&2 + echo "INFO: $info_msg" + exit 1 + fi + + if [ ! -d "$CONTRIB_DIR" ]; then + echo "ERROR: Build failed! Directory does not exist: $CONTRIB_DIR" >&2 + echo "INFO: $info_msg" + exit 1 + fi + +} + +export VERSION=$(cat VERSION) +export BUILD_DIR=$HOME/rpmbuild +export CONTRIB_DIR="contrib/rpm" +export PACKAGE_DIR=prometheus-ipmi-exporter-$VERSION + +do_checks + +make build + +mkdir -p $BUILD_DIR/{BUILD,RPMS,SOURCES,SPECS,SRPMS} +mkdir -p $BUILD_DIR/SOURCES/$PACKAGE_DIR/usr/bin +mkdir -p $BUILD_DIR/SOURCES/$PACKAGE_DIR/usr/lib/systemd/system +mkdir -p $BUILD_DIR/SOURCES/$PACKAGE_DIR/etc/sysconfig +mkdir -p $BUILD_DIR/SOURCES/$PACKAGE_DIR/etc/sudoers.d + +sed "s/VERSION/$VERSION/" $CONTRIB_DIR/prometheus-ipmi-exporter.spec > $BUILD_DIR/SPECS/prometheus-ipmi-exporter.spec + +cp $CONTRIB_DIR/systemd/prometheus-ipmi-exporter.service $BUILD_DIR/SOURCES/$PACKAGE_DIR/usr/lib/systemd/system/ +cp $CONTRIB_DIR/sudoers/prometheus-ipmi-exporter $BUILD_DIR/SOURCES/$PACKAGE_DIR/etc/sudoers.d/ +cp $CONTRIB_DIR/config/prometheus-ipmi-exporter.yml $BUILD_DIR/SOURCES/$PACKAGE_DIR/etc/sysconfig/ +cp ipmi_exporter $BUILD_DIR/SOURCES/$PACKAGE_DIR/usr/bin/ + +cd $BUILD_DIR/SOURCES +tar -czvf $PACKAGE_DIR.tar.gz $PACKAGE_DIR +cd $BUILD_DIR +echo Build dir is: $BUILD_DIR +ls -la $BUILD_DIR/SOURCES +rpmbuild -ba $BUILD_DIR/SPECS/prometheus-ipmi-exporter.spec diff --git a/prometheus-ipmi-exporter.yml b/contrib/rpm/config/prometheus-ipmi-exporter.yml similarity index 100% rename from prometheus-ipmi-exporter.yml rename to contrib/rpm/config/prometheus-ipmi-exporter.yml diff --git a/docker/RPM-Dockerfile b/contrib/rpm/docker/Dockerfile-centos7 similarity index 80% rename from docker/RPM-Dockerfile rename to contrib/rpm/docker/Dockerfile-centos7 index 5e8f417..ce80714 100644 --- a/docker/RPM-Dockerfile +++ b/contrib/rpm/docker/Dockerfile-centos7 @@ -7,9 +7,8 @@ RUN tar -C /usr/local -xvzf go1.18.1.linux-amd64.tar.gz ENV PATH=$PATH:/usr/local/go/bin COPY . /go/ipmi_exporter WORKDIR /go/ipmi_exporter -RUN rpm/build.sh -RUN mkdir /rpm +RUN contrib/rpm/build.sh +RUN mkdir /outdir ENTRYPOINT ["/bin/sh"] ENTRYPOINT ["cp"] -CMD ["-r","/root/rpmbuild/RPMS/x86_64/","/rpm/build"] - +CMD ["-r", "/root/rpmbuild/RPMS/x86_64/", "/outdir"] diff --git a/rpm/prometheus-ipmi-exporter.spec b/contrib/rpm/prometheus-ipmi-exporter.spec similarity index 100% rename from rpm/prometheus-ipmi-exporter.spec rename to contrib/rpm/prometheus-ipmi-exporter.spec diff --git a/sudoers/prometheus-ipmi-exporter b/contrib/rpm/sudoers/prometheus-ipmi-exporter similarity index 100% rename from sudoers/prometheus-ipmi-exporter rename to contrib/rpm/sudoers/prometheus-ipmi-exporter diff --git a/systemd/prometheus-ipmi-exporter.service b/contrib/rpm/systemd/prometheus-ipmi-exporter.service similarity index 100% rename from systemd/prometheus-ipmi-exporter.service rename to contrib/rpm/systemd/prometheus-ipmi-exporter.service diff --git a/rpm/build.sh b/rpm/build.sh deleted file mode 100755 index 2236f5b..0000000 --- a/rpm/build.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -set -e -export VERSION=$(cat VERSION) -export promdir=prometheus-ipmi-exporter-$VERSION -export builddir=$HOME/rpmbuild -make build -sed -i "s/VERSION/$(cat VERSION)/" rpm/prometheus-ipmi-exporter.spec -mkdir -p $builddir/{BUILD,RPMS,SOURCES,SPECS,SRPMS} -mkdir -p $builddir/SOURCES/$promdir/usr/bin -mkdir -p $builddir/SOURCES/$promdir/usr/lib/systemd/system -mkdir -p $builddir/SOURCES/$promdir/etc/sysconfig -mkdir -p $builddir/SOURCES/$promdir/etc/sudoers.d -cp rpm/prometheus-ipmi-exporter.spec $builddir/SPECS/ -cp systemd/prometheus-ipmi-exporter.service $builddir/SOURCES/$promdir/usr/lib/systemd/system/ -cp sudoers/prometheus-ipmi-exporter $builddir/SOURCES/$promdir/etc/sudoers.d/ -cp prometheus-ipmi-exporter.yml $builddir/SOURCES/$promdir/etc/sysconfig/ -cp ipmi_exporter $builddir/SOURCES/$promdir/usr/bin/ -cd $builddir/SOURCES -tar -czvf $promdir.tar.gz $promdir -cd $builddir -echo build dir is $builddir -ls -la $builddir/SOURCES -rpmbuild -ba $builddir/SPECS/prometheus-ipmi-exporter.spec