use of com.github.dockerjava.api.command.PullImageResultCallback in project wildfly-camel by wildfly-extras.
the class DockerManager method pullImage.
public DockerManager pullImage(String imgName) throws TimeoutException {
PullImageResultCallback callback = new PullImageResultCallback() {
@Override
public void onNext(PullResponseItem item) {
ProgressDetail detail = item.getProgressDetail();
if (detail != null) {
Long current = detail.getCurrent();
Long total = detail.getTotal();
if (current != null && total != null) {
LOG.info("{}: {}%", imgName, 100 * current / total);
}
}
super.onNext(item);
}
};
try {
PullImageCmd pullCmd = client.pullImageCmd(imgName);
AuthConfig authConfig = pullCmd.getAuthConfig();
if (authConfig != null) {
// Optional first token is the registry
String[] toks = imgName.split("/");
String imgRegistry = toks.length > 2 ? toks[0] : null;
// Using auth config with no credetials on registry mismatch
String authRegistry = authConfig.getRegistryAddress();
if (imgRegistry != null && !authRegistry.contains(imgRegistry)) {
authConfig = new AuthConfig().withRegistryAddress(authRegistry);
pullCmd.withAuthConfig(authConfig);
}
String username = authConfig.getUsername();
String password = authConfig.getPassword();
password = password != null ? "*******" : null;
LOG.info("Pull {}/{} {} {}", username, password, authRegistry, imgName);
} else {
LOG.info("Pull unauthorized {}", imgName);
}
if (!pullCmd.exec(callback).awaitCompletion(10, TimeUnit.MINUTES)) {
throw new TimeoutException("Timeout pulling: " + imgName);
}
} catch (InterruptedException ex) {
// ignore
}
return this;
}
use of com.github.dockerjava.api.command.PullImageResultCallback in project hortonmachine by TheHortonMachine.
the class DockerHandler method pullImage.
public void pullImage(String imageName, String tag, IHMProgressMonitor pm) throws Exception {
PullImageResultCallback resultCallback = new PullImageResultCallback();
if (pm != null) {
resultCallback = new PullImageResultCallback() {
@Override
public void onNext(PullResponseItem item) {
String id = item.getId();
if (item != null) {
ProgressDetail progressDetail = item.getProgressDetail();
if (progressDetail != null) {
Long currentObj = progressDetail.getCurrent();
Long totalObj = progressDetail.getTotal();
if (currentObj != null && totalObj != null) {
int current = (int) (currentObj / 1000);
int total = (int) (totalObj / 1000);
String totalUnit = "KB";
String currentUnit = "KB";
if (total > 1024) {
total = total / 1000;
totalUnit = "MB";
}
if (current > 1024) {
current = current / 1000;
currentUnit = "MB";
}
String msg = null;
if (current == total) {
msg = "Finished downloading " + id;
} else {
msg = "Downloading " + id + " ( " + current + currentUnit + "/" + total + totalUnit + " )";
}
pm.message(msg);
}
}
}
super.onNext(item);
}
@Override
public void onError(Throwable throwable) {
pm.errorMessage("Failed to start pull command:" + throwable.getMessage());
super.onError(throwable);
}
};
}
//
dockerClient.pullImageCmd(imageName).withTag(//
tag).exec(resultCallback);
resultCallback.awaitCompletion();
}
use of com.github.dockerjava.api.command.PullImageResultCallback in project polaris by ractf.
the class DockerRunner method updatePod.
@Override
public void updatePod(final Task task, final Container pod) {
final var credentials = state.getCredential(pod.getRepoCredentials());
final var authConfig = authConfigFactory.createAuthConfig(credentials);
final Map<String, String> filter = new HashMap<>();
filter.put("polaris-pod", pod.getId());
// TODO: surely there's a better way to do this
try {
log.info("Trying to pull image {}", pod.getImage());
dockerClient.pullImageCmd(pod.getImage()).withTag(pod.getTag()).withAuthConfig(authConfig).exec(new PullImageResultCallback() {
@Override
public void onNext(final PullResponseItem item) {
System.out.println(item.toString());
if (item.getStatus() != null && item.getStatus().contains("Downloaded newer image")) {
for (final var container : dockerClient.listContainersCmd().withLabelFilter(filter).exec()) {
dockerClient.stopContainerCmd(container.getId()).withTimeout(pod.getTerminationTimeout()).exec();
createPod(task, pod, state.getInstance(container.getLabels().get("polaris-instance")));
startPod(task, pod, state.getInstance(container.getLabels().get("polaris-instance")));
}
Namespace namespace = null;
if (pod.getRepoCredentials() != null) {
namespace = state.getNamespace(pod.getRepoCredentials().getNamespace());
}
notifications.info(NotificationTarget.NAMESPACE_ADMIN, namespace, "Updated pod " + pod.getId(), "");
}
}
}).awaitCompletion();
} catch (final Exception e) {
Namespace namespace = null;
if (pod.getRepoCredentials() != null) {
namespace = state.getNamespace(pod.getRepoCredentials().getNamespace());
}
notifications.error(NotificationTarget.NAMESPACE_ADMIN, namespace, "Failed to update pod " + pod.getId(), e.getMessage());
e.printStackTrace();
}
}
use of com.github.dockerjava.api.command.PullImageResultCallback in project developer-be by EdgeGallery.
the class ContainerImageServiceImpl method downloadHarborImage.
/**
* download harbor image.
*
* @param imageId imageId
* @return
*/
@Override
public ResponseEntity<InputStreamResource> downloadHarborImage(String imageId) {
if (StringUtils.isEmpty(imageId)) {
LOGGER.error("imageId is null");
throw new IllegalRequestException("imageId is null", ResponseConsts.RET_REQUEST_PARAM_EMPTY);
}
ContainerImage containerImage = containerImageMapper.getContainerImage(imageId);
if (containerImage == null) {
LOGGER.error("imageId is incorrect");
throw new IllegalRequestException("imageId is incorrect", ResponseConsts.RET_REQUEST_PARAM_ERROR);
}
String image = containerImage.getImagePath();
String fileName = containerImage.getFileName();
if (StringUtils.isEmpty(image) || StringUtils.isEmpty(fileName)) {
String msg = "image or fileName of this record in db is empty";
LOGGER.error(msg);
throw new IllegalRequestException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY);
}
try {
DockerClient dockerClient = ContainerImageUtil.getDockerClient();
// pull image
dockerClient.pullImageCmd(image).exec(new PullImageResultCallback()).awaitCompletion().close();
String[] images = image.trim().split(":");
// save image
SaveImageCmd saveImage = dockerClient.saveImageCmd(images[0]).withTag(images[1]);
InputStream input = saveImage.exec();
if (input == null) {
String msg = "save image failed!";
LOGGER.error(msg);
throw new FileOperateException(msg, ResponseConsts.RET_SAVE_FILE_FAIL);
}
return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE).header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName).body(new InputStreamResource(input));
} catch (InterruptedException | IOException e) {
Thread.currentThread().interrupt();
String msg = "download Harbor image occur exception!";
LOGGER.error("download Harbor image failed! {}", e.getMessage());
throw new FileOperateException(msg, ResponseConsts.RET_DOWNLOAD_FILE_FAIL);
}
}
use of com.github.dockerjava.api.command.PullImageResultCallback in project airavata-data-lake by apache.
the class GenericDataParsingTask method runContainer.
private void runContainer(DataParser parser, String inputPath, String outputPath, Map<String, String> environmentValues) throws Exception {
DefaultDockerClientConfig.Builder config = DefaultDockerClientConfig.createDefaultConfigBuilder();
DockerClient dockerClient = DockerClientBuilder.getInstance(config.build()).build();
logger.info("Pulling image " + parser.getDockerImage());
try {
dockerClient.pullImageCmd(parser.getDockerImage().split(":")[0]).withTag(parser.getDockerImage().split(":")[1]).exec(new PullImageResultCallback()).awaitCompletion();
} catch (InterruptedException e) {
logger.error("Interrupted while pulling image", e);
throw e;
}
logger.info("Successfully pulled image " + parser.getDockerImage());
String containerId = UUID.randomUUID().toString();
String[] commands = parser.getExecCommand().split(" ");
CreateContainerResponse containerResponse = dockerClient.createContainerCmd(parser.getDockerImage()).withCmd(commands).withName(containerId).withBinds(Bind.parse(inputPath + ":" + parser.getInputPath()), Bind.parse(outputPath + ":" + parser.getOutputPath())).withTty(true).withAttachStdin(true).withAttachStdout(true).withEnv(environmentValues.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.toList())).exec();
logger.info("Created the container with id " + containerResponse.getId());
final StringBuilder dockerLogs = new StringBuilder();
if (containerResponse.getWarnings() != null && containerResponse.getWarnings().length > 0) {
StringBuilder warningStr = new StringBuilder();
for (String w : containerResponse.getWarnings()) {
warningStr.append(w).append(",");
}
logger.warn("Container " + containerResponse.getId() + " warnings : " + warningStr);
} else {
logger.info("Starting container with id " + containerResponse.getId());
dockerClient.startContainerCmd(containerResponse.getId()).exec();
LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerResponse.getId()).withStdOut(true).withStdErr(true);
try {
logContainerCmd.exec(new ResultCallback.Adapter<Frame>() {
@Override
public void onNext(Frame item) {
logger.info("Got frame: {}", item);
;
if (item.getStreamType() == StreamType.STDOUT) {
dockerLogs.append(new String(item.getPayload(), StandardCharsets.UTF_8));
dockerLogs.append("\n");
} else if (item.getStreamType() == StreamType.STDERR) {
dockerLogs.append(new String(item.getPayload(), StandardCharsets.UTF_8));
dockerLogs.append("\n");
}
super.onNext(item);
}
@Override
public void onError(Throwable throwable) {
logger.error("Errored while running the container {}", containerId, throwable);
super.onError(throwable);
}
@Override
public void onComplete() {
logger.info("Container {} successfully completed", containerId);
super.onComplete();
}
}).awaitCompletion();
} catch (InterruptedException e) {
logger.error("Interrupted while reading container log" + e.getMessage());
throw e;
}
logger.info("Waiting for the container to stop");
Integer statusCode = dockerClient.waitContainerCmd(containerResponse.getId()).exec(new WaitContainerResultCallback()).awaitStatusCode();
logger.info("Container " + containerResponse.getId() + " exited with status code " + statusCode);
if (statusCode != 0) {
logger.error("Failing as non zero status code was returned");
throw new Exception("Failing as non zero status code was returned");
}
logger.info("Container logs " + dockerLogs.toString());
}
dockerClient.removeContainerCmd(containerResponse.getId()).exec();
logger.info("Successfully removed container with id " + containerResponse.getId());
}
Aggregations