Compare commits
6 commits
97132550b0
...
13a5f4d3f9
| Author | SHA1 | Date | |
|---|---|---|---|
|
13a5f4d3f9 |
|||
|
b8796d0810 |
|||
|
a1f7c43888 |
|||
|
f1492acd85 |
|||
|
6d55b554d7 |
|||
|
105cc0b0f6 |
12 changed files with 161 additions and 3 deletions
54
cmd/meta_cli/main.go
Normal file
54
cmd/meta_cli/main.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
2
conf/agent/config.dev.yml
Normal file
2
conf/agent/config.dev.yml
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
database:
|
||||||
|
path: "./data/"
|
||||||
|
|
@ -17,9 +17,7 @@ func LoadConfig(path string) (*Config, error) {
|
||||||
|
|
||||||
v.SetDefault("database.path", "/var/lib/two/data/")
|
v.SetDefault("database.path", "/var/lib/two/data/")
|
||||||
|
|
||||||
if err := v.ReadInConfig(); err != nil {
|
v.ReadInConfig()
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var cfg Config
|
var cfg Config
|
||||||
if err := v.Unmarshal(&cfg); err != nil {
|
if err := v.Unmarshal(&cfg); err != nil {
|
||||||
|
|
|
||||||
50
internal/load_db/nocloud/render.go
Normal file
50
internal/load_db/nocloud/render.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
10
internal/load_db/nocloud/struct.go
Normal file
10
internal/load_db/nocloud/struct.go
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
package nocloud
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
VpcName string
|
||||||
|
BindIP string
|
||||||
|
BindPort string
|
||||||
|
Name string
|
||||||
|
Password string
|
||||||
|
SSHKEY string
|
||||||
|
}
|
||||||
2
internal/load_db/nocloud/templates/meta-data.tmpl
Normal file
2
internal/load_db/nocloud/templates/meta-data.tmpl
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
instance-id: {{ .Name }}
|
||||||
|
local-hostname: {{ .Name }}
|
||||||
4
internal/load_db/nocloud/templates/network-config.tmpl
Normal file
4
internal/load_db/nocloud/templates/network-config.tmpl
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
version: 2
|
||||||
|
ethernets:
|
||||||
|
eth0:
|
||||||
|
dhcp4: true
|
||||||
3
internal/load_db/nocloud/templates/user-data.tmpl
Normal file
3
internal/load_db/nocloud/templates/user-data.tmpl
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
passwd -d root
|
||||||
13
internal/load_db/nocloud/templates/vendor-data.tmpl
Normal file
13
internal/load_db/nocloud/templates/vendor-data.tmpl
Normal file
|
|
@ -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 }}"
|
||||||
11
pkg/db/kv/addInDB.go
Normal file
11
pkg/db/kv/addInDB.go
Normal file
|
|
@ -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))
|
||||||
|
})
|
||||||
|
}
|
||||||
11
pkg/db/kv/deleteInDB.go
Normal file
11
pkg/db/kv/deleteInDB.go
Normal file
|
|
@ -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))
|
||||||
|
})
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue