Skip to content

Commit

Permalink
[JENKINS-30398] When run inside a container (with the docker workflow…
Browse files Browse the repository at this point in the history
… plugin) the /tmp dir, where the SSH key is copied, is not available. When ansible is invoked from a workflow, copy the provate key into the workspace.
  • Loading branch information
jcsirot committed Dec 4, 2015
1 parent 904c2f6 commit 3b025b2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
Expand Up @@ -51,6 +51,7 @@ abstract class AbstractAnsibleInvocation<T extends AbstractAnsibleInvocation<T>>

private FilePath key = null;
private Inventory inventory;
private boolean copyCredentialsInWorkspace = false;
private final FilePath ws;

protected AbstractAnsibleInvocation(String exe, Run<?, ?> build, FilePath ws, TaskListener listener)
Expand Down Expand Up @@ -129,6 +130,11 @@ public T setCredentials(StandardUsernameCredentials credentials) {
return (T) this;
}

public T setCredentials(StandardUsernameCredentials credentials, boolean copyCredentialsInWorkspace) {
this.copyCredentialsInWorkspace = copyCredentialsInWorkspace;
return setCredentials(credentials);
}

protected ArgumentListBuilder prependPasswordCredentials(ArgumentListBuilder args) {
if (credentials instanceof UsernamePasswordCredentials) {
UsernamePasswordCredentials passwordCredentials = (UsernamePasswordCredentials)credentials;
Expand All @@ -142,7 +148,7 @@ protected ArgumentListBuilder appendCredentials(ArgumentListBuilder args)
{
if (credentials instanceof SSHUserPrivateKey) {
SSHUserPrivateKey privateKeyCredentials = (SSHUserPrivateKey)credentials;
key = Utils.createSshKeyFile(key, ws, privateKeyCredentials);
key = Utils.createSshKeyFile(key, ws, privateKeyCredentials, copyCredentialsInWorkspace);
args.add("--private-key").add(key);
args.add("-u").add(privateKeyCredentials.getUsername());
} else if (credentials instanceof UsernamePasswordCredentials) {
Expand Down
Expand Up @@ -80,6 +80,8 @@ public class AnsiblePlaybookBuilder extends Builder implements SimpleBuildStep

public String additionalParameters = null;

public boolean copyCredentialsInWorkspace = false;

@Deprecated
public AnsiblePlaybookBuilder(String ansibleName, String playbook, Inventory inventory, String limit, String tags,
String skippedTags, String startAtTask, String credentialsId, boolean sudo,
Expand Down Expand Up @@ -136,7 +138,12 @@ public void setStartAtTask(String startAtTask) {

@DataBoundSetter
public void setCredentialsId(String credentialsId) {
setCredentialsId(credentialsId, false);
}

public void setCredentialsId(String credentialsId, boolean copyCredentialsInWorkspace) {
this.credentialsId = credentialsId;
this.copyCredentialsInWorkspace = copyCredentialsInWorkspace;
}

@DataBoundSetter
Expand Down Expand Up @@ -201,8 +208,8 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull Node node, @Nonnull FilePat
invocation.setSudo(sudo, sudoUser);
invocation.setForks(forks);
invocation.setCredentials(StringUtils.isNotBlank(credentialsId) ?
CredentialsProvider.findCredentialById(credentialsId, StandardUsernameCredentials.class, run) :
null);
CredentialsProvider.findCredentialById(credentialsId, StandardUsernameCredentials.class, run) : null,
copyCredentialsInWorkspace);
invocation.setAdditionalParameters(additionalParameters);
invocation.setHostKeyCheck(hostKeyChecking);
invocation.setUnbufferedOutput(unbufferedOutput);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jenkinsci/plugins/ansible/Utils.java
Expand Up @@ -33,13 +33,13 @@ class Utils
* @throws IOException
* @throws InterruptedException
*/
static FilePath createSshKeyFile(FilePath key, FilePath workspace, SSHUserPrivateKey credentials) throws IOException, InterruptedException {
static FilePath createSshKeyFile(FilePath key, FilePath workspace, SSHUserPrivateKey credentials, boolean inWorkspace) throws IOException, InterruptedException {
StringBuilder sb = new StringBuilder();
List<String> privateKeys = credentials.getPrivateKeys();
for (String s : privateKeys) {
sb.append(s);
}
key = workspace.createTextTempFile("ssh", ".key", sb.toString(), false);
key = workspace.createTextTempFile("ssh", ".key", sb.toString(), inWorkspace);
key.chmod(0400);
return key;
}
Expand Down
Expand Up @@ -179,7 +179,7 @@ protected Void run() throws Exception {
builder.setAnsibleName(step.getInstallation());
builder.setSudo(step.isSudo());
builder.setSudoUser(step.getSudoUser());
builder.setCredentialsId(step.getCredentialsId());
builder.setCredentialsId(step.getCredentialsId(), true);
builder.setForks(5);
builder.setLimit(step.getLimit());
builder.setTags(step.getTags());
Expand Down

0 comments on commit 3b025b2

Please sign in to comment.