Skip to content

Commit

Permalink
fix(default-flatpaks): No internet connection in some network setups
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftydinar authored Oct 23, 2024
1 parent 89ea220 commit f355ca6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 11 deletions.
48 changes: 42 additions & 6 deletions modules/default-flatpaks/v1/system-flatpak-setup
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
#!/usr/bin/env bash

# Check for available internet connection before proceeding (network-online.target doesn't work for some network connections)
# Check it 3 times in 3 second interval, to avoid until loop
# Used when adding remotes & when installing flatpaks
check_internet_connection() {
local max_attempts=3
local sleep_time=3
local attempt=1

while [[ ${attempt} -le ${max_attempts} ]]; do
if curl --silent --head --fail "https://fedoraproject.org/static/hotspot.txt" > /dev/null; then
internet_connection=true
return 0
else
internet_connection=false
echo "Internet connection is not available. Waiting..."
sleep ${sleep_time}
attempt=$((attempt + 1))
fi
done

echo "ERROR: Internet connection is not available. Skipping the operation above."
}

# Opt out of and remove Fedora's system flatpak repos
FLATPAK_SYSTEM_REMOTES=($(flatpak --system remotes))

Expand Down Expand Up @@ -103,8 +126,11 @@ done
# Set up system-wide Flatpak repository
if [[ $REPO_URL != "null" && $REPO_NAME != "null" ]]; then
echo "Adding system-wide remote $REPO_NAME from $REPO_URL"
echo "Note that --if-not-exists flag doesn't prevent the repo from modifying repo URL"
flatpak remote-add --if-not-exists --system "$REPO_NAME" "$REPO_URL"
echo "Note that --if-not-exists flag doesn't prevent the repo from modifying repo URL"
check_internet_connection
if "${internet_connection}"; then
flatpak remote-add --if-not-exists --system "$REPO_NAME" "$REPO_URL"
fi
fi

# If configured remote is flathub, enable it here.
Expand Down Expand Up @@ -189,20 +215,30 @@ if [[ -f $INSTALL_LIST_FILE ]] || [[ -f $USER_INSTALL_LIST_FILE ]]; then
INSTALL_LIST="$COMBINED_INSTALL_LIST"
fi
if [[ -n $INSTALL_LIST ]] && [[ ! $NOTIFICATIONS == "true" ]]; then
flatpak install --system --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
echo "Installing system flatpaks from config list"
check_internet_connection
if "${internet_connection}"; then
flatpak install --system --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
fi
elif [[ -n $INSTALL_LIST ]] && [[ $NOTIFICATIONS == "true" ]]; then
notify-send-pre-install
flatpak install --system --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
notify-send-install
echo "Installing system flatpaks from config list"
check_internet_connection
if "${internet_connection}"; then
notify-send-pre-install
flatpak install --system --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
notify-send-install
fi
fi
fi

# Remove flatpaks in list
if [[ -f $REMOVE_LIST_FILE ]] || [[ -f $USER_REMOVE_LIST_FILE ]]; then
REMOVE_LIST=$(comm -12 <(echo "$COMBINED_REMOVE_LIST" | sort) <(echo "$FLATPAK_LIST" | sort))
if [[ -n $REMOVE_LIST ]] && [[ ! $NOTIFICATIONS == "true" ]]; then
echo "Removing system flatpaks from config list"
flatpak uninstall --system --noninteractive ${REMOVE_LIST[@]}
elif [[ -n $REMOVE_LIST ]] && [[ $NOTIFICATIONS == "true" ]]; then
echo "Removing system flatpaks from config list"
notify-send-pre-uninstall
flatpak uninstall --system --noninteractive ${REMOVE_LIST[@]}
notify-send-uninstall
Expand Down
46 changes: 41 additions & 5 deletions modules/default-flatpaks/v1/user-flatpak-setup
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
#!/usr/bin/env bash

# Check for available internet connection before proceeding (network-online.target doesn't work for some network connections)
# Check it 3 times in 3 second interval, to avoid until loop
# Used when adding remotes & when installing flatpaks
check_internet_connection() {
local max_attempts=3
local sleep_time=3
local attempt=1

while [[ ${attempt} -le ${max_attempts} ]]; do
if curl --silent --head --fail "https://fedoraproject.org/static/hotspot.txt" > /dev/null; then
internet_connection=true
return 0
else
internet_connection=false
echo "Internet connection is not available. Waiting..."
sleep ${sleep_time}
attempt=$((attempt + 1))
fi
done

echo "ERROR: Internet connection is not available. Skipping the operation above."
}

# Remove Fedora's flatpak user repos, if they exist
FLATPAK_USER_REMOTES=($(flatpak --user remotes))

Expand Down Expand Up @@ -91,7 +114,10 @@ done
if [[ $REPO_URL != "null" && $REPO_NAME != "null" ]]; then
echo "Adding remote $REPO_NAME from $REPO_URL"
echo "Note that --if-not-exists flag doesn't prevent the repo from modifying repo URL"
flatpak remote-add --if-not-exists --user "$REPO_NAME" "$REPO_URL"
check_internet_connection
if "${internet_connection}"; then
flatpak remote-add --if-not-exists --user "$REPO_NAME" "$REPO_URL"
fi
fi

# Change repository title to configured title, if not null & if not already changed
Expand Down Expand Up @@ -140,20 +166,30 @@ if [[ -f $INSTALL_LIST_FILE ]] || [[ -f $USER_INSTALL_LIST_FILE ]]; then
INSTALL_LIST="$COMBINED_INSTALL_LIST"
fi
if [[ -n $INSTALL_LIST ]] && [[ ! $NOTIFICATIONS == "true" ]]; then
flatpak install --user --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
echo "Installing user flatpaks from config list"
check_internet_connection
if "${internet_connection}"; then
flatpak install --user --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
fi
elif [[ -n $INSTALL_LIST ]] && [[ $NOTIFICATIONS == "true" ]]; then
notify-send "Flatpak Installer" "Started install of user flatpaks" --app-name="Flatpak Installer" -u NORMAL
flatpak install --user --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
notify-send "Flatpak Installer" "Finished install of user flatpaks:\n$INSTALL_LIST" --app-name="Flatpak Installer" -u NORMAL
echo "Installing user flatpaks from config list"
check_internet_connection
if "${internet_connection}"; then
notify-send "Flatpak Installer" "Started install of user flatpaks" --app-name="Flatpak Installer" -u NORMAL
flatpak install --user --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]}
notify-send "Flatpak Installer" "Finished install of user flatpaks:\n$INSTALL_LIST" --app-name="Flatpak Installer" -u NORMAL
fi
fi
fi

# Remove flatpaks in list
if [[ -f $REMOVE_LIST_FILE ]] || [[ -f $USER_REMOVE_LIST_FILE ]]; then
REMOVE_LIST=$(comm -12 <(echo "$COMBINED_REMOVE_LIST" | sort) <(echo "$FLATPAK_LIST" | sort))
if [[ -n $REMOVE_LIST ]] && [[ ! $NOTIFICATIONS == "true" ]]; then
echo "Removing user flatpaks from config list"
flatpak uninstall --user --noninteractive ${REMOVE_LIST[@]}
elif [[ -n $REMOVE_LIST ]] && [[ $NOTIFICATIONS == "true" ]]; then
echo "Removing user flatpaks from config list"
notify-send "Flatpak Installer" "Started uninstall of some user flatpaks" --app-name="Flatpak Installer" -u NORMAL
flatpak uninstall --user --noninteractive ${REMOVE_LIST[@]}
notify-send "Flatpak Installer" "Finished uninstall of user flatpaks:\n$REMOVE_LIST" --app-name="Flatpak Installer" -u NORMAL
Expand Down

0 comments on commit f355ca6

Please sign in to comment.