odin-vsm 1.0.0

Manage your Valheim dedicated server with confidence — a fast, single-binary Rust CLI for Docker, mods, backups, and world sync.
Documentation
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! `odin init` — interactive wizard to bootstrap a new Valheim server.
//!
//! Fetches docker-compose.yaml, valheim.env.example, and the full scripts/
//! directory from the upstream GitHub repository, prompts the user for key
//! values, and writes ready-to-use files in the current directory.

use crate::error::{Error, Result};
use colored::Colorize;
use dialoguer::{Confirm, Input, Password};
use std::collections::HashMap;
use std::fs;
use std::path::Path;

const REPO_RAW: &str = "https://raw.githubusercontent.com/RajaRakoto/odin-vsm/master";
const REPO_API: &str = "https://api.github.com/repos/RajaRakoto/odin-vsm/contents/scripts";

// ── Defaults ──────────────────────────────────────────────────────────────────

/// Static defaults that match the current valheim.env.example structure.
fn static_defaults() -> HashMap<&'static str, &'static str> {
    let mut m = HashMap::new();
    // scheduling
    m.insert("UPDATE_CRON", "30 * * * *");
    m.insert("UPDATE_IF_IDLE", "true");
    m.insert("RESTART_CRON", "30 4 * * *");
    m.insert("RESTART_IF_IDLE", "true");
    m.insert("BACKUPS", "true");
    m.insert("BACKUPS_CRON", "5 * * * *");
    m.insert("BACKUPS_DIRECTORY", "/config/backups");
    m.insert("BACKUPS_MAX_AGE", "7");
    m.insert("BACKUPS_MAX_COUNT", "168");
    m.insert("BACKUPS_ZIP", "true");
    m.insert("BACKUPS_IF_IDLE", "true");
    // mods
    m.insert("BEPINEX", "false");
    m.insert("BEPINEXCFG_Logging_DOT_Console_Enabled", "false");
    m.insert("VALHEIM_PLUS", "false");
    // windows sync
    m.insert("WIN_SSH_PORT", "22");
    m.insert("WIN_SSH_KEY", "/root/.ssh/id_ed25519_valheim_win");
    // container
    m.insert("PUID", "1000");
    m.insert("PGID", "1000");
    // patch
    m.insert("APPLY_DLL_PATCH", "false");
    m.insert("PRE_SERVER_RUN_HOOK", "/scripts/apply-patch.sh");
    // extra
    m.insert("SERVER_PUBLIC", "false");
    m.insert("CROSSPLAY", "false");
    m
}

// ── Timezone detection ────────────────────────────────────────────────────────

fn detect_timezone() -> String {
    if let Ok(tz) = fs::read_to_string("/etc/timezone") {
        let tz = tz.trim().to_string();
        if !tz.is_empty() {
            return tz;
        }
    }

    if let Ok(link) = fs::read_link("/etc/localtime") {
        let s = link.to_string_lossy();
        if let Some(pos) = s.find("zoneinfo/") {
            let tz = &s[pos + "zoneinfo/".len()..];
            if !tz.is_empty() {
                return tz.to_string();
            }
        }
    }

    if let Ok(out) = std::process::Command::new("timedatectl")
        .args(["show", "--property=Timezone", "--value"])
        .output()
    {
        let tz = String::from_utf8_lossy(&out.stdout).trim().to_string();
        if !tz.is_empty() {
            return tz;
        }
    }

    "Etc/UTC".to_string()
}

// ── HTTP helpers ──────────────────────────────────────────────────────────────

async fn build_client() -> Result<reqwest::Client> {
    reqwest::Client::builder()
        .user_agent("odin-vsm/init")
        .timeout(std::time::Duration::from_secs(20))
        .build()
        .map_err(|e| Error::network(e.to_string()))
}

async fn fetch_text(client: &reqwest::Client, url: &str) -> Result<String> {
    let resp = client
        .get(url)
        .send()
        .await
        .map_err(|e| Error::network(format!("GET {url}: {e}")))?;

    if !resp.status().is_success() {
        return Err(Error::network(format!(
            "GET {url} returned HTTP {}",
            resp.status()
        )));
    }

    resp.text().await.map_err(|e| Error::network(e.to_string()))
}

async fn fetch_bytes(client: &reqwest::Client, url: &str) -> Result<bytes::Bytes> {
    let resp = client
        .get(url)
        .send()
        .await
        .map_err(|e| Error::network(format!("GET {url}: {e}")))?;

    if !resp.status().is_success() {
        return Err(Error::network(format!(
            "GET {url} returned HTTP {}",
            resp.status()
        )));
    }

    resp.bytes()
        .await
        .map_err(|e| Error::network(e.to_string()))
}

// ── Scripts directory fetch ───────────────────────────────────────────────────

#[derive(serde::Deserialize)]
struct GhEntry {
    name: String,
    #[serde(rename = "type")]
    kind: String,
    download_url: Option<String>,
}

/// Fetch all files in scripts/ from GitHub and write them to ./scripts/.
/// Returns the list of filenames written.
async fn fetch_scripts(client: &reqwest::Client, dest: &Path) -> Result<Vec<String>> {
    let listing: Vec<GhEntry> = client
        .get(REPO_API)
        .header("Accept", "application/vnd.github+json")
        .send()
        .await
        .map_err(|e| Error::network(format!("GitHub API: {e}")))?
        .json()
        .await
        .map_err(|e| Error::network(format!("GitHub API parse: {e}")))?;

    fs::create_dir_all(dest).map_err(|e| Error::other(format!("Cannot create scripts/: {e}")))?;

    let mut written = Vec::new();

    for entry in listing {
        if entry.kind != "file" {
            continue;
        }
        let url = match entry.download_url {
            Some(ref u) => u.clone(),
            None => continue,
        };

        let content = fetch_bytes(client, &url).await?;
        let file_path = dest.join(&entry.name);
        fs::write(&file_path, &content)
            .map_err(|e| Error::other(format!("Write {}: {e}", file_path.display())))?;

        // Preserve executable bit for shell scripts
        #[cfg(unix)]
        if entry.name.ends_with(".sh") {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(&file_path)?.permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&file_path, perms)?;
        }

        written.push(entry.name);
    }

    Ok(written)
}

// ── Env file rendering ────────────────────────────────────────────────────────

/// Substitute values into the env template, preserving all comments and structure.
fn render_env(template: &str, values: &HashMap<&str, String>) -> String {
    let mut out = String::with_capacity(template.len() + 256);

    for line in template.lines() {
        let trimmed = line.trim_start();

        if trimmed.starts_with('#') || trimmed.is_empty() {
            out.push_str(line);
            out.push('\n');
            continue;
        }

        if let Some(eq) = line.find('=') {
            let key = line[..eq].trim();
            if let Some(val) = values.get(key) {
                out.push_str(key);
                out.push('=');
                out.push_str(val);
                out.push('\n');
                continue;
            }
        }

        out.push_str(line);
        out.push('\n');
    }

    out
}

// ── Interactive prompts ───────────────────────────────────────────────────────

fn prompt_str(prompt: &str, default: &str) -> Result<String> {
    Input::<String>::new()
        .with_prompt(prompt)
        .default(default.to_string())
        .interact_text()
        .map_err(|e| Error::other(e.to_string()))
}

fn prompt_password(prompt: &str) -> Result<String> {
    Password::new()
        .with_prompt(prompt)
        .with_confirmation("Confirm password", "Passwords do not match")
        .interact()
        .map_err(|e| Error::other(e.to_string()))
}

fn prompt_confirm(prompt: &str, default: bool) -> Result<bool> {
    Confirm::new()
        .with_prompt(prompt)
        .default(default)
        .interact()
        .map_err(|e| Error::other(e.to_string()))
}

// ── Public entry point ────────────────────────────────────────────────────────

