91 lines
2.0 KiB
Go
91 lines
2.0 KiB
Go
|
package entrypoint
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
crypticnet "cryptic-net"
|
||
|
"cryptic-net/bootstrap"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/cryptic-io/pmux/pmuxlib"
|
||
|
)
|
||
|
|
||
|
func copyBootstrapToDataDir(env *crypticnet.Env, r io.Reader) error {
|
||
|
|
||
|
path := env.DataDirBootstrapPath()
|
||
|
dirPath := filepath.Dir(path)
|
||
|
|
||
|
if err := os.MkdirAll(dirPath, 0700); err != nil {
|
||
|
return fmt.Errorf("creating directory %q: %w", dirPath, err)
|
||
|
}
|
||
|
|
||
|
f, err := os.Create(path)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("creating file %q: %w", path, err)
|
||
|
}
|
||
|
|
||
|
_, err = io.Copy(f, r)
|
||
|
f.Close()
|
||
|
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("copying bootstrap file to %q: %w", path, err)
|
||
|
}
|
||
|
|
||
|
if err := env.LoadBootstrap(path); err != nil {
|
||
|
return fmt.Errorf("loading bootstrap from %q: %w", path, err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func mergeDaemonIntoBootstrap(env *crypticnet.Env) error {
|
||
|
daemon := env.ThisDaemon()
|
||
|
host := env.Bootstrap.ThisHost()
|
||
|
|
||
|
host.Nebula.PublicAddr = daemon.VPN.PublicAddr
|
||
|
|
||
|
host.Garage = nil
|
||
|
|
||
|
if allocs := daemon.Storage.Allocations; len(allocs) > 0 {
|
||
|
|
||
|
host.Garage = new(bootstrap.GarageHost)
|
||
|
|
||
|
for _, alloc := range allocs {
|
||
|
host.Garage.Instances = append(host.Garage.Instances, bootstrap.GarageHostInstance{
|
||
|
RPCPort: alloc.RPCPort,
|
||
|
S3APIPort: alloc.S3APIPort,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
env.Bootstrap.Hosts[host.Name] = host
|
||
|
|
||
|
buf := new(bytes.Buffer)
|
||
|
if err := env.Bootstrap.WithHosts(env.Bootstrap.Hosts).WriteTo(buf); err != nil {
|
||
|
return fmt.Errorf("writing new bootstrap file to buffer: %w", err)
|
||
|
}
|
||
|
|
||
|
if err := copyBootstrapToDataDir(env, buf); err != nil {
|
||
|
return fmt.Errorf("copying new bootstrap file to data dir: %w", err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func waitForNebulaArgs(env *crypticnet.Env, args ...string) []string {
|
||
|
thisHost := env.Bootstrap.ThisHost()
|
||
|
return append([]string{"wait-for-ip", thisHost.Nebula.IP}, args...)
|
||
|
}
|
||
|
|
||
|
func nebulaEntrypointPmuxProcConfig() pmuxlib.ProcessConfig {
|
||
|
return pmuxlib.ProcessConfig{
|
||
|
Name: "nebula",
|
||
|
Cmd: "cryptic-net-main",
|
||
|
Args: []string{
|
||
|
"nebula-entrypoint",
|
||
|
},
|
||
|
}
|
||
|
}
|