|
| 1 | +#!/usr/bin/env bash |
| 2 | +# This script creates an RPM package containing the binary created by this Cargo project. |
| 3 | +# The script is not universally applicable, since it makes a few assumptions about the project structure: |
| 4 | +# 1. The RPM scaffolding needs to be provided in server/packaging/rpm |
| 5 | +# 2. The binary to be packaged needs to be created in target/release |
| 6 | + |
| 7 | +# The script takes one argument, which is the name of the binary that has been created by the build process. |
| 8 | +# This argument will be reused for naming the final RPM file. |
| 9 | + |
| 10 | +# Check if one parameter was specified - we'll use this as the name parameter for all files |
| 11 | +# This allows us to reuse the script across all operators |
| 12 | +if [ -z $1 ]; then |
| 13 | + echo "This script requires the project name to be specified as the first parameter!" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +export PACKAGE_NAME=$1 |
| 18 | +BINARY_FILE=target/release/$PACKAGE_NAME |
| 19 | + |
| 20 | +# The package description is parsed from the output of `cargo metadata` by using jq. |
| 21 | +# We need to look up the package with a select statement to match the name from an array of packages |
| 22 | +# The name is passed into jq as a jq variable, as no substitution would take place within the single |
| 23 | +# quotes of the jq expression. |
| 24 | +export PACKAGE_DESCRIPTION=$(~/.cargo/bin/cargo metadata --format-version 1| jq --arg NAME "$PACKAGE_NAME" '.packages[] | select(.name == $NAME) | .description') |
| 25 | +if [ -z $PACKAGE_DESCRIPTION ]; then |
| 26 | + echo "Unable to parse package description from output of `cargo metadata`, cannot build RPM without this field!" |
| 27 | + exit 2 |
| 28 | +fi |
| 29 | +echo |
| 30 | + |
| 31 | +# Check that we are being called from the main directory and the release build process has been run |
| 32 | +if [ ! -f $BINARY_FILE ]; then |
| 33 | + echo "Binary file not found at [$BINARY_FILE] - this script should be called from the root directory of the repository and 'cargo build --release' needs to have run before calling this script!" |
| 34 | + exit 3 |
| 35 | +fi |
| 36 | + |
| 37 | +echo Cleaning up prior build attempts |
| 38 | +rm -rf target/rpm |
| 39 | + |
| 40 | +# Parse the version and release strings from the PKGID reported by Cargo |
| 41 | +# This is in the form Path#Projectname:version, which we parse by repeated calls to awk with different separators |
| 42 | +# This could most definitely be improved, but works for now |
| 43 | +export VERSION_STRING=$(~/.cargo/bin/cargo pkgid | awk -F'#' '{print $2}' | awk -F':' '{print $2}') |
| 44 | +echo version: ${VERSION_STRING} |
| 45 | + |
| 46 | +export PACKAGE_VERSION=$(echo ${VERSION_STRING} | awk -F '-' '{print $1}') |
| 47 | + |
| 48 | +# Any suffix like '-nightly' is split out into the release here, as - is not an allowed character in rpm versions |
| 49 | +# The final release will look like 0.suffix or 0 if no suffix is specified. |
| 50 | +export PACKAGE_RELEASE="0$(echo ${VERSION_STRING} | awk -F '-' '{ if ($2 != "") print "."$2;}')" |
| 51 | + |
| 52 | +echo Defined package version: [${PACKAGE_VERSION}] |
| 53 | +echo Defined package release: [${PACKAGE_RELEASE}] |
| 54 | +echo Defined package description: [${PACKAGE_DESCRIPTION}] |
| 55 | + |
| 56 | +echo Creating directory scaffolding for RPM |
| 57 | +cp -r packaging/rpm target/ |
| 58 | +# Create empty directory for the binary to be placed into |
| 59 | +mkdir -p target/rpm/SOURCES/${PACKAGE_NAME}-VERSION/opt/stackable/${PACKAGE_NAME} |
| 60 | + |
| 61 | +# Create config directory and copy config file template over |
| 62 | +mkdir -p target/rpm/SOURCES/${PACKAGE_NAME}-VERSION/etc/stackable/${PACKAGE_NAME} |
| 63 | +cp packaging/config/agent.config target/rpm/SOURCES/${PACKAGE_NAME}-VERSION/etc/stackable/${PACKAGE_NAME} |
| 64 | + |
| 65 | +# The packaging source directory does not contain the version yet, as this will need to be replaced for every |
| 66 | +# execution. Instead the directory name contains the marker "VERSION" which we now replace with the actual version. |
| 67 | +rename VERSION ${PACKAGE_VERSION} target/rpm/SOURCES/${PACKAGE_NAME}-VERSION |
| 68 | + |
| 69 | +cp target/release/${PACKAGE_NAME} target/rpm/SOURCES/${PACKAGE_NAME}-${PACKAGE_VERSION}/opt/stackable/${PACKAGE_NAME}/ |
| 70 | + |
| 71 | +pushd target/rpm/SOURCES |
| 72 | +tar czvf ${PACKAGE_NAME}-${PACKAGE_VERSION}.tar.gz ${PACKAGE_NAME}-${PACKAGE_VERSION} |
| 73 | +popd |
| 74 | + |
| 75 | +rpmbuild --define "_topdir `pwd`/target/rpm" -v -ba target/rpm/SPECS/${PACKAGE_NAME}.spec |
0 commit comments