Skip to content

Commit

Permalink
add plugin group
Browse files Browse the repository at this point in the history
  • Loading branch information
ltamaster committed Mar 28, 2024
1 parent 366b40f commit 8a73d5a
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 14 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ ext.pluginClassNames = [
'com.rundeck.plugins.ansible.plugin.AnsibleModuleWorkflowStep',
'com.rundeck.plugins.ansible.plugin.AnsiblePlaybookWorflowNodeStep',
'com.rundeck.plugins.ansible.plugin.AnsiblePlaybookInlineWorkflowNodeStep',
'com.rundeck.plugins.ansible.logging.AnsibleSetStatsFilterPlugin'
'com.rundeck.plugins.ansible.logging.AnsibleSetStatsFilterPlugin',
'com.rundeck.plugins.ansible.plugin.AnsiblePluginGroup'
].join(',')

apply plugin: 'java'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;

import com.rundeck.plugins.ansible.plugin.AnsiblePluginGroup;
import lombok.Getter;
import org.rundeck.storage.api.Path;

Expand All @@ -39,6 +40,8 @@ public class AnsibleRunnerContextBuilder {
private final Collection<INodeEntry> nodes;
private final Collection<File> tempFiles;

private AnsiblePluginGroup pluginGroup;


public AnsibleRunnerContextBuilder(final ExecutionContext context, final Framework framework, INodeSet nodes, final Map<String, Object> configuration) {
this.context = context;
Expand All @@ -58,6 +61,16 @@ public AnsibleRunnerContextBuilder(final INodeEntry node, final ExecutionContext
this.tempFiles = new LinkedList<>();
}

public AnsibleRunnerContextBuilder(final ExecutionContext context, final Framework framework, INodeSet nodes, final Map<String, Object> configuration, final AnsiblePluginGroup pluginGroup) {
this.context = context;
this.framework = framework;
this.frameworkProject = context.getFrameworkProject();
this.jobConf = configuration;
this.nodes = nodes.getNodes();
this.tempFiles = new LinkedList<>();
this.pluginGroup = pluginGroup;
}

private byte[] loadStoragePathData(final String passwordStoragePath) throws IOException {
if (null == passwordStoragePath) {
return null;
Expand Down Expand Up @@ -667,7 +680,7 @@ public String getLimit() throws ConfigurationException {

public String getConfigFile() {

final String configFile;
String configFile;
configFile = PropertyResolver.resolveProperty(
AnsibleDescribable.ANSIBLE_CONFIG_FILE_PATH,
null,
Expand All @@ -680,6 +693,18 @@ public String getConfigFile() {
if (null != configFile && configFile.contains("${")) {
return DataContextUtils.replaceDataReferencesInString(configFile, getContext().getDataContext());
}

if(null == configFile || configFile.isEmpty()) {
if (this.pluginGroup != null && this.pluginGroup.getAnsibleConfigFilePath() != null && !this.pluginGroup.getAnsibleConfigFilePath().isEmpty()) {
this.context.getExecutionLogger().log(
4, "plugin group set getAnsibleConfigFilePath: " + this.pluginGroup.getAnsibleConfigFilePath()
);

configFile = this.pluginGroup.getAnsibleConfigFilePath();
}
}


return configFile;
}

Expand All @@ -705,11 +730,21 @@ public String getBinariesFilePath() {
getNode(),
getJobConf()
);
if (null != binariesFilePathStr) {
if (binariesFilePathStr.contains("${")) {
return DataContextUtils.replaceDataReferencesInString(binariesFilePathStr, getContext().getDataContext());

if (null != binariesFilePathStr && binariesFilePathStr.contains("${")) {
return DataContextUtils.replaceDataReferencesInString(binariesFilePathStr, getContext().getDataContext());
}

if(null == binariesFilePathStr || binariesFilePathStr.isEmpty()){
if(this.pluginGroup!=null && this.pluginGroup.getAnsibleBinariesDirPath()!= null && !this.pluginGroup.getAnsibleBinariesDirPath().isEmpty()){
this.context.getExecutionLogger().log(
4, "plugin group set getAnsibleBinariesDirPath: "+this.pluginGroup.getAnsibleBinariesDirPath()
);
binariesFilePathStr = this.pluginGroup.getAnsibleBinariesDirPath();
}
}


return binariesFilePathStr;
}

Expand Down Expand Up @@ -808,14 +843,25 @@ public String getPassphraseStorageData(String storagePath) throws ConfigurationE
}

public boolean encryptExtraVars() throws ConfigurationException {
return PropertyResolver.resolveBooleanProperty(
boolean encryptExtraVars = PropertyResolver.resolveBooleanProperty(
AnsibleDescribable.ANSIBLE_ENCRYPT_EXTRA_VARS,
false,
getFrameworkProject(),
getFramework(),
getNode(),
getJobConf()
);

if(!encryptExtraVars){
if(this.pluginGroup!=null && this.pluginGroup.getEncryptExtraVars()!= null && this.pluginGroup.getEncryptExtraVars()){
this.context.getExecutionLogger().log(
4, "plugin group set getEncryptExtraVars: "+this.pluginGroup.getEncryptExtraVars()
);
encryptExtraVars = this.pluginGroup.getEncryptExtraVars();
}
}

return encryptExtraVars;
}

public Map<String,String> getListOptions(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dtolabs.rundeck.core.execution.ExecutionContext;
import com.dtolabs.rundeck.core.execution.proxy.ProxyRunnerPlugin;
import com.dtolabs.rundeck.plugins.config.ConfiguredBy;
import com.rundeck.plugins.ansible.ansible.AnsibleDescribable;
import com.rundeck.plugins.ansible.ansible.AnsibleException;
import com.rundeck.plugins.ansible.ansible.AnsibleRunner;
Expand All @@ -22,14 +23,17 @@
import java.util.Map;

@Plugin(name = AnsiblePlaybookInlineWorkflowNodeStep.SERVICE_PROVIDER_NAME, service = ServiceNameConstants.WorkflowNodeStep)
public class AnsiblePlaybookInlineWorkflowNodeStep implements NodeStepPlugin, AnsibleDescribable, ProxyRunnerPlugin {
public class AnsiblePlaybookInlineWorkflowNodeStep implements NodeStepPlugin, AnsibleDescribable, ProxyRunnerPlugin, ConfiguredBy<AnsiblePluginGroup> {

private AnsiblePluginGroup pluginGroup;

public static final String SERVICE_PROVIDER_NAME = "com.batix.rundeck.plugins.AnsiblePlaybookInlineWorkflowNodeStep";

public static Description DESC = null;

static {
DescriptionBuilder builder = DescriptionBuilder.builder();
builder.pluginGroup(AnsiblePluginGroup.class);
builder.name(SERVICE_PROVIDER_NAME);
builder.title("Ansible Playbook Inline Workflow Node Step");
builder.description("Runs an Inline Ansible Playbook.");
Expand Down Expand Up @@ -81,7 +85,11 @@ public void executeNodeStep(
configuration.put(AnsibleDescribable.ANSIBLE_DEBUG,"False");
}

AnsibleRunnerContextBuilder contextBuilder = new AnsibleRunnerContextBuilder(context.getExecutionContext(), context.getFramework(), context.getNodes(), configuration);
AnsibleRunnerContextBuilder contextBuilder = new AnsibleRunnerContextBuilder(context.getExecutionContext(),
context.getFramework(),
context.getNodes(),
configuration,
pluginGroup);

try {
runner = AnsibleRunner.buildAnsibleRunner(contextBuilder);
Expand Down Expand Up @@ -124,4 +132,9 @@ public Map<String, String> getRuntimeProperties(ExecutionContext context) {
public Map<String, String> getRuntimeFrameworkProperties(ExecutionContext context) {
return AnsibleUtil.getRuntimeProperties(context, AnsibleDescribable.FWK_PROP_PREFIX);
}

@Override
public void setPluginGroup(AnsiblePluginGroup ansiblePluginGroup) {
this.pluginGroup = ansiblePluginGroup;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dtolabs.rundeck.core.execution.ExecutionContext;
import com.dtolabs.rundeck.core.execution.proxy.ProxyRunnerPlugin;
import com.dtolabs.rundeck.plugins.config.ConfiguredBy;
import com.rundeck.plugins.ansible.ansible.AnsibleDescribable;
import com.rundeck.plugins.ansible.ansible.AnsibleException;
import com.rundeck.plugins.ansible.ansible.AnsibleRunner;
Expand All @@ -22,14 +23,17 @@
import java.util.Objects;

@Plugin(name = AnsiblePlaybookInlineWorkflowStep.SERVICE_PROVIDER_NAME, service = ServiceNameConstants.WorkflowStep)
public class AnsiblePlaybookInlineWorkflowStep implements StepPlugin, AnsibleDescribable, ProxyRunnerPlugin {
public class AnsiblePlaybookInlineWorkflowStep implements StepPlugin, AnsibleDescribable, ProxyRunnerPlugin, ConfiguredBy<AnsiblePluginGroup> {

public static final String SERVICE_PROVIDER_NAME = "com.batix.rundeck.plugins.AnsiblePlaybookInlineWorkflowStep";

public static Description DESC = null;

private AnsiblePluginGroup pluginGroup;

static {
DescriptionBuilder builder = DescriptionBuilder.builder();
builder.pluginGroup(AnsiblePluginGroup.class);
builder.name(SERVICE_PROVIDER_NAME);
builder.title("Ansible Playbook Inline");
builder.description("Runs an Inline Ansible Playbook.");
Expand Down Expand Up @@ -83,7 +87,11 @@ public void executeStep(PluginStepContext context, Map<String, Object> configura
configuration.put(AnsibleDescribable.ANSIBLE_DEBUG, "False");
}

AnsibleRunnerContextBuilder contextBuilder = new AnsibleRunnerContextBuilder(context.getExecutionContext(), context.getFramework(), context.getNodes(), configuration);
AnsibleRunnerContextBuilder contextBuilder = new AnsibleRunnerContextBuilder(context.getExecutionContext(),
context.getFramework(),
context.getNodes(),
configuration,
pluginGroup);

try {
runner = AnsibleRunner.buildAnsibleRunner(contextBuilder);
Expand Down Expand Up @@ -135,4 +143,9 @@ public Map<String, String> getRuntimeProperties(ExecutionContext context) {
public Map<String, String> getRuntimeFrameworkProperties(ExecutionContext context) {
return AnsibleUtil.getRuntimeProperties(context, AnsibleDescribable.FWK_PROP_PREFIX);
}

@Override
public void setPluginGroup(AnsiblePluginGroup ansiblePluginGroup) {
this.pluginGroup = ansiblePluginGroup;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dtolabs.rundeck.core.execution.ExecutionContext;
import com.dtolabs.rundeck.core.execution.proxy.ProxyRunnerPlugin;
import com.dtolabs.rundeck.plugins.config.ConfiguredBy;
import com.rundeck.plugins.ansible.ansible.AnsibleDescribable;
import com.rundeck.plugins.ansible.ansible.AnsibleException;
import com.rundeck.plugins.ansible.ansible.AnsibleRunner;
Expand All @@ -21,14 +22,17 @@
import java.util.Map;

@Plugin(name = AnsiblePlaybookWorflowNodeStep.SERVICE_PROVIDER_NAME, service = ServiceNameConstants.WorkflowNodeStep)
public class AnsiblePlaybookWorflowNodeStep implements NodeStepPlugin, AnsibleDescribable, ProxyRunnerPlugin {
public class AnsiblePlaybookWorflowNodeStep implements NodeStepPlugin, AnsibleDescribable, ProxyRunnerPlugin, ConfiguredBy<AnsiblePluginGroup> {

public static final String SERVICE_PROVIDER_NAME = "com.batix.rundeck.plugins.AnsiblePlaybookWorflowNodeStep";

public static Description DESC = null;

private AnsiblePluginGroup pluginGroup;

static {
DescriptionBuilder builder = DescriptionBuilder.builder();
builder.pluginGroup(AnsiblePluginGroup.class);
builder.name(SERVICE_PROVIDER_NAME);
builder.title("Ansible Playbook Workflow Node Step.");
builder.description("Runs an Ansible Playbook");
Expand Down Expand Up @@ -79,7 +83,11 @@ public void executeNodeStep(
}

AnsibleRunnerContextBuilder
contextBuilder = new AnsibleRunnerContextBuilder(context.getExecutionContext(), context.getFramework(), context.getNodes(), configuration);
contextBuilder = new AnsibleRunnerContextBuilder(context.getExecutionContext(),
context.getFramework(),
context.getNodes(),
configuration,
pluginGroup);

try {
runner = AnsibleRunner.buildAnsibleRunner(contextBuilder);
Expand Down Expand Up @@ -114,4 +122,9 @@ public Map<String, String> getRuntimeProperties(ExecutionContext context) {
public Map<String, String> getRuntimeFrameworkProperties(ExecutionContext context) {
return AnsibleUtil.getRuntimeProperties(context, AnsibleDescribable.FWK_PROP_PREFIX);
}

@Override
public void setPluginGroup(AnsiblePluginGroup ansiblePluginGroup) {
this.pluginGroup = ansiblePluginGroup;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dtolabs.rundeck.core.execution.ExecutionContext;
import com.dtolabs.rundeck.core.execution.proxy.ProxyRunnerPlugin;
import com.dtolabs.rundeck.plugins.config.ConfiguredBy;
import com.rundeck.plugins.ansible.ansible.AnsibleDescribable;
import com.rundeck.plugins.ansible.ansible.AnsibleException;
import com.rundeck.plugins.ansible.ansible.AnsibleRunner;
Expand All @@ -21,14 +22,17 @@
import java.util.Map;

@Plugin(name = AnsiblePlaybookWorkflowStep.SERVICE_PROVIDER_NAME, service = ServiceNameConstants.WorkflowStep)
public class AnsiblePlaybookWorkflowStep implements StepPlugin, AnsibleDescribable, ProxyRunnerPlugin {
public class AnsiblePlaybookWorkflowStep implements StepPlugin, AnsibleDescribable, ProxyRunnerPlugin, ConfiguredBy<AnsiblePluginGroup> {

public static final String SERVICE_PROVIDER_NAME = "com.batix.rundeck.plugins.AnsiblePlaybookWorkflowStep";

public static Description DESC = null;

private AnsiblePluginGroup pluginGroup;

static {
DescriptionBuilder builder = DescriptionBuilder.builder();
builder.pluginGroup(AnsiblePluginGroup.class);
builder.name(SERVICE_PROVIDER_NAME);
builder.title("Ansible Playbook");
builder.description("Runs an Ansible Playbook.");
Expand Down Expand Up @@ -82,7 +86,11 @@ public void executeStep(PluginStepContext context, Map<String, Object> configura
configuration.put(AnsibleDescribable.ANSIBLE_DEBUG, "False");
}

AnsibleRunnerContextBuilder contextBuilder = new AnsibleRunnerContextBuilder(context.getExecutionContext(), context.getFramework(), context.getNodes(), configuration);
AnsibleRunnerContextBuilder contextBuilder = new AnsibleRunnerContextBuilder(context.getExecutionContext(),
context.getFramework(),
context.getNodes(),
configuration,
pluginGroup);

try {
runner = AnsibleRunner.buildAnsibleRunner(contextBuilder);
Expand Down Expand Up @@ -129,4 +137,9 @@ public Map<String, String> getRuntimeProperties(ExecutionContext context) {
public Map<String, String> getRuntimeFrameworkProperties(ExecutionContext context) {
return AnsibleUtil.getRuntimeProperties(context, AnsibleDescribable.FWK_PROP_PREFIX);
}

@Override
public void setPluginGroup(AnsiblePluginGroup ansiblePluginGroup) {
this.pluginGroup = ansiblePluginGroup;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.rundeck.plugins.ansible.plugin;

import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.core.plugins.configuration.Describable;
import com.dtolabs.rundeck.core.plugins.configuration.Description;
import com.dtolabs.rundeck.core.plugins.configuration.PluginAdapterUtility;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.config.PluginGroup;
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription;
import com.dtolabs.rundeck.plugins.descriptions.PluginProperty;
import com.dtolabs.rundeck.plugins.util.DescriptionBuilder;


@Plugin(service = ServiceNameConstants.PluginGroup, name = "Ansible Project Configuration")
@PluginDescription(title = "Ansible", description = "Plugin basic ansible configurations")
public class AnsiblePluginGroup implements PluginGroup, Describable {

public String getAnsibleConfigFilePath() {
return ansibleConfigFilePath;
}

public void setAnsibleConfigFilePath(String ansibleConfigFilePath) {
this.ansibleConfigFilePath = ansibleConfigFilePath;
}

public String getAnsibleBinariesDirPath() {
return ansibleBinariesDirPath;
}

public void setAnsibleBinariesDirPath(String ansibleBinariesDirPath) {
this.ansibleBinariesDirPath = ansibleBinariesDirPath;
}

public Boolean getEncryptExtraVars() {
return encryptExtraVars;
}

public void setEncryptExtraVars(Boolean encryptExtraVars) {
this.encryptExtraVars = encryptExtraVars;
}

@PluginProperty(
title = "Ansible config file path",
description = "Set ansible config file path."
)
//@RenderingOptions({
// @RenderingOption(key = StringRenderingConstants.GROUP_NAME, value = "Authentication")
//})
String ansibleConfigFilePath;

@PluginProperty(
title = "Ansible binaries directory path",
description = "Set ansible binaries directory path."
)
//@RenderingOptions({
// @RenderingOption(key = StringRenderingConstants.GROUP_NAME, value = "Authentication")
//})
String ansibleBinariesDirPath;

@PluginProperty(
title = "Encrypt Extra Vars.",
description = "Encrypt the value of the extra vars keys."
)
//@RenderingOptions({
// @RenderingOption(key = StringRenderingConstants.GROUP_NAME, value = "Authentication")
//})
Boolean encryptExtraVars;

@Override
public Description getDescription() {
DescriptionBuilder builder = DescriptionBuilder.builder();
Description description = PluginAdapterUtility.buildDescription(this, builder);
return builder.build();
}
}

0 comments on commit 8a73d5a

Please sign in to comment.