Sfoglia il codice sorgente

docker dependencies

Matthew R Kasun 1 anno fa
parent
commit
523d00cb27

+ 118 - 50
pkg/nm-install/cmd/dependencies.go

@@ -1,71 +1,139 @@
 package cmd
 
 import (
+	"errors"
 	"os"
 	"os/exec"
+	"strings"
+
+	"github.com/bitfield/script"
+	"github.com/pterm/pterm"
 )
 
-type dependecies struct {
-	wireguard      bool
-	wireguardTools bool
-	docker         bool
-	dockerCompose  bool
-	dependencies   []string
-	update         string
-	install        string
-}
+var (
+	distro          string
+	dockerRequired  bool
+	composeRequired bool
+)
 
 func installDependencies() {
-	dep := dependecies{}
-	if exists("/etc/debian_version") {
-		dep.dependencies = []string{"git", "wireguard", "wireguard-tools", "dnsutils",
-			"jq", "docker-io", "docker-compose", "grep", "awk"}
-		dep.update = "apt update"
-		dep.install = "apt-get install -y"
-	} else if exists("/etc/alpine-release") {
-		dep.dependencies = []string{"git wireguard jq docker.io docker-compose grep gawk"}
-		dep.update = "apk update"
-		dep.install = "apk --update add"
-	} else if exists("/etc/centos-release") {
-		dep.dependencies = []string{"git wireguard jq bind-utils docker.io docker-compose grep gawka"}
-		dep.update = "yum update"
-		dep.install = "yum install -y"
-	} else if exists("/etc/fedora-release") {
-		dep.dependencies = []string{"git wireguard bind-utils jq docker.io docker-compose grep gawk"}
-		dep.update = "dnf update"
-		dep.install = "dnf install -y"
-	} else if exists("/etc/redhat-release") {
-		dep.dependencies = []string{"git wireguard jq docker.io bind-utils docker-compose grep gawk"}
-		dep.update = "yum update"
-		dep.install = "yum install -y"
-	} else if exists("/etc/arch-release") {
-		dep.dependencies = []string{"git wireguard-tools dnsutils jq docker.io docker-compose grep gawk"}
-		dep.update = "pacman -Sy"
-		dep.install = "pacman -S --noconfirm"
-	} else {
-		dep.install = ""
-	}
-	//check if installed
-	_, err := exec.LookPath("wg")
+	var err error
+	distro, err = getDistro()
 	if err != nil {
-		dep.wireguardTools = true
-		dep.dependencies = append(dep.dependencies, "wireguard-tools")
+		if err.Error() == "unsupported distribution" {
+			pterm.Println("unable to install dependencies")
+			pterm.Println("you may be using an ", err.Error())
+			pterm.Println("to install netmaker, first install docker, docker-compose, and wireguard-tools")
+			pterm.Println("and then re-run this program")
+			os.Exit(1)
+		} else {
+			pterm.Println("this does not appear to be a linux OS, cannot proceed")
+			os.Exit(2)
+		}
 	}
+	pterm.Println("checking if docker/docker-compose is installed", distro)
 	_, err = exec.LookPath("docker")
 	if err != nil {
-		dep.docker = true
-		dep.dependencies = append(dep.dependencies, "docker-ce")
+		dockerRequired = true
 	}
 	_, err = exec.LookPath("docker-compose")
 	if err != nil {
-		dep.dockerCompose = true
-		dep.dependencies = append(dep.dependencies, "docker-compose")
+		composeRequired = true
+	}
+
+	if dockerRequired || composeRequired {
+		if distro != "ubuntu" && distro != "debian" {
+			installDockerCE(distro)
+		} else {
+			installDocker()
+		}
+	}
+}
+
+func getDistro() (string, error) {
+	id, err := script.File("/etc/os-release").Match("ID").String()
+	if err != nil {
+		return "", err
+	}
+	// the order in which these are checked is important
+	if strings.Contains(id, "ubuntu") {
+		return "ubuntu", nil
+	}
+	if strings.Contains(id, "debian") {
+		return "debian", nil
+	}
+	if strings.Contains(id, "centos") {
+		return "centos", nil
+	}
+	if strings.Contains(id, "rhel") {
+		return "rhel", nil
+	}
+	if strings.Contains(id, "fedora") {
+		return "fedora", nil
+	}
+	if strings.Contains(id, "alpine") {
+		return "alpine", nil
+	}
+	return "", errors.New("unsupported distrobution")
+}
+
+func installDockerCE(distro string) {
+	switch distro {
+	case "centos":
+		_, err := script.Exec("yum install -y yum-utils").Stdout()
+		if err != nil {
+			panic(err)
+		}
+		_, err = script.Exec("yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo").Stdout()
+		if err != nil {
+			panic(err)
+		}
+		_, err = script.Exec("yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin").Stdout()
+		if err != nil {
+			panic(err)
+		}
+	case "rhel":
+		_, err := script.Exec("yum install -y yum-utils").Stdout()
+		if err != nil {
+			panic(err)
+		}
+		_, err = script.Exec("yum-config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo").Stdout()
+		if err != nil {
+			panic(err)
+		}
+		_, err = script.Exec("yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin").Stdout()
+		if err != nil {
+			panic(err)
+		}
+	case "fedora":
+		_, err := script.Exec("dnf install -y dnf-plugins-core").Stdout()
+		if err != nil {
+			panic(err)
+		}
+		_, err = script.Exec("dnf-config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo").Stdout()
+		if err != nil {
+			panic(err)
+		}
+		_, err = script.Exec("dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin").Stdout()
+		if err != nil {
+			panic(err)
+		}
+	default:
+		panic(errors.New("unsupported distribution"))
+	}
+	_, err := script.Exec("systemctl start docker").Stdout()
+	if err != nil {
+		panic(err)
 	}
 }
 
-func exists(file string) bool {
-	if _, err := os.Stat(file); err != nil {
-		return false
+func installDocker() {
+	_, err := script.Exec("apt-get update").Stdout()
+	if err != nil {
+		panic(err)
+	}
+	_, err = script.Exec("apt-get -y install docker docker-compose").Stdout()
+	if err != nil {
+		panic(err)
 	}
-	return true
 }

+ 5 - 0
pkg/nm-install/cmd/netmaker.go

@@ -62,6 +62,11 @@ func installNetmaker() {
 	os.Symlink("netmaker.env", ".env")
 	//Fetch/Update certs
 	pterm.Println("\nGetting certificates")
+	//ensure docker daemon is running
+	_, err = script.Exec("systemctl start docker").Stdout()
+	if err != nil {
+		panic(err)
+	}
 	//fix nm-cert.sh  remove -it from docker run -it --rm .....
 	if _, err := script.File("./nm-certs.sh").Replace("-it", "").WriteFile("./certs.sh"); err != nil {
 		panic(err)

+ 1 - 1
pkg/nm-install/cmd/root.go

@@ -58,8 +58,8 @@ to quickly create a Cobra application.`,
 		pterm.DefaultBigText.WithLetters(
 			putils.LettersFromStringWithStyle("NETMAKER", pterm.FgCyan.ToStyle())).Render()
 		getBuildType(&pro)
-		installDependencies()
 		setInstallVars()
+		installDependencies()
 		installNetmaker()
 		installNmctl()
 		createNetwork()