Skip to content

Commit

Permalink
Add support for Windows commands (#118)
Browse files Browse the repository at this point in the history
Co-authored-by: Alvaro Rausell Guiard <[email protected]>
  • Loading branch information
AlvaroRausell and Alvaro Rausell Guiard authored Jan 4, 2023
1 parent d3f18e9 commit 148d738
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
27 changes: 20 additions & 7 deletions src/main/java/org/jenkinsci/plugins/docker/swarm/DockerSwarmAgentTemplate.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class DockerSwarmAgentTemplate implements Describable<DockerSwarmAgentTem
private String portBinds;
private String label;
private boolean osWindows;
private String command;
private String unixCommand;
private String windowsCommand;
private String user;
private String workingDir;
private String metadata;
Expand All @@ -57,7 +58,7 @@ public DockerSwarmAgentTemplate() {

@DataBoundConstructor
public DockerSwarmAgentTemplate(final String image, final String hostBinds, final String hostNamedPipes, final String dnsIps,
final String dnsSearchDomains, final String command, final String user, final String workingDir,
final String dnsSearchDomains, final String unixCommand,final String windowsCommand, final String user, final String workingDir,
final String hosts, final String metadata, final String secrets, final String configs, final String label, final String cacheDir,
final String tmpfsDir, final String envVars, final long limitsNanoCPUs, final long limitsMemoryBytes,
final long reservationsNanoCPUs, final long reservationsMemoryBytes, String portBinds, final boolean osWindows,
Expand All @@ -68,7 +69,8 @@ public DockerSwarmAgentTemplate(final String image, final String hostBinds, fina
this.hostNamedPipes = hostNamedPipes;
this.dnsIps = dnsIps;
this.dnsSearchDomains = dnsSearchDomains;
this.command = command;
this.unixCommand = unixCommand;
this.windowsCommand = windowsCommand;
this.user = user;
this.workingDir = workingDir;
this.hosts = hosts;
Expand Down Expand Up @@ -146,8 +148,15 @@ public String[] getEnvVarsConfig() {
return StringUtils.isEmpty(this.envVars) ? new String[] {} : this.envVars.split("[\\r\\n]+");
}

public String[] getCommandConfig() {
return StringUtils.isEmpty(this.command) ? new String[] {} : this.command.split("[\\r\\n]+");
public String[] getUnixCommandConfig() {
return StringUtils.isEmpty(this.unixCommand) ? new String[] {} : this.unixCommand.split("[\\r\\n]+");
}
public String[] getWindowsCommandConfig() {
return StringUtils.isEmpty(this.windowsCommand) ? new String[] {} : this.windowsCommand.split("[\\r\\n\\s]+");
}

public String[] getWindowsCommandConfig(String windowsCommand) {
return StringUtils.isEmpty(windowsCommand) ? new String[] {} : windowsCommand.split("[\\r\\n\\s]+");
}

public String[] getHostsConfig() {
Expand Down Expand Up @@ -253,8 +262,12 @@ public String getConfigs() {
return configs;
}

public String getCommand() {
return command;
public String getUnixCommand() {
return unixCommand;
}

public String getWindowsCommand() {
return windowsCommand;
}

public String getHosts() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -86,23 +87,36 @@ private void launch(final DockerSwarmComputer computer, final TaskListener liste
final String[] envVars = envVarsList.toArray(new String[0]);

if (dockerSwarmAgentTemplate.isOsWindows()) {
// On windows use hard-coded command. TODO: Use configured command if
// configured.
final String agentOptions = String.join(" ", "-jnlpUrl", getAgentJnlpUrl(computer, configuration),
"-secret", getAgentSecret(computer), "-noReconnect");
String interpreter;
String interpreterOptions;
String fetchAndLaunchCommand;
interpreter = "powershell.exe";
interpreterOptions = "";
fetchAndLaunchCommand = "& { Invoke-WebRequest -TimeoutSec 20 -OutFile agent.jar "
+ getAgentJarUrl(configuration) + "; if($?) { java -jar agent.jar " + agentOptions + " } }";
final String[] command = new String[] { interpreter, interpreterOptions, fetchAndLaunchCommand };
launchContainer(command, configuration, envVars, dockerSwarmAgentTemplate.getWorkingDir(),
String command = dockerSwarmAgentTemplate.getWindowsCommand();
HashMap<String,String> envHashMap = new HashMap<String,String>(){{
put("\\%DOCKER_SWARM_PLUGIN_JENKINS_AGENT_SECRET\\%",getAgentSecret(computer));
put("\\%DOCKER_SWARM_PLUGIN_JENKINS_AGENT_JAR_URL\\%",getAgentJarUrl(configuration));
put("\\%DOCKER_SWARM_PLUGIN_JENKINS_AGENT_JNLP_URL\\%",getAgentJnlpUrl(computer, configuration));
put("\\%DOCKER_SWARM_PLUGIN_JENKINS_AGENT_NAME\\%",getAgentName(computer));

}};
for (String envVarString: envHashMap.keySet()){
System.out.println("Replacing: "+ envVarString + " with: "+envHashMap.get(envVarString));
command = command.replaceAll(envVarString, envHashMap.get(envVarString));
System.out.println("Command: "+ command);
}
ArrayList<String> finalCommand = new ArrayList<String>();
String [] windowsCommand = dockerSwarmAgentTemplate.getWindowsCommandConfig(command);
for (String windowsCommandSegment: windowsCommand){
if(windowsCommandSegment.equals("${unixCommand}")){
for(String unixCommandSegment: dockerSwarmAgentTemplate.getUnixCommandConfig()){
finalCommand.add(unixCommandSegment);
}
} else{
finalCommand.add(windowsCommandSegment);
}
}

launchContainer(finalCommand.toArray(new String[0]), configuration, envVars, dockerSwarmAgentTemplate.getWorkingDir(),
dockerSwarmAgentTemplate.getUser(), dockerSwarmAgentTemplate, listener, computer,
dockerSwarmAgentTemplate.getHostsConfig());
} else {
launchContainer(dockerSwarmAgentTemplate.getCommandConfig(), configuration, envVars,
launchContainer(dockerSwarmAgentTemplate.getUnixCommandConfig(), configuration, envVars,
dockerSwarmAgentTemplate.getWorkingDir(), dockerSwarmAgentTemplate.getUser(),
dockerSwarmAgentTemplate, listener, computer, dockerSwarmAgentTemplate.getHostsConfig());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
<f:entry title="Image" field="image">
<f:textbox value="${dockerSwarmAgentTemplate.image}" default="java:8"/>
</f:entry>
<f:entry title="Command" field="command">
<f:textarea value="${dockerSwarmAgentTemplate.command}" default="sh&#10;-cx&#10;curl --connect-timeout 20 --max-time 60 -o agent.jar $DOCKER_SWARM_PLUGIN_JENKINS_AGENT_JAR_URL &amp;&amp; java -jar agent.jar -jnlpUrl $DOCKER_SWARM_PLUGIN_JENKINS_AGENT_JNLP_URL -secret $DOCKER_SWARM_PLUGIN_JENKINS_AGENT_SECRET -noReconnect -workDir /tmp"/>
<f:entry title="Unix Command" field="unixCommand">
<f:textarea value="${dockerSwarmAgentTemplate.unixCommand}" default="sh&#10;-cx&#10;curl --connect-timeout 20 --max-time 60 -o agent.jar $DOCKER_SWARM_PLUGIN_JENKINS_AGENT_JAR_URL &amp;&amp; java -jar agent.jar -jnlpUrl $DOCKER_SWARM_PLUGIN_JENKINS_AGENT_JNLP_URL -secret $DOCKER_SWARM_PLUGIN_JENKINS_AGENT_SECRET -noReconnect -workDir /tmp"/>
</f:entry>
<f:entry title="Windows Command" field="windowsCommand">
<f:textbox value="${dockerSwarmAgentTemplate.windowsCommand}" default="powershell.exe &amp; { Invoke-WebRequest -TimeoutSec 20 -OutFile agent.jar %DOCKER_SWARM_PLUGIN_JENKINS_AGENT_JAR_URL%; if($?) { java -jar agent.jar -jnlpUrl %DOCKER_SWARM_PLUGIN_JENKINS_AGENT_JNLP_URL% -secret %DOCKER_SWARM_PLUGIN_JENKINS_AGENT_SECRET% -noReconnect } }"/>
</f:entry>
<f:entry title="Working Directory" field="workingDir">
<f:textbox value="${dockerSwarmAgentTemplate.workingDir}" default="/tmp"/>
Expand Down

0 comments on commit 148d738

Please sign in to comment.