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(just): add option to use toolbox instead of distrobox #218

Merged
merged 10 commits into from
Feb 23, 2024
35 changes: 35 additions & 0 deletions build/ublue-os-just/31-toolbox.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Create toolbox containers from a defined manifest (this spec will not be expanded)
toolbox-assemble CONTAINER="prompt" ACTION="create" FILE="/etc/toolbox/toolbox.ini":
#!/usr/bin/bash
# Toolboxes are gathered from toolbox.ini, please add them there
source /usr/lib/ujust/ujust.sh
ToolboxAssembleList {{ FILE }} {{ ACTION }} {{ CONTAINER }}

# Create a new custom toolbox
toolbox-new IMAGE="prompt" NAME="prompt":
#!/usr/bin/bash
# Please only add distroboxes here if you need special checks and logins!
source /usr/lib/ujust/ujust.sh
# Prepare arguments
IMAGE={{ IMAGE }}
NAME={{ NAME }}
# Ask user to select an image if we need to prompt
if [ "$IMAGE" == "prompt" ]; then
echo -en "Please enter an image (format \"distro:release\" or registry): "
read IMAGE
if [ "$NAME" == "prompt" ]; then
echo -en "Please enter a name for the container: "
read NAME
fi
elif [ -z "${IMAGE,,}" ]; then
exit 1
fi
# If no name is provided and we do not use an image registry, generate one from the image name
if [ -z "$NAME" ] && [[ ! "$IMAGE" =~ / ]]; then
# Split IMAGE string into an array
# shellcheck disable=SC2206
DISTRORELEASE=(${IMAGE//:/ })
NAME="${DISTRORELEASE[0]}"-toolbox-"${DISTRORELEASE[1]}"
fi
# Create the toolbox
Toolbox create "$IMAGE" "$NAME"
10 changes: 3 additions & 7 deletions build/ublue-os-just/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ mkdir -p /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/*.just /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/ujust /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/ugum /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/lib-ujust/ujust.sh /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/lib-ujust/libcolors.sh /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/lib-ujust/libformatting.sh /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/lib-ujust/libfunctions.sh /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/lib-ujust/libdistrobox.sh /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/etc-distrobox/apps.ini /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/etc-distrobox/distrobox.ini /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/lib-ujust/*.sh /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/etc-distrobox/*.ini /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/etc-profile.d/ublue-os-just.sh /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/etc-profile.d/user-motd.sh /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/etc-toolbox/toolbox.ini /tmp/ublue-os/rpmbuild/SOURCES

rpmbuild -ba \
--define '_topdir /tmp/ublue-os/rpmbuild' \
Expand Down
8 changes: 0 additions & 8 deletions build/ublue-os-just/etc-distrobox/distrobox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ nvidia=true
image=quay.io/toolbx-images/centos-toolbox:latest
nvidia=true

[rhel-ubi8]
image=registry.access.redhat.com/ubi8/toolbox:latest
nvidia=true

[rhel-ubi9]
image=registry.access.redhat.com/ubi9/toolbox:latest
nvidia=true

[wolfi]
image=ghcr.io/ublue-os/wolfi-toolbox
nvidia=true
Expand Down
11 changes: 11 additions & 0 deletions build/ublue-os-just/etc-toolbox/toolbox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Add more distros and then use:
# ujust toolbox-assemble prompt create
# This will replace your toolboxes in place
[rhel-toolbox-8]
image=registry.access.redhat.com/ubi8/toolbox:latest

[rhel-toolbox-9]
image=registry.access.redhat.com/ubi9/toolbox:latest

[fedora-toolbox-39]
image=fedora:39
171 changes: 171 additions & 0 deletions build/ublue-os-just/lib-ujust/libtoolbox.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#!/usr/bin/bash
# shellcheck disable=SC2154
########
## Function to create a toolbox with standardized args
########
## Create a debian toolbox toolbox named debian-unstable
# Toolbox create "quay.io/toolbx-images/debian-toolbox:unstable" "debian-unstable"
## Create an ubuntu toolbox and provide an authfile to authenticate with the registry
# Toolbox create "ubuntu:22.04" --authfile "/path/to/file"
function Toolbox (){
# Get the action we want to do
local ACTION="$1"
# Get the "image" argument, we use this as an abstraction layer
# To decide if it is an image registry or a distro+release image argument
local IMAGE="$2"

# Define local variables
local DISTRORELEASE

# If the ACTION is "replace"
if [ "$1" == "replace" ]; then
# Set ACTION to create
ACTION="create"

# Remove old image before continuing
toolbox rm --force "${@:3}"
fi

# Check if $IMAGE is an image registry url
if [[ "$IMAGE" =~ / ]]; then
# Create toolbox based on image from registry
toolbox "$ACTION" --image "$IMAGE" "${@:3}"
else
# Split IMAGE string into an array
# shellcheck disable=SC2206
DISTRORELEASE=(${IMAGE//:/ })
# Create toolbox with distro and release args
toolbox "$ACTION" --distro "${DISTRORELEASE[0]}" --release "${DISTRORELEASE[1]}" "${@:3}"
fi
}

########
## Function to assemble pre-defined toolbox containers from manifest files
########
## Assemble all containers defined in an ini file without confirmation
# ToolboxAssemble noconfirmcreate "/etc/toolbox/toolbox.ini"
# ToolboxAssemble noconfirmcreate "/etc/toolbox/toolbox.ini" ALL
## Assemble ubuntu from default ini manifest, with confirmation
# ToolboxAssemble confirm "/etc/toolbox/toolbox.ini" ubuntu-toolbox-22.04
## Remove a container defined in the default ini manifest
# ToolboxAssemble rm "/etc/toolbox/toolbox.ini" ubuntu-toolbox-22.04
function ToolboxAssemble (){
# Set defaults
local ACTION="create"
local FILE="/etc/toolbox/toolbox.ini"
local NAME=""

# Define local variables
local CONTAINERS
local IMAGE
local CONFIRM

# If an action is provided
if [ -n "$1" ]; then
# Set ACTION to the action specified
# and remove "noconfirm" from $1 when assigning it to ACTION
ACTION="${1/noconfirm/}"
fi

# If a filename is provided
if [ -n "$2" ]; then
# Set FILE to the provided filename
FILE="$2"
fi

# If container name is ALL
if [ "$3" == "ALL" ] || [ -z "$3" ]; then
if [[ ! "$1" =~ ^noconfirm ]]; then
# Ask user if they REALLY want to assemble all the containers
echo -e "${b}WARNING${n}: This will assemble and ${u}replace${n}\nALL containers defined in ${b}$FILE${n}."
CONFIRM=$(Confirm "Are you sure you want to do this?")
if [ "$CONFIRM" == "1" ]; then
echo "Aborting..."
return 1
fi
fi
# Get all the containers
CONTAINERS=$(grep -P "\[.+\]" "$FILE" | sed -E 's/\[(.+)\]/\1/')

# Run the toolbox assemble command
#toolbox assemble "$ACTION" --file "$FILE" --replace
for CONTAINER in $CONTAINERS
do
# Get the image for the container
IMAGE=$(grep -A1 -P "\[$CONTAINER\]" "$FILE" | grep "image" | sed 's/image=//')

# Replace the container
Toolbox replace "$IMAGE" "$CONTAINER"
done
return $?
else
# Set toolbox name to provided name
NAME="$3"
fi

# If we do not want confirmations
if [[ ! "$1" =~ ^noconfirm ]]; then
# Ask the user if they really want to replace $NAME container
echo -e "${b}WARNING${n}: This will assemble and ${u}replace${n} the container ${b}$NAME${n}\nwith the one defined in ${b}$FILE${n}."
CONFIRM=$(Confirm "Are you sure you want to do this?")
if [ "$CONFIRM" == "1" ]; then
echo "Aborting..."
return 1
fi
fi

# Get the image for the container
IMAGE=$(grep -A1 -P "\[$NAME\]" "$FILE" | grep "image" | sed 's/image=//')

# Replace the toolbox container
Toolbox replace "$IMAGE" "$NAME"
}

########
## Function to parse a toolbox.ini file and make a selectable list from it
########
## Parse a toolbox.ini manifest and let user select which container to setup
# ToolboxAssembleList "$HOME/toolbox.ini" create
## Parse a toolbox.ini manifest and create ubuntu container without confirmation
# ToolboxAssembleList "$HOME/toolbox.ini" noconfirmcreate ubuntu-toolbox-22.04
function ToolboxAssembleList (){
# Set defaults
local FILE="$1"
local ACTION="create"
local CHOICE="prompt"

# Define local variables
local CONTAINERS

# If an ACTION is supplied
if [ -n "$2" ]; then
# Replace default action
ACTION="$2"
fi

# If a CHOICE is predefined
if [ -n "$3" ]; then
# Replace default choice
CHOICE="$3"
fi

# If the choice is "prompt" then ask user what container they want
if [ "$CHOICE" == "prompt" ]; then
CONTAINERS=$(grep -P "\[.+\]" "$FILE" | sed -E 's/\[(.+)\]/\1/')
echo "${b}Pre-defined Containers${n}"
echo "Please select a container to create"
# Disable an irrelevant shellscheck for next line as we want word splitting
# shellcheck disable=SC2086
CHOICE=$(Choose ALL $CONTAINERS)
fi

# If choice is not empty by now (will be empty if escaped from Choice function)
if [ -n "$CHOICE" ]; then
# If ACTION is create
if [ "$ACTION" == "create" ]; then
ACTION="replace"
fi
# Assemble the selected container
ToolboxAssemble "$ACTION" "$FILE" "$CHOICE"
fi
}
2 changes: 2 additions & 0 deletions build/ublue-os-just/lib-ujust/ujust.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ source /usr/lib/ujust/libformatting.sh
source /usr/lib/ujust/libfunctions.sh
# Import functionality related to distrobox
source /usr/lib/ujust/libdistrobox.sh
# Import functionality related to toolbox
source /usr/lib/ujust/libtoolbox.sh
18 changes: 15 additions & 3 deletions build/ublue-os-just/ublue-os-just.spec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Name: ublue-os-just
Packager: ublue-os
Vendor: ublue-os
Version: 0.27
Version: 0.28
Release: 1%{?dist}
Summary: ublue-os just integration
License: MIT
Expand Down Expand Up @@ -30,6 +30,9 @@ Source16: libdistrobox.sh
Source17: apps.ini
Source18: distrobox.ini
Source19: user-motd.sh
Source20: libtoolbox.sh
Source21: toolbox.ini
Source22: 31-toolbox.just

%global sub_name %{lua:t=string.gsub(rpm.expand("%{NAME}"), "^ublue%-os%-", ""); print(t)}

Expand All @@ -44,7 +47,7 @@ Adds ublue-os just integration for easier setup
mkdir -p -m0755 %{buildroot}%{_datadir}/%{VENDOR}/%{sub_name}
install -Dm755 %{SOURCE0} %{buildroot}%{_sysconfdir}/profile.d/ublue-os-just.sh
install -Dm755 %{SOURCE19} %{buildroot}%{_sysconfdir}/profile.d/user-motd.sh
cp %{SOURCE1} %{SOURCE2} %{SOURCE3} %{SOURCE4} %{SOURCE5} %{SOURCE6} %{SOURCE7} %{SOURCE8} %{buildroot}%{_datadir}/%{VENDOR}/%{sub_name}
cp %{SOURCE1} %{SOURCE2} %{SOURCE3} %{SOURCE4} %{SOURCE5} %{SOURCE6} %{SOURCE7} %{SOURCE8} %{SOURCE22} %{buildroot}%{_datadir}/%{VENDOR}/%{sub_name}

# Create justfile which contains all .just files included in this package
# Apply header first due to default not working in included justfiles
Expand All @@ -65,12 +68,17 @@ install -Dm644 %{SOURCE13} %{buildroot}/%{_exec_prefix}/lib/ujust
install -Dm644 %{SOURCE14} %{buildroot}/%{_exec_prefix}/lib/ujust
install -Dm644 %{SOURCE15} %{buildroot}/%{_exec_prefix}/lib/ujust
install -Dm644 %{SOURCE16} %{buildroot}/%{_exec_prefix}/lib/ujust
install -Dm644 %{SOURCE20} %{buildroot}/%{_exec_prefix}/lib/ujust

# Add default manifest files for distrobox
mkdir -p -m0755 %{buildroot}/%{_sysconfdir}/distrobox
install -Dm644 %{SOURCE17} %{buildroot}/%{_sysconfdir}/distrobox
install -Dm644 %{SOURCE18} %{buildroot}/%{_sysconfdir}/distrobox

# Add default manifest file for toolbox
mkdir -p -m0755 %{buildroot}/%{_sysconfdir}/toolbox
install -Dm644 %{SOURCE21} %{buildroot}/%{_sysconfdir}/toolbox

%files
%dir %attr(0755,root,root) %{_datadir}/%{VENDOR}/%{sub_name}
%attr(0755,root,root) %{_sysconfdir}/profile.d/ublue-os-just.sh
Expand All @@ -82,17 +90,21 @@ install -Dm644 %{SOURCE18} %{buildroot}/%{_sysconfdir}/distrobox
%attr(0644,root,root) %{_exec_prefix}/lib/ujust/ujust.sh
%attr(0644,root,root) %{_exec_prefix}/lib/ujust/lib*.sh
%attr(0644,root,root) %{_sysconfdir}/distrobox/*.ini
%attr(0644,root,root) %{_sysconfdir}/toolbox/*.ini

%post
# Generate ujust bash completion
just --completions bash | sed -E 's/([\(_" ])just/\1ujust/g' > %{_datadir}/bash-completion/completions/ujust
chmod 644 %{_datadir}/bash-completion/completions/ujust

%changelog
* Sun Feb 04 2024 HikariKnight <[email protected]> - 0.28
- Add option to use toolbox in ujust

* Mon Jan 29 2024 RJ Trujillo <[email protected]> - 0.27
- Add bluefin-cli and wolfi-toolbox to distrobox assemble config

* Sat Jan 29 2024 Benjamin Sherman <[email protected]> - 0.26
* Mon Jan 29 2024 Benjamin Sherman <[email protected]> - 0.26
- Improve versatility of user-motd

* Sat Jan 27 2024 Benjamin Sherman <[email protected]> - 0.25
Expand Down
Loading