add check for already installed packages before installing them
This commit is contained in:
parent
64511aa491
commit
5f0271bf2b
2 changed files with 35 additions and 11 deletions
|
@ -10,20 +10,29 @@ private var aptInit = false
|
||||||
* Installs package(s) by using package manager "apt".
|
* Installs package(s) by using package manager "apt".
|
||||||
*
|
*
|
||||||
* @param packages the packages to be installed, packages must be separated by space if there are more than one
|
* @param packages the packages to be installed, packages must be separated by space if there are more than one
|
||||||
|
* @param ignoreAlreadyInstalled if true, then for an already installed package no action will be taken,
|
||||||
|
* if "ignoreAlreadyInstalled" is false, then installation is always attempted, which normally results in an upgrade if package wa already installed
|
||||||
*/
|
*/
|
||||||
fun Prov.aptInstall(packages: String): ProvResult = task {
|
fun Prov.aptInstall(packages: String, ignoreAlreadyInstalled: Boolean = true): ProvResult = task {
|
||||||
|
val packageList = packages.split(" ")
|
||||||
|
val allInstalled: Boolean = packageList.map { isPackageInstalled(it) }.fold(true, { a, b -> a && b })
|
||||||
|
if (!allInstalled) {
|
||||||
|
if (!isPackageInstalled(packages)) {
|
||||||
if (!aptInit) {
|
if (!aptInit) {
|
||||||
cmd("sudo apt-get update")
|
cmd("sudo apt-get update")
|
||||||
cmd("sudo apt-get install -qy apt-utils")
|
cmd("sudo apt-get install -qy apt-utils")
|
||||||
aptInit = true
|
aptInit = true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val packageList = packages.split(" ")
|
|
||||||
for (packg in packageList) {
|
for (packg in packageList) {
|
||||||
// see https://superuser.com/questions/164553/automatically-answer-yes-when-using-apt-get-install
|
// see https://superuser.com/questions/164553/automatically-answer-yes-when-using-apt-get-install
|
||||||
cmd("sudo DEBIAN_FRONTEND=noninteractive apt-get install -qy $packg")
|
cmd("sudo DEBIAN_FRONTEND=noninteractive apt-get install -qy $packg")
|
||||||
}
|
}
|
||||||
ProvResult(true) // dummy
|
ProvResult(true) // dummy
|
||||||
|
} else {
|
||||||
|
ProvResult(true, out = "All packages are already installed. [$packages]")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package org.domaindrivenarchitecture.provs.framework.ubuntu.install.base
|
||||||
|
|
||||||
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
|
import org.domaindrivenarchitecture.provs.test.defaultTestContainer
|
||||||
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
|
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Assertions.assertTrue
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
import org.junit.jupiter.api.Disabled
|
import org.junit.jupiter.api.Disabled
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
@ -22,6 +23,20 @@ internal class InstallKtTest {
|
||||||
assertTrue(res.success)
|
assertTrue(res.success)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ContainerTest
|
||||||
|
@Test
|
||||||
|
fun aptInstall_ignores_packages_already_installed() {
|
||||||
|
// given
|
||||||
|
val a = defaultTestContainer()
|
||||||
|
|
||||||
|
// when
|
||||||
|
val res = a.aptInstall("sed grep")
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertTrue(res.success)
|
||||||
|
assertEquals("All packages are already installed. [sed grep]", res.out)
|
||||||
|
}
|
||||||
|
|
||||||
@ContainerTest
|
@ContainerTest
|
||||||
@Test
|
@Test
|
||||||
@Disabled // run manually if needed;
|
@Disabled // run manually if needed;
|
||||||
|
|
Loading…
Reference in a new issue