Fix docker image push 26/3026/2
authorSerban Jora <sj2381@att.com>
Wed, 3 Oct 2018 21:45:07 +0000 (17:45 -0400)
committerChris Lott <clott@research.att.com>
Wed, 3 Oct 2018 22:08:48 +0000 (22:08 +0000)
Change-Id: If1a8dc860f6fc1af0d344927c031ecd5339d6891
Issue-ID: ACUMOS-1780, ACUMOS-1781
Signed-off-by: Serban Jora <sj2381@att.com>
gateway/src/main/java/org/acumos/federation/gateway/common/FederationClient.java
gateway/src/main/java/org/acumos/federation/gateway/config/DockerConfiguration.java
gateway/src/main/java/org/acumos/federation/gateway/service/impl/ContentServiceImpl.java

index 852320c..34ce1a5 100644 (file)
@@ -441,7 +441,7 @@ public class FederationClient extends AbstractClient {
 
                @Override
                public InputStream getInputStream() throws IOException, IllegalStateException {
-                       log.info(EELFLoggerDelegate.debugLogger, "Download input stream access at {}",ExceptionUtils.getStackTrace(new RuntimeException("Input stream access")) );
+                       log.trace(EELFLoggerDelegate.debugLogger, "Download input stream access at {}",ExceptionUtils.getStackTrace(new RuntimeException("Input stream access")) );
                        return super.getInputStream();
                }
 
index 752bb70..f1331e1 100644 (file)
@@ -26,6 +26,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
 import com.github.dockerjava.api.DockerClient;
+import com.github.dockerjava.api.model.AuthConfig;
 import com.github.dockerjava.core.DefaultDockerClientConfig;
 import com.github.dockerjava.core.DockerClientBuilder;
 import com.github.dockerjava.core.DockerClientConfig;
@@ -41,6 +42,7 @@ public class DockerConfiguration {
        private DefaultDockerClientConfig.Builder builder;
        // need to repeat as the builder does not expose it and it avoids building a config object every time ..
        private String registryUrl;
+       private String registryUsername;
  
        public DockerConfiguration() {
                reset();
@@ -60,9 +62,14 @@ public class DockerConfiguration {
   }
 
        public void setRegistryUsername(String theUsername) {
+               this.registryUsername = theUsername;
                this.builder.withRegistryUsername(theUsername);
        }
 
+       public String getRegistryUsername() {
+               return this.registryUsername;
+       }
+
        public void setRegistryPassword(String thePassword) {
                this.builder.withRegistryPassword(thePassword);
        }
@@ -107,4 +114,14 @@ public class DockerConfiguration {
                        .withDockerCmdExecFactory(DockerClientBuilder.getDefaultDockerCmdExecFactory())
                        .build();
        }
+
+       /** */
+       public AuthConfig getAuthConfig() {
+               DockerClientConfig config = buildConfig();
+               return new AuthConfig()
+                                               .withUsername(config.getRegistryUsername())
+                                               .withPassword(config.getRegistryPassword())
+                                               .withEmail(config.getRegistryEmail())
+                                               .withRegistryAddress("http://" + config.getRegistryUrl() + "/v2/");
+       }
 }
index 4f7fc4d..d74d0e1 100644 (file)
@@ -147,19 +147,23 @@ public class ContentServiceImpl extends AbstractServiceImpl
                                        throw new ServiceException("Could not find loaded docker image for " + theArtifact);
                                }
        
-                               //new image name for re-tagging. is this really necessary ??
-                               String imageName = theArtifact.getName() + "_" + theSolutionId; 
-                               docker.tagImageCmd(image.getId(), imageName, theArtifact.getVersion()).exec();
-                               log.debug(EELFLoggerDelegate.debugLogger, "Re-tagged docker image: {} to {}:{}", image, imageName, theArtifact.getVersion());
+                               //new image name for re-tagging
+                               String imageName = dockerConfig.getRegistryUrl() + "/" + theArtifact.getName() + "_" + theSolutionId;
+                               String imageTag = imageName;
+                               docker.tagImageCmd(image.getId(), imageTag, theArtifact.getVersion()).exec();
+                               log.debug(EELFLoggerDelegate.debugLogger, "Re-tagged (1) docker image: {} to {}:{}", image, imageTag, theArtifact.getVersion());
+                               //remove old tag that came with the load
+                               docker.removeImageCmd(theArtifact.getDescription())
+                                                       .withForce(Boolean.TRUE)
+                                                       .exec();
                        
-                               log.debug(EELFLoggerDelegate.debugLogger, "Attempt docker push for image {}", image);
-                               docker.pushImageCmd(image.getId())
-                                                       .withName(imageName)
+                               log.debug(EELFLoggerDelegate.debugLogger, "Attempt docker push for image {}, tag {}", image, imageTag);
+                               docker.pushImageCmd(imageName)
+                                                       .withAuthConfig(dockerConfig.getAuthConfig())
                                                        .withTag(theArtifact.getVersion())
-                                                       .exec(pushResult = new TrackingPushImageResultCallback());
+                                                       .exec(pushResult = new TrackingPushImageResultCallback())
+                                                       .awaitCompletion();
 
-                               //pushResult.awaitSuccess(); //this will return with no warning even whan failed
-                               pushResult.awaitCompletion();
                                PushResponseItem pushResponse = pushResult.getResponseItem();
                                if (pushResponse.isErrorIndicated()) {
                                        log.debug(EELFLoggerDelegate.debugLogger, "Failed to push artifact {} image {} to docker registry: {}, {}", theArtifact, image, pushResponse.getError(), pushResponse.getErrorDetail());
@@ -169,7 +173,7 @@ public class ContentServiceImpl extends AbstractServiceImpl
                                        log.debug(EELFLoggerDelegate.debugLogger, "Completed docker push for artifact {} image {}", theArtifact, image);
                                }
                                
-                               String imageUri = dockerConfig.getRegistryUrl() + "/" + imageName + ":" + theArtifact.getVersion();
+                               String imageUri = imageName + ":" + theArtifact.getVersion();
                                // update artifact with local repo reference. we also update the name and description in order to stay
                                // alligned with on-boarding's unwritten rules
                                theArtifact.setSize((int)imageSource.size()); //this is the decompressed size ..