{
187 |
188 | private static final long serialVersionUID = 1;
189 |
190 | @Inject
191 | private transient AnsiblePlaybookStep step;
192 |
193 | @StepContextParameter
194 | private transient TaskListener listener;
195 |
196 | @StepContextParameter
197 | private transient Launcher launcher;
198 |
199 | @StepContextParameter
200 | private transient Run,?> run;
201 |
202 | @StepContextParameter
203 | private transient FilePath ws;
204 |
205 | @StepContextParameter
206 | private transient EnvVars envVars;
207 |
208 | @StepContextParameter
209 | private transient Computer computer;
210 |
211 | @Override
212 | protected Void run() throws Exception {
213 | Inventory inventory = StringUtils.isNotBlank(step.getInventory()) ? new InventoryPath(step.getInventory()) : null;
214 | AnsiblePlaybookBuilder builder = new AnsiblePlaybookBuilder(step.getPlaybook(), inventory);
215 | builder.setAnsibleName(step.getInstallation());
216 | builder.setSudo(step.isSudo());
217 | builder.setSudoUser(step.getSudoUser());
218 | builder.setCredentialsId(step.getCredentialsId(), true);
219 | builder.setForks(5);
220 | builder.setLimit(step.getLimit());
221 | builder.setTags(step.getTags());
222 | builder.setStartAtTask(step.getStartAtTask());
223 | builder.setSkippedTags(step.getSkippedTags());
224 | builder.setAdditionalParameters(step.getExtras());
225 | builder.setHostKeyChecking(false);
226 | builder.setUnbufferedOutput(true);
227 | builder.setColorizedOutput(false);
228 | builder.perform(run, computer.getNode(), ws, launcher, listener, envVars);
229 | return null;
230 | }
231 | }
232 |
233 | }
234 |
--------------------------------------------------------------------------------
/src/main/resources/index.jelly:
--------------------------------------------------------------------------------
1 |
2 | Invoke
Ansible Ad-Hoc commands and playbooks.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | ${inst.name}
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/config.properties:
--------------------------------------------------------------------------------
1 | AnsibleInstallation.error=Needs to know where Ansible is installed. \
2 | Please do so from the system configuration .
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/help-additionalParameters.html:
--------------------------------------------------------------------------------
1 |
2 | Any additional parameters to pass to the ansible command.
3 |
Warning: The content of this textbox will be passed as is to the command line.
4 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/help-colorizedOutput.html:
--------------------------------------------------------------------------------
1 |
2 | Check this box to allow ansible to render ANSI color codes in the Jenkins console.
3 | This option works well with the
Jenkins AnsiColor plugin .
4 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/help-command.html:
--------------------------------------------------------------------------------
1 |
2 | Module arguments or shell command to execute
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/help-credentialsId.html:
--------------------------------------------------------------------------------
1 |
2 | Select the credentials for the SSH connections. Only private key authentication is supported.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/help-forks.html:
--------------------------------------------------------------------------------
1 |
2 | Specify number of parallel processes to use
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/help-hostKeyChecking.html:
--------------------------------------------------------------------------------
1 |
2 | Check this box to enforce the validation of the hosts SSH server keys.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/help-hostPattern.html:
--------------------------------------------------------------------------------
1 |
2 | The host or set of hosts on which the command will be executed. For more details see the ansible documentation
3 | for
Patterns .
4 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/help-module.html:
--------------------------------------------------------------------------------
1 |
2 | Module name to execute. The shell module is used when left empty.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/help-sudo.html:
--------------------------------------------------------------------------------
1 |
2 | Run operations with sudo. It works only when the remote user is sudoer with nopasswd option.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/help-sudoUser.html:
--------------------------------------------------------------------------------
1 |
2 | Desired sudo user. "root" is used when this field is empty.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandBuilder/help-unbufferedOutput.html:
--------------------------------------------------------------------------------
1 |
2 | Skip standard output buffering for the ansible process. The ansible output is directly rendered into the Jenkins
3 | console. This option can be usefull for long running operations.
4 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsibleInstallation/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | ${inst.name}
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/config.properties:
--------------------------------------------------------------------------------
1 | AnsibleInstallation.error=Needs to know where Ansible is installed. \
2 | Please do so from the system configuration .
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-additionalParameters.html:
--------------------------------------------------------------------------------
1 |
2 | Any additional parameters to pass to the ansible command.
3 |
Warning: The content of this textbox will be passed as is to the command line.
4 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-colorizedOutput.html:
--------------------------------------------------------------------------------
1 |
2 | Check this box to allow ansible to render ANSI color codes in the Jenkins console.
3 | This option works well with the
Jenkins AnsiColor plugin .
4 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-credentialsId.html:
--------------------------------------------------------------------------------
1 |
2 | Select the credentials for the SSH connections. Only private key authentication is supported.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-forks.html:
--------------------------------------------------------------------------------
1 |
2 | Specify number of parallel processes to use
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-hostKeyChecking.html:
--------------------------------------------------------------------------------
1 |
2 | Check this box to enforce the validation of the hosts SSH server keys.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-limit.html:
--------------------------------------------------------------------------------
1 |
2 | Further limit selected hosts to an additional pattern.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-playbook.html:
--------------------------------------------------------------------------------
1 |
2 | Path to the ansible playbook file. The path can be absolute or relative to the job workspace.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-skippedTags.html:
--------------------------------------------------------------------------------
1 |
2 | only run plays and tasks whose tags do not match these values.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-startAtTask.html:
--------------------------------------------------------------------------------
1 |
2 | Start the playbook at the task matching this name.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-sudo.html:
--------------------------------------------------------------------------------
1 |
2 | Run operations with sudo. It works only when the remote user is sudoer with nopasswd option.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-sudoUser.html:
--------------------------------------------------------------------------------
1 |
2 | Desired sudo user. "root" is used when this field is empty.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-tags.html:
--------------------------------------------------------------------------------
1 |
2 | Only run plays and tasks tagged with these values.
3 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/AnsiblePlaybookBuilder/help-unbufferedOutput.html:
--------------------------------------------------------------------------------
1 |
2 | Skip standard output buffering for the ansible process. The ansible output is directly rendered into the Jenkins
3 | console. This option can be usefull for long running operations.
4 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/InventoryContent/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/InventoryContent/help-dynamic.html:
--------------------------------------------------------------------------------
1 |
2 | Check this box if a dynamic inventory is used. For more details see the ansible documentation for
Dynamic Inventory
4 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/InventoryPath/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/workflow/AnsiblePlaybookStep/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/src/main/resources/org/jenkinsci/plugins/ansible/workflow/AnsiblePlaybookStep/help.html:
--------------------------------------------------------------------------------
1 |
2 | Execute an Ansible playbook. Only the playbook
parameter is mandatory.
3 |
4 |
--------------------------------------------------------------------------------
/src/test/java/org/jenkinsci/plugins/ansible/AnsibleAdHocCommandInvocationTest.java:
--------------------------------------------------------------------------------
1 | package org.jenkinsci.plugins.ansible;
2 |
3 | import static org.assertj.core.api.Assertions.assertThat;
4 | import static org.mockito.Matchers.any;
5 | import static org.mockito.Matchers.anyMap;
6 | import static org.mockito.Mockito.mock;
7 | import static org.mockito.Mockito.verify;
8 | import static org.mockito.Mockito.when;
9 |
10 | import java.io.File;
11 | import java.util.Map;
12 |
13 | import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
14 | import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
15 | import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
16 | import hudson.EnvVars;
17 | import hudson.FilePath;
18 | import hudson.Launcher;
19 | import hudson.model.AbstractBuild;
20 | import hudson.model.BuildListener;
21 | import hudson.model.TaskListener;
22 | import hudson.util.ArgumentListBuilder;
23 | import hudson.util.Secret;
24 | import org.junit.*;
25 | import org.mockito.ArgumentCaptor;
26 |
27 | /**
28 | * Created with IntelliJ IDEA.
29 | * User: jcsirot
30 | * Date: 22/05/15
31 | * Time: 19:30
32 | * To change this template use File | Settings | File Templates.
33 | */
34 | public class AnsibleAdHocCommandInvocationTest {
35 |
36 | @Test
37 | public void should_generate_simple_invocation() throws Exception {
38 | // Given
39 | Inventory inventory = new InventoryPath("/tmp/hosts");
40 | BuildListener listener = mock(BuildListener.class);
41 | CLIRunner runner = mock(CLIRunner.class);
42 | AbstractBuild,?> build = mock(AbstractBuild.class);
43 | when(build.getEnvironment(any(TaskListener.class))).thenReturn(new EnvVars());
44 | AnsibleAdHocCommandInvocation invocation = new AnsibleAdHocCommandInvocation("/usr/local/bin/ansible", build, listener);
45 | invocation.setHostPattern("localhost");
46 | invocation.setInventory(inventory);
47 | invocation.setModule("ping");
48 | invocation.setForks(5);
49 | // When
50 | invocation.execute(runner);
51 | // Then
52 | ArgumentCaptor argument = ArgumentCaptor.forClass(ArgumentListBuilder.class);
53 | verify(runner).execute(argument.capture(), anyMap());
54 | assertThat(argument.getValue().toString())
55 | .isEqualTo("/usr/local/bin/ansible localhost -i /tmp/hosts -m ping -f 5");
56 | }
57 |
58 | @Test
59 | public void should_generate_simple_invocation_with_env() throws Exception {
60 | // Given
61 | Inventory inventory = new InventoryPath("/tmp/hosts");
62 | BuildListener listener = mock(BuildListener.class);
63 | CLIRunner runner = mock(CLIRunner.class);
64 | AbstractBuild,?> build = mock(AbstractBuild.class);
65 | when(build.getEnvironment(any(TaskListener.class))).thenReturn(new EnvVars());
66 | AnsibleAdHocCommandInvocation invocation = new AnsibleAdHocCommandInvocation("/usr/local/bin/ansible", build, listener);
67 | invocation.setHostPattern("localhost");
68 | invocation.setInventory(inventory);
69 | invocation.setModule("ping");
70 | invocation.setForks(5);
71 | invocation.setColorizedOutput(true);
72 | invocation.setHostKeyCheck(false);
73 | invocation.setUnbufferedOutput(true);
74 | // When
75 | invocation.execute(runner);
76 | // Then
77 | ArgumentCaptor argument = ArgumentCaptor.forClass(Map.class);
78 | verify(runner).execute(any(ArgumentListBuilder.class), argument.capture());
79 | assertThat((Map)argument.getValue())
80 | .containsEntry("PYTHONUNBUFFERED", "1")
81 | .containsEntry("ANSIBLE_FORCE_COLOR", "true")
82 | .containsEntry("ANSIBLE_HOST_KEY_CHECKING", "False");
83 | }
84 |
85 | @Test
86 | @Ignore("build.getWorkspace() cannot be mocked")
87 | public void should_handle_private_key_credentials() throws Exception {
88 | // Given
89 | Inventory inventory = new InventoryPath("/tmp/hosts");
90 | SSHUserPrivateKey pkey = mock(SSHUserPrivateKey.class);
91 | when(pkey.getUsername()).thenReturn("mylogin");
92 | BuildListener listener = mock(BuildListener.class);
93 | CLIRunner runner = mock(CLIRunner.class);
94 | AbstractBuild,?> build = mock(AbstractBuild.class);
95 | when(build.getEnvironment(any(TaskListener.class))).thenReturn(new EnvVars());
96 | AnsibleAdHocCommandInvocation invocation = new AnsibleAdHocCommandInvocation("/usr/local/bin/ansible", build, listener);
97 | invocation.setHostPattern("localhost");
98 | invocation.setInventory(inventory);
99 | invocation.setModule("ping");
100 | invocation.setCredentials(pkey);
101 | invocation.setForks(5);
102 | // When
103 | invocation.execute(runner);
104 | // Then
105 | ArgumentCaptor argument = ArgumentCaptor.forClass(ArgumentListBuilder.class);
106 | verify(runner).execute(argument.capture(), anyMap());
107 | assertThat(argument.getValue().toString())
108 | .matches("/usr/local/bin/ansible localhost -i /tmp/hosts -m ping -f 5 --private-key .+ -u mylogin");
109 | }
110 |
111 | @Test
112 | @Ignore("Secret can neither be instanced nor mocked")
113 | public void should_handle_password_credentials() throws Exception {
114 | // Given
115 | Inventory inventory = new InventoryPath("/tmp/hosts");
116 | StandardUsernamePasswordCredentials password = mock(StandardUsernamePasswordCredentials.class);
117 | when(password.getUsername()).thenReturn("mylogin");
118 | when(password.getPassword()).thenReturn(Secret.fromString("aStrongSecretPassword"));
119 | BuildListener listener = mock(BuildListener.class);
120 | CLIRunner runner = mock(CLIRunner.class);
121 | AbstractBuild,?> build = mock(AbstractBuild.class);
122 | when(build.getEnvironment(any(TaskListener.class))).thenReturn(new EnvVars());
123 | AnsibleAdHocCommandInvocation invocation = new AnsibleAdHocCommandInvocation("/usr/local/bin/ansible", build, listener);
124 | invocation.setHostPattern("localhost");
125 | invocation.setInventory(inventory);
126 | invocation.setModule("ping");
127 | invocation.setCredentials(password);
128 | invocation.setForks(5);
129 | // When
130 | invocation.execute(runner);
131 | // Then
132 | ArgumentCaptor argument = ArgumentCaptor.forClass(ArgumentListBuilder.class);
133 | verify(runner).execute(argument.capture(), anyMap());
134 | assertThat(argument.getValue().toString())
135 | .isEqualTo("sshpass ****** /usr/local/bin/ansible localhost -i /tmp/hosts -m ping -f 5 " +
136 | "-u" +
137 | " mylogin -k");
138 | }
139 |
140 | @Test
141 | public void should_handle_variables() throws Exception {
142 | // Given
143 | Inventory inventory = new InventoryPath("/tmp/hosts");
144 | BuildListener listener = mock(BuildListener.class);
145 | CLIRunner runner = mock(CLIRunner.class);
146 | AbstractBuild,?> build = mock(AbstractBuild.class);
147 | EnvVars vars = new EnvVars();
148 | vars.put("MODULE", "ping");
149 | when(build.getEnvironment(any(TaskListener.class))).thenReturn(vars);
150 | AnsibleAdHocCommandInvocation invocation = new AnsibleAdHocCommandInvocation("/usr/local/bin/ansible", build, listener);
151 | invocation.setHostPattern("localhost");
152 | invocation.setInventory(inventory);
153 | invocation.setModule("${MODULE}");
154 | invocation.setForks(5);
155 | // When
156 | invocation.execute(runner);
157 | // Then
158 | ArgumentCaptor argument = ArgumentCaptor.forClass(ArgumentListBuilder.class);
159 | verify(runner).execute(argument.capture(), anyMap());
160 | assertThat(argument.getValue().toString())
161 | .isEqualTo("/usr/local/bin/ansible localhost -i /tmp/hosts -m ping -f 5");
162 | }
163 | }
164 |
--------------------------------------------------------------------------------
/src/test/java/org/jenkinsci/plugins/ansible/jobdsl/DslJobRule.java:
--------------------------------------------------------------------------------
1 | package org.jenkinsci.plugins.ansible.jobdsl;
2 |
3 | import com.google.common.base.Charsets;
4 | import com.google.common.io.Resources;
5 | import hudson.model.FreeStyleProject;
6 | import javaposse.jobdsl.plugin.ExecuteDslScripts;
7 | import javaposse.jobdsl.plugin.LookupStrategy;
8 | import javaposse.jobdsl.plugin.RemovedJobAction;
9 | import javaposse.jobdsl.plugin.RemovedViewAction;
10 | import org.junit.rules.TestRule;
11 | import org.junit.runner.Description;
12 | import org.junit.runners.model.Statement;
13 | import org.jvnet.hudson.test.JenkinsRule;
14 |
15 | import java.lang.annotation.Retention;
16 | import java.lang.annotation.Target;
17 |
18 | import static java.lang.annotation.ElementType.METHOD;
19 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
20 | import static org.hamcrest.Matchers.hasItem;
21 | import static org.hamcrest.Matchers.is;
22 | import static org.junit.Assert.assertThat;
23 |
24 | /**
25 | * @author lanwen (Merkushev Kirill)
26 | */
27 | public class DslJobRule implements TestRule {
28 |
29 | public static final String JOB_NAME_IN_DSL_SCRIPT = "ansible";
30 |
31 | private JenkinsRule jRule;
32 |
33 | private FreeStyleProject generated;
34 |
35 | public DslJobRule(JenkinsRule jRule) {
36 | this.jRule = jRule;
37 | }
38 |
39 | @Override
40 | public Statement apply(final Statement base, final Description description) {
41 | return new Statement() {
42 | @Override
43 | public void evaluate() throws Throwable {
44 | before(description);
45 | base.evaluate();
46 | }
47 | };
48 | }
49 |
50 | private void before(Description description) throws Exception {
51 | FreeStyleProject job = jRule.createFreeStyleProject();
52 | String script = description.getAnnotation(WithJobDsl.class).value();
53 | String scriptText = Resources.toString(Resources.getResource(script), Charsets.UTF_8);
54 |
55 | job.getBuildersList().add(
56 | new ExecuteDslScripts(
57 | new ExecuteDslScripts.ScriptLocation(
58 | null, null,
59 | scriptText
60 | ),
61 | false,
62 | RemovedJobAction.DELETE,
63 | RemovedViewAction.DELETE,
64 | LookupStrategy.JENKINS_ROOT
65 | )
66 | );
67 |
68 | jRule.buildAndAssertSuccess(job);
69 |
70 | assertThat(jRule.getInstance().getJobNames(), hasItem(is(JOB_NAME_IN_DSL_SCRIPT)));
71 |
72 | generated = jRule.getInstance().getItemByFullName(JOB_NAME_IN_DSL_SCRIPT, FreeStyleProject.class);
73 | }
74 |
75 | public FreeStyleProject getGeneratedJob() {
76 | return generated;
77 | }
78 |
79 | @Target(METHOD)
80 | @Retention(RUNTIME)
81 | public @interface WithJobDsl {
82 | String value();
83 | }
84 | }
85 |
86 |
--------------------------------------------------------------------------------
/src/test/java/org/jenkinsci/plugins/ansible/jobdsl/JobDslIntegrationTest.java:
--------------------------------------------------------------------------------
1 | package org.jenkinsci.plugins.ansible.jobdsl;
2 |
3 | import org.hamcrest.Matcher;
4 | import org.jenkinsci.plugins.ansible.AnsibleAdHocCommandBuilder;
5 | import org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder;
6 | import org.jenkinsci.plugins.ansible.InventoryContent;
7 | import org.jenkinsci.plugins.ansible.InventoryPath;
8 | import org.junit.Rule;
9 | import org.junit.Test;
10 | import org.junit.rules.RuleChain;
11 | import org.jvnet.hudson.test.JenkinsRule;
12 |
13 | import static org.hamcrest.Matchers.is;
14 | import static org.hamcrest.Matchers.isA;
15 | import static org.hamcrest.Matchers.notNullValue;
16 | import static org.junit.Assert.assertThat;
17 |
18 | /**
19 | * @author lanwen (Merkushev Kirill)
20 | */
21 | public class JobDslIntegrationTest {
22 | public static final String ANSIBLE_DSL_GROOVY_PLAYBOOK = "jobdsl/playbook.groovy";
23 | public static final String ANSIBLE_DSL_GROOVY_ADHOC = "jobdsl/adhoc.groovy";
24 |
25 | public JenkinsRule jenkins = new JenkinsRule();
26 | public DslJobRule dsl = new DslJobRule(jenkins);
27 |
28 | @Rule
29 | public RuleChain chain = RuleChain.outerRule(jenkins).around(dsl);
30 |
31 | @Test
32 | @DslJobRule.WithJobDsl(ANSIBLE_DSL_GROOVY_PLAYBOOK)
33 | public void shouldCreateJobWithPlaybookDsl() throws Exception {
34 | AnsiblePlaybookBuilder step = dsl.getGeneratedJob().getBuildersList().get(AnsiblePlaybookBuilder.class);
35 | assertThat("Should add playbook builder", step, notNullValue());
36 |
37 | assertThat("playbook", step.playbook, is("path/playbook.yml"));
38 | assertThat("inventory", step.inventory, (Matcher) isA(InventoryPath.class));
39 | assertThat("ansibleName", step.ansibleName, is("1.9.4"));
40 | assertThat("limit", step.limit, is("retry.limit"));
41 | assertThat("tags", step.tags, is("one,two"));
42 | assertThat("skippedTags", step.skippedTags, is("three"));
43 | assertThat("startAtTask", step.startAtTask, is("task"));
44 | assertThat("credentialsId", step.credentialsId, is("credsid"));
45 | assertThat("sudo", step.sudo, is(true));
46 | assertThat("sudoUser", step.sudoUser, is("user"));
47 | assertThat("forks", step.forks, is(6));
48 | assertThat("unbufferedOutput", step.unbufferedOutput, is(false));
49 | assertThat("colorizedOutput", step.colorizedOutput, is(true));
50 | assertThat("hostKeyChecking", step.hostKeyChecking, is(false));
51 | assertThat("additionalParameters", step.additionalParameters, is("params"));
52 | }
53 |
54 | @Test
55 | @DslJobRule.WithJobDsl(ANSIBLE_DSL_GROOVY_ADHOC)
56 | public void shouldCreateJobAdhocDsl() throws Exception {
57 | AnsibleAdHocCommandBuilder step = dsl.getGeneratedJob().getBuildersList().get(AnsibleAdHocCommandBuilder.class);
58 | assertThat("Should add adhoc builder", step, notNullValue());
59 |
60 | assertThat("module", step.module, is("module"));
61 | assertThat("inventory", step.inventory, (Matcher) isA(InventoryContent.class));
62 | assertThat("ansibleName", step.ansibleName, is("1.9.1"));
63 |
64 | assertThat("credentialsId", step.credentialsId, is("credsid"));
65 | assertThat("hostPattern", step.hostPattern, is("pattern"));
66 | assertThat("sudo", step.sudo, is(false));
67 | assertThat("sudoUser", step.sudoUser, is("root"));
68 | assertThat("forks", step.forks, is(5));
69 | assertThat("unbufferedOutput", step.unbufferedOutput, is(true));
70 | assertThat("colorizedOutput", step.colorizedOutput, is(false));
71 | assertThat("hostKeyChecking", step.hostKeyChecking, is(false));
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/test/resources/jobdsl/adhoc.groovy:
--------------------------------------------------------------------------------
1 | freeStyleJob('ansible') {
2 | steps {
3 | ansibleAdHoc('module', 'command') {
4 | ansibleName('1.9.1')
5 | credentialsId('credsid')
6 | hostPattern('pattern')
7 | inventoryContent('content')
8 | }
9 | }
10 | }
--------------------------------------------------------------------------------
/src/test/resources/jobdsl/playbook.groovy:
--------------------------------------------------------------------------------
1 | freeStyleJob('ansible') {
2 | steps {
3 | ansiblePlaybook('path/playbook.yml') {
4 | inventoryPath('hosts.ini')
5 | ansibleName('1.9.4')
6 | limit('retry.limit')
7 | tags('one,two')
8 | skippedTags('three')
9 | startAtTask('task')
10 | credentialsId('credsid')
11 | sudo(true)
12 | sudoUser("user")
13 | forks(6)
14 | unbufferedOutput(false)
15 | colorizedOutput(true)
16 | hostKeyChecking(false)
17 | additionalParameters('params')
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------