Blob Blame History Raw
#!/bin/bash

# Use newrelease.sh to update the kubernetes spec file with any change in release
# or go lang version. The goal is to separate changes to the spec file related
# to kubernetes from changes to spec file function and capabilities. This is not
# needed for the current kubernetes packages in Fedora (a single version of
# k8s for each Fedora release), but useful if not necessary for the planned
# deployment of multiple versions of k8s per Fedora release.
#
#
set -o errexit
set -o nounset
set -o pipefail

# default configuration file name
CFG_NAME=./newrelease.conf

# default template name postfix
# TEMPLATE_POSTFIX="-template.spec"

###
### newrelease - set up for a new version of kubernetes. Does the following:
###    1. Checks for configuration file and will generate draft
###       configuration file if missing.
###    2. Use data from configuration file to generate kubernetes.spec
###       from the ./template/kubernetes.yaml template file.
###    3. Download source, if needed, based on configuration.
###
###    Configuration file is named 'newrelease.conf'.
###
### Usage:
###   newrelease.sh               -- this help file
###   newrelease.sh -h            -- this help file
###   newrelease.sh -y            -- execute using configuration file
###
### Options:
###  -y         Execute script. If missing configuration file, generate draft and exit
###  -h         Show this message
###

# See: https://gist.github.com/kovetskiy/a4bb510595b3a6b17bfd1bd9ac8bb4a5
help() {
  sed -rn 's/^### ?//;T;p' "$0"
}

if [[ $# == 0 || $1 == -h || "$1" != "-y" ]]; then
  help
  exit 1
fi

# load config file from local directory using CFG_NAME
# format is standard bash source.

# variables are:
# SPEC_NAME - name of the spec file to process
# GIT_TAG - release tag of package
# GOLANG_VERSION - minimal golang version (version used in build)

if [ -f "$CFG_NAME" ]
then
# shellcheck disable=SC1090
  source "${CFG_NAME}"
  echo "Sourcing ${CFG_NAME}"
else
  echo " Creating draft configuration file"
  echo " Edit ${CFG_NAME} with release information and rerun script"
  {
    printf "# spec file name\nexport SPEC_NAME=kubernetes\n\n"
    printf "# spec template name and path\nTEMPLATE=./template/kubernetes-template.spec\n\n"
    printf "# kubernetes release tag from git repository\n# e.g. export GIT_TAG=v1.24.3\nexport GIT_TAG=vxx.xx.xx\n\n"
    printf "# the 'built-with' golang version - expressed as SEMVER\n# e.g. export GOLANG_VERSION=1.18.3\nexport GOLANG_VERSION=xx.xx.xx"
  } >> "${CFG_NAME}"
  exit 1
fi

# generate needed values
# do not edit warning
export WARNING="# Do not edit this spec file. (Re-)Generate using newrelease.sh"
# spec file in this repository
SPEC=${SPEC_NAME}.spec

# strip and v or V prefix, if any for tar file name and spec name
export TAR_VERSION="${GIT_TAG#[vV]}"
# replace dash (if any) with tilde for spec file version
export SPEC_VERSION="${TAR_VERSION//-/\~}"
# export MAJ_MIN
MAJ_MIN=$( echo "${SPEC_VERSION}" | cut -f1,2 -d".")


# echo settings
echo "Spec file: ${SPEC}"
echo "Spec name: ${SPEC_NAME}"
echo "Template: ${TEMPLATE}"
echo "GIT Tag: ${GIT_TAG}"
echo "Tar Version: ${TAR_VERSION}"
echo "Version: ${SPEC_VERSION}"
echo "Major:Minor: ${MAJ_MIN}"
echo "Golang Version: ${GOLANG_VERSION}"

# basic clean up - remove spec if they exist
if [ -f "./${SPEC}" ]; then
  rm "./${SPEC}"
  echo "removed old spec file"
fi

# create spec file from template using variables from
# configuration file
# inspiration: https://blog.tratif.com/2023/01/27/bash-tips-3-templating-in-bash-scripts/
echo "creating ${SPEC}"

# spec template spec file (must exist)
if ! [ -f  "${TEMPLATE}" ]; then
  echo "${TEMPLATE} - spec template missing"
  exit 1
fi

# shellcheck disable=2016
< "${TEMPLATE}" envsubst '$WARNING,$GIT_TAG,$TAR_VERSION,$SPEC_NAME,$SPEC_VERSION,$GOLANG_VERSION' > "${SPEC}"

# download source
echo "Downloading source if needed"
echo "${SPEC_NAME}-${TAR_VERSION}.tar.gz"
spectool -g -s 0 "${SPEC}"

echo "****Don't forget to run: fedpkg new-sources ${SPEC_NAME}-${TAR_VERSION}.tar.gz"