2024-06-12 08:18:33 +00:00
|
|
|
package garagesrv
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
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"
|
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-06-12 08:18:33 +00:00
|
|
|
garage.LocalPeer
|
|
|
|
BootstrapPeers []garage.RemotePeer
|
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-06-12 08:18:33 +00:00
|
|
|
"{{ .RPCPeerAddr }}",
|
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
|
|
|
|
// at the given path.
|
|
|
|
func WriteGarageTomlFile(path string, data GarageTomlData) error {
|
|
|
|
|
|
|
|
file, err := os.OpenFile(
|
2024-07-14 13:50:24 +00:00
|
|
|
path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600,
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
2022-10-26 20:18:16 +00:00
|
|
|
if err := garageTomlTpl.Execute(file, data); err != nil {
|
2021-04-20 21:31:37 +00:00
|
|
|
return fmt.Errorf("rendering template to file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|