Peer Manipulation
This commit is contained in:
@@ -2,9 +2,11 @@ package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||
"github.com/google/uuid"
|
||||
mgb "go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
@@ -164,3 +166,42 @@ func GenericRawUpdateOne(set DBObject, id string, a Accessor) (DBObject, int, er
|
||||
}
|
||||
return a.LoadOne(id)
|
||||
}
|
||||
|
||||
func GetMySelf(wfa Accessor) (string, error) {
|
||||
id, err := GenerateNodeID()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
datas, _, _ := wfa.Search(nil, id, false)
|
||||
if len(datas) > 0 {
|
||||
return datas[0].GetID(), nil
|
||||
}
|
||||
return "", errors.New("peer not found")
|
||||
}
|
||||
|
||||
func IsMySelf(peerID string, wfa Accessor) (bool, string) {
|
||||
id, err := GetMySelf(wfa)
|
||||
if err != nil {
|
||||
return false, ""
|
||||
}
|
||||
return peerID == id, id
|
||||
}
|
||||
|
||||
func GenerateNodeID() (string, error) {
|
||||
folderStatic := "/var/lib/opencloud-node"
|
||||
if _, err := os.Stat(folderStatic); err == nil {
|
||||
os.MkdirAll(folderStatic, 0644)
|
||||
}
|
||||
folderStatic += "/node_id"
|
||||
if _, err := os.Stat(folderStatic); os.IsNotExist(err) {
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
id := uuid.NewSHA1(uuid.NameSpaceOID, []byte("oc-"+hostname))
|
||||
err = os.WriteFile(folderStatic, []byte(id.String()), 0644)
|
||||
return id.String(), err
|
||||
}
|
||||
data, err := os.ReadFile(folderStatic)
|
||||
return string(data), err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user