Skip to content

Commit

Permalink
Feat(just): add distrobox tooling to just
Browse files Browse the repository at this point in the history
  • Loading branch information
HikariKnight committed Jan 18, 2024
1 parent a5eb2d4 commit 508b210
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
1 change: 1 addition & 0 deletions build/ublue-os-just/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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

rpmbuild -ba \
--define '_topdir /tmp/ublue-os/rpmbuild' \
Expand Down
137 changes: 137 additions & 0 deletions build/ublue-os-just/lib-ujust/libdistrobox.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/bash
# shellcheck disable=SC2154
########
## Function to create a distrobox with standardized args
########
## Create a distrobox using default fedora:latest, name the box "my-fedora-box" and give it a custom homedir
# Distrobox "fedora:latest" "my-fedora-box" "$HOME/.var/containers/fedora-box"
## Create a debian toolbox distrobox named debian-unstable
# Distrobox "quay.io/toolbx-images/debian-toolbox:unstable" "debian-unstable"
## Create an ubuntu distrobox named someubuntubox with no custom homedir and unshare network namespace
## ($3 is required if supplying extra args, using "" makes the function skip it)
# Distrobox "ubuntu:latest" "someubuntubox" "" --unshare-ns
function Distrobox (){
IMAGE="$1"
NAME="$2"
HOMEDIR=""
# If custom home directory is supplied
if [ -n "$3" ]; then
HOMEDIR="$3"
fi

# If a custom home directory is not specified
if [ -z "$HOMEDIR" ]; then
distrobox create --nvidia -Y --image "$IMAGE" -n "$NAME" "${@:2}"
else
# Make the custom homedir path if it does not exist
if [ ! -d "$HOMEDIR" ]; then
mkdir -p "$HOMEDIR"
fi
# Create distrobox with custom home path
distrobox create --nvidia -Y --image "$IMAGE" -n "$NAME" -H "$HOMEDIR" "${@:3}"
fi
}

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

# 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
# Run the distrobox assemble command
distrobox assemble "$ACTION" --file "$FILE" --replace
else
# Set distrobox 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

# Run the distrobox assemble command
distrobox assemble "$ACTION" --file "$FILE" --name "$NAME" --replace
}

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

# 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
# Assemble the selected container
Assemble "$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 @@ -6,3 +6,5 @@ source /usr/lib/ujust/libcolors.sh
source /usr/lib/ujust/libformatting.sh
# Import functionality for just
source /usr/lib/ujust/libfunctions.sh
# Import functionality related to distrobox
source /usr/lib/ujust/libdistrobox.sh
8 changes: 7 additions & 1 deletion 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.22
Version: 0.23
Release: 1%{?dist}
Summary: ublue-os just integration
License: MIT
Expand All @@ -26,6 +26,7 @@ Source12: ujust.sh
Source13: libcolors.sh
Source14: libformatting.sh
Source15: libfunctions.sh
Source16: libdistrobox.sh

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

Expand Down Expand Up @@ -59,6 +60,8 @@ install -Dm644 %{SOURCE12} %{buildroot}/%{_exec_prefix}/lib/ujust
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

%files
%dir %attr(0755,root,root) %{_datadir}/%{VENDOR}/%{sub_name}
%attr(0755,root,root) %{_sysconfdir}/profile.d/ublue-os-just.sh
Expand All @@ -75,6 +78,9 @@ just --completions bash | sed -E 's/([\(_" ])just/\1ujust/g' > %{_datadir}/bash-
chmod 644 %{_datadir}/bash-completion/completions/ujust

%changelog
* Thu Jan 18 2024 HikariKnight <[email protected]> - 0.23
- Added tooling for distrobox

* Sun Jan 14 2024 HikariKnight <[email protected]> - 0.22
- Added sourcable libraries for just recipes

Expand Down

0 comments on commit 508b210

Please sign in to comment.