Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-5467

Please add the improvement and fix I already did. For using node properties env vars in the plugin.

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Major Major
    • scp-plugin
    • None

      First I fixed the recursive mkdirs bug that was reported for this scp plugin.
      With my fix the workspace can be copied to an other computer over ssh and all the directory structure will be created.
      The improvement:
      I had the need to be able to use the node properties env vars in the plugin .
      So for example we needed to build the same project on a different hardware and OS.
      Like Solaris SPARC, AIX, HPUX, Windows and so on.
      So in the node properties env vars we set variables for identifying which is the computer and the OS version/platform and others.
      Those environment variables we needed to use in the plugin so we can know which project was build successful or failed or any
      other problems.
      Here is the code changes I made to work as I needed:
      The changes was only in class
      be/certipost/hudson/plugin/SCPRepositoryPublisher.java

      I added a method:
      /**

      • Returns the environment variables set for a node/slave. So you can use
      • them, as are in your environment
      • @param envVars
      • @return
        */
        public static EnvVars getEnvVars() {
        Node node = Computer.currentComputer().getNode();
        DescribableList<NodeProperty<?>, NodePropertyDescriptor> nodeProperties = node
        .getNodeProperties();

      if (Computer.currentComputer() instanceof MasterComputer)

      { Hudson instance = Hudson.getInstance(); nodeProperties=instance.getGlobalNodeProperties(); }

      Iterator<NodeProperty<?>> iterator = nodeProperties.iterator();
      while (iterator.hasNext()) {
      NodeProperty<?> next = iterator.next();
      if (next instanceof EnvironmentVariablesNodeProperty)

      { EnvironmentVariablesNodeProperty envVarProp = (EnvironmentVariablesNodeProperty) next; EnvVars envVars = envVarProp.getEnvVars(); return envVars; }

      }
      return null;
      }

      and changed the method perform :

      public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
      throws InterruptedException, IOException {
      .........................
      .........................

      Map<String, String> envVars = build.getEnvVars();
      // Patched for env vars
      EnvVars objNodeEnvVars = getEnvVars();
      if (objNodeEnvVars != null)

      { envVars.putAll(objNodeEnvVars); }

      //~ Patched for env vars

      for (Entry e : entries) {
      String expanded = Util.replaceMacro(e.sourceFile, envVars);
      FilePath ws = build.getProject().getWorkspace();
      FilePath[] src = ws.list(expanded);
      if (src.length == 0)

      { // try to do error diagnostics log(listener.getLogger(), ("No file(s) found: " + expanded)); String error = ws.validateAntFileMask(expanded); if (error != null) log(listener.getLogger(), error); }

      String folderPath = Util.replaceMacro(e.filePath, envVars);

      // Fix for recursive mkdirs
      String strWorkspacePath = ws.toString();
      envVars.put("strWorkspacePath", strWorkspacePath);
      // ~Fix for recursive mkdirs

      I also changed the class be/certipost/hudson/plugin/SCPSite.java

      I added the method extractRelativePath:

      /**

      • Returns the relative path to the workspace
      • @param strWorkspacePath
      • @param filePath
      • @return
        */
        private String extractRelativePath(String strWorkspacePath, FilePath filePath)
        Unknown macro: { String strRet=""; String strFilePath=filePath.toString(); if(strWorkspacePath.length() == strFilePath.length()) { return ""; } int indexOf = strFilePath.indexOf(strWorkspacePath); if(indexOf>=0) { strRet=strFilePath.substring(strWorkspacePath.length()+1,strFilePath.length());//Exclude first file separator } indexOf = strRet.lastIndexOf(File.separator); if(indexOf>0) { strRet=strRet.substring(0,indexOf); } strRet=strRet.replace((CharSequence) "\", (CharSequence)"/"); return strRet; }

      I changed the upload method to became:

      public void upload(String folderPath, FilePath filePath,
      Map<String, String> envVars, PrintStream logger)
      throws IOException, InterruptedException, SftpException {
      if (session == null || channel == null)

      { throw new IOException("Connection to " + hostname + ", user=" + username + " is not established"); }

      SftpATTRS rootdirstat = channel.stat(rootRepositoryPath);
      if (rootdirstat == null)

      { throw new IOException( "Can't get stat of root repository directory:" + rootRepositoryPath); }

      else {
      if (!rootdirstat.isDir())

      { throw new IOException(rootRepositoryPath + " is not a directory"); }

      }
      if (filePath.isDirectory()) {
      FilePath[] subfiles = filePath.list("*/");
      if (subfiles != null) {
      for (int i = 0; i < subfiles.length; i++)

      { upload(folderPath + "/" + filePath.getName(), subfiles[i], envVars, logger); }

      }
      } else {
      String strWorkspacePath = envVars.get("strWorkspacePath");
      String localfilename = filePath.getName();
      mkdirs(folderPath, logger);
      InputStream in = filePath.read();
      String strRelativePath=extractRelativePath(strWorkspacePath , filePath);
      String strTmp = folderPath + "/" +strRelativePath;
      String strNewPath = rootRepositoryPath + "/" + strTmp;
      if(!strRelativePath.equals(""))

      { strNewPath += "/"; }

      String strNewFilename = strNewPath + localfilename;
      channel.put(in, strNewFilename);
      in.close();
      }
      }

            ramazanyich2 ramazanyich2
            kostakostadinov kostakostadinov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: