Skip to content

Commit

Permalink
docker dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkasun committed Sep 27, 2023
1 parent e0f55f1 commit 523d00c
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 51 deletions.
168 changes: 118 additions & 50 deletions pkg/nm-install/cmd/dependencies.go
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions pkg/nm-install/cmd/netmaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/nm-install/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 523d00c

Please sign in to comment.