Skip to content

Commit 092eccd

Browse files
Merge pull request #36 from yassineazzouz/master
Change logging from context logger to stdout + add missed stderr logging from stream logging
2 parents 1620d9c + 0c07cb5 commit 092eccd

13 files changed

Lines changed: 236 additions & 280 deletions

src/main/java/com/batix/rundeck/AnsibleFailureReason.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
public enum AnsibleFailureReason implements FailureReason {
66
AnsibleNonZero, // Ansible process exited with non-zero value
77
AnsibleError, // Ansible not found etc.
8-
StorageTierAccessError
8+
StorageTierAccessError,
9+
Interrupted,
10+
IOFailure,
11+
Unknown
912
}

src/main/java/com/batix/rundeck/AnsibleFileCopier.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.dtolabs.rundeck.core.plugins.configuration.PropertyUtil;
1414
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
1515
import com.dtolabs.rundeck.plugins.util.DescriptionBuilder;
16+
import com.dtolabs.rundeck.plugins.PluginLogger;
1617

1718
import java.io.File;
1819
import java.io.InputStream;
@@ -59,7 +60,9 @@ private String doFileCopy(
5960
final INodeEntry node,
6061
String destinationPath
6162
) throws FileCopierException {
63+
6264
IRundeckProject project = context.getFramework().getFrameworkProjectMgr().getFrameworkProject(context.getFrameworkProject());
65+
6366
if (destinationPath == null) {
6467
String identity = (context.getDataContext() != null && context.getDataContext().get("job") != null) ?
6568
context.getDataContext().get("job").get("execid") : null;
@@ -93,10 +96,6 @@ private String doFileCopy(
9396
throw new FileCopierException("Error running Ansible.", AnsibleFailureReason.AnsibleError, e);
9497
}
9598

96-
if (result != 0) {
97-
throw new FileCopierException("Ansible exited with non-zero code.", AnsibleFailureReason.AnsibleNonZero);
98-
}
99-
10099
return destinationPath;
101100
}
102101

src/main/java/com/batix/rundeck/AnsibleModuleNodeStep.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,18 @@ public void executeNodeStep(PluginStepContext context, Map<String, Object> confi
2929
String sshPass = (String) configuration.get("sshPassword");
3030
final PluginLogger logger = context.getLogger();
3131

32-
AnsibleRunner runner = AnsibleRunner.adHoc(module, args).limit(entry.getNodename()).extraArgs(extraArgs).sshPass(sshPass).stream();
32+
AnsibleRunner runner = AnsibleRunner.adHoc(module, args).limit(entry.getNodename()).extraArgs(extraArgs).sshPass(sshPass);
33+
3334
if ("true".equals(System.getProperty("ansible.debug"))) {
3435
runner.debug();
3536
}
3637

37-
runner.listener(new AnsibleRunner.Listener() {
38-
@Override
39-
public void output(String line) {
40-
logger.log(Project.MSG_INFO, line);
41-
}
42-
});
43-
4438
int result;
4539
try {
4640
result = runner.run();
4741
} catch (Exception e) {
4842
throw new NodeStepException("Error running Ansible.", e, AnsibleFailureReason.AnsibleError, entry.getNodename());
4943
}
50-
51-
if (result != 0) {
52-
throw new NodeStepException("Ansible exited with non-zero code.", AnsibleFailureReason.AnsibleNonZero, entry.getNodename());
53-
}
5444
}
5545

5646
@Override

src/main/java/com/batix/rundeck/AnsibleModuleWorkflowStep.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,17 @@ public void executeStep(PluginStepContext context, Map<String, Object> configura
2828
String sshPass = (String) configuration.get("sshPassword");
2929
final PluginLogger logger = context.getLogger();
3030

31-
AnsibleRunner runner = AnsibleRunner.adHoc(module, args).limit(context.getNodes()).extraArgs(extraArgs).sshPass(sshPass).stream();
31+
AnsibleRunner runner = AnsibleRunner.adHoc(module, args).limit(context.getNodes()).extraArgs(extraArgs).sshPass(sshPass);
32+
3233
if ("true".equals(System.getProperty("ansible.debug"))) {
3334
runner.debug();
3435
}
3536

36-
runner.listener(new AnsibleRunner.Listener() {
37-
@Override
38-
public void output(String line) {
39-
logger.log(Project.MSG_INFO, line);
40-
}
41-
});
42-
4337
int result;
4438
try {
45-
result = runner.run();
39+
result = runner.run();
4640
} catch (Exception e) {
47-
throw new StepException("Error running Ansible.", e, AnsibleFailureReason.AnsibleError);
48-
}
49-
50-
if (result != 0) {
51-
throw new StepException("Ansible exited with non-zero code.", AnsibleFailureReason.AnsibleNonZero);
41+
throw new StepException("Error running Ansible.", e, AnsibleFailureReason.AnsibleError);
5242
}
5343
}
5444

src/main/java/com/batix/rundeck/AnsibleNodeExecutor.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.dtolabs.rundeck.core.common.IRundeckProject;
55
import com.dtolabs.rundeck.core.common.ProjectManager;
66
import com.dtolabs.rundeck.core.execution.ExecutionContext;
7+
import com.dtolabs.rundeck.plugins.PluginLogger;
78
import com.dtolabs.rundeck.core.execution.service.NodeExecutor;
89
import com.dtolabs.rundeck.core.execution.service.NodeExecutorResult;
910
import com.dtolabs.rundeck.core.execution.service.NodeExecutorResultImpl;
@@ -23,6 +24,7 @@ public class AnsibleNodeExecutor implements NodeExecutor, Describable {
2324

2425
@Override
2526
public NodeExecutorResult executeCommand(ExecutionContext context, String[] command, INodeEntry node) {
27+
2628
StringBuilder cmdArgs = new StringBuilder();
2729
ProjectManager projectManager = context.getFramework().getProjectManager();
2830
IRundeckProject project = projectManager.getFrameworkProject(context.getFrameworkProject());
@@ -35,37 +37,18 @@ public NodeExecutorResult executeCommand(ExecutionContext context, String[] comm
3537
String extraArgs = project.hasProperty("extraArgs") ? project.getProperty("extraArgs") : null;
3638

3739
AnsibleRunner runner = AnsibleRunner.adHoc("shell", cmdArgs.toString()).limit(node.getNodename()).extraArgs(extraArgs);
40+
3841
if ("true".equals(System.getProperty("ansible.debug"))) {
3942
runner.debug();
4043
}
44+
4145
int result;
4246
try {
4347
result = runner.run();
4448
} catch (Exception e) {
45-
System.out.println(e.getMessage());
46-
e.printStackTrace();
4749
return NodeExecutorResultImpl.createFailure(AnsibleFailureReason.AnsibleError, e.getMessage(), e, node, runner.getResult());
4850
}
4951

50-
JsonObject json = runner.getResults().get(0).results.size() > 0 ? runner.getResults().get(0).results.get(0).json : null;
51-
52-
if (json != null && json.has("stdout")) {
53-
String string = json.get("stdout").getAsString();
54-
if (string != null && string.length() > 0) {
55-
System.out.println(string);
56-
}
57-
}
58-
if (json != null && json.has("stderr")) {
59-
String string = json.get("stderr").getAsString();
60-
if (string != null && string.length() > 0) {
61-
System.err.println(string);
62-
}
63-
}
64-
65-
if (result != 0) {
66-
return NodeExecutorResultImpl.createFailure(AnsibleFailureReason.AnsibleNonZero, "Ansible exited with non-zero code.", node, result);
67-
}
68-
6952
return NodeExecutorResultImpl.createSuccess(node);
7053
}
7154

src/main/java/com/batix/rundeck/AnsiblePlaybookNodeStep.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,18 @@ public void executeNodeStep(PluginStepContext context, Map<String, Object> confi
4747
vaultPass = "";
4848
}
4949

50-
AnsibleRunner runner = AnsibleRunner.playbook(playbook).limit(entry.getNodename()).extraArgs(extraArgs).vaultPass(vaultPass).sshPass(sshPass).stream();
50+
AnsibleRunner runner = AnsibleRunner.playbook(playbook).limit(entry.getNodename()).extraArgs(extraArgs).vaultPass(vaultPass).sshPass(sshPass);
5151

5252
if (jobConfig.get("loglevel").equals("DEBUG")) {
5353
runner.debug();
5454
}
5555

56-
runner.listener(new AnsibleRunner.Listener() {
57-
@Override
58-
public void output(String line) {
59-
logger.log(Project.MSG_INFO, line);
60-
}
61-
});
62-
63-
int result;
6456
try {
65-
result = runner.run();
57+
runner.run();
58+
} catch (AnsibleStepException e) {
59+
throw new NodeStepException("Error running Ansible Node Step.", e, e.getFailureReason(), entry.getNodename());
6660
} catch (Exception e) {
67-
throw new NodeStepException("Error running Ansible.", e, AnsibleFailureReason.AnsibleError, entry.getNodename());
68-
}
69-
70-
if (result != 0) {
71-
throw new NodeStepException("Ansible exited with non-zero code.", AnsibleFailureReason.AnsibleNonZero, entry.getNodename());
61+
throw new NodeStepException(e.getMessage(), e, AnsibleFailureReason.AnsibleError, entry.getNodename());
7262
}
7363
}
7464

src/main/java/com/batix/rundeck/AnsiblePlaybookWorkflowStep.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,21 @@ public void executeStep(PluginStepContext context, Map<String, Object> configura
4545
vaultPass = "";
4646
}
4747

48-
AnsibleRunner runner = AnsibleRunner.playbook(playbook).limit(context.getNodes()).extraArgs(extraArgs).vaultPass(vaultPass).sshPass(sshPass).stream();
48+
// Configure the ansible runner
49+
AnsibleRunner runner = AnsibleRunner.playbook(playbook).limit(context.getNodes()).extraArgs(extraArgs).vaultPass(vaultPass).sshPass(sshPass);
4950

51+
// Set the logging level
5052
if (jobConfig.get("loglevel").equals("DEBUG")) {
5153
runner.debug();
5254
}
5355

54-
runner.listener(new AnsibleRunner.Listener() {
55-
@Override
56-
public void output(String line) {
57-
logger.log(Project.MSG_INFO, line);
58-
}
59-
});
60-
61-
int result;
56+
// ansible runner will take care of handling exceptions, here handle only jobs specific stuff
6257
try {
63-
result = runner.run();
58+
runner.run();
59+
} catch (AnsibleStepException e) {
60+
throw new StepException(e.getMessage(), e, e.getFailureReason());
6461
} catch (Exception e) {
65-
throw new StepException("Error running Ansible.", e, AnsibleFailureReason.AnsibleError);
66-
}
67-
68-
if (result != 0) {
69-
throw new StepException("Ansible exited with non-zero code.", AnsibleFailureReason.AnsibleNonZero);
62+
throw new StepException(e.getMessage(),e,AnsibleFailureReason.AnsibleError);
7063
}
7164
}
7265

src/main/java/com/batix/rundeck/AnsibleResourceModelSource.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ public INodeSet getNodes() throws ResourceModelSourceException {
6868

6969
try {
7070
int status = runner.run();
71-
if (status != 0) {
72-
System.out.println("error gathering facts:\n" + runner.getOutput());
73-
}
7471
} catch (Exception e) {
7572
throw new ResourceModelSourceException("Error running playbook.", e);
7673
}

0 commit comments

Comments
 (0)