Introduce admin.CreationParams

This commit is contained in:
Brian Picciano 2022-10-16 17:05:05 +02:00
parent cf00561206
commit 0a826f86da
4 changed files with 65 additions and 27 deletions

View File

@ -16,13 +16,15 @@ tmp="$(mktemp -d -t cryptic-net-dnsmasq-entrypoint-XXX)"
thisHostName=$(tar xzf "$_BOOTSTRAP_PATH" --to-stdout ./hostname) thisHostName=$(tar xzf "$_BOOTSTRAP_PATH" --to-stdout ./hostname)
thisHostIP=$(cat "$tmp"/hosts/"$thisHostName".yml | yq '.nebula.ip') thisHostIP=$(cat "$tmp"/hosts/"$thisHostName".yml | yq '.nebula.ip')
domain=$(tar xzf "$_BOOTSTRAP_PATH" --to-stdout ./admin/creation-params.yml | yq '.domain')
echo "listen-address=$thisHostIP" >> "$conf_path" echo "listen-address=$thisHostIP" >> "$conf_path"
ls -1 "$tmp"/hosts | while read hostYml; do ls -1 "$tmp"/hosts | while read hostYml; do
hostName=$(echo "$hostYml" | cut -d. -f1) hostName=$(echo "$hostYml" | cut -d. -f1)
hostIP=$(cat "$tmp"/hosts/"$hostYml" | yq '.nebula.ip') hostIP=$(cat "$tmp"/hosts/"$hostYml" | yq '.nebula.ip')
echo "address=/${hostName}.hosts.cryptic.io/$hostIP" >> "$conf_path" echo "address=/${hostName}.hosts.$domain/$hostIP" >> "$conf_path"
done done
) )

View File

