2021-04-07 00:33:01 +08:00
|
|
|
package serverctl
|
|
|
|
|
|
|
|
import (
|
2021-07-21 05:18:45 +08:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/database"
|
2021-05-05 05:36:55 +08:00
|
|
|
"github.com/gravitl/netmaker/functions"
|
2021-05-06 06:03:37 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
2021-04-07 00:33:01 +08:00
|
|
|
)
|
|
|
|
|
2021-05-28 02:57:59 +08:00
|
|
|
func GetServerWGConf() (models.IntClient, error) {
|
2021-07-21 05:18:45 +08:00
|
|
|
var server models.IntClient
|
2021-07-22 06:55:19 +08:00
|
|
|
collection, err := database.FetchRecords(database.INT_CLIENTS_TABLE_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return models.IntClient{}, errors.New("could not find comms server")
|
|
|
|
}
|
|
|
|
for _, value := range collection {
|
|
|
|
json.Unmarshal([]byte(value), &server)
|
|
|
|
if server.Network == "comms" && server.IsServer == "yes" {
|
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return models.IntClient{}, errors.New("could not find comms server")
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateCommsNetwork() (bool, error) {
|
|
|
|
|
2021-07-21 05:18:45 +08:00
|
|
|
iscreated := false
|
|
|
|
exists, err := functions.NetworkExists("comms")
|
|
|
|
|
|
|
|
if exists || err != nil {
|
|
|
|
log.Println("comms network already exists. Skipping...")
|
|
|
|
return true, err
|
|
|
|
} else {
|
|
|
|
var network models.Network
|
|
|
|
|
|
|
|
network.NetID = "comms"
|
|
|
|
network.IsIPv6 = "no"
|
|
|
|
network.IsIPv4 = "yes"
|
|
|
|
network.IsGRPCHub = "yes"
|
|
|
|
network.AddressRange = servercfg.GetGRPCWGAddressRange()
|
|
|
|
network.DisplayName = "comms"
|
|
|
|
network.SetDefaults()
|
|
|
|
network.SetNodesLastModified()
|
|
|
|
network.SetNetworkLastModified()
|
|
|
|
network.KeyUpdateTimeStamp = time.Now().Unix()
|
2021-07-24 06:24:34 +08:00
|
|
|
network.IsLocal = "no"
|
2021-07-21 05:18:45 +08:00
|
|
|
network.KeyUpdateTimeStamp = time.Now().Unix()
|
|
|
|
|
|
|
|
log.Println("Creating comms network...")
|
|
|
|
value, err := json.Marshal(network)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
database.Insert(network.NetID, string(value), database.NETWORKS_TABLE_NAME)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
iscreated = true
|
|
|
|
}
|
|
|
|
return iscreated, err
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
|
|
|
|
2021-05-05 05:36:55 +08:00
|
|
|
func DownloadNetclient() error {
|
|
|
|
/*
|
2021-07-21 05:18:45 +08:00
|
|
|
// Get the data
|
|
|
|
resp, err := http.Get("https://github.com/gravitl/netmaker/releases/download/latest/netclient")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("could not download netclient")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Create the file
|
|
|
|
out, err := os.Create("/etc/netclient/netclient")
|
|
|
|
*/
|
|
|
|
if !FileExists("/etc/netclient/netclient") {
|
2021-05-05 05:36:55 +08:00
|
|
|
_, err := copy("./netclient/netclient", "/etc/netclient/netclient")
|
2021-07-21 05:18:45 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println("could not create /etc/netclient")
|
|
|
|
return err
|
|
|
|
}
|
2021-05-05 05:36:55 +08:00
|
|
|
}
|
|
|
|
//defer out.Close()
|
2021-04-07 00:33:01 +08:00
|
|
|
|
2021-04-07 07:13:34 +08:00
|
|
|
// Write the body to file
|
2021-05-05 05:36:55 +08:00
|
|
|
//_, err = io.Copy(out, resp.Body)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FileExists(f string) bool {
|
2021-07-21 05:18:45 +08:00
|
|
|
info, err := os.Stat(f)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !info.IsDir()
|
2021-04-07 00:33:01 +08:00
|
|
|
}
|
2021-05-05 05:36:55 +08:00
|
|
|
|
|
|
|
func copy(src, dst string) (int64, error) {
|
2021-07-21 05:18:45 +08:00
|
|
|
sourceFileStat, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sourceFileStat.Mode().IsRegular() {
|
|
|
|
return 0, errors.New(src + " is not a regular file")
|
|
|
|
}
|
|
|
|
|
|
|
|
source, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
destination, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer destination.Close()
|
|
|
|
nBytes, err := io.Copy(destination, source)
|
|
|
|
err = os.Chmod(dst, 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
return nBytes, err
|
2021-05-05 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
2021-04-07 07:13:34 +08:00
|
|
|
func RemoveNetwork(network string) (bool, error) {
|
|
|
|
_, err := os.Stat("/etc/netclient/netclient")
|
2021-07-21 05:18:45 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println("could not find /etc/netclient")
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
cmdoutput, err := exec.Command("/etc/netclient/netclient", "leave", "-n", network).Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(string(cmdoutput))
|
2021-04-07 07:13:34 +08:00
|
|
|
return false, err
|
|
|
|
}
|
2021-07-21 05:18:45 +08:00
|
|
|
log.Println("Server removed from network " + network)
|
|
|
|
return true, err
|
2021-04-07 00:33:01 +08:00
|
|
|
|
2021-04-07 07:13:34 +08:00
|
|
|
}
|
2021-04-07 00:33:01 +08:00
|
|
|
|
2021-04-07 07:13:34 +08:00
|
|
|
func AddNetwork(network string) (bool, error) {
|
2021-05-06 06:03:37 +08:00
|
|
|
pubip, err := servercfg.GetPublicIP()
|
2021-07-21 05:18:45 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println("could not get public IP.")
|
|
|
|
return false, err
|
|
|
|
}
|
2021-05-05 05:36:55 +08:00
|
|
|
|
|
|
|
_, err = os.Stat("/etc/netclient")
|
2021-07-21 05:18:45 +08:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
os.Mkdir("/etc/netclient", 744)
|
|
|
|
} else if err != nil {
|
|
|
|
log.Println("could not find or create /etc/netclient")
|
|
|
|
return false, err
|
2021-05-30 23:26:10 +08:00
|
|
|
}
|
2021-04-07 07:13:34 +08:00
|
|
|
token, err := functions.CreateServerToken(network)
|
2021-06-02 10:31:41 +08:00
|
|
|
if err != nil {
|
2021-07-21 05:18:45 +08:00
|
|
|
log.Println("could not create server token for " + network)
|
2021-04-15 10:59:25 +08:00
|
|
|
return false, err
|
2021-07-21 05:18:45 +08:00
|
|
|
}
|
|
|
|
_, err = os.Stat("/etc/netclient/netclient")
|
2021-04-07 07:13:34 +08:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = DownloadNetclient()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-04-07 00:33:01 +08:00
|
|
|
}
|
2021-07-21 05:18:45 +08:00
|
|
|
err = os.Chmod("/etc/netclient/netclient", 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("could not change netclient directory permissions")
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
log.Println("executing network join: " + "/etc/netclient/netclient " + "join " + "-t " + token + " -name " + "netmaker" + " -endpoint " + pubip)
|
|
|
|
out, err := exec.Command("/etc/netclient/netclient", "join", "-t", token, "-name", "netmaker", "-endpoint", pubip).Output()
|
|
|
|
if string(out) != "" {
|
|
|
|
log.Println(string(out))
|
2021-06-01 09:13:25 +08:00
|
|
|
}
|
2021-04-07 00:33:01 +08:00
|
|
|
if err != nil {
|
2021-07-21 05:18:45 +08:00
|
|
|
return false, errors.New(string(out) + err.Error())
|
|
|
|
}
|
2021-05-30 23:26:10 +08:00
|
|
|
log.Println("Server added to network " + network)
|
2021-04-07 07:13:34 +08:00
|
|
|
return true, err
|
2021-04-07 00:33:01 +08:00
|
|
|
}
|