pub async fn run() -> Result<()> {
    let cwd = std::env::current_dir()
        .map_err(|e| Error::other(format!("Cannot determine current directory: {e}")))?;

    println!();
    println!(
        "  {} {}",
        "odin init".cyan().bold(),
        "— Valheim Server Setup Wizard".bold()
    );
    println!("  {}", "".repeat(44).cyan());
    println!();
    println!("  {} Fetching latest files from GitHub…", "".cyan());

    let client = build_client().await?;

    let compose_url = format!("{REPO_RAW}/docker-compose.yaml");
    let env_url = format!("{REPO_RAW}/valheim.env.example");

    let (compose_content, env_template) = tokio::try_join!(
        fetch_text(&client, &compose_url),
        fetch_text(&client, &env_url)
    )?;

    println!("  {} docker-compose.yaml", "".green());
    println!("  {} valheim.env.example", "".green());
    println!();

    // ── Guard: existing files ─────────────────────────────────────────────────
    let compose_dest = cwd.join("docker-compose.yaml");
    let env_dest = cwd.join("valheim.env");
    let scripts_dest = cwd.join("scripts");

    let has_existing = compose_dest.exists() || env_dest.exists() || scripts_dest.exists();
    if has_existing {
        println!(
            "  {} Files/directories already exist in this directory:",
            "!".yellow().bold()
        );
        if compose_dest.exists() {
            println!("{}", "docker-compose.yaml".yellow());
        }
        if env_dest.exists() {
            println!("{}", "valheim.env".yellow());
        }
        if scripts_dest.exists() {
            println!("{}", "scripts/".yellow());
        }
        println!();
        let overwrite = prompt_confirm("Overwrite existing files?", false)?;
        if !overwrite {
            println!("  {} Aborted.", "".red());
            return Ok(());
        }
        println!();
    }

    // ── Detect timezone ───────────────────────────────────────────────────────
    let detected_tz = detect_timezone();

    // ── Interactive prompts ───────────────────────────────────────────────────
    println!(
        "  {}",
        "── Server Identity ──────────────────────────".bold()
    );
    println!();

    let server_name = prompt_str("  Server name (shown in browser)", "My Valheim Server")?;
    let world_name = prompt_str("  World name (save file, no extension)", "Dedicated")?;

    println!();
    println!(
        "  {}",
        "Server password must be at least 5 characters.".dimmed()
    );
    let server_pass = loop {
        let p = prompt_password("  Server password")?;
        if p.len() >= 5 {
            break p;
        }
        println!("  {} Password too short (minimum 5 characters).", "".red());
    };

    println!();
    println!(
        "  {}",
        "── Timezone ─────────────────────────────────".bold()
    );
    println!();
    println!("  {} Detected: {}", "".cyan(), detected_tz.cyan());
    let tz = prompt_str("  Timezone (tz database name)", &detected_tz)?;

    println!();
    println!(
        "  {}",
        "── Optional: Windows World Sync ─────────────".bold()
    );
    println!();
    let setup_win_sync = prompt_confirm(
        "  Configure Windows → Linux world sync (odin sync-worlds)?",
        false,
    )?;

    let (win_user, win_host, win_ssh_user) = if setup_win_sync {
        println!();
        let wu = prompt_str("  Windows account name (C:\\Users\\<name>)", "")?;
        let wh = prompt_str("  Windows machine IP or hostname", "")?;
        let wsu = prompt_str("  SSH username on Windows", &wu)?;
        (wu, wh, wsu)
    } else {
        (String::new(), String::new(), String::new())
    };

    // ── Build values map ──────────────────────────────────────────────────────
    let defaults = static_defaults();
    let mut values: HashMap<&str, String> =
        defaults.iter().map(|(&k, &v)| (k, v.to_string())).collect();

    values.insert("SERVER_NAME", server_name);
    values.insert("WORLD_NAME", world_name);
    values.insert("SERVER_PASS", server_pass);
    values.insert("TZ", tz);

    if setup_win_sync {
        values.insert("WIN_USER", win_user);
        values.insert("WIN_HOST", win_host);
        values.insert("WIN_SSH_USER", win_ssh_user);
    }

    // ── Write config files ────────────────────────────────────────────────────
    write_file(&compose_dest, compose_content.as_bytes())?;
    write_file(&env_dest, render_env(&env_template, &values).as_bytes())?;

    // ── Fetch and write scripts/ ──────────────────────────────────────────────
    println!();
    println!("  {} Fetching scripts/ from GitHub…", "".cyan());

    let script_files = fetch_scripts(&client, &scripts_dest).await?;
    for name in &script_files {
        println!("  {} scripts/{}", "".green(), name);
    }

    // ── Summary ───────────────────────────────────────────────────────────────
    println!();
    println!("  {}", "".repeat(44).cyan());
    println!();
    println!("  {} Setup complete!", "".green().bold());
    println!();
    println!("  Files written:");
    println!("    • docker-compose.yaml");
    println!("    • valheim.env");
    for name in &script_files {
        println!("    • scripts/{name}");
    }
    println!();
    println!("  Next steps:");
    println!(
        "    {}  Review valheim.env and adjust any remaining values.",
        "1.".cyan()
    );
    println!(
        "    {}  Run `odin health` to verify your environment.",
        "2.".cyan()
    );
    println!(
        "    {}  Run `odin start` to launch the server.",
        "3.".cyan()
    );
    println!();

    Ok(())
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn write_file(path: &Path, content: &[u8]) -> Result<()> {
    fs::write(path, content)
        .map_err(|e| Error::other(format!("Failed to write {}: {e}", path.display())))
}