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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
use bollard::Docker;
use bollard::container::{
Config as ContainerConfig, CreateContainerOptions, RemoveContainerOptions,
StartContainerOptions, StopContainerOptions,
};
use bollard::image::CreateImageOptions;
use bollard::models::HostConfig;
use futures_util::StreamExt;
use std::collections::HashMap;
use crate::config::Config;
use crate::models::Instance;
#[derive(Clone)]
pub struct DockerManager {
client: Docker,
config: Config,
}
impl DockerManager {
pub fn new(config: Config) -> Result<Self, bollard::errors::Error> {
let client = Docker::connect_with_local_defaults()?;
Ok(Self { client, config })
}
/// Map a Minecraft version string (e.g. "1.12.2", "1.20.1") to the
/// appropriate itzg/minecraft-server Java image tag.
///
/// - < 1.17 → java8 (Minecraft 1.12.2 and earlier)
/// - 1.17 .. 1.20.4 → java17
/// - >= 1.20.5 → java21
fn java_tag(version: &str) -> &'static str {
// Parse major.minor from version strings like "1.12.2", "1.20.1", "1.21"
let parts: Vec<&str> = version.split('.').collect();
if parts.len() < 2 {
return "java17"; // sensible default
}
let major: u32 = parts.first().and_then(|s| s.parse().ok()).unwrap_or(1);
let minor: u32 = parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(12);
if major <= 1 && minor < 17 {
"java8"
} else if major <= 1 && minor < 21 {
// 1.17.x through 1.20.4 use Java 17
"java17"
} else {
// 1.20.5+ and 1.21+ use Java 21
"java21"
}
}
/// Build the full image reference, e.g. "itzg/minecraft-server:java8"
fn image_ref(version: &str) -> String {
format!("itzg/minecraft-server:{}", Self::java_tag(version))
}
pub async fn start_container(&self, instance: &Instance) -> Result<(), String> {
let tag = Self::java_tag(&instance.version);
tracing::info!("Pulling itzg/minecraft-server:{tag} image...");
self.pull_image(tag).await?;
tracing::info!("Creating container for instance '{}'", instance.name);
self.recreate_container(instance).await?;
tracing::info!("Starting container...");
self.start_existing().await
}
async fn pull_image(&self, tag: &str) -> Result<(), String> {
let options = CreateImageOptions {
from_image: "itzg/minecraft-server",
tag,
..Default::default()
};
let mut stream = self.client.create_image(Some(options), None, None);
while let Some(result) = stream.next().await {
match result {
Ok(_info) => {}
Err(e) => return Err(format!("Image pull failed: {e}")),
}
}
Ok(())
}
async fn recreate_container(&self, instance: &Instance) -> Result<(), String> {
// Remove existing container if present
let remove_options = RemoveContainerOptions {
force: true,
..Default::default()
};
match self
.client
.remove_container("mc", Some(remove_options))
.await
{
Ok(_) => tracing::info!("Removed existing container"),
Err(e) => tracing::debug!("No existing container to remove: {e}"),
}
let server_dir = self
.config
.server_directory
.canonicalize()
.unwrap_or_else(|_| self.config.server_directory.clone())
.display()
.to_string();
let instance_data = format!("{server_dir}/instances/{}/data", instance.name);
let image_ref = Self::image_ref(&instance.version);
let mut env_strings: Vec<String> = vec![
"EULA=TRUE".into(),
format!("VERSION={}", instance.version),
format!("TYPE={}", instance.server_type),
format!("MEMORY={}M", instance.memory_mb),
format!("MAX_PLAYERS={}", instance.max_players),
format!("DIFFICULTY={}", instance.difficulty),
"FORCE_GAMEMODE=true".into(),
"SPAWN_PROTECTION=0".into(),
format!("ENABLE_WHITELIST={}", instance.whitelist_enabled),
"PAUSE_WHEN_EMPTY_SECONDS=30".into(),
"ONLINE_MODE=false".into(),
"TZ=Europe/Warsaw".into(),
"USE_AIKAR_FLAGS=true".into(),
"ENABLE_RCON=true".into(),
format!("RCON_PASSWORD={}", instance.rcon_password),
"RCON_PORT=25575".into(),
"JVM_OPTS=-Dfml.queryResult=confirm".into(),
format!("MOTD={}", instance.motd),
format!("GAMEMODE={}", instance.gamemode),
format!("LEVEL_TYPE={}", instance.level_type),
];
if instance.pvp {
env_strings.push("PVP=true".into());
} else {
env_strings.push("PVP=false".into());
}
if let Some(ref seed) = instance.world_seed
&& !seed.is_empty()
{
env_strings.push(format!("LEVEL_SEED={seed}"));
}
let mut exposed_ports: HashMap<String, HashMap<(), ()>> = HashMap::new();
exposed_ports.insert("25565/tcp".into(), HashMap::new());
exposed_ports.insert("25575/tcp".into(), HashMap::new());
let mut port_bindings = HashMap::new();
port_bindings.insert(
"25565/tcp".to_string(),
Some(vec![bollard::models::PortBinding {
host_ip: None,
host_port: Some("25565".to_string()),
}]),
);
port_bindings.insert(
"25575/tcp".to_string(),
Some(vec![bollard::models::PortBinding {
host_ip: None,
host_port: Some("25575".to_string()),
}]),
);
let host_config = HostConfig {
binds: Some(vec![format!("{instance_data}:/data")]),
port_bindings: Some(port_bindings),
network_mode: Some("host".into()),
..Default::default()
};
let container_config = ContainerConfig {
image: Some(image_ref.clone()),
tty: Some(true),
env: Some(env_strings.clone()),
exposed_ports: Some(exposed_ports),
host_config: Some(host_config),
..Default::default()
};
let options = CreateContainerOptions {
name: "mc",
..Default::default()
};
self.client
.create_container(Some(options), container_config)
.await
.map_err(|e| format!("Failed to create container: {e}"))?;
tracing::info!("Container 'mc' created successfully");
Ok(())
}
async fn start_existing(&self) -> Result<(), String> {
self.client
.start_container("mc", None::<StartContainerOptions<&str>>)
.await
.map_err(|e| format!("Failed to start container: {e}"))?;
Ok(())
}
pub async fn stop_container(&self) -> Result<(), String> {
let options = StopContainerOptions { t: 30 };
self.client
.stop_container("mc", Some(options))
.await
.map_err(|e| format!("Failed to stop container: {e}"))?;
Ok(())
}
}
|