Starting jenkins builds with Gearman workers
Gearman worker can now start jenkins jobs with passed in parameters. The uuid is also passed in as a parameter due to a non-existing gearman-java feature: https://answers.launchpad.net/gearman-java/+question/218865. Change-Id: If04488ec2bfc19ca1b7b3fc94ca3e04154fa55c3
This commit is contained in:
parent
6efb971490
commit
e6d78ad75f
|
@ -31,8 +31,6 @@ import hudson.model.Project;
|
|||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
import jenkins.model.Jenkins;
|
||||
|
||||
import org.gearman.common.Constants;
|
||||
import org.gearman.worker.DefaultGearmanFunctionFactory;
|
||||
import org.gearman.worker.GearmanFunction;
|
||||
|
@ -43,7 +41,6 @@ public class CustomGearmanFunctionFactory extends DefaultGearmanFunctionFactory
|
|||
private final Project<?,?> project;
|
||||
private final Node node;
|
||||
private final String theClass;
|
||||
private final Jenkins jenkins;
|
||||
|
||||
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(
|
||||
Constants.GEARMAN_WORKER_LOGGER_NAME);
|
||||
|
@ -54,7 +51,6 @@ public class CustomGearmanFunctionFactory extends DefaultGearmanFunctionFactory
|
|||
this.theClass = className;
|
||||
this.project = project;
|
||||
this.node = node;
|
||||
jenkins = Jenkins.getInstance();
|
||||
}
|
||||
|
||||
|
||||
|
@ -64,13 +60,15 @@ public class CustomGearmanFunctionFactory extends DefaultGearmanFunctionFactory
|
|||
}
|
||||
|
||||
|
||||
private static GearmanFunction createFunctionInstance(String className, Project project, Node node) {
|
||||
|
||||
|
||||
private static GearmanFunction createFunctionInstance(String className, Project<?,?> project, Node node) {
|
||||
|
||||
GearmanFunction f = null;
|
||||
try {
|
||||
|
||||
Class c = Class.forName(className);
|
||||
Constructor con = c.getConstructor(new Class[]{Project.class, Node.class});
|
||||
Class<?> c = Class.forName(className);
|
||||
Constructor<?> con = c.getConstructor(new Class[]{Project.class, Node.class});
|
||||
Object o = con.newInstance(new Object[] {project, node});
|
||||
|
||||
if (o instanceof GearmanFunction) {
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
*
|
||||
* Copyright 2013 Hewlett-Packard Development Company, L.P.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package hudson.plugins.gearman;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import hudson.model.Label;
|
||||
import hudson.model.ParameterValue;
|
||||
import hudson.model.ParametersAction;
|
||||
import hudson.model.labels.LabelAtom;
|
||||
import hudson.model.queue.SubTask;
|
||||
|
||||
/*
|
||||
* Overview: This class provides the ability to send
|
||||
* a build with parameters to a specific jenkins node
|
||||
*
|
||||
*/
|
||||
|
||||
public class NodeParametersAction extends ParametersAction {
|
||||
|
||||
LabelAtom labelAtom; // node label to assign build to a node
|
||||
|
||||
|
||||
public NodeParametersAction(List<ParameterValue> parameters, String label) {
|
||||
super(parameters);
|
||||
this.labelAtom = new LabelAtom(label);
|
||||
|
||||
}
|
||||
|
||||
public NodeParametersAction(List<ParameterValue> parameters) {
|
||||
super(parameters);
|
||||
}
|
||||
|
||||
public NodeParametersAction(ParameterValue... parameters) {
|
||||
super(parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Label getAssignedLabel(SubTask task) {
|
||||
return labelAtom;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -20,14 +20,14 @@
|
|||
package hudson.plugins.gearman;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import hudson.model.Cause;
|
||||
import hudson.model.Node;
|
||||
import hudson.model.ParameterValue;
|
||||
import hudson.model.Project;
|
||||
import hudson.model.labels.LabelAssignmentAction;
|
||||
|
||||
import hudson.model.StringParameterValue;
|
||||
import org.gearman.client.GearmanJobResult;
|
||||
import org.gearman.client.GearmanJobResultImpl;
|
||||
import org.gearman.worker.AbstractGearmanFunction;
|
||||
|
@ -37,6 +37,14 @@ import org.slf4j.LoggerFactory;
|
|||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
/*
|
||||
* Overview: This is a gearman function that will start jenkins builds
|
||||
* Assumptions: When this function is created it has an associated
|
||||
* node and project. The build will start a jenkins build
|
||||
* on its assigned assigned project and node and pass along
|
||||
* all of the parameters from the client.
|
||||
*/
|
||||
|
||||
public class StartJobWorker extends AbstractGearmanFunction {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
|
@ -50,6 +58,7 @@ public class StartJobWorker extends AbstractGearmanFunction {
|
|||
this.node = node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GearmanJobResult executeFunction() {
|
||||
logger.info("----- Running executeFunction in " + name + " ----");
|
||||
|
||||
|
@ -61,15 +70,46 @@ public class StartJobWorker extends AbstractGearmanFunction {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// convert parameters passed in from client to hash map
|
||||
Gson gson = new Gson();
|
||||
Map<String, String> params = gson.fromJson(decoded,
|
||||
Map<String, String> inParams = gson.fromJson(decoded,
|
||||
new TypeToken<Map<String, String>>() {
|
||||
}.getType());
|
||||
|
||||
UUID buildId = UUID.randomUUID();
|
||||
// send build to node with parameters
|
||||
logger.info("Sending job to " + node.getNodeName()
|
||||
+ " with UUID " + buildId + " and build params " + params);
|
||||
// need to pass on uuid from client.
|
||||
// temporarily passing uuid as a build parameter due to
|
||||
// issue: https://answers.launchpad.net/gearman-java/+question/218865
|
||||
|
||||
/*
|
||||
* make this node build this project with build params from the client
|
||||
*/
|
||||
|
||||
// create new parameter objects to pass to jenkins build
|
||||
List<ParameterValue> buildParams = new ArrayList<ParameterValue>();
|
||||
|
||||
|
||||
// tried to use a RunParameterValue but not sure what
|
||||
// jenkins expects for a "valid id". doing this way
|
||||
// fails with java.lang.IllegalArgumentException: Invalid id
|
||||
// buildParams.add(new RunParameterValue("uuid", "16"));
|
||||
|
||||
String uuid = null;
|
||||
// create the build parameters that were passed in from client
|
||||
for (Map.Entry<String, String> entry : inParams.entrySet()) {
|
||||
buildParams.add(new StringParameterValue(entry.getKey(), entry.getValue()));
|
||||
// get the build id for debugging
|
||||
if (entry.getKey().equals("uuid")) {
|
||||
uuid = entry.getValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// schedule the jenkins build
|
||||
logger.info("Scheduling build on " + node.getNodeName()
|
||||
+ " with UUID " + uuid + " and build params " + inParams);
|
||||
|
||||
project.scheduleBuild2(0, new Cause.UserIdCause(), new NodeParametersAction(buildParams, node.getNodeName()));
|
||||
|
||||
|
||||
GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle, true,
|
||||
decoded.toString().getBytes(), new byte[0], new byte[0], 0, 0);
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
package hudson.plugins.gearman;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gearman.client.GearmanJobResult;
|
||||
import org.gearman.client.GearmanJobResultImpl;
|
||||
|
@ -36,9 +35,9 @@ public class StopJobWorker extends AbstractGearmanFunction {
|
|||
public GearmanJobResult executeFunction() {
|
||||
|
||||
String decoded = null;
|
||||
UUID buildId = UUID.randomUUID();
|
||||
|
||||
logger.info("---- Running executeFunction in " + this.getName()+ " -------");
|
||||
logger.info("---- Running executeFunction in " + this.getName()
|
||||
+ " -------");
|
||||
|
||||
try {
|
||||
decoded = new String((byte[]) this.data, "UTF-8");
|
||||
|
@ -47,7 +46,6 @@ public class StopJobWorker extends AbstractGearmanFunction {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
logger.info("---- Aborting Jenkins build " + buildId + " -------");
|
||||
|
||||
GearmanJobResult gjr = new GearmanJobResultImpl(this.jobHandle, true,
|
||||
decoded.toString().getBytes(), new byte[0], new byte[0], 0, 0);
|
||||
|
|
Loading…
Reference in New Issue