-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters