Skip to content

Commit

Permalink
chore(updatecli) add a condition to check the package existence for G…
Browse files Browse the repository at this point in the history
…IT (linux) (#1060)

* add a condition to check the package

* wip

* rename

* return code for condition match

* return code for condition match

* echo debug

* sourceID

* sourceID sourceis

* use a variable

* add ppa:git-code to match provisionning

* add software-properties-common

* remove test for add-apt-repository  as we install it

* add gpg

* add gpg-agent
  • Loading branch information
smerle33 authored Feb 19, 2024
1 parent 03860b8 commit 3a28057
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
50 changes: 50 additions & 0 deletions updatecli/scripts/fetch-git-apt-latest-version.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion updatecli/scripts/run-in-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}" "$@"
8 changes: 8 additions & 0 deletions updatecli/updatecli.d/git-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3a28057

Please sign in to comment.