diff --git a/internal/types/cloud_init.go b/internal/types/cloud_init.go index 73506d6..0f1b360 100644 --- a/internal/types/cloud_init.go +++ b/internal/types/cloud_init.go @@ -1,15 +1,14 @@ package types import ( - "bytes" _ "embed" "encoding/base64" "os" "os/exec" "path/filepath" - "text/template" - "github.com/Masterminds/sprig/v3" + "github.com/projecteru2/yavirt/configs" + "github.com/projecteru2/yavirt/internal/utils/template" "github.com/kdomanski/iso9660/util" @@ -23,10 +22,6 @@ var ( metaData string //go:embed templates/network-config.yaml networkData string - - userDataTpl *template.Template - metaDataTpl *template.Template - networkTpl *template.Template ) type CloudInitGateway struct { @@ -54,29 +49,7 @@ type CloudInitConfig struct { DefaultGW CloudInitGateway `json:"-"` } -func initTpls() (err error) { - if userDataTpl == nil { - if userDataTpl, err = template.New("userdata").Funcs(sprig.TxtFuncMap()).Parse(userData); err != nil { - return - } - } - if metaDataTpl == nil { - if metaDataTpl, err = template.New("metadata").Funcs(sprig.TxtFuncMap()).Parse(metaData); err != nil { - return - } - } - if networkTpl == nil { - if networkTpl, err = template.New("network").Funcs(sprig.TxtFuncMap()).Parse(networkData); err != nil { - return - } - } - return -} - func (ciCfg *CloudInitConfig) GenFilesContent() (string, string, string, error) { - if err := initTpls(); err != nil { - return "", "", "", err - } d1 := map[string]any{ "username": ciCfg.Username, "password": ciCfg.Password, @@ -98,8 +71,12 @@ func (ciCfg *CloudInitConfig) GenFilesContent() (string, string, string, error) "content": base64.StdEncoding.EncodeToString(v), }) } - var userDataBs bytes.Buffer - if err := userDataTpl.Execute(&userDataBs, d1); err != nil { + udataTmplFile := filepath.Join(configs.Conf.VirtTmplDir, "user-data.yaml") + mdataTmplFile := filepath.Join(configs.Conf.VirtTmplDir, "meta-data.yaml") + networkTmplFile := filepath.Join(configs.Conf.VirtTmplDir, "network-config.yaml") + + uDataBS, err := template.Render(udataTmplFile, userData, d1) + if err != nil { return "", "", "", err } @@ -107,15 +84,15 @@ func (ciCfg *CloudInitConfig) GenFilesContent() (string, string, string, error) "instanceID": ciCfg.InstanceID, "hostname": ciCfg.Hostname, } - var metaDataBs bytes.Buffer - if err := metaDataTpl.Execute(&metaDataBs, d2); err != nil { + mDataBS, err := template.Render(mdataTmplFile, metaData, d2) + if err != nil { return "", "", "", err } - var networkBs bytes.Buffer - if err := networkTpl.Execute(&networkBs, d1); err != nil { + networkBS, err := template.Render(networkTmplFile, networkData, d1) + if err != nil { return "", "", "", err } - return userDataBs.String(), metaDataBs.String(), networkBs.String(), nil + return string(uDataBS), string(mDataBS), string(networkBS), nil } func (ciCfg *CloudInitConfig) GenerateISO(fname string) (err error) { diff --git a/internal/virt/template/template.go b/internal/utils/template/template.go similarity index 89% rename from internal/virt/template/template.go rename to internal/utils/template/template.go index ece0d05..9595bdf 100644 --- a/internal/virt/template/template.go +++ b/internal/utils/template/template.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "os" - "strings" "sync" text "text/template" @@ -52,9 +51,6 @@ func (t *Templates) get(fpth string, defaultTemplStr string) (tmpl *text.Templat } func (t *Templates) parse(fpth string, defaultTemplStr string) (*text.Template, error) { - if !t.isTempl(fpth) { - return nil, errors.Errorf("%s is not a template file", fpth) - } f, err := os.Open(fpth) // if file path doesn't exist, then use default template string if err != nil && os.IsNotExist(err) { @@ -73,7 +69,3 @@ func (t *Templates) parse(fpth string, defaultTemplStr string) (*text.Template, } return text.New(fpth).Funcs(sprig.TxtFuncMap()).Parse(string(buf)) } - -func (t *Templates) isTempl(fpth string) bool { - return strings.HasSuffix(fpth, ".xml") -} diff --git a/internal/virt/domain/domain.go b/internal/virt/domain/domain.go index cde9ca0..43a9fcd 100644 --- a/internal/virt/domain/domain.go +++ b/internal/virt/domain/domain.go @@ -20,7 +20,7 @@ import ( "github.com/projecteru2/yavirt/internal/models" "github.com/projecteru2/yavirt/internal/network" "github.com/projecteru2/yavirt/internal/types" - "github.com/projecteru2/yavirt/internal/virt/template" + "github.com/projecteru2/yavirt/internal/utils/template" "github.com/projecteru2/yavirt/internal/vmcache" "github.com/projecteru2/yavirt/pkg/libvirt" "github.com/projecteru2/yavirt/pkg/terrors"