From 633a0ef410035211d35e498c85a0940b408b49e2 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Sun, 5 Jan 2025 05:15:14 +0100 Subject: [PATCH] add docker support --- installs/full.toml | 3 +++ src/config.rs | 21 +++++++++++---------- src/install/docker.rs | 24 ++++++++++++++++++++++++ src/install/mod.rs | 12 ++++++++++-- src/install/user.rs | 2 +- src/print.rs | 18 ++++++++++++++++-- 6 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 src/install/docker.rs diff --git a/installs/full.toml b/installs/full.toml index 42e8a1f..1367d3c 100644 --- a/installs/full.toml +++ b/installs/full.toml @@ -58,6 +58,9 @@ password = "testpass" # Allow user to use `doas` as root doas_root= true +# Add user to Docker group +docker = true + # SSH Configuration # If `[ssh]` is set, openssh will be installed and enabled. [ssh] diff --git a/src/config.rs b/src/config.rs index 710fffd..30fbecc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,7 @@ use std::fmt::Display; use serde::Deserialize; /// Declarative install configuration -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub struct InstallConfig { /// Drive Configuration pub drive: DriveConfig, @@ -19,32 +19,33 @@ pub struct InstallConfig { pub ai: Option, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub struct OllamaConfig { pub models: Option>, pub gpu: bool, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub struct SSHConfig { pub sshd_config: Option, pub key: Option>, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub struct SSHKey { pub key: String, pub users: Vec, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub struct UserConfig { pub name: String, pub password: String, - pub doas_root: bool, + pub doas_root: Option, + pub docker: Option, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub struct PackageConfig { /// Packages to install pub pkg: Vec, @@ -54,7 +55,7 @@ pub struct PackageConfig { pub docker: Option, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub struct DriveConfig { /// Boot Drive Path pub boot: String, @@ -64,7 +65,7 @@ pub struct DriveConfig { pub encryption: Option, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub struct GeneralConfig { /// Presets pub mode: InstallMode, @@ -82,7 +83,7 @@ pub struct GeneralConfig { pub root_password: Option, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub enum InstallMode { /// Basic Arch Linux Installation Base, diff --git a/src/install/docker.rs b/src/install/docker.rs new file mode 100644 index 0000000..b89e34c --- /dev/null +++ b/src/install/docker.rs @@ -0,0 +1,24 @@ +use crate::{ + config::UserConfig, + linux::{arch_chroot, systemd_service_enable}, + pkg::install_pkgs, + print_status, +}; + +/// Setup docker on the system +pub fn setup_docker(conf: &[UserConfig]) { + print_status("Setting up Docker"); + install_pkgs(&["docker", "docker-compose"]); + + systemd_service_enable("docker.service"); + + for user in conf { + if user.docker.unwrap_or_default() { + arch_chroot( + &vec!["usermod", "-a", "-G", "docker", user.name.as_str()], + None, + false, + ); + } + } +} diff --git a/src/install/mod.rs b/src/install/mod.rs index 207704f..e945415 100644 --- a/src/install/mod.rs +++ b/src/install/mod.rs @@ -4,6 +4,7 @@ // DRIVE SELECTION use boot::setup_bootloader; +use docker::setup_docker; use drives::{format_drives, mount_drives}; use first_boot::{first_boot_values, genfstab}; use kernel::setup_mkinitcpio; @@ -17,6 +18,7 @@ use yansi::{Color, Paint}; use zram::setup_zram; pub mod boot; +pub mod docker; pub mod drives; pub mod first_boot; pub mod kernel; @@ -85,7 +87,7 @@ pub fn install(conf: InstallConfig) { // System Setup first_boot_values(&conf.general); setup_skel(&conf.general); - setup_users(&conf.user.unwrap_or_default()); + setup_users(&conf.user.as_ref().unwrap_or(&Vec::new())); setup_ssh(conf.ssh); @@ -118,7 +120,13 @@ pub fn install(conf: InstallConfig) { } if conf.pkg.docker.unwrap_or_default() { - // TODO : Enable docker + let user_conf = if let Some(user_conf) = &conf.user { + user_conf.clone() + } else { + Vec::new() + }; + + setup_docker(&user_conf); } if let Some(ai) = conf.ai { diff --git a/src/install/user.rs b/src/install/user.rs index de8c82c..ad0ca32 100644 --- a/src/install/user.rs +++ b/src/install/user.rs @@ -17,7 +17,7 @@ pub fn setup_users(conf: &[UserConfig]) { change_passwd(&user.name, &user.password); - if user.doas_root { + if user.doas_root.unwrap_or_default() { print_status(&format!("Allowing root doas for {}", user.name)); doas_conf.push_str(&format!("permit {} as root\n", user.name)); } diff --git a/src/print.rs b/src/print.rs index b8b3594..ceede2b 100644 --- a/src/print.rs +++ b/src/print.rs @@ -92,10 +92,24 @@ pub fn print_config(conf: &InstallConfig) { let user_conf = conf.user.as_ref().unwrap_or(&empty); for user in user_conf { + let mut groups = Vec::new(); + + if user.doas_root.unwrap_or_default() { + groups.push("🔑"); + } + + if user.docker.unwrap_or_default() { + groups.push("🐋"); + } + users_info.add_str(format!( - "👤 {} {}", + "👤 {}{}", user.name, - if user.doas_root { "🔑" } else { "" } + if !groups.is_empty() { + format!(" [ {} ]", groups.join(" ")) + } else { + String::new() + } )); }