76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
package garagesrv
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strconv"
|
|
"text/template"
|
|
|
|
"isle/garage"
|
|
|
|
"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
|
|
|
|
RPCSecret string
|
|
AdminToken string
|
|
DBEngine DBEngine
|
|
|
|
garage.LocalNode
|
|
BootstrapPeers []garage.RemoteNode
|
|
}
|
|
|
|
var garageTomlTpl = template.Must(template.New("").Parse(`
|
|
metadata_dir = "{{ .MetaPath }}"
|
|
data_dir = "{{ .DataPath }}"
|
|
db_engine = "{{ .DBEngine }}"
|
|
|
|
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"
|
|
|
|
[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.
|
|
func WriteGarageTomlFile(
|
|
ctx context.Context, logger *mlog.Logger, path string, data GarageTomlData,
|
|
) error {
|
|
buf := new(bytes.Buffer)
|
|
if err := garageTomlTpl.Execute(buf, data); err != nil {
|
|
return fmt.Errorf("executing template: %w", err)
|
|
}
|
|
|
|
if err := os.WriteFile(path, buf.Bytes(), 0600); err != nil {
|
|
return fmt.Errorf("writing file: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|