-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7512 from uyuni-project/master-update-notifications
Notify users when updates are available to the Uyuni server
- Loading branch information
Showing
10 changed files
with
227 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
java/code/src/com/redhat/rhn/domain/notification/types/UpdateAvailable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright (c) 2023 SUSE LLC | ||
* | ||
* This software is licensed to you under the GNU General Public License, | ||
* version 2 (GPLv2). There is NO WARRANTY for this software, express or | ||
* implied, including the implied warranties of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 | ||
* along with this software; if not, see | ||
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. | ||
* | ||
* Red Hat trademarks are not licensed under GPLv2. No permission is | ||
* granted to use or replicate Red Hat trademarks that are incorporated | ||
* in this software or its documentation. | ||
*/ | ||
package com.redhat.rhn.domain.notification.types; | ||
|
||
import com.redhat.rhn.common.conf.ConfigDefaults; | ||
import com.redhat.rhn.common.localization.LocalizationService; | ||
import com.redhat.rhn.domain.notification.NotificationMessage; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Notification data for an update being available for the server. | ||
*/ | ||
public class UpdateAvailable implements NotificationData { | ||
|
||
private static final LocalizationService LOCALIZATION_SERVICE = LocalizationService.getInstance(); | ||
private static final Logger LOG = LogManager.getLogger(UpdateAvailable.class); | ||
private static final String UYUNI_PATCH_REPO = "systemsmanagement_Uyuni_Stable_Patches"; | ||
private static final String UYUNI_UPDATE_REPO = "systemsmanagement_Uyuni_Stable"; | ||
|
||
private final boolean mgr = !ConfigDefaults.get().isUyuni(); | ||
private final String version = StringUtils.substringBeforeLast(ConfigDefaults.get().getProductVersion(), "."); | ||
private final Runtime runtime; | ||
|
||
/** | ||
* Constructor allowing to pass the runtime as argument. | ||
* | ||
* @param runtimeIn runtime object for command execution | ||
*/ | ||
public UpdateAvailable(Runtime runtimeIn) { | ||
this.runtime = runtimeIn; | ||
} | ||
|
||
/** | ||
* returns true if there are updates available. | ||
* | ||
* @return boolean | ||
**/ | ||
public boolean updateAvailable() { | ||
boolean hasUpdates = false; | ||
String repo = mgr ? "SLE-Module-SUSE-Manager-Server-" + version + "-Updates" : UYUNI_PATCH_REPO; | ||
|
||
try { | ||
Process patchProc = runtime.exec(new String[]{"/bin/bash", "-c", | ||
"LC_ALL=C zypper lp -r " + repo + " | grep 'applicable patch'"}); | ||
patchProc.waitFor(); | ||
// 0 here means there are patches | ||
hasUpdates = (0 == patchProc.exitValue()); | ||
if (!hasUpdates && !mgr) { | ||
// Check for updates on uyuni when there are no patches | ||
Process updateProc = runtime.exec(new String[]{"/bin/bash", "-c", | ||
"LC_ALL=C zypper lu -r " + UYUNI_UPDATE_REPO + " | grep 'Available Version'"}); | ||
updateProc.waitFor(); | ||
hasUpdates = (0 == updateProc.exitValue()); | ||
} | ||
} | ||
catch (IOException e) { | ||
LOG.warn("Unable to check for updates", e); | ||
} | ||
catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
LOG.warn("Wait for update check was interrupted", e); | ||
} | ||
return hasUpdates; | ||
} | ||
|
||
@Override | ||
public NotificationMessage.NotificationMessageSeverity getSeverity() { | ||
return NotificationMessage.NotificationMessageSeverity.warning; | ||
} | ||
|
||
@Override | ||
public NotificationType getType() { | ||
return NotificationType.UpdateAvailable; | ||
} | ||
|
||
@Override | ||
public String getSummary() { | ||
return LOCALIZATION_SERVICE.getMessage("notification.updateavailable.summary"); | ||
} | ||
|
||
@Override | ||
public String getDetails() { | ||
String url = mgr ? | ||
"https://www.suse.com/releasenotes/x86_64/SUSE-MANAGER/" + version + "/index.html" : | ||
"https://www.uyuni-project.org/pages/stable-version.html"; | ||
return LOCALIZATION_SERVICE.getMessage("notification.updateavailable.detail", url); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
java/code/src/com/redhat/rhn/domain/notification/types/test/UpdateAvailableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2023 SUSE LLC | ||
* | ||
* This software is licensed to you under the GNU General Public License, | ||
* version 2 (GPLv2). There is NO WARRANTY for this software, express or | ||
* implied, including the implied warranties of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 | ||
* along with this software; if not, see | ||
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. | ||
* | ||
* Red Hat trademarks are not licensed under GPLv2. No permission is | ||
* granted to use or replicate Red Hat trademarks that are incorporated | ||
* in this software or its documentation. | ||
*/ | ||
package com.redhat.rhn.domain.notification.types.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.redhat.rhn.common.conf.ConfigDefaults; | ||
import com.redhat.rhn.domain.notification.NotificationMessage; | ||
import com.redhat.rhn.domain.notification.types.NotificationType; | ||
import com.redhat.rhn.domain.notification.types.UpdateAvailable; | ||
import com.redhat.rhn.testing.MockObjectTestCase; | ||
|
||
import org.jmock.Expectations; | ||
import org.jmock.imposters.ByteBuddyClassImposteriser; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class UpdateAvailableTest extends MockObjectTestCase { | ||
|
||
private Runtime runtimeMock; | ||
private Process processMock; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
setImposteriser(ByteBuddyClassImposteriser.INSTANCE); | ||
runtimeMock = mock(Runtime.class); | ||
processMock = mock(Process.class); | ||
} | ||
|
||
@Test | ||
public void testPropertiesAndStrings() { | ||
UpdateAvailable notification = new UpdateAvailable(runtimeMock); | ||
assertEquals(NotificationType.UpdateAvailable, notification.getType()); | ||
assertEquals(NotificationMessage.NotificationMessageSeverity.warning, notification.getSeverity()); | ||
assertEquals("Updates are available.", notification.getSummary()); | ||
if (ConfigDefaults.get().isUyuni()) { | ||
assertEquals("A new update for Uyuni is now available. For further details, please refer to the " + | ||
"<a href=\"https://www.uyuni-project.org/pages/stable-version.html\">release notes<a>.", | ||
notification.getDetails()); | ||
} | ||
else { | ||
assertEquals("A new update for SUSE Manager is now available. For further details, please refer to the " + | ||
"<a href=\"https://www.suse.com/releasenotes/x86_64/SUSE-MANAGER/4.3/index.html\">release " + | ||
"notes<a>.", notification.getDetails()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testUpdatesAvailable() throws Exception { | ||
// Return 0 on all invocations of exec() -> an update is available | ||
checking(new Expectations() {{ | ||
allowing(runtimeMock).exec(with(any(String[].class))); | ||
will(returnValue(processMock)); | ||
allowing(processMock).waitFor(); | ||
allowing(processMock).exitValue(); | ||
will(returnValue(0)); | ||
}}); | ||
|
||
UpdateAvailable notification = new UpdateAvailable(runtimeMock); | ||
assertTrue(notification.updateAvailable()); | ||
} | ||
|
||
@Test | ||
public void testNoUpdatesAvailable() throws Exception { | ||
// Return 1 on all invocations of exec() -> no update is available | ||
checking(new Expectations() {{ | ||
allowing(runtimeMock).exec(with(any(String[].class))); | ||
will(returnValue(processMock)); | ||
allowing(processMock).waitFor(); | ||
allowing(processMock).exitValue(); | ||
will(returnValue(1)); | ||
}}); | ||
|
||
UpdateAvailable notification = new UpdateAvailable(runtimeMock); | ||
assertFalse(notification.updateAvailable()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Shows a notification when an update for Uyuni is available |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Shows a notification when an update for Uyuni is available |