diff --git a/cmd/meta_cli/main.go b/cmd/meta_cli/main.go new file mode 100644 index 0000000..ce97286 --- /dev/null +++ b/cmd/meta_cli/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "flag" + "fmt" + "os" + "strings" + + configuration "git.g3e.fr/syonad/two/internal/config/agent" + "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() { + conf_file := flag.String("conf", "/etc/two/agent.yml", "configuration file") + vm_name := flag.String("vm_name", "", "Nom de la vm") + vpc := flag.String("vpc_name", "", "vpc name") + bind_ip := flag.String("ip", "", "bind ip") + bind_port := flag.String("port", "", "bind port") + ssh_key := flag.String("key", "", "Clef ssh") + password := flag.String("pass", "", "password user") + start := flag.Bool("start", false, "start metadata server") + stop := flag.Bool("stop", false, "stop metadata server") + + flag.Parse() + + conf, err := configuration.LoadConfig(*conf_file) + if err != nil { + fmt.Println(err) + return + } + fmt.Print(conf) + + DB = kv.InitDB(kv.Config{ + Path: conf.Database.Path, + }) + defer DB.Close() + + fmt.Printf("conf metadata for %s\n - this key %s\n - this password %s\n", *vm_name, *ssh_key, *password) + + os.Exit(0) +} diff --git a/conf/agent/config.dev.yml b/conf/agent/config.dev.yml new file mode 100644 index 0000000..62cb624 --- /dev/null +++ b/conf/agent/config.dev.yml @@ -0,0 +1,2 @@ +database: + path: "./data/" \ No newline at end of file diff --git a/exemple/agent/config.exemple.yml b/conf/agent/config.exemple.yml similarity index 100% rename from exemple/agent/config.exemple.yml rename to conf/agent/config.exemple.yml diff --git a/internal/config/agent/struct.go b/internal/config/agent/struct.go index 12a46d3..c9537bf 100644 --- a/internal/config/agent/struct.go +++ b/internal/config/agent/struct.go @@ -17,9 +17,7 @@ func LoadConfig(path string) (*Config, error) { v.SetDefault("database.path", "/var/lib/two/data/") - if err := v.ReadInConfig(); err != nil { - return nil, err - } + v.ReadInConfig() var cfg Config if err := v.Unmarshal(&cfg); err != nil { diff --git a/internal/load_db/nocloud/render.go b/internal/load_db/nocloud/render.go new file mode 100644 index 0000000..ad7eee1 --- /dev/null +++ b/internal/load_db/nocloud/render.go @@ -0,0 +1,50 @@ +package nocloud + +import ( + "bytes" + "embed" + "text/template" + + "git.g3e.fr/syonad/two/pkg/db/kv" + "github.com/dgraph-io/badger/v4" +) + +//go:embed templates/*.tmpl +var templateFS embed.FS + +func renderConfig(path string, cfg Config) (string, error) { + tpl, err := template.ParseFS(templateFS, path) + if err != nil { + return "", err + } + + var buf bytes.Buffer + if err := tpl.Execute(&buf, cfg); err != nil { + return "", err + } + + return buf.String(), nil +} + +var DB *badger.DB + +func LoadNcCloudInDB(config Config, db *badger.DB) { + meta_data, _ := renderConfig("templates/meta-data.tmpl", config) + user_data, _ := renderConfig("templates/user-data.tmpl", config) + network_config, _ := renderConfig("templates/network-config.tmpl", config) + vendor_data, _ := renderConfig("templates/vendor-data.tmpl", config) + + DB = db + + kv.AddInDB(DB, "metadata/"+config.Name+"/meta-data", meta_data) + kv.AddInDB(DB, "metadata/"+config.Name+"/user-data", user_data) + kv.AddInDB(DB, "metadata/"+config.Name+"/network-config", network_config) + kv.AddInDB(DB, "metadata/"+config.Name+"/vendor-data", vendor_data) + kv.AddInDB(DB, "metadata/"+config.Name+"/vpc", config.VpcName) + kv.AddInDB(DB, "metadata/"+config.Name+"/bind_ip", config.BindIP) + kv.AddInDB(DB, "metadata/"+config.Name+"/bind_port", config.BindPort) +} + +func UnLoadNoCloudInDB(vm_name string, db *badger.DB) { + kv.DeleteInDB(DB, "metadata/"+vm_name) +} diff --git a/internal/load_db/nocloud/struct.go b/internal/load_db/nocloud/struct.go new file mode 100644 index 0000000..4134000 --- /dev/null +++ b/internal/load_db/nocloud/struct.go @@ -0,0 +1,10 @@ +package nocloud + +type Config struct { + VpcName string + BindIP string + BindPort string + Name string + Password string + SSHKEY string +} diff --git a/internal/load_db/nocloud/templates/meta-data.tmpl b/internal/load_db/nocloud/templates/meta-data.tmpl new file mode 100644 index 0000000..ff876bb --- /dev/null +++ b/internal/load_db/nocloud/templates/meta-data.tmpl @@ -0,0 +1,2 @@ +instance-id: {{ .Name }} +local-hostname: {{ .Name }} diff --git a/internal/load_db/nocloud/templates/network-config.tmpl b/internal/load_db/nocloud/templates/network-config.tmpl new file mode 100644 index 0000000..0f8d052 --- /dev/null +++ b/internal/load_db/nocloud/templates/network-config.tmpl @@ -0,0 +1,4 @@ +version: 2 +ethernets: + eth0: + dhcp4: true diff --git a/internal/load_db/nocloud/templates/user-data.tmpl b/internal/load_db/nocloud/templates/user-data.tmpl new file mode 100644 index 0000000..84195ab --- /dev/null +++ b/internal/load_db/nocloud/templates/user-data.tmpl @@ -0,0 +1,3 @@ +#!/bin/sh + +passwd -d root diff --git a/internal/load_db/nocloud/templates/vendor-data.tmpl b/internal/load_db/nocloud/templates/vendor-data.tmpl new file mode 100644 index 0000000..148d6db --- /dev/null +++ b/internal/load_db/nocloud/templates/vendor-data.tmpl @@ -0,0 +1,13 @@ +#cloud-config +users: + - name: syonad + lock_passwd: false + gecos: alpine Cloud User + groups: [adm, wheel] + doas: + - permit nopass syonad + sudo: ["ALL=(ALL) NOPASSWD:ALL"] + shell: /bin/ash + passwd: "{{ .Password }}" + ssh_authorized_keys: + - "{{ .SSHKEY }}" \ No newline at end of file diff --git a/pkg/db/kv/addInDB.go b/pkg/db/kv/addInDB.go new file mode 100644 index 0000000..44ea19c --- /dev/null +++ b/pkg/db/kv/addInDB.go @@ -0,0 +1,11 @@ +package kv + +import ( + "github.com/dgraph-io/badger/v4" +) + +func AddInDB(db *badger.DB, key string, value string) error { + return db.Update(func(txn *badger.Txn) error { + return txn.Set([]byte(key), []byte(value)) + }) +} diff --git a/pkg/db/kv/deleteInDB.go b/pkg/db/kv/deleteInDB.go new file mode 100644 index 0000000..fe8398e --- /dev/null +++ b/pkg/db/kv/deleteInDB.go @@ -0,0 +1,11 @@ +package kv + +import ( + "github.com/dgraph-io/badger/v4" +) + +func DeleteInDB(db *badger.DB, key string) error { + return db.Update(func(txn *badger.Txn) error { + return txn.Delete([]byte(key)) + }) +}