feature-8 #9
6 changed files with 82 additions and 0 deletions
f-8: code: add module to load nocloud in db #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
commit
55ed2c8e53
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 }}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue