2022-11-02 13:34:40 +00:00
|
|
|
// Package bootstrap deals with the parsing and creation of bootstrap.yml files.
|
2022-10-16 14:39:05 +00:00
|
|
|
// It also contains some helpers which rely on bootstrap data.
|
2021-04-20 21:31:37 +00:00
|
|
|
package bootstrap
|
|
|
|
|
|
|
|
import (
|
2023-08-05 21:53:17 +00:00
|
|
|
"isle/admin"
|
|
|
|
"isle/garage"
|
|
|
|
"isle/nebula"
|
2022-10-15 16:41:07 +00:00
|
|
|
"crypto/sha512"
|
2021-04-20 21:31:37 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-10-15 14:28:03 +00:00
|
|
|
"os"
|
2022-10-15 16:41:07 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
|
2022-10-26 22:23:39 +00:00
|
|
|
// DataDirPath returns the path within the user's data directory where the
|
|
|
|
// bootstrap file is stored.
|
|
|
|
func DataDirPath(dataDirPath string) string {
|
2022-11-02 13:34:40 +00:00
|
|
|
return filepath.Join(dataDirPath, "bootstrap.yml")
|
2022-10-26 22:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppDirPath returns the path within the AppDir where an embedded bootstrap
|
|
|
|
// file might be found.
|
|
|
|
func AppDirPath(appDirPath string) string {
|
2022-11-02 13:34:40 +00:00
|
|
|
return filepath.Join(appDirPath, "share/bootstrap.yml")
|
2022-10-26 22:23:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 14:28:03 +00:00
|
|
|
// Bootstrap is used for accessing all information contained within a
|
2022-11-02 13:34:40 +00:00
|
|
|
// bootstrap.yml file.
|
2022-10-15 14:28:03 +00:00
|
|
|
type Bootstrap struct {
|
2022-11-02 13:34:40 +00:00
|
|
|
AdminCreationParams admin.CreationParams `yaml:"admin_creation_params"`
|
2022-10-16 15:05:05 +00:00
|
|
|
|
2022-11-02 13:34:40 +00:00
|
|
|
Hosts map[string]Host `yaml:"hosts"`
|
|
|
|
HostName string `yaml:"hostname"`
|
2022-10-15 14:28:03 +00:00
|
|
|
|
2022-11-02 13:34:40 +00:00
|
|
|
Nebula struct {
|
2022-11-05 14:23:29 +00:00
|
|
|
CAPublicCredentials nebula.CAPublicCredentials `yaml:"ca_public_credentials"`
|
|
|
|
HostCredentials nebula.HostCredentials `yaml:"host_credentials"`
|
|
|
|
SignedPublicCredentials string `yaml:"signed_public_credentials"`
|
2022-11-02 13:34:40 +00:00
|
|
|
} `yaml:"nebula"`
|
2022-10-15 14:28:03 +00:00
|
|
|
|
2022-11-02 13:34:40 +00:00
|
|
|
Garage struct {
|
|
|
|
RPCSecret string `yaml:"rpc_secret"`
|
|
|
|
AdminToken string `yaml:"admin_token"`
|
|
|
|
GlobalBucketS3APICredentials garage.S3APICredentials `yaml:"global_bucket_s3_api_credentials"`
|
|
|
|
} `yaml:"garage"`
|
2022-10-15 14:28:03 +00:00
|
|
|
}
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2022-11-02 13:34:40 +00:00
|
|
|
// FromReader reads a bootstrap.yml file from the given io.Reader.
|
2022-10-15 14:28:03 +00:00
|
|
|
func FromReader(r io.Reader) (Bootstrap, error) {
|
2022-11-02 13:34:40 +00:00
|
|
|
var b Bootstrap
|
|
|
|
err := yaml.NewDecoder(r).Decode(&b)
|
|
|
|
return b, err
|
2022-10-11 19:24:53 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 13:34:40 +00:00
|
|
|
// FromFile reads a bootstrap.yml from a file at the given path.
|
2022-10-15 14:28:03 +00:00
|
|
|
func FromFile(path string) (Bootstrap, error) {
|
2022-10-11 19:24:53 +00:00
|
|
|
|
2022-10-15 14:28:03 +00:00
|
|
|
f, err := os.Open(path)
|
2022-10-11 19:24:53 +00:00
|
|
|
if err != nil {
|
2022-10-15 14:28:03 +00:00
|
|
|
return Bootstrap{}, fmt.Errorf("opening file: %w", err)
|
2022-10-11 19:24:53 +00:00
|
|
|
}
|
2022-10-15 14:28:03 +00:00
|
|
|
defer f.Close()
|
2022-10-11 19:24:53 +00:00
|
|
|
|
2022-10-15 14:28:03 +00:00
|
|
|
return FromReader(f)
|
|
|
|
}
|
2022-10-11 19:24:53 +00:00
|
|
|
|
2022-11-02 13:34:40 +00:00
|
|
|
// WriteTo writes the Bootstrap as a new bootstrap.yml to the given io.Writer.
|
2022-10-15 16:41:07 +00:00
|
|
|
func (b Bootstrap) WriteTo(into io.Writer) error {
|
2022-11-02 13:34:40 +00:00
|
|
|
return yaml.NewEncoder(into).Encode(b)
|
2022-10-15 16:41:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 14:28:03 +00:00
|
|
|
// ThisHost is a shortcut for b.Hosts[b.HostName], but will panic if the
|
|
|
|
// HostName isn't found in the Hosts map.
|
|
|
|
func (b Bootstrap) ThisHost() Host {
|
|
|
|
|
|
|
|
host, ok := b.Hosts[b.HostName]
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("hostname %q not defined in bootstrap's hosts", b.HostName))
|
2022-10-11 19:24:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 14:28:03 +00:00
|
|
|
return host
|
2022-10-11 19:24:53 +00:00
|
|
|
}
|
2022-10-15 16:41:07 +00:00
|
|
|
|
|
|
|
// Hash returns a deterministic hash of the given hosts map.
|
|
|
|
func HostsHash(hostsMap map[string]Host) ([]byte, error) {
|
|
|
|
|
|
|
|
hosts := make([]Host, 0, len(hostsMap))
|
|
|
|
for _, host := range hostsMap {
|
|
|
|
hosts = append(hosts, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(hosts, func(i, j int) bool { return hosts[i].Name < hosts[j].Name })
|
|
|
|
|
|
|
|
h := sha512.New()
|
|
|
|
|
|
|
|
if err := yaml.NewEncoder(h).Encode(hosts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.Sum(nil), nil
|
|
|
|
}
|