All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ai.digital.integration.server.common.util.DockerUtil.kt Maven / Gradle / Ivy

There is a newer version: 23.3.0-1025.941
Show newest version
package ai.digital.integration.server.common.util

import org.gradle.api.Project
import java.io.ByteArrayOutputStream
import java.nio.charset.StandardCharsets
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class DockerUtil {
    companion object {
        fun execute(project: Project, args: List, logOutput: Boolean = true, throwErrorOnFailure: Boolean = true): String {
            return ProcessUtil.executeCommand(project, "docker ${args.joinToString(" ")}",
                    logOutput = logOutput, throwErrorOnFailure = throwErrorOnFailure)
        }

        fun inspect(project: Project, format: String, instanceId: String): String {
            val stdout = ByteArrayOutputStream()
            project.exec {
                executable = "docker"
                args = listOf(
                    "inspect",
                    "-f",
                    format,
                    instanceId
                )
                standardOutput = stdout
            }

            return stdout.toString(StandardCharsets.UTF_8).trim()
        }

        private fun findContainerIdByName(project: Project, containerName: String): String {
            val args = arrayListOf("ps", "-a", "-f", "name=$containerName", "--format", "{{.ID}}")
            return execute(project, args, false).trim()
        }

        fun dockerLogs(project: Project, containerName: String, lastUpdate: LocalDateTime): String {
            val formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME
            val containerId = findContainerIdByName(project, containerName)
            val args = arrayListOf("logs", containerId, "--since", lastUpdate.format(formatter))
            return execute(project, args, false)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy