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 { 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) } }