isle/go/garage/garagesrv/tpl.go

82 lines
1.7 KiB
Go
Raw Permalink Normal View History

package garagesrv
import (
"cmp"
"context"
"io"
"slices"
"strconv"
"text/template"
"isle/garage"
"isle/toolkit"
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
)
// GarageTomlData describes all fields needed for rendering a garage.toml
// file via this package's template.
type GarageTomlData struct {
MetaPath string
DataPath string
2022-10-16 19:22:58 +00:00
RPCSecret string
AdminToken string
garage.LocalNode
BootstrapPeers []garage.RemoteNode
}
var garageTomlTpl = template.Must(template.New("").Parse(`
metadata_dir = "{{ .MetaPath }}"
data_dir = "{{ .DataPath }}"
replication_mode = "` + strconv.Itoa(garage.ReplicationFactor) + `"
rpc_secret = "{{ .RPCSecret }}"
rpc_bind_addr = "{{ .RPCAddr }}"
rpc_public_addr = "{{ .RPCAddr }}"
bootstrap_peers = [{{- range .BootstrapPeers }}
"{{ .RPCNodeAddr }}",
{{ end -}}]
[s3_api]
api_bind_addr = "{{ .S3APIAddr }}"
s3_region = "garage"
2022-10-16 19:22:58 +00:00
[admin]
api_bind_addr = "{{ .AdminAddr }}"
admin_token = "{{ .AdminToken }}"
`))
// RenderGarageToml renders a garage.toml using the given data into the writer.
func RenderGarageToml(into io.Writer, data GarageTomlData) error {
return garageTomlTpl.Execute(into, data)
}
// WriteGarageTomlFile renders a garage.toml using the given data to a new file
// at the given path, returning true if the file changed or didn't
// previously exist.
func WriteGarageTomlFile(
ctx context.Context,
logger *mlog.Logger,
path string,
data GarageTomlData,
) (
bool, error,
) {
slices.SortFunc(data.BootstrapPeers, func(i, j garage.RemoteNode) int {
return cmp.Or(
cmp.Compare(i.IP, j.IP),
cmp.Compare(i.RPCPort, j.RPCPort),
)
})
return toolkit.WriteFileCheckChanged(
ctx, logger, path, 0600, func(w io.Writer) error {
return garageTomlTpl.Execute(w, data)
},
)
}