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

Add Execute Windows batch command to slave-setup plugin

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major Major
    • slave-setup-plugin
    • None
    • Windows XP, Jenkins 1.488, slave-setup v1.6

      I want to execute a batch script on my Windows XP slaves, before the slave gets connected. But this fails, as the slave-setup only supports shell. From Jenkins configuration -> Slave setups

      prepare script -
      setup files directory D:\jenkins_slavesetup
      setup script after copy .\launch-slave.bat
      deploy on save now disabled
      Label Expression SlaveA
      Executing script '.\launch-slave.bat' on SlaveA
      [jenkins] $ sh -xe C:\Temp\hudson7567579782678732298.sh
      The system cannot find the file specified
      ERROR: Unexpected error in launching a slave. This is probably a bug in Jenkins
      java.io.IOException: Cannot run program "sh" (in directory "d:\jenkins"): CreateProcess error=2, The system cannot find the file specified
      	at java.lang.ProcessBuilder.start(Unknown Source)
      	at hudson.Proc$LocalProc.<init>(Proc.java:244)
      	at hudson.Proc$LocalProc.<init>(Proc.java:216)
      	at hudson.Launcher$LocalLauncher.launch(Launcher.java:716)
      	at hudson.Launcher$ProcStarter.start(Launcher.java:345)
      	at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:941)
      	at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:908)
      	at hudson.remoting.UserRequest.perform(UserRequest.java:118)
      	at hudson.remoting.UserRequest.perform(UserRequest.java:48)
      	at hudson.remoting.Request$2.run(Request.java:326)
      	at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:72)
      	at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
      	at java.util.concurrent.FutureTask.run(Unknown Source)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      	at java.lang.Thread.run(Unknown Source)
      Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
      	at java.lang.ProcessImpl.create(Native Method)
      	at java.lang.ProcessImpl.<init>(Unknown Source)
      	at java.lang.ProcessImpl.start(Unknown Source)
      	... 16 more
      

      When I override the Shell executable to 'cmd'. I get the following.

      Executing script '.\launch-slave.bat' on SlaveA
      [jenkins] $ cmd -xe C:\Temp\hudson6054783183706487524.sh
      Microsoft Windows XP [Version 5.1.2600]
      (C) Copyright 1985-2001 Microsoft Corp.
      
      d:\jenkins>script executed successfully.
      slave setup done.
      Slave successfully connected and online
      

      But the commands from the launch-slave.bat file are not executed. Expected is something like the following command is executed. This is equal to the Execute Windows batch command from the Build step.

      [jenkins] $ cmd /c call C:\Temp\hudson6746024923916378374.bat
      

          [JENKINS-15739] Add Execute Windows batch command to slave-setup plugin

          Blaine Whited added a comment -

          My organization (IXI Corporation/Equifax) are currently transitioning to a Jenkins CI system to help with deployments and box setup and configuration, to better support a swarm, and this plugin provides some very nice features to the system as a whole, I noticed this issue as well and started digging. I have created a potential fix to this error and I wanted some input.

          What I have discovered and done:
          Because of where the slave-setup injects itself into the slave start-up procedure, the OS of the slave doesn't seem to be available, no matter how I tried to retrieve it, the call you think might provide this value, is only available on the Master node, per the documentation.
          I currently have a patch that I am using in our production system that uses a slave label of windows (case-insensitive) to determine whether to launch the shell or batch object.
          Even with this change the plugin cannot be built on a windows box because of the echo command syntax in the unit test, but because of the way this unit test is preformed, I am able to query for the slave's OS and tweak the echo command accordingly to pass the test on windows (without effecting any-non windows builds) and allow for complete compilation and packaging.

          What I wanted to discuss:
          Would it be more desirable to automatically detect a Windows OS by try-catching the current plugin's process of starting the shell with the error it is expected to throw, automatically trying the batch alternative, and failing if neither are successful, as I don't want to force anyone to use labels they don't want.
          I could even have the system skip the try-catch if the user decided to add a Windows label, effectively implementing both solutions, and reducing memory usage by skipping the overhead of a try-catch block.

          Any input would be greatly appreciated.

          Blaine Whited added a comment - My organization (IXI Corporation/Equifax) are currently transitioning to a Jenkins CI system to help with deployments and box setup and configuration, to better support a swarm, and this plugin provides some very nice features to the system as a whole, I noticed this issue as well and started digging. I have created a potential fix to this error and I wanted some input. What I have discovered and done: Because of where the slave-setup injects itself into the slave start-up procedure, the OS of the slave doesn't seem to be available, no matter how I tried to retrieve it, the call you think might provide this value, is only available on the Master node, per the documentation. I currently have a patch that I am using in our production system that uses a slave label of windows (case-insensitive) to determine whether to launch the shell or batch object. Even with this change the plugin cannot be built on a windows box because of the echo command syntax in the unit test, but because of the way this unit test is preformed, I am able to query for the slave's OS and tweak the echo command accordingly to pass the test on windows (without effecting any-non windows builds) and allow for complete compilation and packaging. What I wanted to discuss: Would it be more desirable to automatically detect a Windows OS by try-catching the current plugin's process of starting the shell with the error it is expected to throw, automatically trying the batch alternative, and failing if neither are successful, as I don't want to force anyone to use labels they don't want. I could even have the system skip the try-catch if the user decided to add a Windows label, effectively implementing both solutions, and reducing memory usage by skipping the overhead of a try-catch block. Any input would be greatly appreciated.

          David Ishee added a comment -

          I have a Linux master and a Windows slave. I would also like to have the option to launch a Windows .bat file on startup.

          David Ishee added a comment - I have a Linux master and a Windows slave. I would also like to have the option to launch a Windows .bat file on startup.

          Darren Pearce added a comment -

          I too would like to execute a .bat file on slave startup. It looks like the Slave Setup Plugin assumes that it is running sh (or possibly other shells) since -xe are flags for sh not for cmd. This seems like a very strange assumption for a plugin that will obviously run on multiple platforms.

          Darren Pearce added a comment - I too would like to execute a .bat file on slave startup. It looks like the Slave Setup Plugin assumes that it is running sh (or possibly other shells) since -xe are flags for sh not for cmd. This seems like a very strange assumption for a plugin that will obviously run on multiple platforms.

            kohsuke Kohsuke Kawaguchi
            metalmolly MeTAlMollY
            Votes:
            10 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated: