This repository has been archived by the owner on Jun 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
MesosClusterConfig.java
122 lines (96 loc) · 3.89 KB
/
MesosClusterConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package org.apache.mesos.mini.mesos;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import org.apache.commons.lang.StringUtils;
import org.apache.mesos.mini.MesosCluster;
import java.util.Map;
import java.util.TreeMap;
public class MesosClusterConfig {
public final DockerClient dockerClient;
public final int numberOfSlaves;
public final String[] slaveResources;
public final Integer mesosMasterPort;
public final Integer privateRegistryPort;
public final Map<String,String> extraEnvironmentVariables;
private MesosClusterConfig(
DockerClient dockerClient,
int numberOfSlaves,
String[] slaveResources,
Integer mesosMasterPort,
Integer privateRegistryPort,
Map<String,String> extraEnvironmentVariables
) {
this.dockerClient = dockerClient;
this.numberOfSlaves = numberOfSlaves;
this.slaveResources = slaveResources;
this.mesosMasterPort = mesosMasterPort;
this.privateRegistryPort = privateRegistryPort;
this.extraEnvironmentVariables = extraEnvironmentVariables;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
DockerClient dockerClient;
int numberOfSlaves = 3;
String[] slaveResources = new String[]{};
Integer mesosMasterPort = 5050;
Integer privateRegistryPort = 5000;
Map<String,String> extraEnvironmentVariables = new TreeMap<>();
private Builder() {
}
public Builder dockerClient(DockerClient dockerClient) {
this.dockerClient = dockerClient;
return this;
}
public Builder numberOfSlaves(int numberOfSlaves) {
this.numberOfSlaves = numberOfSlaves;
return this;
}
public Builder slaveResources(String[] slaveResources) {
this.slaveResources = slaveResources;
return this;
}
public Builder privateRegistryPort(int port){
this.privateRegistryPort = port;
return this;
}
public Builder masterPort(int port) {
this.mesosMasterPort = port;
return this;
}
public Builder extraEnvironmentVariables(Map<String,String> extraEnvironmentVariables) {
this.extraEnvironmentVariables = extraEnvironmentVariables;
return this;
}
public Builder defaultDockerClient() {
DockerClientConfig.DockerClientConfigBuilder builder = DockerClientConfig.createDefaultConfigBuilder();
String dockerHostEnv = System.getenv("DOCKER_HOST");
if (StringUtils.isBlank(dockerHostEnv)) {
builder.withUri("unix:///var/run/docker.sock");
}
DockerClientConfig config = builder.build();
this.dockerClient = DockerClientBuilder.getInstance(config).build();
return this;
}
public MesosCluster build() {
if (numberOfSlaves <= 0) {
throw new IllegalStateException("At least one slave is required to run a mesos cluster");
}
if (slaveResources.length != numberOfSlaves) {
throw new IllegalStateException("Please provide one resource config for each slave");
}
if (dockerClient == null) {
defaultDockerClient();
if (dockerClient == null) {
throw new IllegalStateException("Specify a docker dockerClient");
}
}
return new MesosCluster(new MesosClusterConfig(dockerClient, numberOfSlaves, slaveResources, mesosMasterPort, privateRegistryPort, extraEnvironmentVariables));
}
}
public int getNumberOfSlaves() {
return numberOfSlaves;
}
}