2022-10-16 18:33:31 +00:00
|
|
|
package entrypoint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-10-19 14:20:26 +00:00
|
|
|
"context"
|
2022-10-16 18:33:31 +00:00
|
|
|
crypticnet "cryptic-net"
|
|
|
|
"cryptic-net/bootstrap"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/cryptic-io/pmux/pmuxlib"
|
|
|
|
)
|
|
|
|
|
2022-10-19 14:20:26 +00:00
|
|
|
func copyBootstrapToDataDirAndReload(env crypticnet.Env, r io.Reader) (crypticnet.Env, error) {
|
2022-10-16 18:33:31 +00:00
|
|
|
|
|
|
|
path := env.DataDirBootstrapPath()
|
|
|
|
dirPath := filepath.Dir(path)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(dirPath, 0700); err != nil {
|
2022-10-19 14:20:26 +00:00
|
|
|
return crypticnet.Env{}, fmt.Errorf("creating directory %q: %w", dirPath, err)
|
2022-10-16 18:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
2022-10-19 14:20:26 +00:00
|
|
|
return crypticnet.Env{}, fmt.Errorf("creating file %q: %w", path, err)
|
2022-10-16 18:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(f, r)
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
2022-10-19 14:20:26 +00:00
|
|
|
return crypticnet.Env{}, fmt.Errorf("copying bootstrap file to %q: %w", path, err)
|
2022-10-16 18:33:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 14:20:26 +00:00
|
|
|
return env.LoadBootstrap(path)
|
2022-10-16 18:33:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 14:20:26 +00:00
|
|
|
func mergeDaemonIntoBootstrap(env crypticnet.Env) (crypticnet.Env, error) {
|
2022-10-16 18:33:31 +00:00
|
|
|
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 {
|
2022-10-19 14:20:26 +00:00
|
|
|
return crypticnet.Env{}, fmt.Errorf("writing new bootstrap file to buffer: %w", err)
|
2022-10-16 18:33:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 14:20:26 +00:00
|
|
|
return copyBootstrapToDataDirAndReload(env, buf)
|
|
|
|
}
|
2022-10-16 18:33:31 +00:00
|
|
|
|
2022-10-19 14:20:26 +00:00
|
|
|
func doOnce(ctx context.Context, fn func(context.Context) error) error {
|
|
|
|
for {
|
|
|
|
if err := fn(ctx); err == nil {
|
|
|
|
return nil
|
|
|
|
} else if ctxErr := ctx.Err(); ctxErr != nil {
|
|
|
|
return ctxErr
|
|
|
|
}
|
|
|
|
}
|
2022-10-16 18:33:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 14:20:26 +00:00
|
|
|
func waitForNebulaArgs(env crypticnet.Env, args ...string) []string {
|
2022-10-16 18:33:31 +00:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|