2024-06-12 08:18:33 +00:00
|
|
|
package garagesrv
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
import (
|
2024-10-27 13:31:10 +00:00
|
|
|
"cmp"
|
|
|
|
"context"
|
2021-04-20 21:31:37 +00:00
|
|
|
"io"
|
2024-10-27 13:31:10 +00:00
|
|
|
"slices"
|
2024-06-12 08:18:33 +00:00
|
|
|
"strconv"
|
2021-04-20 21:31:37 +00:00
|
|
|
"text/template"
|
2024-06-12 08:18:33 +00:00
|
|
|
|
|
|
|
"isle/garage"
|
2024-10-27 13:31:10 +00:00
|
|
|
"isle/toolkit"
|
|
|
|
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-11-08 16:46:44 +00:00
|
|
|
garage.LocalNode
|
|
|
|
BootstrapPeers []garage.RemoteNode
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var garageTomlTpl = template.Must(template.New("").Parse(`
|
|
|
|
metadata_dir = "{{ .MetaPath }}"
|
|
|
|
data_dir = "{{ .DataPath }}"
|
|
|
|
|
2024-06-12 08:18:33 +00:00
|
|
|
replication_mode = "` + strconv.Itoa(garage.ReplicationFactor) + `"
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
rpc_secret = "{{ .RPCSecret }}"
|
|
|
|
rpc_bind_addr = "{{ .RPCAddr }}"
|
|
|
|
rpc_public_addr = "{{ .RPCAddr }}"
|
|
|
|
|
|
|
|
bootstrap_peers = [{{- range .BootstrapPeers }}
|
2024-11-08 16:46:44 +00:00
|
|
|
"{{ .RPCNodeAddr }}",
|
2021-04-20 21:31:37 +00:00
|
|
|
{{ end -}}]
|
|
|
|
|
|
|
|
[s3_api]
|
2022-10-28 22:09:18 +00:00
|
|
|
api_bind_addr = "{{ .S3APIAddr }}"
|
2021-04-20 21:31:37 +00:00
|
|
|
s3_region = "garage"
|
|
|
|
|
2022-10-16 19:22:58 +00:00
|
|
|
[admin]
|
|
|
|
api_bind_addr = "{{ .AdminAddr }}"
|
|
|
|
admin_token = "{{ .AdminToken }}"
|
2021-04-20 21:31:37 +00:00
|
|
|
`))
|
|
|
|
|
|
|
|
// 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
|
2024-10-27 13:31:10 +00:00
|
|
|
// 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,
|
|
|
|
) {
|
2024-11-08 16:46:44 +00:00
|
|
|
slices.SortFunc(data.BootstrapPeers, func(i, j garage.RemoteNode) int {
|
2024-10-27 13:31:10 +00:00
|
|
|
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)
|
|
|
|
},
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
}
|