Skip to content

Commit

Permalink
Refactor updating/installing/running Zwift into their own scripts. (#108
Browse files Browse the repository at this point in the history
)

* Refactor updating/installing/running Zwift into their own scripts.

* Add new update/run files to workflow files

* Remove zwift_common.sh

Fix issue where $HOME doesn't work as expected in entrypoint.sh because
it's run as root in Docker.
  • Loading branch information
hobeone committed May 6, 2024
1 parent cdd887c commit e7bc5c7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/zwift_push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ jobs:
declare -A files_map
files_map["/bin/entrypoint"]="entrypoint.sh"
files_map["/bin/zwift-auth"]="zwift-auth.sh"
files_map["/bin/setup_and_run_zwift"]="setup_and_run_zwift.sh"
files_map["/bin/update_zwift.sh"]="update_zwift.sh"
files_map["/bin/run_zwift.sh"]="run_zwift.sh"
has_changes=false
for container_path in "${!files_map[@]}"; do
Expand Down
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ RUN apt-get update && \
COPY entrypoint.sh /bin/entrypoint
RUN chmod +x /bin/entrypoint

COPY setup_and_run_zwift.sh /bin/setup_and_run_zwift
RUN chmod +x /bin/setup_and_run_zwift
COPY update_zwift.sh /bin/update_zwift.sh
RUN chmod +x /bin/update_zwift.sh

COPY run_zwift.sh /bin/run_zwift.sh
RUN chmod +x /bin/run_zwift.sh

COPY zwift-auth.sh /bin/zwift-auth
RUN chmod +x /bin/zwift-auth
Expand Down
20 changes: 17 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
set -e
set -x

# This script runs as the root user in Docker so need to do this to find the
# home directory of the "user" user.
ZWIFT_USER_HOME=$( getent passwd "user" | cut -d: -f6 )
ZWIFT_HOME="$ZWIFT_USER_HOME/.wine/drive_c/Program Files (x86)/Zwift"

cd "$ZWIFT_HOME"

USER_UID=`id -u user`
USER_GID=`id -g user`

Expand All @@ -19,10 +26,17 @@ fi

# The next two should be no-ops if ZWIFT_UID/GID are not set but no harm
# running them anyway.
usermod -o -u ${USER_UID} user && groupmod -o -g ${USER_GID} user
usermod -o -u ${USER_UID} user
groupmod -o -g ${USER_GID} user
chown -R ${USER_UID}:${USER_GID} /home/user

mkdir -p /run/user/${USER_UID} && chown -R user:user /run/user/${USER_UID}
mkdir -p /run/user/${USER_UID}
chown -R user:user /run/user/${USER_UID}
sed -i "s/1000/${USER_UID}/g" /etc/pulse/client.conf

gosu user:user /bin/setup_and_run_zwift "$@"
# Run update if that's the first argument or if zwift directory is empty
if [ "$1" = "update" ] || [ ! "$(ls -A .)" ] ; then
gosu user:user /bin/update_zwift.sh "$@"
else
gosu user:user /bin/run_zwift.sh "$@"
fi
48 changes: 48 additions & 0 deletions run_zwift.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash
set -e
set -x

ZWIFT_HOME="$HOME/.wine/drive_c/Program Files (x86)/Zwift"

if [ ! -d "$ZWIFT_HOME" ]; then
echo "Directory $ZWIFT_HOME does not exist. Has Zwift been installed?"
exit 1
fi

cd "$ZWIFT_HOME"

# Do this in the run script in case a user sets this option post-install.
if [[ ! -z "$WINE_EXPERIMENTAL_WAYLAND" ]];
then
echo "enabling wayland support in wine 9.0"
wine reg.exe add HKCU\\Software\\Wine\\Drivers /v Graphics /d x11,wayland
fi

echo "starting zwift..."
wine start ZwiftLauncher.exe SilentLaunch

LAUNCHER_PID_HEX=$(winedbg --command "info proc" | grep -P "ZwiftLauncher.exe" | grep -oP "^\s\K.+?(?=\s)")
LAUNCHER_PID=$((16#$LAUNCHER_PID_HEX))

if [[ ! -z "$ZWIFT_USERNAME" ]] && [[ ! -z "$ZWIFT_PASSWORD" ]];
then
echo "authenticating with zwift..."
wine start /exec /bin/runfromprocess-rs.exe $LAUNCHER_PID ZwiftApp.exe --token=$(zwift-auth)
else
wine start /exec /bin/runfromprocess-rs.exe $LAUNCHER_PID ZwiftApp.exe
fi

sleep 3

until pgrep -f ZwiftApp.exe &> /dev/null
do
echo "Waiting for zwift to start ..."
sleep 1
done

echo "Killing uneccesary applications"
pkill ZwiftLauncher
pkill ZwiftWindowsCra
pkill -f MicrosoftEdgeUpdate

wineserver -w
44 changes: 2 additions & 42 deletions setup_and_run_zwift.sh → update_zwift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
set -e
set -x


echo "Called with arguments: '$@'"

ZWIFT_HOME="$HOME/.wine/drive_c/Program Files (x86)/Zwift"

mkdir -p "$ZWIFT_HOME"
Expand Down Expand Up @@ -69,7 +66,7 @@ then

# install dotnet48 for zwift
winetricks -q dotnet48

# install webview 2
wget -O webview2-setup.exe https://go.microsoft.com/fwlink/p/?LinkId=2124703
wine webview2-setup.exe /silent /install
Expand All @@ -82,48 +79,11 @@ then
# update game through zwift launcher
wait_for_zwift_game_update
wineserver -k

# cleanup
rm "$ZWIFT_HOME/ZwiftSetup.exe"
rm "$ZWIFT_HOME/webview2-setup.exe"
rm -rf "$HOME/.wine/drive_c/users/user/Downloads/Zwift"
rm -rf "$HOME/.cache/wine*"
exit 0
fi

if [[ ! -z "$WINE_EXPERIMENTAL_WAYLAND" ]];
then
echo "enabling wayland support in wine 9.0"
wine reg.exe add HKCU\\Software\\Wine\\Drivers /v Graphics /d x11,wayland
fi

echo "starting zwift..."
wine start ZwiftLauncher.exe SilentLaunch

LAUNCHER_PID_HEX=$(winedbg --command "info proc" | grep -P "ZwiftLauncher.exe" | grep -oP "^\s\K.+?(?=\s)")
LAUNCHER_PID=$((16#$LAUNCHER_PID_HEX))

if [[ ! -z "$ZWIFT_USERNAME" ]] && [[ ! -z "$ZWIFT_PASSWORD" ]];
then
echo "authenticating with zwift..."
wine start /exec /bin/runfromprocess-rs.exe $LAUNCHER_PID ZwiftApp.exe --token=$(zwift-auth)
else
wine start /exec /bin/runfromprocess-rs.exe $LAUNCHER_PID ZwiftApp.exe
fi

sleep 3

until pgrep -f ZwiftApp.exe &> /dev/null
do
echo "Waiting for zwift to start ..."
sleep 1
done

[[ -n "${DBUS_SESSION_BUS_ADDRESS}" ]] && watch -n 30 xdg-screensaver reset &

echo "Killing uneccesary applications"
pkill ZwiftLauncher
pkill ZwiftWindowsCra
pkill -f MicrosoftEdgeUpdate

wineserver -w

0 comments on commit e7bc5c7

Please sign in to comment.