Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ublue-rebase-helper): add ublue-rebase helper and ublue-dx-helper #186

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions ublue/rebase-helper/src/ublue-dx-helper
Original file line number Diff line number Diff line change
@@ -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 <<EOF
The DX helper is not enabled here, the image most likely has some reason why it is disabled, check their documentation.
EOF
exit 0
fi

# FIXME: have a way to depend on ujust on the spec
#shellcheck disable=1091
source /usr/lib/ujust/ujust.sh

CURRENT_IMAGE=$(rpm-ostree status -b --json | jq -r '.deployments[0]."container-image-reference"')

if /bin/grep -q "\-dx" <<< $CURRENT_IMAGE ; then
CURRENT_STATE="enabled"
else
CURRENT_STATE="disabled"
fi
echo "Developer mode is currently ${CURRENT_STATE}"
echo "Enable or Disable developer mode"
OPTION=$(Choose Enable Disable)
if [[ "${OPTION,,}" =~ ^enable ]]; then
if [ "$CURRENT_STATE" = "enabled" ] ; then
echo "You are already on a developer image"
exit 0
fi
echo "Rebasing to a developer image"
NEW_IMAGE=$(sed "s/$IMAGE_BASE_NAME/$IMAGE_BASE_NAME-dx/" <<< $CURRENT_IMAGE)
rpm-ostree rebase $NEW_IMAGE && echo -e "\nUse \`ujust dx-group\` to add your user to the correct groups and complete the installation"
fi

if [[ "${OPTION,,}" =~ ^disable ]]; then
if [ "$CURRENT_STATE" != "enabled" ]; then
echo "You are not currently on a developer image"
exit 0
fi
echo "Rebasing to a non developer image"
# Remove -dx suffix from image, specifies ":" to mark the end of the image name
NEW_IMAGE=$(sed "s/\-dx//" <<< $CURRENT_IMAGE)
rpm-ostree rebase $NEW_IMAGE
fi
96 changes: 96 additions & 0 deletions ublue/rebase-helper/src/ublue-rebase-helper
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env bash

# Ublue Rebase Helper 2.0

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 -c "$QUERY" "$REBASE_HELPER_CONFIG_FILE" 2>/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
26 changes: 26 additions & 0 deletions ublue/rebase-helper/ublue-rebase-helper.spec
Original file line number Diff line number Diff line change
@@ -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