Check root, existing config, pull containers

This commit is contained in:
Owen Schwartz 2024-12-26 20:56:13 -05:00
parent b78e7a324d
commit 419eb00583
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD

View file

@ -35,16 +35,24 @@ type Config struct {
func main() { func main() {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
config := collectUserInput(reader) // check if the user is root
createConfigFiles(config) if os.Geteuid() != 0 {
fmt.Println("This script must be run as root")
os.Exit(1)
}
if !isDockerInstalled() && runtime.GOOS == "linux" { // check if there is already a config file
if shouldInstallDocker() { if _, err := os.Stat("config/config.yml"); err != nil {
// ask user if they want to install docker config := collectUserInput(reader)
if readBool(reader, "Would you like to install Docker?", true) { createConfigFiles(config)
if !isDockerInstalled() && runtime.GOOS == "linux" {
if shouldInstallDocker() {
installDocker() installDocker()
} }
} }
} else {
fmt.Println("Config file already exists... skipping configuration")
} }
if isDockerInstalled() { if isDockerInstalled() {
@ -124,11 +132,11 @@ func collectUserInput(reader *bufio.Reader) Config {
config.EnableEmail = readBool(reader, "Enable email functionality", false) config.EnableEmail = readBool(reader, "Enable email functionality", false)
if config.EnableEmail { if config.EnableEmail {
config.EmailSMTPHost = readString(reader, "Enter SMTP host: ", "") config.EmailSMTPHost = readString(reader, "Enter SMTP host", "")
config.EmailSMTPPort = readInt(reader, "Enter SMTP port (default 587): ", 587) config.EmailSMTPPort = readInt(reader, "Enter SMTP port (default 587)", 587)
config.EmailSMTPUser = readString(reader, "Enter SMTP username: ", "") config.EmailSMTPUser = readString(reader, "Enter SMTP username", "")
config.EmailSMTPPass = readString(reader, "Enter SMTP password: ", "") config.EmailSMTPPass = readString(reader, "Enter SMTP password", "")
config.EmailNoReply = readString(reader, "Enter no-reply email address: ", "") config.EmailNoReply = readString(reader, "Enter no-reply email address", "")
} }
// Validate required fields // Validate required fields
@ -301,6 +309,26 @@ func installDocker() error {
dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo && dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo &&
dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
`) `)
case strings.Contains(osRelease, "ID=opensuse") || strings.Contains(osRelease, "ID=\"opensuse-"):
installCmd = exec.Command("bash", "-c", `
zypper install -y docker docker-compose &&
systemctl enable docker
`)
case strings.Contains(osRelease, "ID=rhel") || strings.Contains(osRelease, "ID=\"rhel"):
installCmd = exec.Command("bash", "-c", `
dnf remove -y runc &&
dnf -y install yum-utils &&
dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo &&
dnf install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin &&
systemctl enable docker
`)
case strings.Contains(osRelease, "ID=amzn"):
installCmd = exec.Command("bash", "-c", `
yum update -y &&
yum install -y docker &&
systemctl enable docker &&
usermod -a -G docker ec2-user
`)
default: default:
return fmt.Errorf("unsupported Linux distribution") return fmt.Errorf("unsupported Linux distribution")
} }
@ -319,36 +347,27 @@ func isDockerInstalled() bool {
} }
func pullAndStartContainers() error { func pullAndStartContainers() error {
containers := []string{
"traefik:v3.1",
"fossorial/pangolin:latest",
"fossorial/gerbil:latest",
}
for _, container := range containers {
fmt.Printf("Pulling %s...\n", container)
cmd := exec.Command("docker", "pull", container)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to pull %s: %v", container, err)
}
}
fmt.Println("Starting containers...") fmt.Println("Starting containers...")
// First try docker compose (new style) // First try docker compose (new style)
cmd := exec.Command("docker", "compose", "-f", "docker-compose.yml", "up", "-d") cmd := exec.Command("docker", "compose", "-f", "docker-compose.yml", "pull")
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
err := cmd.Run() err := cmd.Run()
// If docker compose fails, try docker-compose (legacy style)
if err != nil { if err != nil {
cmd = exec.Command("docker-compose", "-f", "docker-compose.yml", "up", "-d") fmt.Println("Failed to start containers using docker compose, falling back to docker-compose command")
cmd.Stdout = os.Stdout os.Exit(1)
cmd.Stderr = os.Stderr }
err = cmd.Run()
cmd = exec.Command("docker", "compose", "-f", "docker-compose.yml", "up", "-d")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
fmt.Println("Failed to start containers using docker-compose command")
os.Exit(1)
} }
return err return err