Skip to content
Permalink
Browse files

[JENKINS-44599] Copie classpath librairies on node

  • Loading branch information...
rachaumi
rachaumi committed Jun 23, 2017
1 parent 8ff1f87 commit 307c57fb40d2d864f2fe10f6035f69c571fa6b6d
@@ -5,17 +5,18 @@

import java.io.File;

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

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.deploy.weblogic.data.WebLogicAuthenticationMode;
import org.jenkinsci.plugins.deploy.weblogic.data.WebLogicStageMode;
import org.jenkinsci.plugins.deploy.weblogic.properties.WebLogicDeploymentPluginConstantes;
import org.jenkinsci.plugins.deploy.weblogic.util.DeployerClassPathUtils;
import org.jenkinsci.plugins.deploy.weblogic.util.ParameterValueResolver;

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

/**
* @author rchaumie
*
@@ -150,7 +151,8 @@ private static void processJavaLauncher(WebLogicDeployerParameters parameter, Ar

// 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());

// args.add(parameter.getUsedJdk().getBinDir().getAbsolutePath().concat("/java"));

//java options specifique
if(StringUtils.isNotBlank(parameter.getJavaOpts())){
//On parse l'ensemble des options et on les rajoute des le args[]
@@ -166,12 +168,16 @@ 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();
String remotingJar = StringUtils.EMPTY;
// On recalcule le classpath à partir du workspace si on est en remote
if(! StringUtils.EMPTY.equalsIgnoreCase(parameter.getBuild().getBuiltOnStr())){
remotingJar = DeployerClassPathUtils.formatClasspath(parameter.getClasspath(), parameter.getBuild(), parameter.getListener());
} else {
remotingJar = parameter.getClasspath();
}

args.add(remotingJar);
args.add(WebLogicDeploymentPluginConstantes.WL_WEBLOGIC_API_DEPLOYER_MAIN_CLASS);

}

}
@@ -3,6 +3,7 @@
*/
package org.jenkinsci.plugins.deploy.weblogic.task;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -47,6 +48,7 @@
import hudson.model.BuildListener;
import hudson.model.JDK;
import hudson.model.Node;
import hudson.remoting.VirtualChannel;

/**
* @author Raphael
@@ -89,7 +91,6 @@ public DeploymentTaskResult perform(DeploymentTask task, String globalJdk, Abstr
try {

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){
@@ -102,7 +103,7 @@ public DeploymentTaskResult perform(DeploymentTask task, String globalJdk, Abstr
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. On loggue systematiquement sans faire de controle car non bloquant
// Check version.
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.");
@@ -147,9 +148,7 @@ public DeploymentTaskResult perform(DeploymentTask task, String globalJdk, Abstr
throw new DeploymentTaskException(new DeploymentTaskResult(WebLogicPreRequisteStatus.OK, WebLogicDeploymentStatus.ABORTED, convertParameters(task, envVars), null));
}

//Deploiement
//Recuperation du parametrage
WeblogicEnvironment weblogicEnvironmentTargeted = null;
// Filtrage, parametrage et deploiement
try {

//Gestion de liste d'exclusions
@@ -161,15 +160,20 @@ public DeploymentTaskResult perform(DeploymentTask task, String globalJdk, Abstr
}

//Recuperation du parametrage
weblogicEnvironmentTargeted = getWeblogicEnvironmentTargeted(task.getWeblogicEnvironmentTargetedName(), listener);
WeblogicEnvironment weblogicEnvironmentTargeted = getWeblogicEnvironmentTargeted(task.getWeblogicEnvironmentTargetedName(), listener);

if(weblogicEnvironmentTargeted == null){
listener.error("[WeblogicDeploymentPlugin] - WebLogic environment Name " +task.getWeblogicEnvironmentTargetedName()+ " not found in the list. Please check the configuration file.");
throw new DeploymentTaskException(new DeploymentTaskResult(WebLogicPreRequisteStatus.OK, WebLogicDeploymentStatus.ABORTED, convertParameters(task, envVars), fullArtifactFinalName));
}
listener.getLogger().println("[WeblogicDeploymentPlugin] - Deploying the artifact on the following target : (name="+task.getWeblogicEnvironmentTargetedName()+") (host=" + weblogicEnvironmentTargeted.getHost() + ") (port=" +weblogicEnvironmentTargeted.getPort()+ ")");

// copie des libraries sur le remote node
if(! StringUtils.EMPTY.equalsIgnoreCase(build.getBuiltOnStr())){
copyWeblogicLibraries(build, listener, launcher, getDescriptor().getExtraClasspath());
}

//Deploiement
listener.getLogger().println("[WeblogicDeploymentPlugin] - Deploying the artifact on the following target : (name="+task.getWeblogicEnvironmentTargetedName()+") (host=" + weblogicEnvironmentTargeted.getHost() + ") (port=" +weblogicEnvironmentTargeted.getPort()+ ")");
if(StringUtils.isBlank(task.getCommandLine())){
// undeploy task
undeploy(task, build, listener, launcher, weblogicEnvironmentTargeted, selectedJdk, artifactName, deploymentLogOut, envVars);
@@ -434,6 +438,22 @@ private WeblogicEnvironment getWeblogicEnvironmentTargeted(String weblogicEnviro
return out;
}

private void copyWeblogicLibraries(AbstractBuild<?, ?> build, BuildListener listener, Launcher launcher, String classpath) throws IOException, InterruptedException{
FilePath fp = null;
if(build.getWorkspace().isRemote()) {
VirtualChannel channel = build.getWorkspace().getChannel();
for(String path : classpath.split(File.pathSeparator)){
FilePath srcFile = new FilePath(new File(path));
fp = new FilePath(channel, build.getWorkspace() + "/"+srcFile.getName());
if(! fp.exists()){
listener.getLogger().println("[WeblogicDeploymentPlugin] - copying file "+srcFile.getName()+" on node "+build.getBuiltOnStr()+" ...");
fp.copyFrom(srcFile);
} else {
listener.getLogger().println("[WeblogicDeploymentPlugin] - file "+srcFile.getName()+" already exists in workspace on node "+build.getBuiltOnStr()+".");
}
}
}
}


/**
@@ -6,7 +6,14 @@
import org.apache.commons.io.FilenameUtils;
import org.jenkinsci.plugins.deploy.weblogic.properties.WebLogicDeploymentPluginConstantes;

import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Run.RunnerAbortedException;
import hudson.remoting.VirtualChannel;

import java.io.File;
import java.io.IOException;

/**
* @author rchaumie
@@ -30,4 +37,56 @@ public static String getDefaultPathToWebLogicJar() {
String envWlHome = System.getenv(WebLogicDeploymentPluginConstantes.WL_HOME_ENV_VAR_NAME);
return FilenameUtils.normalize(envWlHome+WebLogicDeploymentPluginConstantes.WL_HOME_LIB_DIR+WebLogicDeploymentPluginConstantes.WL_WEBLOGIC_LIBRARY_NAME);
}

/**
*
* @param classpath
* @param build
* @return
*/
public static String formatClasspath(final String classpath, AbstractBuild<?, ?> build, BuildListener listener){
StringBuilder fromWorkspaceClassPath = new StringBuilder();
try {
VirtualChannel channel = build.getWorkspace().getChannel();
for(String path : classpath.split(File.pathSeparator)){
FilePath srcFile = new FilePath(new File(path));
FilePath fp = new FilePath(channel, build.getWorkspace() + "/"+srcFile.getName());
String remotePath = fp.getRemote();
if(! fp.exists()){
listener.error("[WeblogicDeploymentPlugin] - The following library '"+remotePath+"' declared on classpath is missing on node '"+build.getBuiltOnStr()+"'.");
throw new RunnerAbortedException();
}

// TODO il y a toujours un dernier separator
fromWorkspaceClassPath.append(remotePath).append(isUnix(channel, remotePath) ? ":" : ";");
}
} catch (IOException e) {
listener.error("[WeblogicDeploymentPlugin] - Unable to compute classpath for remote invocation.", e);
throw new RunnerAbortedException();
} catch (InterruptedException e) {
listener.error("[WeblogicDeploymentPlugin] - Unable to compute classpath for remote invocation.", e);
throw new RunnerAbortedException();
}
return fromWorkspaceClassPath.toString();
}

/**
* Checks if the remote path is Unix.
* Recuperer de la classe FilePath car methode non acessible en dehors du package
*/
private static boolean isUnix(VirtualChannel channel, String remote) {
// if the path represents a local path, there' no need to guess.
if(channel == null)
return File.pathSeparatorChar!=';';

// note that we can't use the usual File.pathSeparator and etc., as the OS of
// the machine where this code runs and the OS that this FilePath refers to may be different.

// Windows absolute path is 'X:\...', so this is usually a good indication of Windows path
if(remote.length()>3 && remote.charAt(1)==':' && remote.charAt(2)=='\\')
return false;
// Windows can handle '/' as a path separator but Unix can't,
// so err on Unix side
return remote.indexOf("\\")==-1;
}
}

0 comments on commit 307c57f

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