Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle legacy SC CLI options #232

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 45 additions & 15 deletions src/main/java/com/saucelabs/jenkins/pipeline/SauceConnectStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.saucelabs.ci.sauceconnect.AbstractSauceTunnelManager;
import com.saucelabs.ci.sauceconnect.SauceConnectManager;
import com.saucelabs.jenkins.HudsonSauceManagerFactory;
import com.saucelabs.saucerest.DataCenter;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.ProxyConfiguration;
Expand Down Expand Up @@ -48,25 +49,28 @@ public class SauceConnectStep extends Step {
private Boolean useLatestSauceConnect = false;
private Boolean useGeneratedTunnelIdentifier = false;
private String options;
private String optionsSC5;
private String sauceConnectPath;

@DataBoundConstructor
public SauceConnectStep() {
}

public SauceConnectStep(String options, Boolean verboseLogging, Boolean useLatestSauceConnect, Boolean useGeneratedTunnelIdentifier, String sauceConnectPath) {
public SauceConnectStep(String options, String optionsSC5, Boolean verboseLogging, Boolean useLatestSauceConnect, Boolean useGeneratedTunnelIdentifier, String sauceConnectPath) {
this.verboseLogging = verboseLogging;
this.useLatestSauceConnect = useLatestSauceConnect;
this.useGeneratedTunnelIdentifier = useGeneratedTunnelIdentifier;
this.sauceConnectPath = Util.fixEmptyAndTrim(sauceConnectPath);
this.options = StringUtils.trimToEmpty(options);
this.optionsSC5 = StringUtils.trimToEmpty(optionsSC5);
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new SauceConnectStepExecution(context,
PluginImpl.get().getSauceConnectOptions(),
options,
optionsSC5,
useGeneratedTunnelIdentifier,
verboseLogging,
sauceConnectPath,
Expand All @@ -82,7 +86,16 @@ public String getOptions() {

@DataBoundSetter
public void setOptions(String options) {
this.options = options;
this.options = options.strip();
}

public String getOptionsSC5() {
return optionsSC5;
}

@DataBoundSetter
public void setOptionsSC5(String optionsSC5) {
this.optionsSC5 = optionsSC5.strip();
}

public String getSauceConnectPath() {
Expand Down Expand Up @@ -154,16 +167,18 @@ private static final class SauceStartConnectHandler extends MasterToSlaveCallabl
private final Boolean verboseLogging;
private final String sauceConnectPath;
private final Boolean useLatestSauceConnect;
private final Boolean legacyCLI;
private final ProxyConfiguration proxy;

SauceStartConnectHandler(SauceCredentials sauceCredentials, int port, String options, TaskListener listener, Boolean verboseLogging, String sauceConnectPath, Boolean useLatestSauceConnect, ProxyConfiguration proxy) {
SauceStartConnectHandler(SauceCredentials sauceCredentials, int port, String options, TaskListener listener, Boolean verboseLogging, String sauceConnectPath, Boolean useLatestSauceConnect, Boolean legacyCLI, ProxyConfiguration proxy) {
this.sauceCredentials = sauceCredentials;
this.port = port;
this.options = options;
this.options = options.strip();
this.listener = listener;
this.verboseLogging = verboseLogging;
this.sauceConnectPath = sauceConnectPath;
this.useLatestSauceConnect = useLatestSauceConnect;
this.legacyCLI = legacyCLI;
this.proxy = proxy;
}

Expand All @@ -175,13 +190,14 @@ public Void call() throws AbstractSauceTunnelManager.SauceConnectException {
sauceTunnelManager.openConnection(
sauceCredentials.getUsername(),
sauceCredentials.getApiKey().getPlainText(),
sauceCredentials.getRestEndpointName(),
DataCenter.fromString(sauceCredentials.getRestEndpointName()),
port,
null, /*sauceConnectJar,*/
options,
listener.getLogger(),
verboseLogging,
sauceConnectPath
sauceConnectPath,
legacyCLI
);
return null;
}
Expand Down Expand Up @@ -217,6 +233,7 @@ public Void call() throws AbstractSauceTunnelManager.SauceConnectException {
public static class SauceConnectStepExecution extends StepExecution {
private final String globalOptions;
private final String options;
private final String optionsSC5;
private final boolean useGeneratedTunnelIdentifier;
private final boolean verboseLogging;
private final String sauceConnectPath;
Expand All @@ -231,6 +248,7 @@ public SauceConnectStepExecution(
@NonNull StepContext context,
String globalOptions,
String options,
String optionsSC5,
boolean useGeneratedTunnelIdentifier,
boolean verboseLogging,
String sauceConnectPath,
Expand All @@ -240,6 +258,7 @@ public SauceConnectStepExecution(
super(context);
this.globalOptions = globalOptions;
this.options = options;
this.optionsSC5 = optionsSC5;
this.useGeneratedTunnelIdentifier = useGeneratedTunnelIdentifier;
this.verboseLogging = verboseLogging;
this.sauceConnectPath = sauceConnectPath;
Expand All @@ -249,6 +268,14 @@ public SauceConnectStepExecution(

@Override
public boolean start() throws Exception {
boolean legacyCLI = false;
if (options != null && optionsSC5 != null && !options.isEmpty() && !optionsSC5.isEmpty()) {
throw new Exception("Legacy and SC5 CLI options cannot both be specified");
}

if (options != null && !options.isEmpty()) {
legacyCLI = true;
}

Run<?, ?> run = getContext().get(Run.class);
Job<?,?> job = run.getParent();
Expand All @@ -266,10 +293,14 @@ public boolean start() throws Exception {

ArrayList<String> optionsArray = new ArrayList<String>();
optionsArray.add(globalOptions);
optionsArray.add(options);
if (legacyCLI) {
optionsArray.add(options);
} else {
optionsArray.add(optionsSC5);
}
optionsArray.removeAll(Collections.singleton("")); // remove the empty strings

String options = StringUtils.join(optionsArray, " ");
String combinedOptions = StringUtils.join(optionsArray, " ");

HashMap<String,String> overrides = new HashMap<String,String>();
overrides.put(SauceOnDemandBuildWrapper.SELENIUM_PORT, String.valueOf(port));
Expand All @@ -278,26 +309,25 @@ public boolean start() throws Exception {
if (useGeneratedTunnelIdentifier) {
final String tunnelName = SauceEnvironmentUtil.generateTunnelName(job.getName(), run.number);
overrides.put(SauceOnDemandBuildWrapper.TUNNEL_NAME, tunnelName);
options = options + " --tunnel-name " + tunnelName;
combinedOptions = combinedOptions + " --tunnel-name " + tunnelName;
}

SauceCredentials sauceCredentials = getContext().get(SauceCredentials.class);
final String restEndpoint = sauceCredentials.getRestEndpoint();

overrides.put(SauceOnDemandBuildWrapper.SAUCE_REST_ENDPOINT, restEndpoint);
options = options + " -x " + restEndpoint + "rest/v1";
final String region = sauceCredentials.getRegion();
combinedOptions = combinedOptions + " --region " + region;

TaskListener listener = getContext().get(TaskListener.class);
listener.getLogger().println("Starting sauce connect");

SauceStartConnectHandler handler = new SauceStartConnectHandler(
sauceCredentials,
port,
options,
combinedOptions,
listener,
verboseLogging,
sauceConnectPath,
useLatestSauceConnect,
legacyCLI,
proxy
);
computer.getChannel().call(handler);
Expand Down Expand Up @@ -352,4 +382,4 @@ private static final class Callback extends BodyExecutionCallback.TailCall {

}
}
}
}
15 changes: 13 additions & 2 deletions src/main/java/hudson/plugins/sauce_ondemand/PluginImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public class PluginImpl extends Plugin implements Describable<PluginImpl> {

private String sauceConnectDirectory;

private String sauceConnectOptions;
private String sauceConnectOptions; // SC4 Legacy CLI Options

private String sauceConnectCLIOptions; // SC5 options

private String sauceConnectMaxRetries;

Expand Down Expand Up @@ -150,6 +152,7 @@ public void configure(StaplerRequest req, JSONObject formData)
throws IOException, ServletException, Descriptor.FormException {
sauceConnectDirectory = formData.getString("sauceConnectDirectory");
sauceConnectOptions = formData.getString("sauceConnectOptions");
sauceConnectCLIOptions = formData.getString("sauceConnectCLIOptions");
environmentVariablePrefix = formData.getString("environmentVariablePrefix");
setDisableUsageStats(formData.getBoolean("disableUsageStats"));
sauceConnectMaxRetries = formData.getString("sauceConnectMaxRetries");
Expand Down Expand Up @@ -220,6 +223,14 @@ public void setSauceConnectOptions(String sauceConnectOptions) {
this.sauceConnectOptions = sauceConnectOptions;
}

public String getSauceConnectCLIOptions() {
return sauceConnectCLIOptions;
}

public void setSauceConnectCLIOptions(String sauceConnectCLIOptions) {
this.sauceConnectCLIOptions = sauceConnectCLIOptions;
}

public void setDisableUsageStats(boolean disableUsageStats) {
this.disableUsageStats = disableUsageStats;
}
Expand All @@ -243,4 +254,4 @@ public ListBoxModel doFillCredentialIdItems(final @AncestorInPath ItemGroup<?> c
return new StandardUsernameListBoxModel().withAll(SauceCredentials.all(context));
}
}
}
}
Loading
Loading