mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-13 08:34:44 +08:00
new config fully functional
This commit is contained in:
parent
de3d81db3a
commit
4ddd83aefb
4 changed files with 17 additions and 50 deletions
|
@ -592,7 +592,6 @@ func createAccessKey(w http.ResponseWriter, r *http.Request) {
|
||||||
if accesskey.Uses == 0 {
|
if accesskey.Uses == 0 {
|
||||||
accesskey.Uses = 1
|
accesskey.Uses = 1
|
||||||
}
|
}
|
||||||
_, gconf, err := functions.GetGlobalConfig()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnErrorResponse(w, r, formatError(err, "internal"))
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
||||||
return
|
return
|
||||||
|
@ -604,7 +603,7 @@ func createAccessKey(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
netID := params["networkname"]
|
netID := params["networkname"]
|
||||||
address := gconf.ServerGRPC + gconf.PortGRPC
|
address := servercfg.GetGRPCHost() + ":" + servercfg.GetGRPCPort()
|
||||||
|
|
||||||
accessstringdec := address + "|" + netID + "|" + accesskey.Value + "|" + privAddr
|
accessstringdec := address + "|" + netID + "|" + accesskey.Value + "|" + privAddr
|
||||||
accesskey.AccessString = base64.StdEncoding.EncodeToString([]byte(accessstringdec))
|
accesskey.AccessString = base64.StdEncoding.EncodeToString([]byte(accessstringdec))
|
||||||
|
|
|
@ -16,6 +16,7 @@ import (
|
||||||
|
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
"github.com/gravitl/netmaker/mongoconn"
|
"github.com/gravitl/netmaker/mongoconn"
|
||||||
|
"github.com/gravitl/netmaker/servercfg"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
@ -38,11 +39,7 @@ func CreateServerToken(netID string) (string, error) {
|
||||||
accesskey.Name = GenKeyName()
|
accesskey.Name = GenKeyName()
|
||||||
accesskey.Value = GenKey()
|
accesskey.Value = GenKey()
|
||||||
accesskey.Uses = 1
|
accesskey.Uses = 1
|
||||||
_, gconf, errG := GetGlobalConfig()
|
address := "127.0.0.1:" + servercfg.GetGRPCPort()
|
||||||
if errG != nil {
|
|
||||||
return "", errG
|
|
||||||
}
|
|
||||||
address := "localhost" + gconf.PortGRPC
|
|
||||||
|
|
||||||
privAddr := ""
|
privAddr := ""
|
||||||
if *network.IsLocal {
|
if *network.IsLocal {
|
||||||
|
@ -556,35 +553,6 @@ func UniqueAddress6(networkName string) (string, error) {
|
||||||
return "W1R3: NO UNIQUE ADDRESSES AVAILABLE", err1
|
return "W1R3: NO UNIQUE ADDRESSES AVAILABLE", err1
|
||||||
}
|
}
|
||||||
|
|
||||||
//pretty simple get
|
|
||||||
func GetGlobalConfig() (bool, models.GlobalConfig, error) {
|
|
||||||
|
|
||||||
create := false
|
|
||||||
|
|
||||||
filter := bson.M{}
|
|
||||||
|
|
||||||
var globalconf models.GlobalConfig
|
|
||||||
|
|
||||||
collection := mongoconn.Client.Database("netmaker").Collection("config")
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
|
|
||||||
err := collection.FindOne(ctx, filter).Decode(&globalconf)
|
|
||||||
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
if err == mongo.ErrNoDocuments {
|
|
||||||
fmt.Println("Global config does not exist. Need to create.")
|
|
||||||
create = true
|
|
||||||
return create, globalconf, err
|
|
||||||
} else if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
fmt.Println("Could not get global config")
|
|
||||||
return create, globalconf, err
|
|
||||||
}
|
|
||||||
return create, globalconf, err
|
|
||||||
}
|
|
||||||
|
|
||||||
//generate an access key value
|
//generate an access key value
|
||||||
func GenKey() string {
|
func GenKey() string {
|
||||||
|
|
||||||
|
|
14
main.go
14
main.go
|
@ -68,12 +68,26 @@ func main() {
|
||||||
|
|
||||||
//Run Agent Server
|
//Run Agent Server
|
||||||
if servercfg.IsAgentBackend() {
|
if servercfg.IsAgentBackend() {
|
||||||
|
if !(servercfg.DisableRemoteIPCheck()) && servercfg.GetGRPCHost() == "127.0.0.1" {
|
||||||
|
err := servercfg.SetHost()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Unable to Set host. Exiting.")
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
waitnetwork.Add(1)
|
waitnetwork.Add(1)
|
||||||
go runGRPC(&waitnetwork, installserver)
|
go runGRPC(&waitnetwork, installserver)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Run Rest Server
|
//Run Rest Server
|
||||||
if servercfg.IsRestBackend() {
|
if servercfg.IsRestBackend() {
|
||||||
|
if !servercfg.DisableRemoteIPCheck() && servercfg.GetAPIHost() == "127.0.0.1" {
|
||||||
|
err := servercfg.SetHost()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Unable to Set host. Exiting.")
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
waitnetwork.Add(1)
|
waitnetwork.Add(1)
|
||||||
controller.HandleRESTRequests(&waitnetwork)
|
controller.HandleRESTRequests(&waitnetwork)
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,20 +46,6 @@ func TestMain(m *testing.M) {
|
||||||
var waitgroup sync.WaitGroup
|
var waitgroup sync.WaitGroup
|
||||||
waitgroup.Add(1)
|
waitgroup.Add(1)
|
||||||
go controller.HandleRESTRequests(&waitgroup)
|
go controller.HandleRESTRequests(&waitgroup)
|
||||||
var gconf models.GlobalConfig
|
|
||||||
gconf.ServerGRPC = "localhost:8081"
|
|
||||||
gconf.PortGRPC = "50051"
|
|
||||||
//err := SetGlobalConfig(gconf)
|
|
||||||
collection := mongoconn.Client.Database("netmaker").Collection("config")
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
//create, _, err := functions.GetGlobalConfig()
|
|
||||||
_, err := collection.InsertOne(ctx, gconf)
|
|
||||||
if err != nil {
|
|
||||||
panic("could not create config store")
|
|
||||||
}
|
|
||||||
|
|
||||||
//wait for http server to start
|
|
||||||
time.Sleep(time.Second * 1)
|
time.Sleep(time.Second * 1)
|
||||||
os.Exit(m.Run())
|
os.Exit(m.Run())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue