Skip to content
Permalink
Browse files

[JENKINS-44599] Support master-slave architecture

  • Loading branch information...
rachaumi
rachaumi committed Jun 23, 2017
1 parent 38b943f commit 8ff1f8782eaa733328034827697023c1271320cd
@@ -14,7 +14,6 @@
import hudson.model.BuildListener;
import hudson.model.Cause;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.model.JDK;
import hudson.model.Job;
import hudson.model.Result;
@@ -131,10 +130,10 @@ public WeblogicDeploymentPlugin() {
super();
}

@Initializer
public static void load(){
JdkToolService.loadJdkToolAvailables();
}
// @Initializer
// public static void load(){
// JdkToolService.loadJdkToolAvailables();
// }

/**
* Invoque lors de la mise a jour des configurations des projets ayant active le plugin et de la mise a jour globale
@@ -346,7 +345,7 @@ private WebLogicPreRequisteStatus checkPreRequisites(AbstractBuild<?, ?> build,
if(StringUtils.isNotBlank(deployedProjectsDependencies)){
String[] listeDependances = StringUtils.split(StringUtils.trim(deployedProjectsDependencies), ',');
for(int i = 0; i<listeDependances.length; i++){
TopLevelItem item = Hudson.getInstance().getItem(listeDependances[i]);
TopLevelItem item = Jenkins.getInstance().getItem(listeDependances[i]);
if(item instanceof Job){
WatchingWeblogicDeploymentAction deploymentAction = ((Job<?,?>) item).getLastBuild().getAction(WatchingWeblogicDeploymentAction.class);
listener.getLogger().println("[WeblogicDeploymentPlugin] - Satisfying dependencies project involved: " + item.getName());
@@ -727,31 +726,31 @@ public FormValidation doCheckJavaOpts(@QueryParameter String value) throws IOExc
return FormValidation.ok();
}

/**
* This method provides auto-completion items for the 'jdkName' field.
* Stapler finds this method via the naming convention.
*
* @param value
* The text that the user entered.
*/
public AutoCompletionCandidates doAutoCompleteJdkName(@QueryParameter String value) {
AutoCompletionCandidates c = new AutoCompletionCandidates();
for (JDK jdk : JdkToolService.getJdkToolAvailables()) {
if (jdk.getName().contains(value.toLowerCase())) {
c.add(jdk.getName());
}
}
return c;
}

@JavaScriptMethod
public String completeJdkHome(String jdkName) {
JDK jdk = JdkToolService.getJDKByName(jdkName);
if(jdk != null){
return jdk.getHome();
}
return "";
}
// /**
// * This method provides auto-completion items for the 'jdkName' field.
// * Stapler finds this method via the naming convention.
// *
// * @param value
// * The text that the user entered.
// */
// public AutoCompletionCandidates doAutoCompleteJdkName(@QueryParameter String value) {
// AutoCompletionCandidates c = new AutoCompletionCandidates();
// for (JDK jdk : JdkToolService.getJdkToolAvailables()) {
// if (jdk.getName().contains(value.toLowerCase())) {
// c.add(jdk.getName());
// }
// }
// return c;
// }
//
// @JavaScriptMethod
// public String completeJdkHome(String jdkName) {
// JDK jdk = JdkToolService.getJDKByName(jdkName);
// if(jdk != null){
// return jdk.getHome();
// }
// return "";
// }

/**
*
@@ -6,6 +6,7 @@
import java.io.File;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.Run.RunnerAbortedException;
import hudson.util.ArgumentListBuilder;

@@ -146,9 +147,10 @@ private static void processJavaLauncher(WebLogicDeployerParameters parameter, Ar
parameter.getListener().error("[WeblogicDeploymentPlugin] - No JDK selected to deploy artifact.");
throw new RunnerAbortedException();
}
args.add(parameter.getUsedJdk().getBinDir().getAbsolutePath().concat("/java"));
// args.add(parameter.getUsedJdk().getBinDir().getAbsolutePath().concat(File.separator).concat("java"));


// On prend le path de l'exe sur le remote
args.add(new FilePath(parameter.getBuild().getBuiltOn().getChannel(), parameter.getUsedJdk().getHome().concat("/bin/java")).getRemote());

//java options specifique
if(StringUtils.isNotBlank(parameter.getJavaOpts())){
//On parse l'ensemble des options et on les rajoute des le args[]
@@ -164,6 +166,8 @@ private static void processJavaLauncher(WebLogicDeployerParameters parameter, Ar
parameter.getListener().error("[WeblogicDeploymentPlugin] - Classpath is not set. Please configure correctly the plugin.");
throw new RunnerAbortedException();
}
// TODO : On devrait copier les elements aux classpath dans le workspace et pointer directement dessus pour eviter les pb de chemins erronés en architecture master-slave
// TODO : On doit verifier que les librairies sont bien presentes si on les copie pas et remonter l'information de facon claire pour aider à l'analyse.
String remotingJar = parameter.getClasspath();
args.add(remotingJar);
args.add(WebLogicDeploymentPluginConstantes.WL_WEBLOGIC_API_DEPLOYER_MAIN_CLASS);
@@ -5,10 +5,12 @@

import java.io.File;

import hudson.FilePath;
import hudson.Launcher;
import hudson.model.TaskListener;
import hudson.model.Hudson;
import hudson.model.JDK;
import hudson.model.Node;
import hudson.util.NullStream;
import hudson.util.StreamTaskListener;

import java.io.ByteArrayOutputStream;
@@ -21,7 +23,6 @@

import jenkins.model.Jenkins;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;

@@ -38,10 +39,13 @@
public static final String JAVA_VERSION_COMMAND_VERSION_LINE_REGEX = ".*\\r*\\n*(java version )(\")(.+)(\").*\\r*\\n*.*\\r*\\n*.*\\r*\\n*";


static List<JDK> jdkToolAvailables = new ArrayList<JDK>();
// static List<JDK> jdkToolAvailables = new ArrayList<JDK>();

public static void loadJdkToolAvailables() {
public static List<JDK> getJdkToolAvailables() throws IOException, InterruptedException {

List<JDK> jdkToolAvailables = new ArrayList<JDK>();

// TODO Concerne uniquement le master : a refactorer ....
//Ajout de la java home system
if (StringUtils.isNotBlank(SystemUtils.JAVA_HOME)){
jdkToolAvailables.add(new JDK(SYSTEM_JDK, System.getProperty("java.home")));
@@ -51,42 +55,41 @@ public static void loadJdkToolAvailables() {
jdkToolAvailables.add(new JDK(EXTERNAL_ENV_JDK, System.getenv("JAVA_HOME")));
}
// Ajout de tous les jdk declares dans Jenkins
jdkToolAvailables.addAll(Jenkins.getInstance().getJDKs());
// jdkToolAvailables.addAll(Jenkins.getInstance().getJDKs());

}

/**
*
* @return
*/
public static List<JDK> getJdkToolAvailables() {
if(CollectionUtils.isEmpty(jdkToolAvailables)){
loadJdkToolAvailables();
//Ajout des outils JDK declare sur le master (configuration globale -> Emplacement des outils)
for(JDK jdk : Jenkins.getInstance().getJDKs()){
// Le getNode ne cherche que dans les slaves pas le master libelle (master)
// Node masterNode = Jenkins.getInstance().getNode(nodeName);
Node masterNode = Jenkins.getInstance();
JDK jdkToolOnMasterNode = jdk.forNode(masterNode, new StreamTaskListener(new NullStream()));
jdkToolAvailables.add(jdkToolOnMasterNode);
}
return jdkToolAvailables;
}

public static JDK getJDKByName(String name) {
public static JDK getJDKByName(Node node, String name) throws IOException, InterruptedException {
JDK out = null;
for(JDK jdk : getJdkToolAvailables()) {
if(name.equalsIgnoreCase(jdk.getName())){
out = jdk;
// Recuperer le jdk declare au niveau du node correspondant
return jdk.forNode(node, new StreamTaskListener(new NullStream()));
}
}
return out;
}

public static String getJDKHomeByName(String name) {
String out = null;
for(JDK jdk : getJdkToolAvailables()) {
if(jdk.getName().equalsIgnoreCase(name)){
out = new File(jdk.getHome()).getAbsolutePath();
}
}
return out;
}
// public static String getJDKHomeByName(String name) {
// String out = null;
// for(JDK jdk : getJdkToolAvailables()) {
// if(jdk.getName().equalsIgnoreCase(name)){
// out = new File(jdk.getHome()).getAbsolutePath();
// }
// }
// return out;
// }

public static boolean checkJdkVersion(JDK jdk, PrintStream logger){
public static boolean checkJdkVersion(Node node, JDK jdk, PrintStream logger){

if(jdk == null || ! jdk.getExists()){
return false;
@@ -95,15 +98,17 @@ public static boolean checkJdkVersion(JDK jdk, PrintStream logger){
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
TaskListener listener = new StreamTaskListener(out);
Launcher launcher = Hudson.getInstance().createLauncher(listener);
String cmd = jdk.getBinDir().getAbsolutePath().concat("/java");
Launcher launcher = node.createLauncher(listener);
String cmd = new FilePath(node.getChannel(), jdk.getHome().concat("/bin/java")).getRemote();
// int result = launcher.launch().cmds(cmd,"-fullversion").stdout(out).join();
int result = launcher.launch().cmds(cmd,"-version").stdout(out).join();
// L'executable n'existe pas
if(result != 0){
logger.println("[WeblogicDeploymentPlugin] - Unable to detect JDK version");
return false;
}

// TODO ne semble pas fonctionner a distance
Pattern pattern = Pattern.compile(JAVA_VERSION_COMMAND_VERSION_LINE_REGEX);
Matcher matcher = pattern.matcher(out.toString());
if(matcher.matches()){
@@ -122,4 +127,24 @@ public static boolean checkJdkVersion(JDK jdk, PrintStream logger){

return true;
}

/**
* Checks if JDK exists on the given node.
*
* <p>
* If it's not, then the user must specify a configured JDK,
* so this is often useful for form field validation.
*/
public static boolean isJDKValid(Node node, JDK jdk) {
try {
TaskListener listener = new StreamTaskListener(new NullStream());
Launcher launcher = node.createLauncher(listener);
String cmd = new FilePath(node.getChannel(), jdk.getHome().concat("/bin/java")).getRemote();
return launcher.launch().cmds(cmd,"-fullversion").stdout(listener).join()==0;
} catch (IOException e) {
return false;
} catch (InterruptedException e) {
return false;
}
}
}
@@ -3,16 +3,6 @@
*/
package org.jenkinsci.plugins.deploy.weblogic.task;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Proc;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.JDK;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -25,14 +15,14 @@
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.deploy.weblogic.ArtifactSelector;
import org.jenkinsci.plugins.deploy.weblogic.FreeStyleJobArtifactSelectorImpl;
import org.jenkinsci.plugins.deploy.weblogic.WeblogicDeploymentPluginLog;
import org.jenkinsci.plugins.deploy.weblogic.WeblogicDeploymentPlugin.WeblogicDeploymentPluginDescriptor;
import org.jenkinsci.plugins.deploy.weblogic.WeblogicDeploymentPluginLog;
import org.jenkinsci.plugins.deploy.weblogic.data.DeploymentTask;
import org.jenkinsci.plugins.deploy.weblogic.data.DeploymentTaskResult;
import org.jenkinsci.plugins.deploy.weblogic.data.TransfertConfiguration;
import org.jenkinsci.plugins.deploy.weblogic.data.WebLogicDeploymentStatus;
import org.jenkinsci.plugins.deploy.weblogic.data.WebLogicPreRequisteStatus;
import org.jenkinsci.plugins.deploy.weblogic.data.WeblogicEnvironment;
import org.jenkinsci.plugins.deploy.weblogic.data.DeploymentTask;
import org.jenkinsci.plugins.deploy.weblogic.deployer.WebLogicCommand;
import org.jenkinsci.plugins.deploy.weblogic.deployer.WebLogicDeployer;
import org.jenkinsci.plugins.deploy.weblogic.deployer.WebLogicDeployerParameters;
@@ -42,11 +32,22 @@
import org.jenkinsci.plugins.deploy.weblogic.jdk.JdkToolService;
import org.jenkinsci.plugins.deploy.weblogic.properties.WebLogicDeploymentPluginConstantes;
import org.jenkinsci.plugins.deploy.weblogic.util.FTPUtils;
import org.jenkinsci.plugins.deploy.weblogic.util.VarUtils;
import org.jenkinsci.plugins.deploy.weblogic.util.ParameterValueResolver;
import org.jenkinsci.plugins.deploy.weblogic.util.VarUtils;

import com.google.inject.Inject;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Proc;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.JDK;
import hudson.model.Node;

/**
* @author Raphael
*
@@ -84,25 +85,33 @@ public DeploymentTaskResult perform(DeploymentTask task, String globalJdk, Abstr
// Recuperation du JDK
// The default JDK
JDK selectedJdk = null;
Node node = build.getBuiltOn();
try {

if(task.getJdk() != null && task.getJdk().getExists()){
selectedJdk = task.getJdk();
} else {
selectedJdk = JdkToolService.getJDKByName(globalJdk);
}
listener.getLogger().println("[WeblogicDeploymentPlugin] - Loading JDK '"+globalJdk+"' ...");
// TODO potentiellement le JDK a pu etre supprime pourtant on garde la conf et le node aussi
selectedJdk = JdkToolService.getJDKByName(node, globalJdk);

if(selectedJdk == null){
throw new RequiredJDKNotFoundException("No JDK '"+globalJdk+"' found on node "+node.getNodeName()+"("+node.getLabelString()+") .");
}

// Check exists
if(selectedJdk == null || ! selectedJdk.getExists()){
String execu = selectedJdk != null ? selectedJdk.getHome(): "";
throw new RequiredJDKNotFoundException("Unable to find PATH to the JDK's executable ["+execu+"]");
listener.getLogger().println("[WeblogicDeploymentPlugin] - Checking if JDK '"+globalJdk+"' exists on node "+node.getNodeName()+"("+node.getLabelString()+") ...");
if(! JdkToolService.isJDKValid(node, selectedJdk)){
throw new RequiredJDKNotFoundException("Unable to find the JDK's executable ["+selectedJdk.getName()+", exec: "+new FilePath(node.getChannel(), selectedJdk.getHome().concat("/bin/java")).getRemote()+"] on node : "+node.getNodeName()+"("+node.getLabelString()+")");
}

// Check version
JdkToolService.checkJdkVersion(selectedJdk, listener.getLogger());

// Check version. On loggue systematiquement sans faire de controle car non bloquant
JdkToolService.checkJdkVersion(node, selectedJdk, listener.getLogger());
} catch (IOException e) {
listener.getLogger().println("[WeblogicDeploymentPlugin] - Unable to load JDK '"+globalJdk+"' from node '"+node+"'. The plugin execution is disabled.");
throw new DeploymentTaskException(new DeploymentTaskResult(WebLogicPreRequisteStatus.OK, WebLogicDeploymentStatus.ABORTED, convertParameters(task, envVars), null));
} catch (InterruptedException e) {
listener.getLogger().println("[WeblogicDeploymentPlugin] - Unable to load JDK '"+globalJdk+"' from node '"+node+"'. The plugin execution is disabled.");
throw new DeploymentTaskException(new DeploymentTaskResult(WebLogicPreRequisteStatus.OK, WebLogicDeploymentStatus.ABORTED, convertParameters(task, envVars), null));
} catch (RequiredJDKNotFoundException rjnfe) {
listener.getLogger().println("[WeblogicDeploymentPlugin] - No JDK found. The plugin execution is disabled.");
listener.getLogger().println("[WeblogicDeploymentPlugin] - No JDK found [reason : "+rjnfe.getMessage()+"]. The plugin execution is disabled.");
throw new DeploymentTaskException(new DeploymentTaskResult(WebLogicPreRequisteStatus.OK, WebLogicDeploymentStatus.ABORTED, convertParameters(task, envVars), null));
}
listener.getLogger().println("[WeblogicDeploymentPlugin] - The JDK " +selectedJdk.getHome() + " will be used.");
@@ -350,11 +359,6 @@ private void customize(DeploymentTask task, AbstractBuild<?, ?> build, BuildList
private DeploymentTask convertParameters(DeploymentTask task, EnvVars envVars) {
DeploymentTask taskHistory = new DeploymentTask(task);
taskHistory.setDeploymentTargets(ParameterValueResolver.resolveEnvVar(task.getDeploymentTargets(), envVars));
// task.getWeblogicEnvironmentTargetedName();
// ParameterValueResolver.resolveEnvVar(parameter.getEnvironment().getHost(), envars);
// ParameterValueResolver.resolveEnvVar(parameter.getEnvironment().getPort(), envars);
// ParameterValueResolver.resolveEnvVar(parameter.getEnvironment().getLogin(), envars));
// ParameterValueResolver.resolveEnvVar(parameter.getEnvironment().getPassword(), envars);
return taskHistory;
}

0 comments on commit 8ff1f87

Please sign in to comment.
You can’t perform that action at this time.