Skip to content

Commit

Permalink
[JENKINS-30398] code snippet generator support
Browse files Browse the repository at this point in the history
  • Loading branch information
jcsirot committed Dec 24, 2015
1 parent 3ee2ffb commit 71aaeda
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
@@ -1,24 +1,37 @@
package org.jenkinsci.plugins.ansible.workflow;

import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
import com.google.inject.Inject;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.Computer;
import hudson.model.Project;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.ListBoxModel;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.ansible.AnsibleInstallation;
import org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder;
import org.jenkinsci.plugins.ansible.Inventory;
import org.jenkinsci.plugins.ansible.InventoryPath;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import static com.cloudbees.plugins.credentials.CredentialsMatchers.anyOf;
import static com.cloudbees.plugins.credentials.CredentialsMatchers.instanceOf;

/**
* The Ansible playbook invocation step for the Jenkins workflow plugin.
*/
Expand All @@ -43,12 +56,12 @@ public AnsiblePlaybookStep(String playbook) {

@DataBoundSetter
public void setInventory(String inventory) {
this.inventory = inventory;
this.inventory = Util.fixEmptyAndTrim(inventory);
}

@DataBoundSetter
public void setCredentialsId(String credentialsId) {
this.credentialsId = credentialsId;
this.credentialsId = Util.fixEmptyAndTrim(credentialsId);
}

@DataBoundSetter
Expand All @@ -58,37 +71,37 @@ public void setSudo(boolean sudo) {

@DataBoundSetter
public void setSudoUser(String sudoUser) {
this.sudoUser = sudoUser;
this.sudoUser = Util.fixEmptyAndTrim(sudoUser);
}

@DataBoundSetter
public void setInstallation(String installation) {
this.installation = installation;
this.installation = Util.fixEmptyAndTrim(installation);
}

@DataBoundSetter
public void setLimit(String limit) {
this.limit = limit;
this.limit = Util.fixEmptyAndTrim(limit);
}

@DataBoundSetter
public void setTags(String tags) {
this.tags = tags;
this.tags = Util.fixEmptyAndTrim(tags);
}

@DataBoundSetter
public void setSkippedTags(String skippedTags) {
this.skippedTags = skippedTags;
this.skippedTags = Util.fixEmptyAndTrim(skippedTags);
}

@DataBoundSetter
public void setStartAtTask(String startAtTask) {
this.startAtTask = startAtTask;
this.startAtTask = Util.fixEmptyAndTrim(startAtTask);
}

@DataBoundSetter
public void setExtras(String extras) {
this.extras = extras;
this.extras = Util.fixEmptyAndTrim(extras);
}

public String getInstallation() {
Expand Down Expand Up @@ -151,6 +164,23 @@ public String getFunctionName() {
public String getDisplayName() {
return "Invoke an ansible playbook";
}

public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Project project) {
return new StandardListBoxModel()
.withEmptySelection()
.withMatching(anyOf(
instanceOf(SSHUserPrivateKey.class),
instanceOf(UsernamePasswordCredentials.class)),
CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, project));
}

public ListBoxModel doFillInstallationItems() {
ListBoxModel model = new ListBoxModel();
for (AnsibleInstallation tool : AnsibleInstallation.allInstallations()) {
model.add(tool.getName());
}
return model;
}
}

public static final class AnsiblePlaybookExecution extends AbstractSynchronousStepExecution<Void> {
Expand Down
@@ -0,0 +1,36 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:c="/lib/credentials">
<f:entry field="installation" title="Ansible tool">
<f:select/>
</f:entry>
<f:entry field="playbook" title="Playbook file path in workspace">
<f:textbox/>
</f:entry>
<f:entry field="inventory" title="Inventory file path in workspace">
<f:textbox/>
</f:entry>
<f:entry field="credentialsId" title="SSH connection credentials">
<c:select/>
</f:entry>
<f:entry field="sudo" title="Use sudo">
<f:checkbox/>
</f:entry>
<f:entry field="sudoUser" title="Sudo username">
<f:textbox/>
</f:entry>
<f:entry field="limit" title="Host subset">
<f:textbox/>
</f:entry>
<f:entry field="tags" title="Tags">
<f:textbox/>
</f:entry>
<f:entry field="skippedTags" title="Tags to skip">
<f:textbox/>
</f:entry>
<f:entry field="startAtTask" title="Task to start at">
<f:textbox/>
</f:entry>
<f:entry field="extras" title="Extra parameters">
<f:textbox/>
</f:entry>
</j:jelly>
@@ -0,0 +1,3 @@
<div>
Execute an Ansible playbook. Only the <code>playbook</code> parameter is mandatory.
</div>

0 comments on commit 71aaeda

Please sign in to comment.