Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-38018] Extend DockerRegistryEndpoint API to take a Launcher …
…and TaskListener.
  • Loading branch information
jglick committed Oct 3, 2016
1 parent 78f6f39 commit 702579f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.14</version>
<relativePath />
<version>2.15</version>
<relativePath/>
</parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>docker-commons</artifactId>
<version>1.4.2-SNAPSHOT</version>
<version>1.5-SNAPSHOT</version>
<packaging>hpi</packaging>

<name>Docker Commons Plugin</name>
Expand Down
Expand Up @@ -59,6 +59,9 @@
import java.util.regex.Pattern;

import static com.cloudbees.plugins.credentials.CredentialsMatchers.*;
import hudson.AbortException;
import hudson.Launcher;
import hudson.model.TaskListener;

/**
* Encapsulates the endpoint of DockerHub and how to interact with it.
Expand Down Expand Up @@ -182,9 +185,9 @@ DockerRegistryToken getToken(Item context) {
}

/**
* Makes the credentials available locally for the on-going build
* and returns {@link KeyMaterialFactory} that gives you the parameters needed to access it.
* @deprecated Call {@link #newKeyMaterialFactory(Item, VirtualChannel, Launcher, TaskListener)}
*/
@Deprecated
public KeyMaterialFactory newKeyMaterialFactory(@Nonnull AbstractBuild build) throws IOException, InterruptedException {
final FilePath workspace = build.getWorkspace();
if (workspace == null) {
Expand All @@ -193,15 +196,27 @@ public KeyMaterialFactory newKeyMaterialFactory(@Nonnull AbstractBuild build) th
return newKeyMaterialFactory(build.getParent(), workspace.getChannel());
}

/**
* @deprecated Call {@link #newKeyMaterialFactory(Item, VirtualChannel, Launcher, TaskListener)}
*/
@Deprecated
public KeyMaterialFactory newKeyMaterialFactory(Item context, @Nonnull VirtualChannel target) throws IOException, InterruptedException {
return newKeyMaterialFactory(context, target, null, TaskListener.NULL);
}

/**
* Makes the credentials available locally and returns {@link KeyMaterialFactory} that gives you the parameters
* needed to access it.
*/
public KeyMaterialFactory newKeyMaterialFactory(Item context, @Nonnull VirtualChannel target) throws IOException, InterruptedException {
public KeyMaterialFactory newKeyMaterialFactory(@CheckForNull Item context, @Nonnull VirtualChannel target, @CheckForNull Launcher launcher, @Nonnull TaskListener listener) throws IOException, InterruptedException {
if (credentialsId == null) {
return KeyMaterialFactory.NULL; // nothing needed to be done
}
DockerRegistryToken token = getToken(context);
if (token==null) return KeyMaterialFactory.NULL; // nothing needed to be done

return token.newKeyMaterialFactory(getEffectiveUrl(), target);
if (token == null) {
throw new AbortException("Could not find credentials matching " + credentialsId);
}
return token.newKeyMaterialFactory(getEffectiveUrl(), target, launcher, listener);
}

/**
Expand Down
Expand Up @@ -24,6 +24,8 @@
package org.jenkinsci.plugins.docker.commons.credentials;

import com.cloudbees.plugins.credentials.Credentials;
import hudson.Launcher;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import jenkins.security.MasterToSlaveCallable;
Expand All @@ -35,6 +37,7 @@
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import javax.annotation.CheckForNull;

/**
* Represents an authentication token that docker(1) understands when pushing/pulling
Expand All @@ -61,13 +64,21 @@ public String getToken() {
return token;
}

/**
* @deprecated Call {@link #newKeyMaterialFactory(URL, VirtualChannel, Launcher, TaskListener)}
*/
@Deprecated
public KeyMaterialFactory newKeyMaterialFactory(final URL endpoint, @Nonnull VirtualChannel target) throws InterruptedException, IOException {
return newKeyMaterialFactory(endpoint, target, null, TaskListener.NULL);
}

/**
* Makes the credentials available locally and returns {@link KeyMaterialFactory} that gives you the parameters
* needed to access it.
*
* This is done by inserting the token into {@code ~/.dockercfg}
*/
public KeyMaterialFactory newKeyMaterialFactory(final URL endpoint, @Nonnull VirtualChannel target) throws InterruptedException, IOException {
public KeyMaterialFactory newKeyMaterialFactory(final @Nonnull URL endpoint, @Nonnull VirtualChannel target, @CheckForNull Launcher launcher, final @Nonnull TaskListener listener) throws InterruptedException, IOException {
target.call(new MasterToSlaveCallable<Void, IOException>() {
/**
* Insert the token into {@code ~/.dockercfg}
Expand Down Expand Up @@ -100,6 +111,7 @@ public Void call() throws IOException {
.accumulate("email", getEmail()));

FileUtils.writeStringToFile(config, json.toString(2), "UTF-8");
listener.getLogger().println("Wrote authentication to " + config);
}
return null;
}
Expand Down

0 comments on commit 702579f

Please sign in to comment.