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