|
| 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