Skip to content

Commit d07bd57

Browse files
committed
feat: add basic container management functionality
0 parents  commit d07bd57

31 files changed

+1918
-0
lines changed

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*
24+
25+
target
26+
27+
.idea

docker-compose.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
version: '3'
2+
services:
3+
bungeecord:
4+
image: itzg/bungeecord
5+
environment:
6+
- UID=1000
7+
ports:
8+
- 25565:25577
9+
volumes:
10+
- ./target/assembly/ContainerManager.jar:/server/plugins/ContainerManager.jar
11+
- //var/run/docker.sock:/var/run/docker.sock
12+
networks:
13+
- local
14+
15+
redis:
16+
image: bitnami/redis:latest
17+
environment:
18+
- ALLOW_EMPTY_PASSWORD=yes
19+
networks:
20+
- local
21+
22+
eu-lobby-1:
23+
image: itzg/minecraft-server
24+
environment:
25+
- VERSION=1.12.2
26+
- SERVER_TYPE=spigot
27+
- SERVER_PORT=25565
28+
- SERVER_NAME=eu-lobby-1
29+
- SERVER_GROUP=eu-lobby
30+
- SERVER_CATEGORY=eu-lobby
31+
- ONLINE_MODE=FALSE
32+
- EULA=true
33+
networks:
34+
- local
35+
36+
eu-lobby-2:
37+
image: itzg/minecraft-server
38+
environment:
39+
- VERSION=1.12.2
40+
- SERVER_TYPE=spigot
41+
- SERVER_PORT=25565
42+
- SERVER_NAME=eu-lobby-2
43+
- SERVER_GROUP=eu-lobby
44+
- SERVER_CATEGORY=eu-lobby
45+
- ONLINE_MODE=FALSE
46+
- EULA=true
47+
networks:
48+
- local
49+
50+
us-lobby-1:
51+
image: itzg/minecraft-server
52+
environment:
53+
- VERSION=1.12.2
54+
- SERVER_TYPE=spigot
55+
- SERVER_PORT=25565
56+
- SERVER_NAME=us-lobby-1
57+
- SERVER_GROUP=us-lobby
58+
- SERVER_CATEGORY=us-lobby
59+
- SERVER_TAGS=some,awesome,tags
60+
- ONLINE_MODE=FALSE
61+
- EULA=true
62+
networks:
63+
- local
64+
networks:
65+
local:

pom.xml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>de.craftmania</groupId>
8+
<artifactId>ContainerManager</artifactId>
9+
<version>0.1-beta1</version>
10+
11+
<repositories>
12+
<repository>
13+
<id>bungeecord-repo</id>
14+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
15+
</repository>
16+
</repositories>
17+
18+
<build>
19+
<plugins>
20+
<plugin>
21+
<groupId>org.apache.maven.plugins</groupId>
22+
<artifactId>maven-shade-plugin</artifactId>
23+
<version>3.1.1</version>
24+
<executions>
25+
<execution>
26+
<phase>package</phase>
27+
<goals>
28+
<goal>shade</goal>
29+
</goals>
30+
<configuration>
31+
<outputFile>target/assembly/${project.artifactId}.jar</outputFile>
32+
<artifactSet>
33+
<includes>
34+
</includes>
35+
</artifactSet>
36+
</configuration>
37+
</execution>
38+
</executions>
39+
40+
<configuration>
41+
<shadedArtifactAttached>true</shadedArtifactAttached>
42+
<createDependencyReducedPom>true</createDependencyReducedPom>
43+
<minimizeJar>false</minimizeJar>
44+
<filters>
45+
<filter>
46+
<artifact>*:*</artifact>
47+
<excludes>
48+
<exclude>META-INF/*.SF</exclude>
49+
<exclude>META-INF/*.DSA</exclude>
50+
<exclude>META-INF/*.RSA</exclude>
51+
</excludes>
52+
</filter>
53+
</filters>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
<dependencies>
60+
<dependency>
61+
<groupId>net.md-5</groupId>
62+
<artifactId>bungeecord-api</artifactId>
63+
<version>1.12-SNAPSHOT</version>
64+
<type>jar</type>
65+
<scope>provided</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>net.md-5</groupId>
69+
<artifactId>bungeecord-api</artifactId>
70+
<version>1.12-SNAPSHOT</version>
71+
<type>javadoc</type>
72+
<scope>provided</scope>
73+
</dependency>
74+
<dependency>
75+
<groupId>com.github.docker-java</groupId>
76+
<artifactId>docker-java</artifactId>
77+
<version>3.1.0-rc-2</version>
78+
<scope>compile</scope>
79+
</dependency>
80+
<dependency>
81+
<groupId>redis.clients</groupId>
82+
<artifactId>jedis</artifactId>
83+
<version>2.9.0</version>
84+
<type>jar</type>
85+
<scope>compile</scope>
86+
</dependency>
87+
</dependencies>
88+
</project>
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package de.craftmania.dockerized_craft.container_management;
2+
3+
import com.github.dockerjava.api.DockerClient;
4+
import de.craftmania.dockerized_craft.container_management.connection.BalancedReconnectHandler;
5+
import de.craftmania.dockerized_craft.container_management.connection.session.RedisSessionStorage;
6+
import de.craftmania.dockerized_craft.container_management.connection.session.SessionStorage;
7+
import de.craftmania.dockerized_craft.container_management.notifier.ServerListNotifier;
8+
import de.craftmania.dockerized_craft.container_management.server.DockerEventObserverFactory;
9+
import de.craftmania.dockerized_craft.container_management.connection.Balancer;
10+
import de.craftmania.dockerized_craft.container_management.docker.DockerClientFactory;
11+
import de.craftmania.dockerized_craft.container_management.docker.DockerUtils;
12+
import de.craftmania.dockerized_craft.container_management.docker.event.ObserverInterface;
13+
import net.md_5.bungee.api.ReconnectHandler;
14+
import net.md_5.bungee.api.plugin.Plugin;
15+
import net.md_5.bungee.config.*;
16+
17+
import java.io.IOException;
18+
import java.io.File;
19+
import java.io.InputStream;
20+
import java.nio.file.Files;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.concurrent.TimeUnit;
24+
25+
@SuppressWarnings("unused")
26+
public class ContainerManager extends Plugin {
27+
private Configuration configuration;
28+
29+
@Override
30+
public void onEnable() {
31+
try {
32+
this.loadConfiguration();
33+
} catch (IOException e) {
34+
getLogger().warning("Not able to write Configuration File.");
35+
getLogger().warning("Stopped Plugin enabling (Plugin will not work!)");
36+
e.printStackTrace();
37+
return;
38+
}
39+
40+
if (this.configuration.getBoolean("connection_balancer.enabled")) {
41+
this.getLogger().info("[Connection Balancer] Enabled!");
42+
this.bootstrapConnectionBalancer();
43+
}
44+
45+
if (this.configuration.getBoolean("docker.enabled")) {
46+
this.getLogger().info("[Container Inspector] Enabled!");
47+
this.bootstrapDockerEvents(this.getDockerEventObservers());
48+
}
49+
50+
if (this.configuration.getBoolean("plugin_notifier.enabled")) {
51+
this.bootstrapPluginNotifier();
52+
}
53+
54+
}
55+
56+
private void bootstrapDockerEvents(List<ObserverInterface> observers) {
57+
58+
DockerClient dockerClient = DockerClientFactory.getByConfiguration(configuration);
59+
DockerUtils dockerUtils = new DockerUtils(dockerClient);
60+
61+
getProxy().getScheduler().runAsync(this,dockerUtils.getRunnableEventBootstrapperWithObservers(
62+
observers,
63+
this.configuration.getString("docker.event_listener.network"),
64+
getLogger()
65+
));
66+
67+
getProxy().getScheduler().runAsync(this, dockerUtils.getRunnableEventSubscriberWithObservers(
68+
observers,
69+
this.configuration.getString("docker.event_listener.network"),
70+
getLogger()
71+
));
72+
73+
}
74+
75+
private void bootstrapConnectionBalancer() {
76+
Balancer balancer = new Balancer(
77+
this.configuration.getSection("connection_balancer.groups"),
78+
this.configuration.getString("connection_balancer.environment_variables.group_key"),
79+
this.configuration.getString("connection_balancer.environment_variables.forced_host_key"),
80+
this.configuration.getSection("connection_balancer.forced_hosts"),
81+
this.configuration.getString("connection_balancer.default_group"),
82+
getLogger()
83+
);
84+
85+
SessionStorage sessionStorage = new RedisSessionStorage(
86+
this.configuration.getString("connection_balancer.player_session_store.redis.host"),
87+
this.configuration.getInt("connection_balancer.player_session_store.redis.port"),
88+
this.configuration.getBoolean("connection_balancer.player_session_store.redis.ssl")
89+
);
90+
ReconnectHandler reconnectHandler = new BalancedReconnectHandler(balancer ,getProxy(), sessionStorage);
91+
getProxy().setReconnectHandler(reconnectHandler);
92+
getProxy().getPluginManager().registerListener(this, balancer);
93+
}
94+
95+
96+
private void loadConfiguration() throws IOException {
97+
98+
if (!getDataFolder().exists()) {
99+
if (!getDataFolder().mkdir()) {
100+
throw new IOException("Not able to generate Plugin Data Folder");
101+
}
102+
}
103+
104+
File file = new File(getDataFolder(), "config.yml");
105+
106+
if (!file.exists()) {
107+
try (InputStream in = getResourceAsStream("config.yml")) {
108+
Files.copy(in, file.toPath());
109+
} catch (IOException e) {
110+
e.printStackTrace();
111+
}
112+
}
113+
try {
114+
this.configuration = ConfigurationProvider.getProvider(
115+
YamlConfiguration.class).load(new File(getDataFolder(), "config.yml")
116+
);
117+
} catch (IOException e) {
118+
e.printStackTrace();
119+
}
120+
121+
}
122+
123+
@SuppressWarnings("unused")
124+
public Configuration getConfiguration() {
125+
return configuration;
126+
}
127+
128+
private List<ObserverInterface> getDockerEventObservers() {
129+
130+
List<ObserverInterface> observers = new ArrayList<>(1);
131+
132+
observers.add(DockerEventObserverFactory.getByConfiguration(
133+
this.configuration,
134+
getProxy(),
135+
getLogger()
136+
));
137+
138+
return observers;
139+
}
140+
141+
private void bootstrapPluginNotifier() {
142+
ServerListNotifier notifier = new ServerListNotifier(
143+
this.configuration.getSection("plugin_notifier.meta_data_mapper"),
144+
getLogger()
145+
);
146+
147+
getProxy().getPluginManager().registerListener(this, notifier);
148+
getProxy().getScheduler().schedule(this, () -> {
149+
notifier.sendUpdate();
150+
}, 0, 10, TimeUnit.SECONDS);
151+
}
152+
153+
}

0 commit comments

Comments
 (0)