Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JENKINS-43330
  • Loading branch information
liozN committed Jul 26, 2017
1 parent cea16bb commit d667ea0
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 3 deletions.
Expand Up @@ -5,6 +5,8 @@
import hudson.model.*;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Logger;

Expand All @@ -23,7 +25,7 @@ public Descriptor<ConsulOperation> getDescriptor() {
}
}

public abstract boolean perform(Run build, Launcher launcher, TaskListener listener);
public abstract boolean perform(Run build, Launcher launcher, TaskListener listener) throws IOException, InterruptedException;

public abstract String getOperationName();

Expand Down
Expand Up @@ -94,6 +94,9 @@ protected String run() throws Exception {
if ( consulAgentProcess != null) {
for (ConsulOperation operation : step.operationList){
operation.perform(run, launcher, taskListener);
if (operation.getOperationName().equals("SetKeyValueStore")){
continue;
}
if (jsonObject.has(operation.getOperationName())){
jsonObject.getJSONObject(operation.getOperationName()).put(operation.getVariableName(), operation.getResponse().get(((ConsulServiceDiscoveryOperation) operation).getServiceName()));
} else {
Expand Down
@@ -0,0 +1,78 @@
package com.inneractive.jenkins.plugins.consul.operations;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
import com.inneractive.jenkins.plugins.consul.ConsulOperation;
import com.inneractive.jenkins.plugins.consul.ConsulOperationDescriptor;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.FormValidation;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import java.io.IOException;

/**
* Created by lioz on 7/26/17.
*/
public class ConsulSetKV extends ConsulOperation {

private final String valuePath;
private final String value;

@DataBoundConstructor
public ConsulSetKV(String valuePath, String value) {
this.valuePath = valuePath;
this.value = value;
}

public String getValuePath() {
return valuePath;
}

public String getValue() {
return value;
}

@Override
public boolean perform(Run build, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
String finalValue = build.getEnvironment(listener).get(this.value);
if (finalValue == null || finalValue.isEmpty()){
finalValue = value;
}
Response<Boolean> kvResponse = new ConsulClient("localhost").setKVValue(valuePath, value);
return kvResponse.getValue();
}

@Override
public String getOperationName() {
return "SetKeyValueStore";
}

@Override
public String getVariableName() {
return value;
}

@Extension(optional = true)
public static class DescriptorImpl extends ConsulOperationDescriptor {
@Override
public String getDisplayName() {
return "Set a value in K/V store";
}

public FormValidation doCheckValuePath(@QueryParameter String value) {
if (value.isEmpty())
return FormValidation.error("Path is a mandatory field");
return FormValidation.ok();
}

public FormValidation doCheckValue(@QueryParameter String value) {
if (value.isEmpty())
return FormValidation.error("Environment variable name is a mandatory field");
return FormValidation.ok();
}
}
}
Expand Up @@ -3,7 +3,7 @@
<f:entry title="K/V path" field="valuePath">
<f:textbox />
</f:entry>
<f:entry title="Environment variable name" field="environmentVariableName">
<f:entry title="Environment variable name" field="value">
<f:textbox />
</f:entry>
</j:jelly>
Expand Down
Expand Up @@ -17,7 +17,7 @@
<f:entry title="Service tag" field="serviceTag">
<f:textbox/>
</f:entry>
<f:entry title="Environment variable name" field="environmentVariableName">
<f:entry title="Environment variable name" field="value">
<f:textbox/>
</f:entry>

Expand Down
@@ -0,0 +1,12 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="K/V path" field="valuePath">
<f:textbox />
</f:entry>
<f:entry title="value" field="value">
<f:textbox />
</f:entry>
</j:jelly>



@@ -0,0 +1,3 @@
<div>
Value or ENV var name to use.
</div>
@@ -0,0 +1,4 @@
<div>
Consul path to set with your value.<BR/>
e.g. mysql/config/users/admin
</div>

0 comments on commit d667ea0

Please sign in to comment.