Skip to content

Commit

Permalink
[grid] Wire up the new file-based configs
Browse files Browse the repository at this point in the history
We've set things up so that environment variables, system properties,
and command line flags all take precedence.
  • Loading branch information
shs96c committed Mar 30, 2020
1 parent cddf89f commit 2f4cf5b
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class DefaultHubConfig extends MapConfig {
"subscribe", "tcp://*:4443",
"bind", true),
"sessions", ImmutableMap.of(
"implementation", "org.openqa.selenium.grid.sessionmap.local.LocalSessionMap")));
"implementation", "org.openqa.selenium.grid.sessionmap.local.LocalSessionMap"),
"server", ImmutableMap.of(
"port", 4444)));
}
}
17 changes: 10 additions & 7 deletions java/server/src/org/openqa/selenium/grid/commands/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openqa.selenium.grid.config.CompoundConfig;
import org.openqa.selenium.grid.config.ConcatenatingConfig;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigFlags;
import org.openqa.selenium.grid.config.EnvConfig;
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.grid.distributor.local.LocalDistributor;
Expand Down Expand Up @@ -67,15 +68,16 @@ public String getDescription() {
@Override
public Executable configure(String... args) {
HelpFlags help = new HelpFlags();
BaseServerFlags baseFlags = new BaseServerFlags(4444);
ConfigFlags configFlags = new ConfigFlags();
BaseServerFlags baseFlags = new BaseServerFlags();
EventBusFlags eventBusFlags = new EventBusFlags();

JCommander commander = JCommander.newBuilder()
.programName("standalone")
.addObject(baseFlags)
.addObject(eventBusFlags)
.addObject(help)
.build();
.programName("standalone")
.addObject(baseFlags)
.addObject(configFlags)
.addObject(eventBusFlags)
.addObject(help)
.build();

return () -> {
try {
Expand All @@ -96,6 +98,7 @@ public Executable configure(String... args) {
new AnnotatedConfig(help),
new AnnotatedConfig(eventBusFlags),
new AnnotatedConfig(baseFlags),
configFlags.readConfigFiles(),
new DefaultHubConfig());

LoggingOptions loggingOptions = new LoggingOptions(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public boolean isShown() {
@Override
public Executable configure(String... args) {
HelpFlags help = new HelpFlags();
BaseServerFlags baseFlags = new BaseServerFlags(5557);
BaseServerFlags baseFlags = new BaseServerFlags();
EventBusFlags eventBusFlags = new EventBusFlags();

JCommander commander = JCommander.newBuilder()
Expand Down Expand Up @@ -100,7 +100,9 @@ public Executable configure(String... args) {
"events", ImmutableMap.of(
"bind", true,
"publish", "tcp://*:4442",
"subscribe", "tcp://*:4443"))));
"subscribe", "tcp://*:4443"),
"server", ImmutableMap.of(
"port", 5557))));

LoggingOptions loggingOptions = new LoggingOptions(config);
loggingOptions.configureLogging();
Expand Down
22 changes: 13 additions & 9 deletions java/server/src/org/openqa/selenium/grid/commands/Standalone.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openqa.selenium.grid.config.CompoundConfig;
import org.openqa.selenium.grid.config.ConcatenatingConfig;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigFlags;
import org.openqa.selenium.grid.config.EnvConfig;
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.grid.distributor.local.LocalDistributor;
Expand Down Expand Up @@ -76,19 +77,21 @@ public String getDescription() {
@Override
public Executable configure(String... args) {
HelpFlags help = new HelpFlags();
BaseServerFlags baseFlags = new BaseServerFlags(4444);
ConfigFlags configFlags = new ConfigFlags();
BaseServerFlags baseFlags = new BaseServerFlags();
EventBusFlags eventFlags = new EventBusFlags();
DockerFlags dockerFlags = new DockerFlags();
StandaloneFlags standaloneFlags = new StandaloneFlags();

JCommander commander = JCommander.newBuilder()
.programName("standalone")
.addObject(baseFlags)
.addObject(help)
.addObject(eventFlags)
.addObject(dockerFlags)
.addObject(standaloneFlags)
.build();
.programName("standalone")
.addObject(baseFlags)
.addObject(configFlags)
.addObject(dockerFlags)
.addObject(eventFlags)
.addObject(help)
.addObject(standaloneFlags)
.build();

return () -> {
try {
Expand All @@ -109,8 +112,9 @@ public Executable configure(String... args) {
new AnnotatedConfig(help),
new AnnotatedConfig(baseFlags),
new AnnotatedConfig(dockerFlags),
new AnnotatedConfig(standaloneFlags),
new AnnotatedConfig(eventFlags),
configFlags.readConfigFiles(),
new AnnotatedConfig(standaloneFlags),
new DefaultStandaloneConfig());

LoggingOptions loggingOptions = new LoggingOptions(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public class StandaloneFlags {
@ConfigValue(section = "node", name = "detect-drivers")
public boolean autoconfigure = true;

@ConfigValue(section = "server", name = "port")
public int port = 4444;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ java_library(
],
deps = [
"//java/client/src/org/openqa/selenium/json",
artifact("com.beust:jcommander"),
artifact("com.google.guava:guava"),
artifact("io.ous:jtoml"),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@

package org.openqa.selenium.grid.config;

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.common.collect.ImmutableList;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.google.common.collect.ImmutableList.toImmutableList;

public class CompoundConfig implements Config {

private final List<Config> allConfigs;

public CompoundConfig(Config mostImportant, Config... othersInDescendingOrderOfImportance) {
this.allConfigs = ImmutableList.<Config>builder()
.add(mostImportant)
.add(othersInDescendingOrderOfImportance)
.build();
public CompoundConfig(Config... allConfigsInDescendingOrderOfImportance) {
if (allConfigsInDescendingOrderOfImportance.length == 0) {
throw new ConfigException("List of config files must be greater than 0.");
}

this.allConfigs = ImmutableList.copyOf(allConfigsInDescendingOrderOfImportance);
}

@Override
Expand Down
41 changes: 41 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/ConfigFlags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.config;

import com.beust.jcommander.Parameter;
import com.google.common.collect.ImmutableMap;

import java.nio.file.Path;
import java.util.List;

public class ConfigFlags {

@Parameter(names = "--config", description = "Config file to read from (may be specified more than once)")
private List<Path> configFiles;

public Config readConfigFiles() {
if (configFiles == null || configFiles.isEmpty()) {
return new MapConfig(ImmutableMap.of());
}

return new CompoundConfig(
configFiles.stream()
.map(Configs::from)
.toArray(Config[]::new));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class DefaultDistributorConfig extends MapConfig {
"events", ImmutableMap.of(
"publish", "tcp://*:4442",
"subscribe", "tcp://*:4443",
"bind", true)));
"bind", true),
"server", ImmutableMap.of(
"port", 5553)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openqa.selenium.grid.config.CompoundConfig;
import org.openqa.selenium.grid.config.ConcatenatingConfig;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigFlags;
import org.openqa.selenium.grid.config.EnvConfig;
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.grid.distributor.local.LocalDistributor;
Expand Down Expand Up @@ -68,16 +69,18 @@ public String getDescription() {
public Executable configure(String... args) {

HelpFlags help = new HelpFlags();
BaseServerFlags serverFlags = new BaseServerFlags(5553);
ConfigFlags configFlags = new ConfigFlags();
BaseServerFlags serverFlags = new BaseServerFlags();
SessionMapFlags sessionMapFlags = new SessionMapFlags();
EventBusFlags eventBusFlags = new EventBusFlags();

JCommander commander = JCommander.newBuilder()
.programName(getName())
.addObject(help)
.addObject(eventBusFlags)
.addObject(sessionMapFlags)
.addObject(serverFlags)
.programName(getName())
.addObject(configFlags)
.addObject(eventBusFlags)
.addObject(help)
.addObject(serverFlags)
.addObject(sessionMapFlags)
.build();

return () -> {
Expand All @@ -100,6 +103,7 @@ public Executable configure(String... args) {
new AnnotatedConfig(eventBusFlags),
new AnnotatedConfig(serverFlags),
new AnnotatedConfig(sessionMapFlags),
configFlags.readConfigFiles(),
new DefaultDistributorConfig());

LoggingOptions loggingOptions = new LoggingOptions(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class DefaultNodeConfig extends MapConfig {
super(ImmutableMap.of(
"events", ImmutableMap.of(
"publish", "tcp://*:4442",
"subscribe", "tcp://*:4443")));
"subscribe", "tcp://*:4443"),
"server", ImmutableMap.of(
"port", 5555)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.openqa.selenium.grid.config.CompoundConfig;
import org.openqa.selenium.grid.config.ConcatenatingConfig;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigFlags;
import org.openqa.selenium.grid.config.EnvConfig;
import org.openqa.selenium.grid.data.NodeStatusEvent;
import org.openqa.selenium.grid.docker.DockerFlags;
Expand Down Expand Up @@ -69,19 +70,21 @@ public String getDescription() {
public Executable configure(String... args) {

HelpFlags help = new HelpFlags();
BaseServerFlags serverFlags = new BaseServerFlags(5555);
ConfigFlags configFlags = new ConfigFlags();
BaseServerFlags serverFlags = new BaseServerFlags();
EventBusFlags eventBusFlags = new EventBusFlags();
NodeFlags nodeFlags = new NodeFlags();
DockerFlags dockerFlags = new DockerFlags();

JCommander commander = JCommander.newBuilder()
.programName(getName())
.addObject(help)
.addObject(serverFlags)
.addObject(eventBusFlags)
.addObject(dockerFlags)
.addObject(nodeFlags)
.build();
.programName(getName())
.addObject(configFlags)
.addObject(dockerFlags)
.addObject(eventBusFlags)
.addObject(help)
.addObject(nodeFlags)
.addObject(serverFlags)
.build();

return () -> {
try {
Expand All @@ -104,6 +107,7 @@ public Executable configure(String... args) {
new AnnotatedConfig(eventBusFlags),
new AnnotatedConfig(nodeFlags),
new AnnotatedConfig(dockerFlags),
configFlags.readConfigFiles(),
new DefaultNodeConfig());

LoggingOptions loggingOptions = new LoggingOptions(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ java_library(
"//java/server/src/org/openqa/selenium/grid/web",
"//java/server/src/org/openqa/selenium/netty/server",
artifact("com.beust:jcommander"),
artifact("com.google.guava:guava"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.trace.Tracer;
import org.openqa.selenium.BuildInfo;
import org.openqa.selenium.cli.CliCommand;
import org.openqa.selenium.grid.config.AnnotatedConfig;
import org.openqa.selenium.grid.config.CompoundConfig;
import org.openqa.selenium.grid.config.ConcatenatingConfig;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigFlags;
import org.openqa.selenium.grid.config.EnvConfig;
import org.openqa.selenium.grid.config.MapConfig;
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.grid.distributor.config.DistributorFlags;
import org.openqa.selenium.grid.distributor.config.DistributorOptions;
Expand Down Expand Up @@ -65,17 +68,19 @@ public String getDescription() {
public Executable configure(String... args) {

HelpFlags help = new HelpFlags();
BaseServerFlags serverFlags = new BaseServerFlags(4444);
ConfigFlags configFlags = new ConfigFlags();
BaseServerFlags serverFlags = new BaseServerFlags();
SessionMapFlags sessionMapFlags = new SessionMapFlags();
DistributorFlags distributorFlags = new DistributorFlags();

JCommander commander = JCommander.newBuilder()
.programName(getName())
.addObject(help)
.addObject(serverFlags)
.addObject(sessionMapFlags)
.addObject(distributorFlags)
.build();
.programName(getName())
.addObject(configFlags)
.addObject(distributorFlags)
.addObject(help)
.addObject(serverFlags)
.addObject(sessionMapFlags)
.build();

return () -> {
try {
Expand All @@ -91,12 +96,14 @@ public Executable configure(String... args) {
}

Config config = new CompoundConfig(
new EnvConfig(),
new ConcatenatingConfig("router", '.', System.getProperties()),
new AnnotatedConfig(help),
new AnnotatedConfig(serverFlags),
new AnnotatedConfig(sessionMapFlags),
new AnnotatedConfig(distributorFlags));
new EnvConfig(),
new ConcatenatingConfig("router", '.', System.getProperties()),
new AnnotatedConfig(help),
new AnnotatedConfig(serverFlags),
new AnnotatedConfig(sessionMapFlags),
new AnnotatedConfig(distributorFlags),
configFlags.readConfigFiles(),
new MapConfig(ImmutableMap.of("server", ImmutableMap.of("port", 4444))));

LoggingOptions loggingOptions = new LoggingOptions(config);
loggingOptions.configureLogging();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,4 @@ public class BaseServerFlags {
@Parameter(description = "Use a self-signed certificate for HTTPS communication", names = "--self-signed-https", hidden = true)
@ConfigValue(section = "server", name = "https-self-signed")
private boolean isSelfSigned = false;

public BaseServerFlags(int defaultPort) {
this.port = defaultPort;
}
}
Loading

0 comments on commit 2f4cf5b

Please sign in to comment.