From 247a99e42a8f68e417b7c18b0842eb50193d89e5 Mon Sep 17 00:00:00 2001 From: fiftydinar <65243233+fiftydinar@users.noreply.github.com> Date: Fri, 25 Oct 2024 19:13:53 +0200 Subject: [PATCH] fix(default-flatpaks): Make notifications be more reliable by delaying service start for 30s & cleaner internet-related logs --- .../default-flatpaks/v1/default-flatpaks.sh | 7 ++- .../default-flatpaks/v1/system-flatpak-setup | 46 +++++++++---------- .../v1/system-flatpak-setup.service | 3 -- .../v1/system-flatpak-setup.timer | 8 ++++ .../default-flatpaks/v1/user-flatpak-setup | 46 +++++++++---------- .../v1/user-flatpak-setup.service | 5 +- .../v1/user-flatpak-setup.timer | 8 ++++ 7 files changed, 68 insertions(+), 55 deletions(-) create mode 100644 modules/default-flatpaks/v1/system-flatpak-setup.timer create mode 100644 modules/default-flatpaks/v1/user-flatpak-setup.timer diff --git a/modules/default-flatpaks/v1/default-flatpaks.sh b/modules/default-flatpaks/v1/default-flatpaks.sh index 7483db8..f95741c 100644 --- a/modules/default-flatpaks/v1/default-flatpaks.sh +++ b/modules/default-flatpaks/v1/default-flatpaks.sh @@ -9,6 +9,9 @@ cp -r "$MODULE_DIRECTORY"/default-flatpaks/system-flatpak-setup /usr/bin/system- cp -r "$MODULE_DIRECTORY"/default-flatpaks/user-flatpak-setup /usr/bin/user-flatpak-setup cp -r "$MODULE_DIRECTORY"/default-flatpaks/system-flatpak-setup.service /usr/lib/systemd/system/system-flatpak-setup.service cp -r "$MODULE_DIRECTORY"/default-flatpaks/user-flatpak-setup.service /usr/lib/systemd/user/user-flatpak-setup.service +cp -r "$MODULE_DIRECTORY"/default-flatpaks/system-flatpak-setup.timer /usr/lib/systemd/system/system-flatpak-setup.timer +cp -r "$MODULE_DIRECTORY"/default-flatpaks/user-flatpak-setup.timer /usr/lib/systemd/user/user-flatpak-setup.timer + configure_flatpak_repo () { CONFIG_FILE=$1 @@ -146,8 +149,8 @@ check_flatpak_id_validity_from_flathub () { echo "Enabling flatpaks module" mkdir -p /usr/share/bluebuild/default-flatpaks/{system,user} mkdir -p /etc/bluebuild/default-flatpaks/{system,user} -systemctl enable -f system-flatpak-setup.service -systemctl enable -f --global user-flatpak-setup.service +systemctl enable -f system-flatpak-setup.timer +systemctl enable -f --global user-flatpak-setup.timer # Check that `system` is present before configuring. Also copy template list files before writing Flatpak IDs. if [[ ! $(echo "$1" | yq -I=0 ".system") == "null" ]]; then diff --git a/modules/default-flatpaks/v1/system-flatpak-setup b/modules/default-flatpaks/v1/system-flatpak-setup index c62a502..9666e9c 100755 --- a/modules/default-flatpaks/v1/system-flatpak-setup +++ b/modules/default-flatpaks/v1/system-flatpak-setup @@ -1,26 +1,24 @@ #!/usr/bin/env bash # Check for available internet connection before proceeding (network-online.target doesn't work for some network connections) -# Check it 5 times in 3 second interval, to avoid until loop +# Check it 3 times in 2 second interval, to avoid until loop # Used when adding remotes & when installing flatpaks check_internet_connection() { - local max_attempts=5 - local sleep_time=3 + local max_attempts=3 + local sleep_time=2 local attempt=1 - while [[ ${attempt} -le ${max_attempts} ]]; do + while (( attempt <= max_attempts )); do if curl --silent --head --fail "https://fedoraproject.org/static/hotspot.txt" > /dev/null; then - internet_connection=true - return 0 + return 0 else - internet_connection=false - echo "Internet connection is not available. Waiting..." - sleep ${sleep_time} - attempt=$((attempt + 1)) + echo "Internet connection is not available. Waiting..." + sleep ${sleep_time} + (( attempt++ )) fi done - echo "ERROR: Internet connection is not available. Skipping the operation above." + return 1 } # Opt out of and remove Fedora's system flatpak repos @@ -125,11 +123,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 if it doesn't exist (requires internet)" - check_internet_connection - if "${internet_connection}"; then - echo "Internet connection is successful, applying the operation above" + if check_internet_connection; then + echo "Adding system-wide remote $REPO_NAME from $REPO_URL if it doesn't exist (requires internet)" flatpak remote-add --if-not-exists --system "$REPO_NAME" "$REPO_URL" + else + echo "NOTE: Skipping the addition of system-wide remote, because there is no internet connection" fi fi @@ -229,20 +227,22 @@ if [[ -f $INSTALL_LIST_FILE ]] || [[ -f $USER_INSTALL_LIST_FILE ]]; then INSTALL_LIST="$COMBINED_INSTALL_LIST" fi if [[ -n $INSTALL_LIST ]] && [[ ! $NOTIFICATIONS == "true" ]]; then - echo "Installing system flatpaks from config list (requires internet)" - check_internet_connection - if "${internet_connection}"; then - echo "Internet connection is successful, applying the operation above" + if check_internet_connection; then + echo "Installing system flatpaks from config list (requires internet)" flatpak install --system --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]} + else + echo "ERROR: Installation of system flatpaks was not performed, because there was no internet connection" + exit 1 fi elif [[ -n $INSTALL_LIST ]] && [[ $NOTIFICATIONS == "true" ]]; then - echo "Installing system flatpaks from config list (requires internet)" - check_internet_connection - if "${internet_connection}"; then - echo "Internet connection is successful, applying the operation above" + if check_internet_connection; then + echo "Installing system flatpaks from config list (requires internet)" notify-send-pre-install flatpak install --system --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]} notify-send-install + else + echo "ERROR: Installation of system flatpaks was not performed, because there was no internet connection" + exit 1 fi fi fi diff --git a/modules/default-flatpaks/v1/system-flatpak-setup.service b/modules/default-flatpaks/v1/system-flatpak-setup.service index 344e083..1c57914 100644 --- a/modules/default-flatpaks/v1/system-flatpak-setup.service +++ b/modules/default-flatpaks/v1/system-flatpak-setup.service @@ -9,6 +9,3 @@ ExecStart=/usr/bin/system-flatpak-setup Restart=on-failure RestartSec=30 StartLimitInterval=3 - -[Install] -WantedBy=multi-user.target diff --git a/modules/default-flatpaks/v1/system-flatpak-setup.timer b/modules/default-flatpaks/v1/system-flatpak-setup.timer new file mode 100644 index 0000000..e18c323 --- /dev/null +++ b/modules/default-flatpaks/v1/system-flatpak-setup.timer @@ -0,0 +1,8 @@ +[Unit] +Description=Timer for system-flatpak-setup + +[Timer] +OnBootSec=30 + +[Install] +WantedBy=timers.target \ No newline at end of file diff --git a/modules/default-flatpaks/v1/user-flatpak-setup b/modules/default-flatpaks/v1/user-flatpak-setup index b2ef9b8..d4e06cf 100755 --- a/modules/default-flatpaks/v1/user-flatpak-setup +++ b/modules/default-flatpaks/v1/user-flatpak-setup @@ -1,26 +1,24 @@ #!/usr/bin/env bash # Check for available internet connection before proceeding (network-online.target doesn't work for some network connections) -# Check it 5 times in 3 second interval, to avoid until loop +# Check it 3 times in 2 second interval, to avoid until loop # Used when adding remotes & when installing flatpaks check_internet_connection() { - local max_attempts=5 - local sleep_time=3 + local max_attempts=3 + local sleep_time=2 local attempt=1 - while [[ ${attempt} -le ${max_attempts} ]]; do + while (( attempt <= max_attempts )); do if curl --silent --head --fail "https://fedoraproject.org/static/hotspot.txt" > /dev/null; then - internet_connection=true - return 0 + return 0 else - internet_connection=false - echo "Internet connection is not available. Waiting..." - sleep ${sleep_time} - attempt=$((attempt + 1)) + echo "Internet connection is not available. Waiting..." + sleep ${sleep_time} + (( attempt++ )) fi done - echo "ERROR: Internet connection is not available. Skipping the operation above." + return 1 } # Remove Fedora's flatpak user repos, if they exist @@ -112,11 +110,11 @@ done # Set up per-user Flatpak repository if [[ $REPO_URL != "null" && $REPO_NAME != "null" ]]; then - echo "Adding user-wide remote $REPO_NAME from $REPO_URL if it doesn't exist (requires internet)" - check_internet_connection - if "${internet_connection}"; then - echo "Internet connection is successful, applying the operation above" + if check_internet_connection; then + echo "Adding user-wide remote $REPO_NAME from $REPO_URL if it doesn't exist (requires internet)" flatpak remote-add --if-not-exists --user "$REPO_NAME" "$REPO_URL" + else + echo "NOTE: Skipping the addition of user-wide remote, because there is no internet connection" fi fi @@ -180,20 +178,22 @@ if [[ -f $INSTALL_LIST_FILE ]] || [[ -f $USER_INSTALL_LIST_FILE ]]; then INSTALL_LIST="$COMBINED_INSTALL_LIST" fi if [[ -n $INSTALL_LIST ]] && [[ ! $NOTIFICATIONS == "true" ]]; then - echo "Installing user flatpaks from config list (requires internet)" - check_internet_connection - if "${internet_connection}"; then - echo "Internet connection is successful, applying the operation above" + if check_internet_connection; then + echo "Installing user flatpaks from config list (requires internet)" flatpak install --user --noninteractive "$REPO_NAME" ${INSTALL_LIST[@]} + else + echo "ERROR: Installation of user flatpaks was not performed, because there was no internet connection" + exit 1 fi elif [[ -n $INSTALL_LIST ]] && [[ $NOTIFICATIONS == "true" ]]; then - echo "Installing user flatpaks from config list (requires internet)" - check_internet_connection - if "${internet_connection}"; then - echo "Internet connection is successful, applying the operation above" + if check_internet_connection; then + echo "Installing user flatpaks from config list (requires internet)" 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 + else + echo "ERROR: Installation of user flatpaks was not performed, because there was no internet connection" + exit 1 fi fi fi diff --git a/modules/default-flatpaks/v1/user-flatpak-setup.service b/modules/default-flatpaks/v1/user-flatpak-setup.service index ae4ccbd..37d752d 100644 --- a/modules/default-flatpaks/v1/user-flatpak-setup.service +++ b/modules/default-flatpaks/v1/user-flatpak-setup.service @@ -4,11 +4,8 @@ Wants=network-online.target After=system-flatpak-setup.service [Service] -Type=simple +Type=oneshot ExecStart=/usr/bin/user-flatpak-setup Restart=on-failure RestartSec=30 StartLimitInterval=3 - -[Install] -WantedBy=default.target diff --git a/modules/default-flatpaks/v1/user-flatpak-setup.timer b/modules/default-flatpaks/v1/user-flatpak-setup.timer new file mode 100644 index 0000000..023ea65 --- /dev/null +++ b/modules/default-flatpaks/v1/user-flatpak-setup.timer @@ -0,0 +1,8 @@ +[Unit] +Description=Timer for user-flatpak-setup + +[Timer] +OnBootSec=30 + +[Install] +WantedBy=timers.target \ No newline at end of file