Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.

Commit

Permalink
Make master and agent support HTTP[s]
Browse files Browse the repository at this point in the history
  • Loading branch information
davidxia committed Aug 26, 2015
1 parent fef0289 commit 418633f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@

import com.spotify.helios.servicescommon.DockerHost;

import io.dropwizard.Configuration;

import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import io.dropwizard.Configuration;

/**
* The configuration of the Helios agent.
*/
Expand All @@ -56,8 +56,8 @@ public class AgentConfig extends Configuration {
private Path serviceRegistrarPlugin;
private String id;
private List<String> dns;
private int adminPort;
private InetSocketAddress httpEndpoint;
private URI adminUriEndpoint;
private URI uriEndpoint;
private boolean noHttp;
private List<String> binds;
private List<String> kafkaBrokers;
Expand Down Expand Up @@ -247,22 +247,22 @@ public AgentConfig setDns(List<String> dns) {
return this;
}

public AgentConfig setAdminPort(int adminPort) {
this.adminPort = adminPort;
public AgentConfig setAdminUriEndpoint(URI adminUriEndpoint) {
this.adminUriEndpoint = adminUriEndpoint;
return this;
}

public AgentConfig setHttpEndpoint(InetSocketAddress httpEndpoint) {
this.httpEndpoint = httpEndpoint;
public AgentConfig setUriEndpoint(URI uriEndpoint) {
this.uriEndpoint = uriEndpoint;
return this;
}

public int getAdminPort() {
return adminPort;
public URI getAdminUriEndpoint() {
return adminUriEndpoint;
}

public InetSocketAddress getHttpEndpoint() {
return httpEndpoint;
public URI getUriEndpoint() {
return uriEndpoint;
}

public AgentConfig setNoHttp(boolean noHttp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;

import java.net.InetSocketAddress;
import java.net.URI;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -81,7 +81,8 @@ public AgentParser(final String... args) throws ArgumentParserException {
"\nLabels need to be in the format key=value.");
}

final InetSocketAddress httpAddress = parseSocketAddress(options.getString(httpArg.getDest()));
final URI httpAddress = parseUriAddress(options.getString(httpArg.getDest()));
final URI adminAddress = parseUriAddress(options.getString(adminArg.getDest()));

final String portRangeString = options.getString(portRangeArg.getDest());
final List<String> parts = Splitter.on(':').splitToList(portRangeString);
Expand Down Expand Up @@ -119,8 +120,8 @@ public AgentParser(final String... args) throws ArgumentParserException {
.setSentryDsn(getSentryDsn())
.setServiceRegistryAddress(getServiceRegistryAddress())
.setServiceRegistrarPlugin(getServiceRegistrarPlugin())
.setAdminPort(options.getInt(adminArg.getDest()))
.setHttpEndpoint(httpAddress)
.setAdminUriEndpoint(adminAddress)
.setUriEndpoint(httpAddress)
.setNoHttp(options.getBoolean(noHttpArg.getDest()))
.setKafkaBrokers(getKafkaBrokers())
.setLabels(labels);
Expand Down Expand Up @@ -164,12 +165,11 @@ protected void addArgs(final ArgumentParser parser) {

httpArg = parser.addArgument("--http")
.setDefault("http://0.0.0.0:5803")
.help("http endpoint");
.help("http[s] endpoint");

adminArg = parser.addArgument("--admin")
.type(Integer.class)
.setDefault(5804)
.help("admin http port");
.setDefault("http://0.0.0.0:5804")
.help("admin http[s] port");

agentIdArg = parser.addArgument("--id")
.help("Agent unique ID. Generated and persisted on first run if not specified.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.dropwizard.configuration.ConfigurationException;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.setup.Environment;

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
Expand All @@ -76,6 +72,10 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.dropwizard.configuration.ConfigurationException;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.setup.Environment;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.spotify.helios.agent.Agent.EMPTY_EXECUTIONS;
Expand Down Expand Up @@ -300,7 +300,9 @@ public AgentService(final AgentConfig config, final Environment environment)
environment.healthChecks().register("zookeeper", zkHealthChecker);
environment.lifecycle().manage(this);

this.server = ServiceUtil.createServerFactory(config.getHttpEndpoint(), config.getAdminPort(),
this.server = ServiceUtil.createServerFactory(
config.getUriEndpoint(),
config.getAdminUriEndpoint(),
config.getNoHttp())
.build(environment);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

package com.spotify.helios.master;

import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.file.Path;
import java.util.List;

Expand All @@ -48,8 +48,8 @@ public class MasterConfig extends Configuration {
private String zooKeeperNamespace;
private String zooKeeperClusterId;
private boolean noZooKeeperMasterRegistration;
private int adminPort;
private InetSocketAddress httpEndpoint;
private URI adminUriEndpoint;
private URI uriEndpoint;
private List<String> kafkaBrokers;
private Path stateDirectory;

Expand Down Expand Up @@ -179,13 +179,13 @@ public MasterConfig setServiceRegistrarPlugin(final Path serviceRegistrarPlugin)
return this;
}

public MasterConfig setAdminPort(int adminPort) {
this.adminPort = adminPort;
public MasterConfig setAdminUriEndpoint(URI adminUriEndpoint) {
this.adminUriEndpoint = adminUriEndpoint;
return this;
}

public MasterConfig setHttpEndpoint(InetSocketAddress httpEndpoint) {
this.httpEndpoint = httpEndpoint;
public MasterConfig setUriEndpoint(URI uriEndpoint) {
this.uriEndpoint = uriEndpoint;
return this;
}

Expand All @@ -207,11 +207,11 @@ public MasterConfig setStateDirectory(final Path stateDirectory) {
return this;
}

public int getAdminPort() {
return adminPort;
public URI getAdminUriEndpoint() {
return adminUriEndpoint;
}

public InetSocketAddress getHttpEndpoint() {
return httpEndpoint;
public URI getUriEndpoint() {
return uriEndpoint;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;

import java.net.InetSocketAddress;
import java.net.URI;

/**
* Parses command-line arguments to produce the {@link MasterConfig}.
Expand All @@ -44,9 +44,10 @@ public MasterParser(final String... args) throws ArgumentParserException {
super("helios-master", "Spotify Helios Master", args);

final Namespace options = getNamespace();
final InetSocketAddress httpAddress = parseSocketAddress(options.getString(httpArg.getDest()));
final URI httpAddress = parseUriAddress(options.getString(httpArg.getDest()));
final URI adminAddress = parseUriAddress(options.getString(adminArg.getDest()));

final MasterConfig config = new MasterConfig()
this.masterConfig = new MasterConfig()
.setZooKeeperConnectString(getZooKeeperConnectString())
.setZooKeeperSessionTimeoutMillis(getZooKeeperSessionTimeoutMillis())
.setZooKeeperConnectionTimeoutMillis(getZooKeeperConnectionTimeoutMillis())
Expand All @@ -61,24 +62,21 @@ public MasterParser(final String... args) throws ArgumentParserException {
.setSentryDsn(getSentryDsn())
.setServiceRegistryAddress(getServiceRegistryAddress())
.setServiceRegistrarPlugin(getServiceRegistrarPlugin())
.setAdminPort(options.getInt(adminArg.getDest()))
.setHttpEndpoint(httpAddress)
.setAdminUriEndpoint(adminAddress)
.setUriEndpoint(httpAddress)
.setKafkaBrokers(getKafkaBrokers())
.setStateDirectory(getStateDirectory());

this.masterConfig = config;
}

@Override
protected void addArgs(final ArgumentParser parser) {
httpArg = parser.addArgument("--http")
.setDefault("http://0.0.0.0:5801")
.help("http endpoint");
.help("http[s] endpoint");

adminArg = parser.addArgument("--admin")
.type(Integer.class)
.setDefault(5802)
.help("admin http port");
.setDefault("http://0.0.0.0:5802")
.help("admin http[s] port");
}

public MasterConfig getMasterConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public MasterService(final MasterConfig config, final Environment environment,
environment.jersey().register(new DeploymentGroupResource(model));

final DefaultServerFactory serverFactory = ServiceUtil.createServerFactory(
config.getHttpEndpoint(), config.getAdminPort(), false);
config.getUriEndpoint(), config.getAdminUriEndpoint(), false);

final RequestLogFactory requestLog = new RequestLogFactory();
requestLog.setAppenders(ImmutableList.<AppenderFactory>of());
Expand Down Expand Up @@ -269,7 +269,7 @@ protected void startUp() throws Exception {
}

final ServiceRegistration serviceRegistration = ServiceRegistration.newBuilder()
.endpoint("helios", "http", config.getHttpEndpoint().getPort(),
.endpoint("helios", "http", config.getUriEndpoint().getPort(),
config.getDomain(), config.getName())
.build();
registrar.register(serviceRegistration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
Expand Down Expand Up @@ -277,14 +276,11 @@ private static String exec(final String command) {
}
}

protected InetSocketAddress parseSocketAddress(final String addressString) {
final InetSocketAddress address;
protected URI parseUriAddress(final String addressString) {
try {
final URI u = new URI(addressString);
address = new InetSocketAddress(u.getHost(), u.getPort());
return new URI(addressString);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Bad address: " + addressString, e);
throw new IllegalArgumentException("Bad URI address: " + addressString, e);
}
return address;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,63 @@

package com.spotify.helios.servicescommon;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;

import com.spotify.helios.common.HeliosRuntimeException;

import java.net.URI;
import java.util.Collections;
import java.util.List;

import io.dropwizard.jetty.ConnectorFactory;
import io.dropwizard.jetty.HttpConnectorFactory;
import io.dropwizard.jetty.HttpsConnectorFactory;
import io.dropwizard.server.DefaultServerFactory;

import java.net.InetSocketAddress;
import java.util.Collections;

public class ServiceUtil {
public static DefaultServerFactory createServerFactory(final InetSocketAddress httpEndpoint,
final int adminPort,
final boolean noHttp) {
// TODO(drewc) be more flexible on the httpEndpoint -- make it a URI -- so if/when we support
// SSL, it'll *just work*

private static final List<String> VALID_PROTOCOLS = ImmutableList.of("http", "https");

public static DefaultServerFactory createServerFactory(final URI endpoint,
final URI adminEndpoint,
final boolean noHttp) {
final DefaultServerFactory serverFactory = new DefaultServerFactory();
if (noHttp) {
serverFactory.setApplicationConnectors(Collections.<ConnectorFactory>emptyList());
serverFactory.setAdminConnectors(Collections.<ConnectorFactory>emptyList());
} else {
final HttpConnectorFactory serviceConnector = new HttpConnectorFactory();
serviceConnector.setPort(httpEndpoint.getPort());
serviceConnector.setBindHost(httpEndpoint.getHostString());
final String endpointScheme = endpoint.getScheme();
final HttpConnectorFactory serviceConnector = getServiceConnector(endpointScheme);
serviceConnector.setPort(endpoint.getPort());
serviceConnector.setBindHost(endpoint.getHost());
serverFactory.setApplicationConnectors(ImmutableList.<ConnectorFactory>of(serviceConnector));

final HttpConnectorFactory adminConnector = new HttpConnectorFactory();
adminConnector.setPort(adminPort);
final String adminEndpointScheme = adminEndpoint.getScheme();
final HttpConnectorFactory adminConnector = getServiceConnector(adminEndpointScheme);
adminConnector.setPort(adminEndpoint.getPort());
serviceConnector.setBindHost(adminEndpoint.getHost());
serverFactory.setAdminConnectors(ImmutableList.<ConnectorFactory>of(adminConnector));
}

return serverFactory;
}

private static HttpConnectorFactory getServiceConnector(final String scheme) {
final HttpConnectorFactory serviceConnector;
switch (scheme) {
case "http":
serviceConnector = new HttpConnectorFactory();
break;
case "https":
serviceConnector = new HttpsConnectorFactory();
break;
default:
throw new HeliosRuntimeException(String.format(
"Unrecognized server endpoint scheme of '%s'. Must be one of %s.",
scheme, Joiner.on(", ").join(VALID_PROTOCOLS)));
}

return serviceConnector;
}
}

0 comments on commit 418633f

Please sign in to comment.