Skip to content
Permalink
Browse files

Merge pull request #1 from lanwen/JENKINS-31790_jobdsl

[JENKINS-31790] Job DSL support in ansible-plugin as extension point
  • Loading branch information...
jcsirot committed Dec 25, 2015
2 parents 71aaeda + 09b47e9 commit 3432387a522e311e7d0a1a1ce7a92d4d91c5043a
@@ -6,3 +6,57 @@ This plugin gives the possibility to run [Ansible](http://www.ansible.com/) ad-h
Jenkins Wiki page: https://wiki.jenkins-ci.org/display/JENKINS/Ansible+Plugin

[![Build Status](https://jenkins.ci.cloudbees.com/buildStatus/icon?job=plugins/ansible-plugin)](https://jenkins.ci.cloudbees.com/job/plugins/job/ansible-plugin/)

## Job DSL support

```groovy
steps {
ansiblePlaybook(String playbook) {
inventoryPath(String path)
inventoryContent(String content, boolean dynamic = false)
ansibleName(String name)
limit(String limit)
tags(String tags)
skippedTags(String tags)
startAtTask(String task)
credentialsId(String id)
sudo(boolean sudo = true)
sudoUser(String user = 'root')
forks(int forks = 5)
unbufferedOutput(boolean unbufferedOutput = true)
colorizedOutput(boolean colorizedOutput = false)
hostKeyChecking(boolean hostKeyChecking = false)
additionalParameters(String params)
}
ansibleAdHoc(String module, String command) {
ansibleName(String name)
inventoryPath(String path)
inventoryContent(String content, boolean dynamic = false)
credentialsId(String id)
hostPattern(String pattern)
sudo(boolean sudo = true)
sudoUser(String user = 'root')
forks(int forks = 5)
unbufferedOutput(boolean unbufferedOutput = true)
colorizedOutput(boolean colorizedOutput = false)
hostKeyChecking(boolean hostKeyChecking = false)
additionalParameters(String params)
}
}
```

### Example

```groovy
steps {
ansiblePlaybook('path/playbook.yml') {
inventoryPath('hosts.ini')
ansibleName('1.9.4')
tags('one,two')
credentialsId('credsid')
sudo(true)
sudoUser("user")
}
}
```
@@ -67,6 +67,15 @@
<version>${workflow-step-api.version}</version>
<optional>true</optional>
</dependency>

<!--https://github.com/jenkinsci/job-dsl-plugin/wiki/Extending-the-DSL-->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>job-dsl</artifactId>
<version>1.36</version>
<optional>true</optional>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
@@ -19,15 +19,12 @@
import java.io.IOException;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import hudson.AbortException;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Computer;
import hudson.model.Run;
import hudson.model.TaskListener;
@@ -99,7 +96,7 @@ public AnsibleAdHocCommandBuilder(String ansibleName, String hostPattern, Invent
this.additionalParameters = additionalParameters;
}

@Deprecated
@DataBoundConstructor
public AnsibleAdHocCommandBuilder(String hostPattern, Inventory inventory, String module, String command) {
this.hostPattern = hostPattern;
this.inventory = inventory;
@@ -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;
}
}
@@ -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;
}
}

0 comments on commit 3432387

Please sign in to comment.
You can’t perform that action at this time.