use of io.fabric8.maven.docker.service.RegistryService.RegistryConfig in project docker-maven-plugin by fabric8io.
the class RegistryService method pullImageWithPolicy.
/**
* Check an image, and, if <code>autoPull</code> is set to true, fetch it. Otherwise if the image
* is not existent, throw an error
*
* @param image image which is required to be pulled
* @param pullManager image pull manager
* @param registryConfig registry configuration
* @param buildImageConfiguration image build configuration
* @throws DockerAccessException in case of error in contacting docker daemon
* @throws MojoExecutionException in case of any other misc failure
*/
public void pullImageWithPolicy(String image, ImagePullManager pullManager, RegistryConfig registryConfig, BuildImageConfiguration buildImageConfiguration) throws DockerAccessException, MojoExecutionException {
// Already pulled, so we don't need to take care
if (pullManager.hasAlreadyPulled(image)) {
return;
}
// Check if a pull is required
if (!imageRequiresPull(queryService.hasImage(image), pullManager.getImagePullPolicy(), image)) {
return;
}
final ImageName imageName = new ImageName(image);
final long pullStartTime = System.currentTimeMillis();
final String actualRegistry = EnvUtil.firstRegistryOf(imageName.getRegistry(), registryConfig.getRegistry());
final CreateImageOptions createImageOptions = new CreateImageOptions(buildImageConfiguration != null ? buildImageConfiguration.getCreateImageOptions() : Collections.emptyMap()).fromImage(imageName.getNameWithoutTag(actualRegistry)).tag(imageName.getDigest() != null ? imageName.getDigest() : imageName.getTag());
docker.pullImage(imageName.getFullName(), createAuthConfig(false, null, actualRegistry, registryConfig), actualRegistry, createImageOptions);
log.info("Pulled %s in %s", imageName.getFullName(), EnvUtil.formatDurationTill(pullStartTime));
pullManager.pulled(image);
if (actualRegistry != null && !imageName.hasRegistry()) {
// If coming from a registry which was not contained in the original name, add a tag from the
// full name with the registry to the short name with no-registry.
docker.tag(imageName.getFullName(actualRegistry), image, false);
}
}
use of io.fabric8.maven.docker.service.RegistryService.RegistryConfig in project docker-maven-plugin by fabric8io.
the class JibBuildServiceTest method testGetRegistryCredentialsForPush.
@Test
@java.lang.SuppressWarnings("squid:S00112")
public void testGetRegistryCredentialsForPush() throws MojoExecutionException {
// Given
ImageConfiguration imageConfiguration = getImageConfiguration();
RegistryService.RegistryConfig registryConfig = new RegistryService.RegistryConfig.Builder().authConfig(Collections.emptyMap()).authConfigFactory(authConfigFactory).settings(settings).build();
mockAuthConfigFactory(true, registryConfig);
// When
Credential credential = JibBuildService.getRegistryCredentials(registryConfig, true, imageConfiguration, logger);
// Then
assertNotNull(credential);
assertEquals("testuserpush", credential.getUsername());
assertEquals("testpass", credential.getPassword());
}
use of io.fabric8.maven.docker.service.RegistryService.RegistryConfig in project docker-maven-plugin by fabric8io.
the class CopyMojoTest method thenImageIsPulled.
private void thenImageIsPulled(ImageConfiguration image, String pullRegistry, boolean imageExists) throws DockerAccessException, MojoExecutionException {
new Verifications() {
{
final String imageName;
final RegistryConfig registryConfig;
registryService.pullImageWithPolicy(imageName = withCapture(), (ImagePullManager) any, registryConfig = withCapture(), image.getBuildConfiguration());
times = 1;
assertEquals(image.getName(), imageName);
assertNotNull(registryConfig);
assertEquals(pullRegistry, registryConfig.getRegistry());
}
};
}
use of io.fabric8.maven.docker.service.RegistryService.RegistryConfig in project docker-maven-plugin by fabric8io.
the class JibBuildService method getRegistryCredentials.
static Credential getRegistryCredentials(RegistryService.RegistryConfig registryConfig, boolean isPush, ImageConfiguration imageConfiguration, Logger log) throws MojoExecutionException {
String registry;
if (isPush) {
registry = EnvUtil.firstRegistryOf(new ImageName(imageConfiguration.getName()).getRegistry(), imageConfiguration.getRegistry(), registryConfig.getRegistry());
} else {
registry = EnvUtil.firstRegistryOf(new ImageName(getBaseImage(imageConfiguration)).getRegistry(), registryConfig.getRegistry());
}
if (registry == null || DEFAULT_DOCKER_REGISTRIES.contains(registry)) {
// Let's assume docker is default registry.
registry = DOCKER_LOGIN_DEFAULT_REGISTRY;
}
AuthConfigFactory authConfigFactory = registryConfig.getAuthConfigFactory();
AuthConfig standardAuthConfig = authConfigFactory.createAuthConfig(isPush, registryConfig.isSkipExtendedAuth(), registryConfig.getAuthConfig(), registryConfig.getSettings(), null, registry);
Credential credentials = null;
if (standardAuthConfig != null) {
credentials = Credential.from(standardAuthConfig.getUsername(), standardAuthConfig.getPassword());
}
return credentials;
}
use of io.fabric8.maven.docker.service.RegistryService.RegistryConfig in project docker-maven-plugin by fabric8io.
the class RegistryService method pushImages.
/**
* Push a set of images to a registry
*
* @param imageConfigs images to push (but only if they have a build configuration)
* @param retries how often to retry
* @param registryConfig a global registry configuration
* @param skipTag flag to skip pushing tagged images
* @throws DockerAccessException
* @throws MojoExecutionException
*/
public void pushImages(Collection<ImageConfiguration> imageConfigs, int retries, RegistryConfig registryConfig, boolean skipTag) throws DockerAccessException, MojoExecutionException {
for (ImageConfiguration imageConfig : imageConfigs) {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
String name = imageConfig.getName();
if (buildConfig != null) {
if (buildConfig.skipPush()) {
log.info("%s : Skipped pushing", imageConfig.getDescription());
continue;
}
String configuredRegistry = EnvUtil.firstRegistryOf(new ImageName(imageConfig.getName()).getRegistry(), imageConfig.getRegistry(), registryConfig.getRegistry());
AuthConfig authConfig = createAuthConfig(true, new ImageName(name).getUser(), configuredRegistry, registryConfig);
long start = System.currentTimeMillis();
docker.pushImage(name, authConfig, configuredRegistry, retries);
log.info("Pushed %s in %s", name, EnvUtil.formatDurationTill(start));
if (!skipTag) {
for (String tag : imageConfig.getBuildConfiguration().getTags()) {
if (tag != null) {
docker.pushImage(new ImageName(name, tag).getFullName(), authConfig, configuredRegistry, retries);
}
}
}
}
}
}
Aggregations