Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker registry login #17

Merged
merged 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=1.1.36
version=1.3.0
release.useAutomaticVersion=true
11 changes: 11 additions & 0 deletions src/main/kotlin/com/cognifide/gradle/environment/docker/Docker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ class Docker(val environment: EnvironmentExtension) {
*/
val runtime by lazy { Runtime.determine(environment) }

/**
* Holds registry-related configuration options.
*/
val registry by lazy { DockerRegistry(this) }

/**
* Configure registry-related options.
*/
fun registry(options: DockerRegistry.() -> Unit) = registry.using(options)

val composeFile = common.obj.relativeFile(environment.rootDir, "docker-compose.yml")

val composeTemplateFile = common.obj.relativeFile(environment.sourceDir, "docker-compose.yml.peb")
Expand All @@ -60,6 +70,7 @@ class Docker(val environment: EnvironmentExtension) {
}

fun init() {
registry.loginAuto()
generateComposeFile()
containers.resolve()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.cognifide.gradle.environment.docker

import org.buildobjects.process.ProcBuilder
import org.gradle.internal.os.OperatingSystem

@Suppress("TooGenericExceptionCaught")
class DockerRegistry(private val docker: Docker) {

private val common = docker.environment.common

val url = common.obj.string {
common.prop.string("docker.registry.url")?.let { set(it) }
}

val user = common.obj.string {
common.prop.string("docker.registry.user")?.let { set(it) }
}

val password = common.obj.string {
common.prop.string("docker.registry.password")?.let { set(it) }
}

val loginAuto = common.obj.boolean {
convention(common.obj.provider { loggable })
common.prop.boolean("docker.registry.loginAuto")?.let { set(it) }
}

fun loginAuto() {
if (loginAuto.get()) {
login()
}
}

val loggable get() = listOf(url.orNull, user.orNull, password.orNull).all { !it.isNullOrBlank() }

fun login() {
if (!loggable) {
throw DockerException("Cannot login to Docker registry as credentials are not specified!")
}

val exitCode = try {
if (OperatingSystem.current().isWindows) {
ProcBuilder("docker")
.withArgs("login", url.orNull, "-u", user.orNull, "-p", password.orNull)
.ignoreExitStatus()
.run()
} else {
val passwordFile = docker.environment.rootDir.file("docker-registry-pwd.txt").get().asFile.apply {
parentFile.mkdirs()
writeText(password.orNull.orEmpty())
}
ProcBuilder("sh")
.withArgs("-c", "cat ${passwordFile.absolutePath} | docker login ${url.orNull} -u ${user.orNull} --password-stdin")
.ignoreExitStatus()
.run()
}.exitValue
} catch (e: Exception) {
throw DockerException("Error occurred while logging in to Docker registry '${url.orNull}' as '${user.orNull}'! Cause: ${e.message}", e)
}

if (exitCode != 0) {
throw DockerException("Cannot login to Docker registry '${url.orNull}' as '${user.orNull}'! Exit code: $exitCode")
}
}

fun logout() {
if (!loggable) {
throw DockerException("Cannot logout from Docker registry as credentials are not specified!")
}

val exitCode = try {
ProcBuilder("docker")
.withArgs("logout", url.orNull)
.ignoreExitStatus()
.run()
} catch (e: Exception) {
throw DockerException("Error occurred while logging out from Docker registry '${url.orNull}'! Cause: ${e.message}", e)
}.exitValue

if (exitCode != 0) {
throw DockerException("Cannot logout from Docker registry '${url.orNull}'! Exit code: $exitCode")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ class Stack(val environment: EnvironmentExtension) {

try {
val composeFilePath = environment.docker.composeFile.get().asFile.path
DockerProcess.exec { withArgs("stack", "deploy", "-c", composeFilePath, internalName.get()) }
DockerProcess.exec {
withArgs("stack", "deploy", "-c", composeFilePath, internalName.get(), "--with-registry-auth", "--resolve-image=always")
}
} catch (e: DockerException) {
throw StackException("Failed to deploy Docker stack '${internalName.get()}'!", e)
}
Expand Down