852f6b2
#!/bin/bash
852f6b2
852f6b2
# Use newrelease.sh to update the kubernetes spec file with any change in release
852f6b2
# or go lang version. The goal is to separate changes to the spec file related
852f6b2
# to kubernetes from changes to spec file function and capabilities. This is not
852f6b2
# needed for the current kubernetes packages in Fedora (a single version of
852f6b2
# k8s for each Fedora release), but useful if not necessary for the planned
852f6b2
# deployment of multiple versions of k8s per Fedora release.
852f6b2
#
852f6b2
#
852f6b2
set -o errexit
852f6b2
set -o nounset
852f6b2
set -o pipefail
852f6b2
852f6b2
# default configuration file name
852f6b2
CFG_NAME=./newrelease.conf
852f6b2
852f6b2
# default template name postfix
852f6b2
# TEMPLATE_POSTFIX="-template.spec"
852f6b2
852f6b2
###
852f6b2
### newrelease - set up for a new version of kubernetes. Does the following:
93344a0
###    1. Checks for configuration file and creates if missing. Optionally,
93344a0
###       create a draft configuration file.
852f6b2
###    2. Use data from configuration file to generate kubernetes.spec
93344a0
###       from the ./template/kubernetes-template.spec template file.
852f6b2
###    3. Download source, if needed, based on configuration.
852f6b2
###
852f6b2
###    Configuration file is named 'newrelease.conf'.
852f6b2
###
852f6b2
### Usage:
852f6b2
###   newrelease.sh               -- this help file
852f6b2
###   newrelease.sh -h            -- this help file
93344a0
###   newrelease.sh -c            -- generate configuration file
852f6b2
###   newrelease.sh -y            -- execute using configuration file
852f6b2
###
852f6b2
### Options:
93344a0
###  -y         Execute script using configuration file. If configuration
93344a0
###             does not exist then create draft
93344a0
###  -c         Create draft configuration file
852f6b2
###  -h         Show this message
852f6b2
###
852f6b2
852f6b2
# See: https://gist.github.com/kovetskiy/a4bb510595b3a6b17bfd1bd9ac8bb4a5
852f6b2
help() {
852f6b2
  sed -rn 's/^### ?//;T;p' "$0"
852f6b2
}
852f6b2
93344a0
# display help and exit if no arguements, -h or not either
93344a0
# -y or -c
93344a0
if [[ $# == 0 || $1 == "-h" || ! ($1 =~ (-y|-c)) ]]; then
852f6b2
  help
852f6b2
  exit 1
852f6b2
fi
852f6b2
93344a0
# if -c flag, create config file and exit
93344a0
if [[ ($1 =~ (-c|-C)) || ($1 == "-y" && ! -f "$CFG_NAME") ]]; then
852f6b2
93344a0
  if [ -f "$CFG_NAME" ]
93344a0
  then
93344a0
    echo "** Configuration file already exists. "
93344a0
    echo "** Remove to create a new configuration file or edit and reuse."
93344a0
    help
93344a0
    exit 1
93344a0
  fi
852f6b2
852f6b2
  echo " Creating draft configuration file"
852f6b2
  echo " Edit ${CFG_NAME} with release information and rerun script"
852f6b2
  {
852f6b2
    printf "# spec file name\nexport SPEC_NAME=kubernetes\n\n"
916edfd
    printf "# spec template name and path\nTEMPLATE=./template/kubernetes-template.spec\n\n"
852f6b2
    printf "# kubernetes release tag from git repository\n# e.g. export GIT_TAG=v1.24.3\nexport GIT_TAG=vxx.xx.xx\n\n"
852f6b2
    printf "# the 'built-with' golang version - expressed as SEMVER\n# e.g. export GOLANG_VERSION=1.18.3\nexport GOLANG_VERSION=xx.xx.xx"
852f6b2
  } >> "${CFG_NAME}"
852f6b2
  exit 1
852f6b2
fi
852f6b2
93344a0
# load config file from local directory using CFG_NAME
93344a0
# format is standard bash source.
93344a0
93344a0
# variables are:
93344a0
# SPEC_NAME - name of the spec file to process
93344a0
# GIT_TAG - release tag of package
93344a0
# GOLANG_VERSION - minimal golang version (version used in build)
93344a0
93344a0
# shellcheck disable=SC1090
93344a0
source "${CFG_NAME}"
93344a0
echo "Sourcing ${CFG_NAME}"
93344a0
852f6b2
# generate needed values
852f6b2
# do not edit warning
852f6b2
export WARNING="# Do not edit this spec file. (Re-)Generate using newrelease.sh"
852f6b2
# spec file in this repository
852f6b2
SPEC=${SPEC_NAME}.spec
852f6b2
852f6b2
# strip and v or V prefix, if any for tar file name and spec name
852f6b2
export TAR_VERSION="${GIT_TAG#[vV]}"
852f6b2
# replace dash (if any) with tilde for spec file version
852f6b2
export SPEC_VERSION="${TAR_VERSION//-/\~}"
852f6b2
# export MAJ_MIN
852f6b2
MAJ_MIN=$( echo "${SPEC_VERSION}" | cut -f1,2 -d".")
852f6b2
852f6b2
852f6b2
# echo settings
852f6b2
echo "Spec file: ${SPEC}"
852f6b2
echo "Spec name: ${SPEC_NAME}"
852f6b2
echo "Template: ${TEMPLATE}"
852f6b2
echo "GIT Tag: ${GIT_TAG}"
916edfd
echo "Tar Version: ${TAR_VERSION}"
852f6b2
echo "Version: ${SPEC_VERSION}"
852f6b2
echo "Major:Minor: ${MAJ_MIN}"
852f6b2
echo "Golang Version: ${GOLANG_VERSION}"
852f6b2
852f6b2
# basic clean up - remove spec if they exist
852f6b2
if [ -f "./${SPEC}" ]; then
852f6b2
  rm "./${SPEC}"
7a44c19
  echo "removed old spec file - ${SPEC}"
852f6b2
fi
852f6b2
7a44c19
# basic clean up - remove other spec file(s) if they exist
7a44c19
if [ -f "./*.spec" ]; then
7a44c19
  rm "./*.spec"
7a44c19
  echo "removed other spec file(s)"
7a44c19
fi
852f6b2
# create spec file from template using variables from
852f6b2
# configuration file
852f6b2
# inspiration: https://blog.tratif.com/2023/01/27/bash-tips-3-templating-in-bash-scripts/
852f6b2
echo "creating ${SPEC}"
852f6b2
852f6b2
# spec template spec file (must exist)
852f6b2
if ! [ -f  "${TEMPLATE}" ]; then
852f6b2
  echo "${TEMPLATE} - spec template missing"
852f6b2
  exit 1
852f6b2
fi
852f6b2
852f6b2
# shellcheck disable=2016
852f6b2
< "${TEMPLATE}" envsubst '$WARNING,$GIT_TAG,$TAR_VERSION,$SPEC_NAME,$SPEC_VERSION,$GOLANG_VERSION' > "${SPEC}"
852f6b2
852f6b2
# download source
852f6b2
echo "Downloading source if needed"
852f6b2
echo "${SPEC_NAME}-${TAR_VERSION}.tar.gz"
852f6b2
spectool -g -s 0 "${SPEC}"
852f6b2
852f6b2
echo "****Don't forget to run: fedpkg new-sources ${SPEC_NAME}-${TAR_VERSION}.tar.gz"