Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-31790] Job DSL support in ansible-plugin as extension point
- Loading branch information
Showing
9 changed files
with
476 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/org/jenkinsci/plugins/ansible/jobdsl/AnsibleJobDslExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.jenkinsci.plugins.ansible.jobdsl; | ||
|
||
import hudson.Extension; | ||
import javaposse.jobdsl.dsl.helpers.step.StepContext; | ||
import javaposse.jobdsl.plugin.ContextExtensionPoint; | ||
import javaposse.jobdsl.plugin.DslExtensionMethod; | ||
import org.jenkinsci.plugins.ansible.AnsibleAdHocCommandBuilder; | ||
import org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder; | ||
import org.jenkinsci.plugins.ansible.jobdsl.context.AnsibleContext; | ||
|
||
/** | ||
* @author lanwen (Merkushev Kirill) | ||
*/ | ||
@Extension(optional = true) | ||
public class AnsibleJobDslExtension extends ContextExtensionPoint { | ||
|
||
@DslExtensionMethod(context = StepContext.class) | ||
public Object ansibleAdHoc(String module, String command, Runnable closure) { | ||
AnsibleContext context = new AnsibleContext(); | ||
executeInContext(closure, context); | ||
|
||
AnsibleAdHocCommandBuilder adhoc = new AnsibleAdHocCommandBuilder( | ||
context.getHostPattern(), context.getInventory(), module, command | ||
); | ||
|
||
adhoc.setAdditionalParameters(context.getAdditionalParameters()); | ||
adhoc.setAnsibleName(context.getAnsibleName()); | ||
adhoc.setCredentialsId(context.getCredentialsId()); | ||
adhoc.setColorizedOutput(context.isColorizedOutput()); | ||
adhoc.setForks(context.getForks()); | ||
adhoc.setHostKeyChecking(context.isHostKeyChecking()); | ||
adhoc.setSudo(context.isSudo()); | ||
adhoc.setSudoUser(context.getSudoUser()); | ||
adhoc.setUnbufferedOutput(context.isUnbufferedOutput()); | ||
|
||
return adhoc; | ||
} | ||
|
||
@DslExtensionMethod(context = StepContext.class) | ||
public Object ansiblePlaybook(String playbook, Runnable closure) { | ||
AnsibleContext context = new AnsibleContext(); | ||
executeInContext(closure, context); | ||
|
||
AnsiblePlaybookBuilder plbook = new AnsiblePlaybookBuilder(playbook, context.getInventory()); | ||
|
||
plbook.setAdditionalParameters(context.getAdditionalParameters()); | ||
plbook.setAnsibleName(context.getAnsibleName()); | ||
plbook.setCredentialsId(context.getCredentialsId()); | ||
plbook.setColorizedOutput(context.isColorizedOutput()); | ||
plbook.setForks(context.getForks()); | ||
plbook.setHostKeyChecking(context.isHostKeyChecking()); | ||
plbook.setSudo(context.isSudo()); | ||
plbook.setSudoUser(context.getSudoUser()); | ||
plbook.setUnbufferedOutput(context.isUnbufferedOutput()); | ||
|
||
plbook.setLimit(context.getLimit()); | ||
plbook.setTags(context.getTags()); | ||
plbook.setSkippedTags(context.getSkippedTags()); | ||
plbook.setStartAtTask(context.getStartAtTask()); | ||
|
||
return plbook; | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
src/main/java/org/jenkinsci/plugins/ansible/jobdsl/context/AnsibleContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package org.jenkinsci.plugins.ansible.jobdsl.context; | ||
|
||
import javaposse.jobdsl.dsl.Context; | ||
import org.jenkinsci.plugins.ansible.Inventory; | ||
import org.jenkinsci.plugins.ansible.InventoryContent; | ||
import org.jenkinsci.plugins.ansible.InventoryPath; | ||
|
||
/** | ||
* @author lanwen (Merkushev Kirill) | ||
*/ | ||
public class AnsibleContext implements Context { | ||
private Inventory inventory; | ||
private String ansibleName; | ||
private String credentialsId; | ||
private boolean sudo = false; | ||
private String sudoUser = "root"; | ||
private int forks = 5; | ||
private boolean unbufferedOutput = true; | ||
private boolean colorizedOutput = false; | ||
private boolean hostKeyChecking = false; | ||
private String additionalParameters; | ||
|
||
/* adhoc-only */ | ||
|
||
private String hostPattern; | ||
|
||
/* playbook-only */ | ||
|
||
public String limit; | ||
public String tags; | ||
public String skippedTags; | ||
public String startAtTask; | ||
|
||
public void inventoryContent(String content, boolean dynamic) { | ||
this.inventory = new InventoryContent(content, dynamic); | ||
} | ||
|
||
public void inventoryContent(String content) { | ||
this.inventory = new InventoryContent(content, false); | ||
} | ||
|
||
public void inventoryPath(String path) { | ||
this.inventory = new InventoryPath(path); | ||
} | ||
|
||
public void ansibleName(String ansibleName) { | ||
this.ansibleName = ansibleName; | ||
} | ||
|
||
public void credentialsId(String credentialsId) { | ||
this.credentialsId = credentialsId; | ||
} | ||
|
||
public void sudo(boolean sudo) { | ||
this.sudo = sudo; | ||
} | ||
|
||
public void sudoUser(String sudoUser) { | ||
this.sudoUser = sudoUser; | ||
} | ||
|
||
public void forks(int forks) { | ||
this.forks = forks; | ||
} | ||
|
||
public void unbufferedOutput(boolean unbufferedOutput) { | ||
this.unbufferedOutput = unbufferedOutput; | ||
} | ||
|
||
public void colorizedOutput(boolean colorizedOutput) { | ||
this.colorizedOutput = colorizedOutput; | ||
} | ||
|
||
public void hostKeyChecking(boolean hostKeyChecking) { | ||
this.hostKeyChecking = hostKeyChecking; | ||
} | ||
|
||
public void additionalParameters(String additionalParameters) { | ||
this.additionalParameters = additionalParameters; | ||
} | ||
|
||
public void hostPattern(String hostPattern) { | ||
this.hostPattern = hostPattern; | ||
} | ||
|
||
public void limit(String limit) { | ||
this.limit = limit; | ||
} | ||
|
||
public void tags(String tags) { | ||
this.tags = tags; | ||
} | ||
|
||
public void skippedTags(String skippedTags) { | ||
this.skippedTags = skippedTags; | ||
} | ||
|
||
public void startAtTask(String startAtTask) { | ||
this.startAtTask = startAtTask; | ||
} | ||
|
||
public String getAnsibleName() { | ||
return ansibleName; | ||
} | ||
|
||
public String getCredentialsId() { | ||
return credentialsId; | ||
} | ||
|
||
public Inventory getInventory() { | ||
return inventory; | ||
} | ||
|
||
public boolean isSudo() { | ||
return sudo; | ||
} | ||
|
||
public String getSudoUser() { | ||
return sudoUser; | ||
} | ||
|
||
public int getForks() { | ||
return forks; | ||
} | ||
|
||
public boolean isUnbufferedOutput() { | ||
return unbufferedOutput; | ||
} | ||
|
||
public boolean isColorizedOutput() { | ||
return colorizedOutput; | ||
} | ||
|
||
public boolean isHostKeyChecking() { | ||
return hostKeyChecking; | ||
} | ||
|
||
public String getAdditionalParameters() { | ||
return additionalParameters; | ||
} | ||
|
||
public String getHostPattern() { | ||
return hostPattern; | ||
} | ||
|
||
public String getLimit() { | ||
return limit; | ||
} | ||
|
||
public String getTags() { | ||
return tags; | ||
} | ||
|
||
public String getSkippedTags() { | ||
return skippedTags; | ||
} | ||
|
||
public String getStartAtTask() { | ||
return startAtTask; | ||
} | ||
} |
Oops, something went wrong.