f-25: use file for metadata
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
f5707c343c
commit
862406f041
4 changed files with 80 additions and 137 deletions
|
|
@ -2,26 +2,29 @@ package main
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/metadata"
|
||||
)
|
||||
|
||||
var (
|
||||
iface = flag.String("interface", "0.0.0.0", "Interface IP à écouter")
|
||||
port = flag.Int("port", 0, "Port à utiliser")
|
||||
netns_name = flag.String("netns", "", "Network namespace à utiliser")
|
||||
conf_file = flag.String("conf", "/etc/two/agent.yml", "configuration file")
|
||||
vm_name = flag.String("vm", "", "Name of the vm")
|
||||
confFile = flag.String("conf", "/etc/two/agent.yml", "configuration file")
|
||||
vm_name = flag.String("vm", "", "Name of the vm")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
cfg, err := configuration.LoadConfig(*confFile)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
metadata.StartServer(metadata.ServerConfig{
|
||||
Netns: *netns_name,
|
||||
Iface: *iface,
|
||||
Port: *port,
|
||||
ConfFile: *conf_file,
|
||||
VmName: *vm_name,
|
||||
VmName: *vm_name,
|
||||
RunDir: cfg.Metadata.RunDir,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@ package metadata
|
|||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
func newCfg() NoCloudConfig {
|
||||
|
|
@ -20,11 +20,9 @@ func newCfg() NoCloudConfig {
|
|||
}
|
||||
}
|
||||
|
||||
func newTestDB(t *testing.T) interface{ Close() error } {
|
||||
func useTestDir(t *testing.T) string {
|
||||
t.Helper()
|
||||
db := kv.InitDB(kv.Config{Path: t.TempDir()}, false)
|
||||
t.Cleanup(func() { db.Close() })
|
||||
return db
|
||||
return t.TempDir()
|
||||
}
|
||||
|
||||
// --- RenderConfig ---
|
||||
|
|
@ -108,78 +106,67 @@ func TestRenderConfig_SpecialCharsInName(t *testing.T) {
|
|||
|
||||
// --- LoadNcCloudInDB / UnLoadNoCloudInDB ---
|
||||
|
||||
func TestLoadNcCloudInDB_StoresAllKeys(t *testing.T) {
|
||||
db := kv.InitDB(kv.Config{Path: t.TempDir()}, false)
|
||||
t.Cleanup(func() { db.Close() })
|
||||
|
||||
cfg := newCfg()
|
||||
LoadNcCloudInDB(cfg, db)
|
||||
|
||||
keys := []string{
|
||||
"metadata/vm1/meta-data",
|
||||
"metadata/vm1/user-data",
|
||||
"metadata/vm1/network-config",
|
||||
"metadata/vm1/vendor-data",
|
||||
"metadata/vm1/vpc",
|
||||
"metadata/vm1/bind_ip",
|
||||
"metadata/vm1/bind_port",
|
||||
func readTestFile(t *testing.T, dir, vmName, name string) string {
|
||||
t.Helper()
|
||||
b, err := os.ReadFile(filepath.Join(dir, vmName, name))
|
||||
if err != nil {
|
||||
t.Errorf("fichier %q absent après LoadNcCloudInDB : %v", name, err)
|
||||
return ""
|
||||
}
|
||||
for _, key := range keys {
|
||||
val, err := kv.GetFromDB(db, key)
|
||||
if err != nil {
|
||||
t.Errorf("clé %q absente après LoadNcCloudInDB : %v", key, err)
|
||||
}
|
||||
if val == "" && key != "metadata/vm1/user-data" {
|
||||
t.Errorf("clé %q vide après LoadNcCloudInDB", key)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func TestLoadNcCloudInDB_StoresAllFiles(t *testing.T) {
|
||||
dir := useTestDir(t)
|
||||
LoadNcCloudInDB(newCfg(), dir)
|
||||
|
||||
files := []string{"meta-data", "user-data", "network-config", "vendor-data", "vpc", "bind_ip", "bind_port"}
|
||||
for _, f := range files {
|
||||
path := filepath.Join(dir, "vm1", f)
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
t.Errorf("fichier %q absent : %v", f, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadNcCloudInDB_VpcAndBindValues(t *testing.T) {
|
||||
db := kv.InitDB(kv.Config{Path: t.TempDir()}, false)
|
||||
t.Cleanup(func() { db.Close() })
|
||||
dir := useTestDir(t)
|
||||
LoadNcCloudInDB(newCfg(), dir)
|
||||
|
||||
cfg := newCfg()
|
||||
LoadNcCloudInDB(cfg, db)
|
||||
|
||||
vpc, _ := kv.GetFromDB(db, "metadata/vm1/vpc")
|
||||
if vpc != "vpc-test" {
|
||||
if vpc := readTestFile(t, dir, "vm1", "vpc"); vpc != "vpc-test" {
|
||||
t.Errorf("vpc attendu %q, obtenu %q", "vpc-test", vpc)
|
||||
}
|
||||
|
||||
ip, _ := kv.GetFromDB(db, "metadata/vm1/bind_ip")
|
||||
if ip != "169.254.169.254" {
|
||||
if ip := readTestFile(t, dir, "vm1", "bind_ip"); ip != "169.254.169.254" {
|
||||
t.Errorf("bind_ip attendu %q, obtenu %q", "169.254.169.254", ip)
|
||||
}
|
||||
|
||||
port, _ := kv.GetFromDB(db, "metadata/vm1/bind_port")
|
||||
if port != "80" {
|
||||
if port := readTestFile(t, dir, "vm1", "bind_port"); port != "80" {
|
||||
t.Errorf("bind_port attendu %q, obtenu %q", "80", port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnLoadNoCloudInDB_RemovesAllKeys(t *testing.T) {
|
||||
db := kv.InitDB(kv.Config{Path: t.TempDir()}, false)
|
||||
t.Cleanup(func() { db.Close() })
|
||||
func TestUnLoadNoCloudInDB_RemovesAllFiles(t *testing.T) {
|
||||
dir := useTestDir(t)
|
||||
LoadNcCloudInDB(newCfg(), dir)
|
||||
UnLoadNoCloudInDB("vm1", dir)
|
||||
|
||||
cfg := newCfg()
|
||||
LoadNcCloudInDB(cfg, db)
|
||||
UnLoadNoCloudInDB("vm1", db)
|
||||
|
||||
keys := []string{
|
||||
"metadata/vm1/meta-data",
|
||||
"metadata/vm1/user-data",
|
||||
"metadata/vm1/network-config",
|
||||
"metadata/vm1/vendor-data",
|
||||
"metadata/vm1/vpc",
|
||||
"metadata/vm1/bind_ip",
|
||||
"metadata/vm1/bind_port",
|
||||
if _, err := os.Stat(filepath.Join(dir, "vm1")); !os.IsNotExist(err) {
|
||||
t.Error("répertoire vm1 devrait être supprimé après UnLoadNoCloudInDB")
|
||||
}
|
||||
for _, key := range keys {
|
||||
_, err := kv.GetFromDB(db, key)
|
||||
if err == nil {
|
||||
t.Errorf("clé %q devrait être supprimée après UnLoadNoCloudInDB", key)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnLoadNoCloudInDB_DoesNotAffectOtherVMs(t *testing.T) {
|
||||
dir := useTestDir(t)
|
||||
|
||||
cfg1 := newCfg()
|
||||
cfg2 := newCfg()
|
||||
cfg2.Name = "vm2"
|
||||
LoadNcCloudInDB(cfg1, dir)
|
||||
LoadNcCloudInDB(cfg2, dir)
|
||||
|
||||
UnLoadNoCloudInDB("vm1", dir)
|
||||
|
||||
if _, err := os.Stat(filepath.Join(dir, "vm2", "vpc")); err != nil {
|
||||
t.Errorf("vm2 ne devrait pas être supprimée : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -277,23 +264,3 @@ func TestRootHandler_ContentType(t *testing.T) {
|
|||
t.Errorf("Content-Type attendu text/yaml, obtenu %q", ct)
|
||||
}
|
||||
}
|
||||
|
||||
// --- UnLoadNoCloudInDB_DoesNotAffectOtherVMs ---
|
||||
|
||||
func TestUnLoadNoCloudInDB_DoesNotAffectOtherVMs(t *testing.T) {
|
||||
db := kv.InitDB(kv.Config{Path: t.TempDir()}, false)
|
||||
t.Cleanup(func() { db.Close() })
|
||||
|
||||
cfg1 := newCfg()
|
||||
cfg2 := newCfg()
|
||||
cfg2.Name = "vm2"
|
||||
LoadNcCloudInDB(cfg1, db)
|
||||
LoadNcCloudInDB(cfg2, db)
|
||||
|
||||
UnLoadNoCloudInDB("vm1", db)
|
||||
|
||||
_, err := kv.GetFromDB(db, "metadata/vm2/vpc")
|
||||
if err != nil {
|
||||
t.Errorf("vm2 ne devrait pas être supprimée : %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,12 +5,13 @@ import (
|
|||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
var data NoCloudData
|
||||
|
|
@ -23,47 +24,23 @@ func getIP(r *http.Request) string {
|
|||
return ip
|
||||
}
|
||||
|
||||
func getFromDB(config ServerConfig) NoCloudData {
|
||||
var netns_name string
|
||||
var port int
|
||||
var iface string
|
||||
func readFile(dir, name string) string {
|
||||
b, _ := os.ReadFile(filepath.Join(dir, name))
|
||||
return strings.TrimRight(string(b), "\n")
|
||||
}
|
||||
|
||||
conf_db, _ := configuration.LoadConfig(config.ConfFile)
|
||||
func getFromFiles(config ServerConfig) NoCloudData {
|
||||
dir := filepath.Join(config.RunDir, config.VmName)
|
||||
|
||||
db := kv.InitDB(kv.Config{Path: conf_db.Database.Path}, true)
|
||||
defer db.Close()
|
||||
|
||||
metadata, _ := kv.GetFromDB(db, "metadata/"+config.VmName+"/meta-data")
|
||||
userdata, _ := kv.GetFromDB(db, "metadata/"+config.VmName+"/user-data")
|
||||
networkconfig, _ := kv.GetFromDB(db, "metadata/"+config.VmName+"/network-config")
|
||||
vendordata, _ := kv.GetFromDB(db, "metadata/"+config.VmName+"/vendor-data")
|
||||
|
||||
if config.Netns == "" {
|
||||
netns_name, _ = kv.GetFromDB(db, "metadata/"+config.VmName+"/vpc")
|
||||
} else {
|
||||
netns_name = config.Netns
|
||||
}
|
||||
|
||||
if config.Iface == "" {
|
||||
iface, _ = kv.GetFromDB(db, "metadata/"+config.VmName+"/bind_ip")
|
||||
} else {
|
||||
iface = config.Iface
|
||||
}
|
||||
|
||||
if config.Port == 0 {
|
||||
sport, _ := kv.GetFromDB(db, "metadata/"+config.VmName+"/bind_port")
|
||||
port, _ = strconv.Atoi(sport)
|
||||
} else {
|
||||
port = config.Port
|
||||
}
|
||||
port, _ := strconv.Atoi(readFile(dir, "bind_port"))
|
||||
|
||||
return NoCloudData{
|
||||
MetaData: metadata,
|
||||
UserData: userdata,
|
||||
NetworkConfig: networkconfig,
|
||||
VendorData: vendordata,
|
||||
NetNs: netns_name,
|
||||
Iface: iface,
|
||||
MetaData: readFile(dir, "meta-data"),
|
||||
UserData: readFile(dir, "user-data"),
|
||||
NetworkConfig: readFile(dir, "network-config"),
|
||||
VendorData: readFile(dir, "vendor-data"),
|
||||
NetNs: readFile(dir, "vpc"),
|
||||
Iface: readFile(dir, "bind_ip"),
|
||||
Port: port,
|
||||
}
|
||||
}
|
||||
|
|
@ -93,7 +70,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func StartServer(config ServerConfig) {
|
||||
data = getFromDB(config)
|
||||
data = getFromFiles(config)
|
||||
|
||||
if data.NetNs != "" {
|
||||
if err := netns.Enter(data.NetNs); err != nil {
|
||||
|
|
|
|||
|
|
@ -11,12 +11,8 @@ type NoCloudData struct {
|
|||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Netns string
|
||||
File string
|
||||
Iface string
|
||||
Port int
|
||||
ConfFile string
|
||||
VmName string
|
||||
VmName string
|
||||
RunDir string
|
||||
}
|
||||
|
||||
type NoCloudConfig struct {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue