Skip to content
Draft
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
10 changes: 10 additions & 0 deletions nmcp-tasks/api/nmcp-tasks.api
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public final class nmcp/internal/task/NmcpCheckFilesEntryPoint$Companion {
public final fun run (Ljava/util/List;Ljava/io/File;Z)V
}

public final class nmcp/internal/task/NmcpPublishDeploymentEntryPoint {
public static final field Companion Lnmcp/internal/task/NmcpPublishDeploymentEntryPoint$Companion;
public fun <init> ()V
public static final fun run (Ljava/util/function/BiConsumer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;)V
}

public final class nmcp/internal/task/NmcpPublishDeploymentEntryPoint$Companion {
public final fun run (Ljava/util/function/BiConsumer;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;)V
}

public final class nmcp/internal/task/NmcpPublishFileByFileToFileSystemEntryPoint {
public static final field Companion Lnmcp/internal/task/NmcpPublishFileByFileToFileSystemEntryPoint$Companion;
public fun <init> ()V
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package nmcp.internal.task

import gratatouille.tasks.GLogger
import gratatouille.tasks.GTask
import kotlin.time.Duration.Companion.seconds
import nmcp.transport.Success
import nmcp.transport.executeWithRetries
import nmcp.transport.nmcpClient
import okhttp3.Request
import okhttp3.RequestBody

@GTask(pure = false)
internal fun nmcpPublishDeployment(
logger: GLogger,
username: String?,
password: String?,
deploymentId: String?,
baseUrl: String?,
publishingTimeoutSeconds: Long?
) {
check(!deploymentId.isNullOrBlank()) {
"Nmcp: deploymentId is missing"
}

val token = toBearerToken(username, password)

@Suppress("NAME_SHADOWING")
val baseUrl = baseUrl ?: "https://central.sonatype.com/"
val url = baseUrl + "api/v1/publisher/deployment/$deploymentId"

logger.lifecycle("Publishing previously uploaded deployment bundle '$deploymentId'")
val request = Request.Builder()
.post(RequestBody.EMPTY)
.addHeader("Authorization", "Bearer $token")
.url(url)
.build()
val result = executeWithRetries(logger, nmcpClient, request)

if (result !is Success) {
error("Cannot publish deployment '$deploymentId' to maven central: ($result)}")
}

logger.lifecycle("Nmcp: deployment bundle '$deploymentId' moved to 'publishing' status.")

val timeout = publishingTimeoutSeconds?.seconds ?: 0.seconds
if (timeout.isPositive()) {
logger.lifecycle("Nmcp: waiting for publication...")
waitForStatus(setOf(PUBLISHED), timeout, logger, deploymentId, baseUrl, token)
logger.lifecycle("Nmcp: deployment is published.")
} else {
logger.lifecycle("Nmcp: deployment is publishing... Check the central portal UI to verify its status.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@ package nmcp.internal.task
import gratatouille.tasks.GInputFile
import gratatouille.tasks.GLogger
import gratatouille.tasks.GTask
import java.net.SocketTimeoutException
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
import kotlin.time.TimeSource.Monotonic.markNow
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import nmcp.transport.Success
import nmcp.transport.executeWithRetries
import nmcp.transport.nmcpClient
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okio.Buffer
import okio.ByteString
import okio.use

@GTask(pure = false)
Expand All @@ -35,16 +27,7 @@ internal fun nmcpPublishWithPublisherApi(
publishingTimeoutSeconds: Long?,
inputFile: GInputFile,
) {
check(!username.isNullOrBlank()) {
"Nmcp: username is missing"
}
check(!password.isNullOrBlank()) {
"Nmcp: password is missing"
}

val token = "$username:$password".let {
Buffer().writeUtf8(it).readByteString().base64()
}
val token = toBearerToken(username, password)

val body = MultipartBody.Builder()
.addFormDataPart(
Expand Down Expand Up @@ -79,13 +62,13 @@ internal fun nmcpPublishWithPublisherApi(
val timeout1 = validationTimeoutSeconds?.seconds ?: 10.minutes
if (timeout1.isPositive()) {
logger.lifecycle("Nmcp: waiting for validation...")
waitFor(setOf(VALIDATED, PUBLISHING, PUBLISHED), timeout1, logger, deploymentId, baseUrl, token)
waitForStatus(setOf(VALIDATED, PUBLISHING, PUBLISHED), timeout1, logger, deploymentId, baseUrl, token)

val timeout2 = publishingTimeoutSeconds?.seconds ?: 0.seconds
if (publishingType == "AUTOMATIC") {
if (timeout2.isPositive()) {
logger.lifecycle("Nmcp: deployment is validated, waiting for publication...")
waitFor(setOf(PUBLISHED), timeout2, logger, deploymentId, baseUrl, token)
waitForStatus(setOf(PUBLISHED), timeout2, logger, deploymentId, baseUrl, token)
logger.lifecycle("Nmcp: deployment is published.")
} else {
logger.lifecycle("Nmcp: deployment is publishing... Check the central portal UI to verify its status.")
Expand All @@ -94,104 +77,9 @@ internal fun nmcpPublishWithPublisherApi(
check(publishingTimeoutSeconds == null) {
"Nmcp: 'publishingTimeout' has no effect if 'publishingType' is USER_MANAGED. Either set 'publishingType = AUTOMATIC' or remove 'publishingTimeout'"
}
logger.lifecycle("Nmcp: deployment has passed validation, publish it manually from the Central Portal UI.")
logger.lifecycle("Nmcp: deployment has passed validation, publish it manually from the Central Portal UI or call './gradlew nmcpPublishCentralPortalDeployment -PnmcpDeploymentId=$deploymentId'.")
}
} else {
logger.lifecycle("Nmcp: deployment is validating... Check the central portal UI to verify its status.")
}
}

private fun waitFor(
target: Set<Status>,
timeout: Duration,
logger: GLogger,
deploymentId: String,
baseUrl: String,
token: String,
) {
val pollingInterval = 5.seconds
val mark = markNow()
while (true) {
check(mark.elapsedNow() < timeout) {
"Nmcp: timeout while checking deployment '$deploymentId'. You might need to check the deployment status on the Central Portal UI (see $baseUrl), or you could increase the timeout."
}

val status = verifyStatus(
logger = logger,
deploymentId = deploymentId,
baseUrl = baseUrl,
token = token,
)
if (status is FAILED) {
error("Nmcp: deployment has failed:\n${status.error}")
} else if (status in target) {
return
} else {
logger.lifecycle("Nmcp: deployment status is '$status', will try again in ${pollingInterval.inWholeSeconds}s (${(timeout - mark.elapsedNow()).inWholeSeconds.seconds} left)...")
// Wait for the next attempt to reduce the load on the Central Portal API
Thread.sleep(pollingInterval.inWholeMilliseconds)
continue
}
}
}

private sealed interface Status

// A deployment is uploaded and waiting for processing by the validation service
private data object PENDING : Status

// A deployment is being processed by the validation service
private data object VALIDATING : Status

// A deployment has passed validation and is waiting on a user to manually publish via the Central Portal UI
private data object VALIDATED : Status

// A deployment has been either automatically or manually published and is being uploaded to Maven Central
private data object PUBLISHING : Status

// A deployment has successfully been uploaded to Maven Central
private data object PUBLISHED : Status

// A deployment has encountered an error
private class FAILED(val error: String) : Status

private fun verifyStatus(
logger: GLogger,
deploymentId: String,
baseUrl: String,
token: String,
): Status {
val request = Request.Builder()
.post(ByteString.EMPTY.toRequestBody())
.addHeader("Authorization", "Bearer $token")
.url(baseUrl + "api/v1/publisher/status?id=$deploymentId")
.build()
val result = executeWithRetries(logger, nmcpClient, request)
if (result !is Success) {
error("Cannot verify deployment $deploymentId status ($result)")
}

val str = result.body.use { it.readUtf8() }
val element = Json.parseToJsonElement(str)
check(element is JsonObject) {
"Nmcp: unexpected status response for deployment $deploymentId: $str"
}

val state = element["deploymentState"]
check(state is JsonPrimitive && state.isString) {
"Nmcp: unexpected deploymentState for deployment $deploymentId: $state"
}

return when (state.content) {
"PENDING" -> PENDING
"VALIDATING" -> VALIDATING
"VALIDATED" -> VALIDATED
"PUBLISHING" -> PUBLISHING
"PUBLISHED" -> PUBLISHED
"FAILED" -> {
FAILED(element["errors"].toString())
}
else -> error("Nmcp: unexpected deploymentState for deployment $deploymentId: $state")
}

}
126 changes: 126 additions & 0 deletions nmcp-tasks/src/main/kotlin/nmcp/internal/task/portal.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package nmcp.internal.task

import gratatouille.tasks.GLogger
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.TimeSource.Monotonic.markNow
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import nmcp.transport.Success
import nmcp.transport.executeWithRetries
import nmcp.transport.nmcpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okio.Buffer
import okio.ByteString
import okio.use

internal fun waitForStatus(
target: Set<Status>,
timeout: Duration,
logger: GLogger,
deploymentId: String,
baseUrl: String,
token: String,
) {
val pollingInterval = 5.seconds
val mark = markNow()
while (true) {
check(mark.elapsedNow() < timeout) {
"Nmcp: timeout while checking deployment '$deploymentId'. You might need to check the deployment status on the Central Portal UI (see $baseUrl), or you could increase the timeout."
}

val status = verifyStatus(
logger = logger,
deploymentId = deploymentId,
baseUrl = baseUrl,
token = token,
)
if (status is FAILED) {
error("Nmcp: deployment has failed:\n${status.error}")
} else if (status in target) {
return
} else {
logger.lifecycle("Nmcp: deployment status is '$status', will try again in ${pollingInterval.inWholeSeconds}s (${(timeout - mark.elapsedNow()).inWholeSeconds.seconds} left)...")
// Wait for the next attempt to reduce the load on the Central Portal API
Thread.sleep(pollingInterval.inWholeMilliseconds)
continue
}
}
}

internal sealed interface Status

// A deployment is uploaded and waiting for processing by the validation service
internal data object PENDING : Status

// A deployment is being processed by the validation service
internal data object VALIDATING : Status

// A deployment has passed validation and is waiting on a user to manually publish via the Central Portal UI
internal data object VALIDATED : Status

// A deployment has been either automatically or manually published and is being uploaded to Maven Central
internal data object PUBLISHING : Status

// A deployment has successfully been uploaded to Maven Central
internal data object PUBLISHED : Status

// A deployment has encountered an error
internal class FAILED(val error: String) : Status

internal fun verifyStatus(
logger: GLogger,
deploymentId: String,
baseUrl: String,
token: String,
): Status {
val request = Request.Builder()
.post(ByteString.EMPTY.toRequestBody())
.addHeader("Authorization", "Bearer $token")
.url(baseUrl + "api/v1/publisher/status?id=$deploymentId")
.build()
val result = executeWithRetries(logger, nmcpClient, request)
if (result !is Success) {
error("Cannot verify deployment $deploymentId status ($result)")
}

val str = result.body.use { it.readUtf8() }
val element = Json.parseToJsonElement(str)
check(element is JsonObject) {
"Nmcp: unexpected status response for deployment $deploymentId: $str"
}

val state = element["deploymentState"]
check(state is JsonPrimitive && state.isString) {
"Nmcp: unexpected deploymentState for deployment $deploymentId: $state"
}

return when (state.content) {
"PENDING" -> PENDING
"VALIDATING" -> VALIDATING
"VALIDATED" -> VALIDATED
"PUBLISHING" -> PUBLISHING
"PUBLISHED" -> PUBLISHED
"FAILED" -> {
FAILED(element["errors"].toString())
}
else -> error("Nmcp: unexpected deploymentState for deployment $deploymentId: $state")
}

}

internal fun toBearerToken(username: String?, password: String?): String {
check(!username.isNullOrBlank()) {
"Nmcp: username is missing"
}
check(!password.isNullOrBlank()) {
"Nmcp: password is missing"
}

val token = "$username:$password".let {
Buffer().writeUtf8(it).readByteString().base64()
}
return token
}
3 changes: 1 addition & 2 deletions nmcp-tasks/src/main/kotlin/nmcp/transport/transport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ internal fun executeWithRetries(logger: GLogger, client: OkHttpClient, request:
return result
}

logger.lifecycle("Nmcp: put '${request.url}' failed (${result}), retrying... (attempt ${attempt + 1}/${attemptCount})")
logger.lifecycle("Nmcp: ${request.method} '${request.url}' failed (${result}), retrying... (attempt ${attempt + 1}/${attemptCount})")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Thread.sleep(2.0.pow(attempt.toDouble()).toLong() * 1_000)
attempt++
}
Expand Down Expand Up @@ -262,4 +262,3 @@ internal class FilesystemTransport(
}
}
}

Loading