private Argument dnsArg;
public AgentParser(final String... args) throws ArgumentParserException {
super("helios-agent", "Spotify Helios Agent", args);
final Namespace options = getNamespace();
final DockerHost dockerHost = DockerHost.from(
options.getString(dockerArg.getDest()),
options.getString(dockerCertPathArg.getDest()));
final List<List<String>> env = options.getList(envArg.getDest());
final Map<String, String> envVars = Maps.newHashMap();
if (env != null) {
for (final List<String> group : env) {
for (final String s : group) {
final String[] parts = s.split("=", 2);
if (parts.length != 2) {
throw new IllegalArgumentException("Bad environment variable: " + s);
}
envVars.put(parts[0], parts[1]);
}
}
}
final InetSocketAddress httpAddress = parseSocketAddress(options.getString(httpArg.getDest()));
final String portRangeString = options.getString(portRangeArg.getDest());
final List<String> parts = Splitter.on(':').splitToList(portRangeString);
if (parts.size() != 2) {
throw new IllegalArgumentException("Bad port range: " + portRangeString);
}
final int start;
final int end;
try {
start = Integer.valueOf(parts.get(0));
end = Integer.valueOf(parts.get(1));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Bad port range: " + portRangeString);
}
if (end <= start) {
throw new IllegalArgumentException("Bad port range: " + portRangeString);
}
this.agentConfig = new AgentConfig()
.setName(getName())
.setZooKeeperConnectionString(getZooKeeperConnectString())
.setZooKeeperSessionTimeoutMillis(getZooKeeperSessionTimeoutMillis())
.setZooKeeperConnectionTimeoutMillis(getZooKeeperConnectionTimeoutMillis())
.setZooKeeperNamespace(getZooKeeperNamespace())
.setZooKeeperClusterId(getZooKeeperClusterId())
.setDomain(getDomain())
.setEnvVars(envVars)
.setDockerHost(dockerHost)
.setInhibitMetrics(getInhibitMetrics())
.setRedirectToSyslog(options.getString(syslogRedirectToArg.getDest()))
.setStateDirectory(Paths.get(options.getString(stateDirArg.getDest())))
.setStatsdHostPort(getStatsdHostPort())
.setRiemannHostPort(getRiemannHostPort())
.setPortRange(start, end)
.setSentryDsn(getSentryDsn())
.setServiceRegistryAddress(getServiceRegistryAddress())
.setServiceRegistrarPlugin(getServiceRegistrarPlugin())
.setAdminPort(options.getInt(adminArg.getDest()))
.setHttpEndpoint(httpAddress)
.setNoHttp(options.getBoolean(noHttpArg.getDest()));
final String explicitId = options.getString(agentIdArg.getDest());
if (explicitId != null) {
agentConfig.setId(explicitId);
} else {
final byte[] idBytes = new byte[20];
new SecureRandom().nextBytes(idBytes);
agentConfig.setId(base16().encode(idBytes));
}
final List<String> dns = options.getList(dnsArg.getDest());
if (!dns.isEmpty()) {
for (String d : dns) {
if (!isInetAddress(d)) {
throw new IllegalArgumentException("Invalid IP address " + d);
}