add functions to support new deployments

This commit is contained in:
Mikaël Cluseau
2026-01-25 11:23:47 +01:00
parent f57aa581f3
commit 47f695a8cd

View File

@ -4,6 +4,7 @@ import (
"bytes"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
@ -184,6 +185,10 @@ func (ctx *renderContext) TemplateFuncs() map[string]any {
}
for name, method := range map[string]any{
"base64": func(input string) string {
enc := base64.StdEncoding.WithPadding(base64.NoPadding)
return enc.EncodeToString([]byte(input))
},
"host_ip": func() (s string) {
return ctx.Host.IPs[0]
},
@ -236,6 +241,10 @@ func (ctx *renderContext) TemplateFuncs() map[string]any {
Content: string(userCA),
}})
},
"ssh_user_ca_pub": func(cluster string) (s string, err error) {
userCA, err := sshCAPubKey(cluster)
return string(userCA), err
},
"ssh_host_keys": func(dir, cluster, host string) (s string, err error) {
if host == "" {
host = ctx.Host.Name
@ -270,6 +279,20 @@ func (ctx *renderContext) TemplateFuncs() map[string]any {
return asYaml(files)
},
"ssh_host_key": func(type_ string) (s string, err error) {
pair, err := ctx.sshHostKeyPair(type_)
if err != nil {
return
}
return pair.Private, nil
},
"ssh_host_pubkey": func(type_ string) (s string, err error) {
pair, err := ctx.sshHostKeyPair(type_)
if err != nil {
return
}
return pair.Public, nil
},
"host_download_token": func() (token string, err error) {
key := ctx.Host.Name
token, found, err := hostDownloadTokens.Get(key)
@ -337,3 +360,19 @@ func (ctx *renderContext) TemplateFuncs() map[string]any {
return funcs
}
func (ctx *renderContext) sshHostKeyPair(type_ string) (kp SSHKeyPair, err error) {
pairs, err := getSSHKeyPairs(ctx.Host.Name)
if err != nil {
return
}
for _, pair := range pairs {
if pair.Type == type_ {
return pair, nil
}
}
err = fmt.Errorf("no key pair with type %q", type_)
return
}