improve installPpaFirefox to set priority for ppa above default (snap) installation and allow unattended upgrades

This commit is contained in:
ansgarz 2023-07-30 19:45:35 +02:00
parent 32840cdd46
commit e8fcdae778
2 changed files with 71 additions and 32 deletions

View file

@ -1,42 +1,53 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure
import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.addTextToFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import java.io.File
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstallFromPpa
/**
* Installs non-snap firefox, removing a firefox snap-installation if existing
* Installs ppa firefox (i.e. non-snap), removing snap-firefox if existing.
*/
fun Prov.installPpaFirefox() = task {
fun Prov.installPpaFirefox() = taskWithResult {
// inspired by: https://www.omgubuntu.co.uk/2022/04/how-to-install-firefox-deb-apt-ubuntu-22-04
// inspired by: https://wiki.ubuntuusers.de/Firefox/Installation/PPA/
task("remove snap firefox") {
if (chk("snap list | grep firefox")) {
val unattendeUpgradesForPpaFirefox = "/etc/apt/apt.conf.d/51unattended-upgrades-firefox"
val preCondition = checkFile(unattendeUpgradesForPpaFirefox)
if (preCondition) {
return@taskWithResult ProvResult(true, out = "Firefox already installed with ppa")
}
cmd("sudo apt-get -qy remove firefox", sudo = true)
optional {
cmd("snap remove firefox", sudo = true)
}
}
aptInstall("software-properties-common")
cmd("add-apt-repository -y ppa:mozillateam/ppa", sudo = true)
createFile("/etc/apt/preferences.d/mozillateam", mozillaTeamFileContent, sudo = true)
// set prio in order to use ppa-firefox above snap
addTextToFile(
"\nPackage: *\n" +
"Pin: release o=LP-PPA-mozillateam\n" +
"Pin-Priority: 1001\n",
File("/etc/apt/preferences.d/mozilla-firefox"),
aptInstallFromPpa("mozillateam", "ppa", "firefox")
createFile(
unattendeUpgradesForPpaFirefox,
"Unattended-Upgrade::Allowed-Origins:: \"LP-PPA-mozillateam:\${distro_codename}\";\n",
sudo = true
)
addTextToFile(
"""Unattended-Upgrade::Allowed-Origins:: "LP-PPA-mozillateam:${'$'}{distro_codename}";""",
File("/etc/apt/preferences.d/mozilla-firefox"),
sudo = true
)
aptInstall("firefox")
cmd("apt-get upgrade -y --allow-downgrades firefox", sudo = true)
}
private val mozillaTeamFileContent = """
Package: *
Pin: release o=LP-PPA-mozillateam
Pin-Priority: 100
Package: firefox*
Pin: release o=LP-PPA-mozillateam
Pin-Priority: 1001
Package: firefox*
Pin: release o=Ubuntu
Pin-Priority: -1
""".trimIndent()

View file

@ -1,32 +1,60 @@
package org.domaindrivenarchitecture.provs.desktop.infrastructure
import org.domaindrivenarchitecture.provs.framework.core.remote
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.deleteFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileContainsText
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.checkPackageInstalled
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.isPackageInstalled
import org.domaindrivenarchitecture.provs.framework.ubuntu.secret.secretSources.PromptSecretSource
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
internal class FirefoxKtTest {
// Attention: this test does not test full functionality of installFirefox, e.g. does not test
// remove snap, as this test runs against a container which does not have snap-firefox installed
// Attention: this test does not test full functionality of installPpaFirefox, e.g. does not test
// remove snap-firefox, as this test runs against a container, which does have neither snap nor snap-firefox installed
@ExtensiveContainerTest
fun installFirefox() {
// given
val prov = defaultTestContainer()
// when
val result = defaultTestContainer().session {
val result = prov.session {
deleteFile("/etc/apt/apt.conf.d/51unattended-upgrades-firefox", sudo = true)
deleteFile("/etc/apt/preferences.d/mozillateam", sudo = true)
installPpaFirefox()
checkPackageInstalled("firefox")
}
val result2 = prov.installPpaFirefox()
// then
assertTrue(result.success)
assertEquals("Firefox already installed with ppa", result2.out)
assertTrue(prov.isPackageInstalled("firefox"))
assertTrue(
prov.fileContainsText(
"/etc/apt/apt.conf.d/51unattended-upgrades-firefox",
"Unattended-Upgrade::Allowed-Origins:: \"LP-PPA-mozillateam:\${distro_codename}\";\n",
sudo = true
)
)
val expectedPolicyLine = Regex("1001? https?://ppa.launchpad(?:content)?.net/mozillateam/ppa/ubuntu")
val policy = prov.cmd("apt policy firefox").out
assertTrue(
policy?.contains(expectedPolicyLine) ?: false,
"$expectedPolicyLine was not found in $policy"
)
}
/**
* Tests installing firefox on a remote machine, e.g. a virtual machine
*/
@Test
@Disabled("Update connection details,then enable and run manually")
@Disabled("Update connection details, then enable the test and run manually")
fun installFirefox_remotely() {
val host = "192.168.56.123"
val user = "user"