diff --git a/updatecli/scripts/fetch-git-apt-latest-version.sh b/updatecli/scripts/fetch-git-apt-latest-version.sh new file mode 100755 index 000000000..378056756 --- /dev/null +++ b/updatecli/scripts/fetch-git-apt-latest-version.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# This script uses apt to find the latest version of git available and check if it match parameter +set -eux -o pipefail + +for cli in apt-get apt-cache grep cut xargs +do + if ! command -v $cli >/dev/null 2>&1 + then + echo "ERROR: command line ${cli} required but not found. Exiting." + exit 1 + fi +done + +{ + # Updating the list of latest updates available for installed packages on the system + apt-get update -q + apt-get install --yes --no-install-recommends \ + software-properties-common \ + gpg-agent + add-apt-repository -y ppa:git-core/ppa + apt-get update -q +} 1>&2 # Only write logs to stderr to avoid polluting updatecli's source (retrieved from the stdout) + +# Retrieve from apt-cache the latest version of git available +# Note: apt-cache policy will return a result like: +# :# apt-cache policy git +# git: +# Installed: 1:2.43.0-0ppa1~ubuntu22.04.1 +# Candidate: 1:2.43.2-0ppa1~ubuntu22.04.1 +# Version table: +# 1:2.43.2-0ppa1~ubuntu22.04.1 500 +# We want to get the Candidate version (which is the latest available) +# And we want it in a readable format +last=$(apt-cache policy git `# 1. Retrieve information about git from apt` \ + | grep 'Candidate' `# 2. Keep only the line about the Candidate version (latest available)` \ + | cut -f2,3 -d':' `# 3. Cut it so we only keep the version and remove title (version contains a :, hence keeping fields 2 and 3)` \ + | xargs `# 4. Trimming the result (removing spaces before and after)` \ + | cut -d'-' -f1 | cut -d':' -f2 `# 5. Remove the ubuntu package prefix and suffix and last line fails if empty` \ + | { read -r x ; if [ "$x" == '(none)' ]; then exit 1; else echo "${x}"; fi }) + + +# comparing parameter with last version available +if [[ "${last}" == "${1}" ]] +then + echo " ${last} match the selected version in parameter ${1}" + exit 0 # ok +else + echo " ${last} DOES NOT match the selected version in parameter ${1}" + exit 1 # ko +fi diff --git a/updatecli/scripts/run-in-docker.sh b/updatecli/scripts/run-in-docker.sh index c061f9efb..024b608cc 100755 --- a/updatecli/scripts/run-in-docker.sh +++ b/updatecli/scripts/run-in-docker.sh @@ -8,7 +8,8 @@ then fi SCRIPT_PATH=$1 +shift ABS_SCRIPT_PATH="$(readlink -f "${SCRIPT_PATH}")" SCRIPT_DIR=$(cd "$(dirname "${ABS_SCRIPT_PATH}")" && pwd -P) -docker run --rm --volume="${SCRIPT_DIR}:${SCRIPT_DIR}":ro --entrypoint=bash ubuntu:20.04 "${ABS_SCRIPT_PATH}" +docker run --rm --volume="${SCRIPT_DIR}:${SCRIPT_DIR}":ro --entrypoint=bash ubuntu:22.04 "${ABS_SCRIPT_PATH}" "$@" diff --git a/updatecli/updatecli.d/git-linux.yml b/updatecli/updatecli.d/git-linux.yml index 7677a3b33..356dafcb0 100644 --- a/updatecli/updatecli.d/git-linux.yml +++ b/updatecli/updatecli.d/git-linux.yml @@ -27,6 +27,14 @@ sources: transformers: - trimprefix: v +conditions: + checkIfAptPackageExists: + kind: shell + spec: + command: bash ./updatecli/scripts/run-in-docker.sh ./updatecli/scripts/fetch-git-apt-latest-version.sh + environments: + - name: PATH + targets: updateGitVersion: name: Bump Git version on Linux in the Packer default values