diff --git a/ublue/rebase-helper/src/ublue-dx-helper b/ublue/rebase-helper/src/ublue-dx-helper new file mode 100755 index 0000000..36141a6 --- /dev/null +++ b/ublue/rebase-helper/src/ublue-dx-helper @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +get_config() { + REBASE_HELPER_CONFIG_FILE="${REBASE_HELPER_CONFIG_FILE:-/etc/ublue-os/rebase_helper.json}" + QUERY="$1" + FALLBACK="$2" + shift + shift + OUTPUT="$(jq -r "$QUERY" "$REBASE_HELPER_CONFIG_FILE" 2>/dev/null || echo "$FALLBACK")" + if [ "$OUTPUT" == "null" ] ; then + echo "$FALLBACK" + return + fi + echo "$OUTPUT" +} + +DX_HELPER_ENABLED="$(get_config '."dx-helper-enabled"' "true")" +IMAGE_BASE_NAME="$(get_config '."image-base-name"' "invalid-image-name")" + +if [[ "$DX_HELPER_ENABLED" == 'false' || "$IMAGE_BASE_NAME" == "invalid-image-name" ]] ; then + # FIXME: write this better + cat </dev/null || echo "$FALLBACK")" + if [ "$OUTPUT" == "null" ] ; then + echo "$FALLBACK" + return + fi + echo "$OUTPUT" +} + +#shellcheck disable=1091 +source /usr/lib/ujust/ujust.sh + +IMAGE_INFO="${REBASE_HELPER_INFO_FILE:-"/usr/share/ublue-os/image-info.json"}" +IMAGE_NAME=$(jq -r '."image-name"' < "$IMAGE_INFO") +IMAGE_VENDOR=$(jq -r '."image-vendor"' < "$IMAGE_INFO") +IMAGE_REGISTRY="ghcr.io/${IMAGE_VENDOR}" + +function list_tags(){ + local branch="${branch:-$1}" + shift + local regex='.Tags[]' + regex="$regex"\ ' | select(test("^PLACEHOLDER|^PLACEHOLDER-(?:\\d+\\IMAGE_DATE_SEPARATOR\\d+|\\d+)"))' + regex=${regex//PLACEHOLDER/$branch} + regex=${regex//IMAGE_DATE_SEPARATOR/$IMAGE_DATE_SEPARATOR} + echo -e >&2 "Listing images, this can take a bit of time..." + skopeo list-tags "docker://${IMAGE_REGISTRY}/${IMAGE_NAME}" | jq -r "$regex" +} + +IMAGE_DATE_SEPARATOR="$(get_config '."image-date-separator"' ".")" + +# Generates AVAILABLE_TAGS array from JSON array +AVAILABLE_TAGS="$(get_config '."available-tags"[]' "null")" +AVAILABLE_TAGS=${AVAILABLE_TAGS//\"/""} +mapfile -t AVAILABLE_TAGS < <(echo "$AVAILABLE_TAGS") +if [ "${AVAILABLE_TAGS[*]}" == "null" ] ; then + echo "Failure listing available tags" + exit 1 +fi + +echo "Which stream would you like to rebase to?" +CHOSEN_TAG="$(Choose date "${AVAILABLE_TAGS[@]}" cancel)" + +if [ "${CHOSEN_TAG}" == "date" ] ; then + echo "Which stream do you want to rebase to a specific date to?" + CHOSEN_TAG="$(Choose all current "${AVAILABLE_TAGS[@]}" cancel)" + if [[ ! " ${AVAILABLE_TAGS[*]} all current " =~ [[:space:]]${CHOSEN_TAG}[[:space:]] ]]; then + exit 0 + fi + case "$CHOSEN_TAG" in + "all") + CHOSEN_TAG="" + ;; + + "current") + CURRENT_IMAGE=$(rpm-ostree status -b --json | jq -r '.deployments[0]."container-image-reference"') + #shellcheck disable=2001 + CHOSEN_TAG="$(echo "$CURRENT_IMAGE" | sed "s/.*://")" + ;; + + "cancel") + exit 0 + ;; + esac + + echo "Warning: This will pin you to a specific version, do not forget to rebase back to a channel to resume receiving updates." + mapfile -t valid_tags < <(list_tags "$CHOSEN_TAG" | sed 's/\"//g' | sed 's/,//g') + CHOSEN_TAG=$(Choose cancel "${valid_tags[@]}") +elif [[ ! " ${AVAILABLE_TAGS[*]} " =~ [[:space:]]${CHOSEN_TAG}[[:space:]] ]]; then + # No valid things were found + exit 0 +fi + +if [[ "$CHOSEN_TAG" == "cancel" || "$CHOSEN_TAG" == "" ]] ; then + exit 0 +fi + +if [ "$rebase_target" == "" ] ; then + rebase_target="$IMAGE_REGISTRY/$IMAGE_NAME:$CHOSEN_TAG" +fi + +echo "Rebase target is ${rebase_target}" +if [[ $(Confirm "Do you really wish to rebase?") -ne "0" ]]; then + exit 1 +fi + +rpm-ostree rebase "ostree-image-signed:docker://${rebase_target}" +exit 0 diff --git a/ublue/rebase-helper/ublue-rebase-helper.spec b/ublue/rebase-helper/ublue-rebase-helper.spec new file mode 100644 index 0000000..f340af2 --- /dev/null +++ b/ublue/rebase-helper/ublue-rebase-helper.spec @@ -0,0 +1,26 @@ +%global debug_package %{nil} + +Name: ublue-rebase-helper +Version: 0.1.0 +Release: 1%{?dist} +Summary: Rebase helper for Universal Blue images + +License: Apache-2.0 +URL: https://github.com/ublue-os/packages +VCS: {{{ git_dir_vcs }}} +Source0: {{{ git_dir_pack }}} + +%description +Rebase helper script and develoepr edition script for Universal Blue images + +%prep +{{{ git_dir_setup_macro }}} + +%install +install -Dm0755 -t %{buildroot}%{_libexecdir} ./src/ublue-*-helper + +%files +%{_libexecdir}/ublue-*-helper + +%changelog +%autochangelog