fixed startup logs. Removed comms from network list

This commit is contained in:
afeiszli
2021-05-30 11:26:10 -04:00
parent 8dfd81794d
commit b3655d2fdf
7 changed files with 121 additions and 212 deletions

View File

@@ -1,7 +1,7 @@
package serverctl
import (
"fmt"
"log"
"github.com/gravitl/netmaker/functions"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/mongoconn"
@@ -18,13 +18,13 @@ import (
func CreateDefaultNetwork() (bool, error) {
fmt.Println("Creating default network...")
log.Println("Creating default network...")
iscreated := false
exists, err := functions.NetworkExists("default")
if exists || err != nil {
fmt.Println("Default network already exists. Skipping...")
log.Println("Default network already exists. Skipping...")
return iscreated, err
} else {
@@ -43,8 +43,6 @@ func CreateDefaultNetwork() (bool, error) {
allow := true
network.AllowManualSignUp = &allow
fmt.Println("Creating default network.")
collection := mongoconn.Client.Database("netmaker").Collection("networks")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
@@ -56,6 +54,7 @@ func CreateDefaultNetwork() (bool, error) {
if err == nil {
iscreated = true
}
log.Println("1")
return iscreated, err
@@ -75,13 +74,11 @@ func GetServerWGConf() (models.IntClient, error) {
func CreateCommsNetwork() (bool, error) {
fmt.Println("Creating GRPC network...")
iscreated := false
exists, err := functions.NetworkExists("comms")
if exists || err != nil {
fmt.Println("GRPC network already exists. Skipping...")
log.Println("comms network already exists. Skipping...")
return true, nil
} else {
@@ -101,7 +98,7 @@ func CreateCommsNetwork() (bool, error) {
network.IsLocal = &priv
network.KeyUpdateTimeStamp = time.Now().Unix()
fmt.Println("Creating comms network.")
log.Println("Creating comms network...")
collection := mongoconn.Client.Database("netmaker").Collection("networks")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
@@ -122,7 +119,7 @@ func DownloadNetclient() error {
// Get the data
resp, err := http.Get("https://github.com/gravitl/netmaker/releases/download/latest/netclient")
if err != nil {
fmt.Println("could not download netclient")
log.Println("could not download netclient")
return err
}
defer resp.Body.Close()
@@ -133,7 +130,7 @@ func DownloadNetclient() error {
if !FileExists("/etc/netclient/netclient") {
_, err := copy("./netclient/netclient", "/etc/netclient/netclient")
if err != nil {
fmt.Println("could not create /etc/netclient")
log.Println("could not create /etc/netclient")
return err
}
}
@@ -159,7 +156,7 @@ func copy(src, dst string) (int64, error) {
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
return 0, errors.New(src + " is not a regular file")
}
source, err := os.Open(src)
@@ -176,7 +173,7 @@ func copy(src, dst string) (int64, error) {
nBytes, err := io.Copy(destination, source)
err = os.Chmod(dst, 0755)
if err != nil {
fmt.Println(err)
log.Println(err)
}
return nBytes, err
}
@@ -184,15 +181,15 @@ func copy(src, dst string) (int64, error) {
func RemoveNetwork(network string) (bool, error) {
_, err := os.Stat("/etc/netclient/netclient")
if err != nil {
fmt.Println("could not find /etc/netclient")
log.Println("could not find /etc/netclient")
return false, err
}
cmdoutput, err := exec.Command("/etc/netclient/netclient","-c","remove","-n",network).Output()
if err != nil {
fmt.Println(string(cmdoutput))
log.Println(string(cmdoutput))
return false, err
}
fmt.Println("Server removed from network " + network)
log.Println("Server removed from network " + network)
return true, err
}
@@ -200,7 +197,7 @@ func RemoveNetwork(network string) (bool, error) {
func AddNetwork(network string) (bool, error) {
pubip, err := servercfg.GetPublicIP()
if err != nil {
fmt.Println("could not get public IP.")
log.Println("could not get public IP.")
return false, err
}
@@ -208,36 +205,36 @@ func AddNetwork(network string) (bool, error) {
if os.IsNotExist(err) {
os.Mkdir("/etc/netclient", 744)
} else if err != nil {
fmt.Println("could not find or create /etc/netclient")
log.Println("could not find or create /etc/netclient")
return false, err
}
fmt.Println("Directory is ready.")
}
log.Println("Directory is ready.")
token, err := functions.CreateServerToken(network)
if err != nil {
fmt.Println("could not create server token for " + network)
log.Println("could not create server token for " + network)
return false, err
}
fmt.Println("Token is ready.")
log.Println("Token is ready.")
_, err = os.Stat("/etc/netclient/netclient")
if os.IsNotExist(err) {
err = DownloadNetclient()
fmt.Println("could not download netclient")
log.Println("could not download netclient")
if err != nil {
return false, err
}
}
err = os.Chmod("/etc/netclient/netclient", 0755)
if err != nil {
fmt.Println("could not change netclient directory permissions")
log.Println("could not change netclient directory permissions")
return false, err
}
fmt.Println("Client is ready. Running install.")
log.Println("Client is ready. Running install.")
out, err := exec.Command("/etc/netclient/netclient","-c","install","-t",token,"-name","netmaker","-ip4",pubip).Output()
fmt.Println(string(out))
log.Println(string(out))
if err != nil {
return false, errors.New(string(out) + err.Error())
}
fmt.Println("Server added to network " + network)
log.Println("Server added to network " + network)
return true, err
}