Compare commits

..

11 commits

Author SHA1 Message Date
97132550b0
f-8: code: move Load in DB to internal lib #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-12-26 23:48:42 +01:00
38f2ed1b83
f-8: code: use recursive delete #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-12-26 23:48:41 +01:00
b2d1922eaa
f-8: pkg: make delete in db recursif #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-12-26 23:48:41 +01:00
df95d8b4a7
f-8: code: use flags in db binary #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-12-26 23:48:36 +01:00
1ac1e61864
f-8: code: move binary name #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-12-26 23:48:35 +01:00
55ed2c8e53
f-8: code: add module to load nocloud in db #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-12-26 23:48:35 +01:00
b43e45488e
f-8: code: add func to control db #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-12-26 23:48:35 +01:00
2418e08ee0
f-8: code: add params for meta_cli #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-12-26 23:48:34 +01:00
014ae61dbb
f-8: internal/conf: delete conf file existance check #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-12-26 23:48:25 +01:00
1ac5a9fe49
f-8: conf: move configuration files #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-12-26 23:47:45 +01:00
e1f317aeb9
f-8: code: first implement of metadata cli #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-12-26 23:47:31 +01:00
4 changed files with 76 additions and 40 deletions

View file

@ -5,10 +5,13 @@ import (
"os" "os"
) )
var (
bin_name = os.Args[0]
)
func main() { func main() {
bin_name := os.Args[0]
fmt.Printf("%s: Start process\n", bin_name) fmt.Printf("%s: Start process\n", bin_name)
os.Exit(5) os.Exit(0)
} }

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@ -59,9 +60,7 @@ func AddInDB(dbName string, line string) error {
func DeleteInDB(dbName, id string) error { func DeleteInDB(dbName, id string) error {
key := []byte(dbName + "/" + id + "/bash") key := []byte(dbName + "/" + id + "/bash")
return DB.Update(func(txn *badger.Txn) error { return kv.DeleteInDB(DB, string(key))
return txn.Delete(key)
})
} }
func CountInDB(dbName, id string) int { func CountInDB(dbName, id string) int {
@ -125,7 +124,13 @@ func printDB() {
} }
func main() { func main() {
conf, err := configuration.LoadConfig("/etc/two/agent.yml") conf_file := flag.String("conf", "/etc/two/agent.yml", "configuration file")
flag.Parse()
args := flag.Args()
conf, err := configuration.LoadConfig(*conf_file)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
@ -136,53 +141,53 @@ func main() {
}) })
defer DB.Close() defer DB.Close()
if len(os.Args) < 2 { if len(args) < 1 {
fmt.Println("Usage: db <cmd> [args...]") fmt.Println("Usage: db <cmd> [args...]")
return return
} }
cmd := os.Args[1] cmd := args[0]
switch cmd { switch cmd {
case "check_in_db": case "check_in_db":
if len(os.Args) != 4 { if len(args) != 3 {
fmt.Println("Usage: check_in_db <db_name> <id>") fmt.Println("Usage: check_in_db <db_name> <id>")
os.Exit(1) os.Exit(1)
} }
ret := CheckInDB(os.Args[2], os.Args[3]) ret := CheckInDB(args[1], args[2])
os.Exit(ret) os.Exit(ret)
case "add_in_db": case "add_in_db":
if len(os.Args) < 4 { if len(args) < 3 {
fmt.Println("Usage: add_in_db <db_name> <line...>") fmt.Println("Usage: add_in_db <db_name> <line...>")
os.Exit(1) os.Exit(1)
} }
line := strings.Join(os.Args[3:], ";") line := strings.Join(args[2:], ";")
if err := AddInDB(os.Args[2], line); err != nil { if err := AddInDB(args[1], line); err != nil {
fmt.Println("Error:", err) fmt.Println("Error:", err)
os.Exit(1) os.Exit(1)
} }
case "delete_in_db": case "delete_in_db":
if len(os.Args) != 4 { if len(args) != 3 {
fmt.Println("Usage: delete_in_db <db_name> <id>") fmt.Println("Usage: delete_in_db <db_name> <id>")
os.Exit(1) os.Exit(1)
} }
if err := DeleteInDB(os.Args[2], os.Args[3]); err != nil { if err := DeleteInDB(args[1], args[2]); err != nil {
fmt.Println("Error:", err) fmt.Println("Error:", err)
os.Exit(1) os.Exit(1)
} }
case "count_in_db": case "count_in_db":
if len(os.Args) != 4 { if len(args) != 3 {
fmt.Println("Usage: count_in_db <db_name> <id>") fmt.Println("Usage: count_in_db <db_name> <id>")
os.Exit(1) os.Exit(1)
} }
count := CountInDB(os.Args[2], os.Args[3]) count := CountInDB(args[1], args[2])
fmt.Println(count) fmt.Println(count)
case "get_from_db": case "get_from_db":
if len(os.Args) != 4 { if len(args) != 3 {
fmt.Println("Usage: get_from_db <db_name> <id>") fmt.Println("Usage: get_from_db <db_name> <id>")
os.Exit(1) os.Exit(1)
} }
line, _ := GetFromDB(os.Args[2], os.Args[3]) line, _ := GetFromDB(args[1], args[2])
fmt.Println(line) fmt.Println(line)
case "print": case "print":
printDB() printDB()

View file

@ -3,26 +3,12 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os"
"strings"
configuration "git.g3e.fr/syonad/two/internal/config/agent" configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/load_db/nocloud"
"git.g3e.fr/syonad/two/pkg/db/kv" "git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
) )
var DB *badger.DB
func AddInDB(dbName string, line string) error {
// ID = partie avant le premier ';'
id := strings.Split(line, ";")[0] + "/bash"
key := []byte(dbName + "/" + id)
return DB.Update(func(txn *badger.Txn) error {
return txn.Set(key, []byte(line))
})
}
func main() { func main() {
conf_file := flag.String("conf", "/etc/two/agent.yml", "configuration file") conf_file := flag.String("conf", "/etc/two/agent.yml", "configuration file")
vm_name := flag.String("vm_name", "", "Nom de la vm") vm_name := flag.String("vm_name", "", "Nom de la vm")
@ -43,12 +29,21 @@ func main() {
} }
fmt.Print(conf) fmt.Print(conf)
DB = kv.InitDB(kv.Config{ db := kv.InitDB(kv.Config{
Path: conf.Database.Path, Path: conf.Database.Path,
}) })
defer DB.Close() defer db.Close()
fmt.Printf("conf metadata for %s\n - this key %s\n - this password %s\n", *vm_name, *ssh_key, *password) if *start {
nocloud.LoadNcCloudInDB(nocloud.Config{
os.Exit(0) VpcName: *vpc,
Name: *vm_name,
BindIP: *bind_ip,
BindPort: *bind_port,
Password: *password,
SSHKEY: *ssh_key,
}, db)
} else if *stop {
nocloud.UnLoadNoCloudInDB(*vm_name, db)
}
} }

View file

@ -1,11 +1,44 @@
package kv package kv
import ( import (
"fmt"
"log"
"github.com/dgraph-io/badger/v4" "github.com/dgraph-io/badger/v4"
) )
func DeleteInDB(db *badger.DB, key string) error { func deleteKey(db *badger.DB, key string) error {
return db.Update(func(txn *badger.Txn) error { return db.Update(func(txn *badger.Txn) error {
return txn.Delete([]byte(key)) return txn.Delete([]byte(key))
}) })
} }
func DeleteInDB(db *badger.DB, key string) error {
prefix := []byte(key + "/")
err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
it := txn.NewIterator(opts)
defer it.Close()
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
key := item.Key()
k := append([]byte{}, key...)
fmt.Println(string(k))
if err := deleteKey(db, string(k)); err != nil {
return err
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
return deleteKey(db, key)
}