You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.0 KiB
Kotlin

package me.eater.hefbrug.platform_utils.service
import me.eater.hefbrug.access.AccessSkeleton
import me.eater.hefbrug.access.FileType
import me.eater.hefbrug.executor.ExecutionContext
import me.eater.hefbrug.platform_utils.PlatformUtil
class RunitManager(context: ExecutionContext) : ServiceManager(context) {
override suspend fun isRunning(name: String): Boolean {
return ro.execute("sv", "status", name).stdout.startsWith("run:")
}
override suspend fun isEnabled(name: String): Boolean {
return ro.exists("/var/service/$name", FileType.Directory)
}
override suspend fun hasAutoStart(name: String): Boolean {
return !ro.exists("/etc/sv/$name/stop")
}
override suspend fun setState(name: String, enabled: Boolean, running: Boolean, autostart: Boolean) {
val serviceConf = "/etc/sv/$name"
val stopFile = "$serviceConf/stop"
val activeService = "/var/service/$name"
if (!autostart) {
rw.execute("touch", stopFile).orThrow()
} else if (!hasAutoStart(name)) {
rw.execute("rm", stopFile).orThrow()
}
val isEnabled = isEnabled(name)
if (isEnabled && !enabled) {
rw.execute("rm", activeService).orThrow()
}
if (!isEnabled && (enabled || running)) {
rw.execute("ln", "-s", serviceConf, "/var/service").orThrow()
}
val isRunning = isRunning(name)
if (isRunning && !running) {
rw.execute("sv", "stop", name).orThrow()
}
if (!isRunning && running) {
rw.execute("sv", "start", name).orThrow()
}
}
object Util : PlatformUtil<RunitManager> {
override suspend fun isSupported(access: AccessSkeleton): Boolean {
return access.exists("/var/service", FileType.Directory) &&
access.exists("/etc/sv", FileType.Directory) &&
access.execute("which", "sv").exitCode == 0
}
override suspend fun getManager(context: ExecutionContext) = RunitManager(context)
}
}