de.gesellix.docker.client.image.ManageImageClient.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of docker-client Show documentation
Show all versions of docker-client Show documentation
A Docker client for the JVM written in Groovy
The newest version!
package de.gesellix.docker.client.image
import de.gesellix.docker.client.EngineResponseContent
import de.gesellix.docker.client.authentication.ManageAuthentication
import de.gesellix.docker.client.repository.RepositoryAndTag
import de.gesellix.docker.client.repository.RepositoryTagParser
import de.gesellix.docker.remote.api.BuildInfo
import de.gesellix.docker.remote.api.CreateImageInfo
import de.gesellix.docker.remote.api.EngineApiClient
import de.gesellix.docker.remote.api.HistoryResponseItem
import de.gesellix.docker.remote.api.ImageDeleteResponseItem
import de.gesellix.docker.remote.api.ImageInspect
import de.gesellix.docker.remote.api.ImagePruneResponse
import de.gesellix.docker.remote.api.ImageSearchResponseItem
import de.gesellix.docker.remote.api.ImageSummary
import de.gesellix.docker.remote.api.PushImageInfo
import de.gesellix.docker.remote.api.client.ImageApi
import de.gesellix.docker.remote.api.core.StreamCallback
import de.gesellix.util.QueryParameterEncoder
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.time.Duration
class ManageImageClient implements ManageImage {
private final Logger log = LoggerFactory.getLogger(ManageImageClient)
private EngineApiClient client
private RepositoryTagParser repositoryTagParser
private QueryParameterEncoder queryParameterEncoder
private ManageAuthentication manageAuthentication
ManageImageClient(EngineApiClient client, ManageAuthentication manageAuthentication) {
this.client = client
this.manageAuthentication = manageAuthentication
this.repositoryTagParser = new RepositoryTagParser()
this.queryParameterEncoder = new QueryParameterEncoder()
}
@Override
EngineResponseContent> search(String term, Integer limit = 25) {
log.info("docker search")
List imageSearch = client.imageApi.imageSearch(term, limit, null)
return new EngineResponseContent>(imageSearch)
}
@Override
void build(InputStream buildContext) {
build(null, null, buildContext)
}
@Override
void build(StreamCallback callback, Duration timeout, InputStream buildContext) {
build(callback, timeout,
null, null, null, null, null, null, null, null, null, null,
buildContext)
}
@Override
void build(String tag,
InputStream buildContext) {
build(null, null, buildContext)
}
@Override
void build(StreamCallback callback, Duration timeout,
String tag,
InputStream buildContext) {
build(callback, timeout,
null, tag, null, null, null, null, null, null, null, null,
buildContext)
}
@Override
void build(String dockerfile, String tag, Boolean quiet, Boolean nocache, String pull, Boolean rm,
String buildargs, String labels, String encodedRegistryConfig, String contentType, InputStream buildContext) {
build(null, null,
dockerfile, tag, quiet, nocache, pull, rm,
buildargs, labels, encodedRegistryConfig, contentType,
buildContext)
}
@Override
void build(StreamCallback callback, Duration timeout,
String dockerfile, String tag, Boolean quiet, Boolean nocache, String pull, Boolean rm,
String buildargs, String labels, String encodedRegistryConfig, String contentType, InputStream buildContext) {
log.info("docker build")
if (!encodedRegistryConfig) {
encodedRegistryConfig = manageAuthentication.encodeAuthConfigs(manageAuthentication.getAllAuthConfigs())
}
ImageApi.ContentTypeImageBuild contentTypeImageBuild = contentType
? ImageApi.ContentTypeImageBuild.valueOf(contentType)
: ImageApi.ContentTypeImageBuild.ApplicationSlashXMinusTar
client.imageApi.imageBuild(dockerfile,
tag, null, null, quiet, nocache, null, pull,
rm == null ? true : rm, null,
null, null, null, null, null, null,
buildargs,
null,
null,
labels,
null,
contentTypeImageBuild,
encodedRegistryConfig,
null, null,
null,
buildContext,
callback, timeout ? timeout.toMillis() : null)
}
@Override
EngineResponseContent> history(String imageId) {
log.info("docker history")
List imageHistory = client.imageApi.imageHistory(imageId)
return new EngineResponseContent>(imageHistory)
}
@Override
EngineResponseContent inspectImage(String imageId) {
log.info("docker inspect image")
ImageInspect imageInspect = client.imageApi.imageInspect(imageId)
return new EngineResponseContent(imageInspect)
}
@Override
void load(InputStream imagesTarball) {
log.info("docker load")
client.imageApi.imageLoad(null, imagesTarball)
}
/**
* @see #images(Boolean, String, Boolean)
* @deprecated use {@link #images(Boolean, String, Boolean)}
*/
@Deprecated
@Override
EngineResponseContent> images(Map query) {
Map actualQuery = new HashMap()
if (query != null) {
actualQuery.putAll(query)
}
queryParameterEncoder.jsonEncodeQueryParameter(actualQuery, "filters")
return images(
(Boolean) actualQuery.getOrDefault("all", false),
(String) actualQuery.get("filters"),
(Boolean) actualQuery.get("digests"))
}
@Override
EngineResponseContent> images(Boolean all = false, String filters = null, Boolean digests = null) {
log.info("docker images")
List imageList = client.imageApi.imageList(all, filters, digests)
return new EngineResponseContent>(imageList)
}
/**
* @see #pruneImages(String)
* @deprecated use {@link #pruneImages(String)}
*/
@Deprecated
@Override
EngineResponseContent pruneImages(Map query) {
Map actualQuery = new HashMap()
if (query != null) {
actualQuery.putAll(query)
}
queryParameterEncoder.jsonEncodeQueryParameter(actualQuery, "filters")
return pruneImages(actualQuery.filters as String)
}
@Override
EngineResponseContent pruneImages(String filters = null) {
log.info("docker image prune")
ImagePruneResponse imagePrune = client.imageApi.imagePrune(filters)
return new EngineResponseContent(imagePrune)
}
@Override
void pull(StreamCallback callback, Duration timeout,
String imageName, String tag = "", String authBase64Encoded = ".") {
log.info("docker pull '{}:{}'", imageName, tag)
client.imageApi.imageCreate(
imageName,
null,
null,
tag ?: "", // "latest" as default?
null,
authBase64Encoded,
null,
null,
null,
callback,
timeout ? timeout.toMillis() : null
)
// return findImageId(actualImageName, tag)
}
@Override
void importUrl(StreamCallback callback, Duration timeout,
String url, String repository = "", String tag = "") {
log.info("docker import '{}' into {}:{}", url, repository, tag)
client.imageApi.imageCreate(
null,
url,
repository ?: "",
tag ?: "", // "latest" as default?
null,
null,
null,
null,
null,
callback,
timeout ? timeout.toMillis() : null
)
}
@Override
void importStream(StreamCallback callback, Duration timeout,
InputStream stream, String repository = "", String tag = "") {
log.info("docker import stream into ${repository}:${tag}")
client.imageApi.imageCreate(
null,
"-",
repository ?: "",
tag ?: "", // "latest" as default?
null,
null,
null,
null,
stream,
callback,
timeout ? timeout.toMillis() : null
)
}
@Override
void push(String imageName, String authBase64Encoded = ".", String registry = "") {
push(null, null, imageName, authBase64Encoded, registry)
}
@Override
void push(StreamCallback callback, Duration timeout, String imageName, String authBase64Encoded = ".", String registry = "") {
log.info("docker push '${imageName}'")
String actualImageName = imageName
if (registry) {
actualImageName = "$registry/$imageName".toString()
tag(imageName, actualImageName)
}
RepositoryAndTag repoAndTag = repositoryTagParser.parseRepositoryTag(actualImageName)
client.imageApi.imagePush(repoAndTag.repo as String,
authBase64Encoded ?: ".",
repoAndTag.tag as String,
callback,
timeout ? timeout.toMillis() : null)
}
@Override
EngineResponseContent> rmi(String imageId) {
log.info("docker rmi")
List imageDelete = client.imageApi.imageDelete(imageId, null, null)
return new EngineResponseContent>(imageDelete)
}
@Override
EngineResponseContent save(List images) {
log.info("docker save")
InputStream savedImages = client.imageApi.imageGetAll(images)
return new EngineResponseContent(savedImages)
}
@Override
void tag(String imageId, String repository) {
log.info("docker tag")
RepositoryAndTag repoAndTag = repositoryTagParser.parseRepositoryTag(repository)
client.imageApi.imageTag(imageId, repoAndTag.repo, repoAndTag.tag)
}
@Override
String findImageId(String imageName, String tag = "") {
boolean isDigest = imageName.contains '@'
List images = images((isDigest) ? [digests: '1'] : [:]).content
// println new JsonBuilder(images).toString()
def imageIdsByRepoDigest = images.collectEntries { image ->
image.repoDigests?.collectEntries { String repoDigest ->
Map idByDigest = new HashMap()
idByDigest[repoDigest] = (String) image.id
idByDigest
} ?: [:]
}
def imageIdsByName = images.collectEntries { image ->
image.repoTags?.collectEntries { String repoTag ->
Map idByName = new HashMap()
idByName[repoTag] = (String) image.id
idByName
} ?: [:]
}
if (isDigest) {
if (imageIdsByRepoDigest[imageName.toString()]) {
return imageIdsByRepoDigest[imageName.toString()]
}
log.warn("couldn't find imageId for `${imageName}` via `docker images`")
return imageName
} else {
String canonicalImageName = "$imageName:${tag ?: 'latest'}"
if (imageIdsByName[canonicalImageName]) {
return imageIdsByName[canonicalImageName]
}
log.warn("couldn't find imageId for `${canonicalImageName}` via `docker images`")
return canonicalImageName
}
}
}