@ -14,6 +14,8 @@ import (
) )
const ( const (
creationParamsPath = "admin/creation-params.yml"
nebulaCertsCACertPath = "nebula/certs/ca.crt" nebulaCertsCACertPath = "nebula/certs/ca.crt"
nebulaCertsCAKeyPath = "nebula/certs/ca.key" nebulaCertsCAKeyPath = "nebula/certs/ca.key"
@ -22,8 +24,17 @@ const (
garageRPCSecretPath = "garage/rpc-secret.txt" garageRPCSecretPath = "garage/rpc-secret.txt"
) )
// CreationParams are general parameters used when creating a new network. These
// are available to all hosts within the network via their bootstrap files.
type CreationParams struct {
Domain string `yaml:"domain"`
CIDRs []string `yaml:"cidrs"`
}
// Admin is used for accessing all information contained within an admin.tgz. // Admin is used for accessing all information contained within an admin.tgz.
type Admin struct { type Admin struct {
CreationParams CreationParams
NebulaCACert nebula.CACert NebulaCACert nebula.CACert
GarageRPCSecret string GarageRPCSecret string
@ -41,6 +52,7 @@ func FromFS(adminFS fs.FS) (Admin, error) {
into interface{} into interface{}
path string path string
}{ }{
{&a.CreationParams, creationParamsPath},
{&a.GarageGlobalBucketS3APICredentials, garageGlobalBucketKeyYmlPath}, {&a.GarageGlobalBucketS3APICredentials, garageGlobalBucketKeyYmlPath},
{&a.GarageAdminBucketS3APICredentials, garageAdminBucketKeyYmlPath}, {&a.GarageAdminBucketS3APICredentials, garageAdminBucketKeyYmlPath},
} }
@ -91,6 +103,7 @@ func (a Admin) WriteTo(into io.Writer) error {
value interface{} value interface{}
path string path string
}{ }{
{a.CreationParams, creationParamsPath},
{a.GarageGlobalBucketS3APICredentials, garageGlobalBucketKeyYmlPath}, {a.GarageGlobalBucketS3APICredentials, garageGlobalBucketKeyYmlPath},
{a.GarageAdminBucketS3APICredentials, garageAdminBucketKeyYmlPath}, {a.GarageAdminBucketS3APICredentials, garageAdminBucketKeyYmlPath},
} }

View File

@ -3,6 +3,7 @@
package bootstrap package bootstrap
import ( import (
"cryptic-net/admin"
"cryptic-net/garage" "cryptic-net/garage"
"cryptic-net/nebula" "cryptic-net/nebula"
"cryptic-net/tarutil" "cryptic-net/tarutil"
@ -20,12 +21,15 @@ import (
// Paths within the bootstrap FS which for general data. // Paths within the bootstrap FS which for general data.
const ( const (
adminCreationParamsPath = "admin/creation-params.yml"
hostNamePath = "hostname" hostNamePath = "hostname"
) )
// Bootstrap is used for accessing all information contained within a // Bootstrap is used for accessing all information contained within a
// bootstrap.tgz file. // bootstrap.tgz file.
type Bootstrap struct { type Bootstrap struct {
AdminCreationParams admin.CreationParams
Hosts map[string]Host Hosts map[string]Host
HostName string HostName string
@ -48,12 +52,18 @@ func FromFS(bootstrapFS fs.FS) (Bootstrap, error) {
return Bootstrap{}, fmt.Errorf("loading hosts info from fs: %w", err) return Bootstrap{}, fmt.Errorf("loading hosts info from fs: %w", err)
} }
if err = yamlutil.LoadYamlFSFile( filesToLoadAsYAML := []struct {
&b.GarageGlobalBucketS3APICredentials, into interface{}
bootstrapFS, path string
garageGlobalBucketKeyYmlPath, }{
); err != nil { {&b.AdminCreationParams, adminCreationParamsPath},
return Bootstrap{}, fmt.Errorf("loading %q from fs: %w", garageGlobalBucketKeyYmlPath, err) {&b.GarageGlobalBucketS3APICredentials, garageGlobalBucketKeyYmlPath},
}
for _, f := range filesToLoadAsYAML {
if err := yamlutil.LoadYamlFSFile(f.into, bootstrapFS, f.path); err != nil {
return Bootstrap{}, fmt.Errorf("loading %q from fs: %w", f.path, err)
}
} }
filesToLoadAsString := []struct { filesToLoadAsString := []struct {
@ -106,6 +116,36 @@ func (b Bootstrap) WriteTo(into io.Writer) error {
w := tarutil.NewTGZWriter(into) w := tarutil.NewTGZWriter(into)
for _, host := range b.Hosts {
hostB, err := yaml.Marshal(host)
if err != nil {
return fmt.Errorf("yaml encoding host %#v: %w", host, err)
}
path := filepath.Join(hostsDirPath, host.Name+".yml")
w.WriteFileBytes(path, hostB)
}
filesToWriteAsYAML := []struct {
value interface{}
path string
}{
{b.AdminCreationParams, adminCreationParamsPath},
{b.GarageGlobalBucketS3APICredentials, garageGlobalBucketKeyYmlPath},
}
for _, f := range filesToWriteAsYAML {
b, err := yaml.Marshal(f.value)
if err != nil {
return fmt.Errorf("yaml encoding data for %q: %w", f.path, err)
}
w.WriteFileBytes(f.path, b)
}
filesToWriteAsString := []struct { filesToWriteAsString := []struct {
value string value string
path string path string
@ -121,25 +161,6 @@ func (b Bootstrap) WriteTo(into io.Writer) error {
w.WriteFileBytes(f.path, []byte(f.value)) w.WriteFileBytes(f.path, []byte(f.value))
} }
garageGlobalBucketKeyB, err := yaml.Marshal(b.GarageGlobalBucketS3APICredentials)
if err != nil {
return fmt.Errorf("yaml encoding garage global bucket creds: %w", err)
}
w.WriteFileBytes(garageGlobalBucketKeyYmlPath, garageGlobalBucketKeyB)
for _, host := range b.Hosts {
hostB, err := yaml.Marshal(host)
if err != nil {
return fmt.Errorf("yaml encoding host %#v: %w", host, err)
}
path := filepath.Join(hostsDirPath, host.Name+".yml")
w.WriteFileBytes(path, hostB)
}
return w.Close() return w.Close()
} }

View File

@ -88,6 +88,8 @@ var subCmdAdminMakeBootstrap = subCmd{
} }
newBootstrap := bootstrap.Bootstrap{ newBootstrap := bootstrap.Bootstrap{
AdminCreationParams: adm.CreationParams,
Hosts: hosts, Hosts: hosts,
HostName: *name, HostName: *name,