Compare commits
3 Commits
fb151865b4
...
3f3ad43cb2
Author | SHA1 | Date | |
---|---|---|---|
|
3f3ad43cb2 | ||
|
15c5c904a2 | ||
|
81d4a35b24 |
@ -17,7 +17,6 @@ state AppDir {
|
|||||||
entrypoint : * Merge given and default daemon.yml files
|
entrypoint : * Merge given and default daemon.yml files
|
||||||
entrypoint : * Copy bootstrap.tgz into $_DATA_DIR_PATH, if it's not there
|
entrypoint : * Copy bootstrap.tgz into $_DATA_DIR_PATH, if it's not there
|
||||||
entrypoint : * Merge daemon.yml config into bootstrap.tgz
|
entrypoint : * Merge daemon.yml config into bootstrap.tgz
|
||||||
entrypoint : * Create $_RUNTIME_DIR_PATH/garage-N.toml\n (one per storage allocation)
|
|
||||||
entrypoint : * Run child processes
|
entrypoint : * Run child processes
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,17 +41,20 @@ state AppDir {
|
|||||||
entrypoint --> nebulaEntrypoint : child
|
entrypoint --> nebulaEntrypoint : child
|
||||||
nebulaEntrypoint --> nebula : exec
|
nebulaEntrypoint --> nebula : exec
|
||||||
|
|
||||||
state "Garage processes (only if any storage allocs are defined)" as garageChildren {
|
state "./bin/cryptic-net-main garage-entrypoint" as garageEntrypoint {
|
||||||
|
garageEntrypoint : * Create $_RUNTIME_DIR_PATH/garage-N.toml\n (one per storage allocation)
|
||||||
|
garageEntrypoint : * Run child processes
|
||||||
|
}
|
||||||
|
|
||||||
state "./bin/garage -c $_RUNTIME_DIR_PATH/garage-N.toml server" as garage
|
state "./bin/garage -c $_RUNTIME_DIR_PATH/garage-N.toml server" as garage
|
||||||
state "./bin/garage-apply-layout-diff" as garageApplyLayoutDiff {
|
state "./bin/garage-apply-layout-diff" as garageApplyLayoutDiff {
|
||||||
garageApplyLayoutDiff : * Runs once then exits
|
garageApplyLayoutDiff : * Runs once then exits
|
||||||
garageApplyLayoutDiff : * Updates cluster topo
|
garageApplyLayoutDiff : * Updates cluster topo
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
entrypoint --> garage : child (one per storage allocation)
|
entrypoint --> garageEntrypoint : child (only if >1 storage allocation defined in daemon.yml)
|
||||||
entrypoint --> garageApplyLayoutDiff : child
|
garageEntrypoint --> garage : child (one per storage allocation)
|
||||||
|
garageEntrypoint --> garageApplyLayoutDiff : child
|
||||||
|
|
||||||
state "./bin/cryptic-net-main update-global-bucket" as updateGlobalBucket {
|
state "./bin/cryptic-net-main update-global-bucket" as updateGlobalBucket {
|
||||||
updateGlobalBucket : * Runs once then exits
|
updateGlobalBucket : * Runs once then exits
|
||||||
|
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 18 KiB |
@ -27,8 +27,8 @@ const (
|
|||||||
// CreationParams are general parameters used when creating a new network. These
|
// CreationParams are general parameters used when creating a new network. These
|
||||||
// are available to all hosts within the network via their bootstrap files.
|
// are available to all hosts within the network via their bootstrap files.
|
||||||
type CreationParams struct {
|
type CreationParams struct {
|
||||||
ID string `yaml:"id"`
|
|
||||||
Domain string `yaml:"domain"`
|
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.
|
||||||
|
@ -16,6 +16,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"cryptic-net/cmd/entrypoint"
|
"cryptic-net/cmd/entrypoint"
|
||||||
|
garage_entrypoint "cryptic-net/cmd/garage-entrypoint"
|
||||||
garage_layout_diff "cryptic-net/cmd/garage-layout-diff"
|
garage_layout_diff "cryptic-net/cmd/garage-layout-diff"
|
||||||
garage_peer_keygen "cryptic-net/cmd/garage-peer-keygen"
|
garage_peer_keygen "cryptic-net/cmd/garage-peer-keygen"
|
||||||
nebula_entrypoint "cryptic-net/cmd/nebula-entrypoint"
|
nebula_entrypoint "cryptic-net/cmd/nebula-entrypoint"
|
||||||
@ -31,6 +32,7 @@ type mainFn struct {
|
|||||||
|
|
||||||
var mainFns = []mainFn{
|
var mainFns = []mainFn{
|
||||||
{"entrypoint", entrypoint.Main},
|
{"entrypoint", entrypoint.Main},
|
||||||
|
{"garage-entrypoint", garage_entrypoint.Main},
|
||||||
{"garage-layout-diff", garage_layout_diff.Main},
|
{"garage-layout-diff", garage_layout_diff.Main},
|
||||||
{"garage-peer-keygen", garage_peer_keygen.Main},
|
{"garage-peer-keygen", garage_peer_keygen.Main},
|
||||||
{"nebula-entrypoint", nebula_entrypoint.Main},
|
{"nebula-entrypoint", nebula_entrypoint.Main},
|
||||||
|
@ -1,29 +1,16 @@
|
|||||||
package entrypoint
|
package entrypoint
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"cryptic-net/admin"
|
"cryptic-net/admin"
|
||||||
"cryptic-net/bootstrap"
|
"cryptic-net/bootstrap"
|
||||||
"cryptic-net/nebula"
|
"cryptic-net/nebula"
|
||||||
"crypto/rand"
|
|
||||||
"encoding/hex"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/cryptic-io/pmux/pmuxlib"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func randStr(l int) string {
|
|
||||||
b := make([]byte, l)
|
|
||||||
if _, err := rand.Read(b); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return hex.EncodeToString(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func readAdmin(path string) (admin.Admin, error) {
|
func readAdmin(path string) (admin.Admin, error) {
|
||||||
|
|
||||||
if path == "-" {
|
if path == "-" {
|
||||||
@ -67,9 +54,9 @@ var subCmdAdminCreateNetwork = subCmd{
|
|||||||
"Domain name that should be used as the root domain in the network.",
|
"Domain name that should be used as the root domain in the network.",
|
||||||
)
|
)
|
||||||
|
|
||||||
subnetStr := flags.StringP(
|
ipCIDRStrs := flags.StringP(
|
||||||
"subnet", "s", "",
|
"ip-cidrs", "i", "",
|
||||||
"CIDR which denotes the subnet that IPs hosts on the network can be assigned.",
|
"Comma-separated list of CIDRs which denote what IPs hosts on the network can be assigned.",
|
||||||
)
|
)
|
||||||
|
|
||||||
if err := flags.Parse(subCmdCtx.args); err != nil {
|
if err := flags.Parse(subCmdCtx.args); err != nil {
|
||||||
@ -82,27 +69,33 @@ var subCmdAdminCreateNetwork = subCmd{
|
|||||||
return writeBuiltinDaemonYml(env, os.Stdout)
|
return writeBuiltinDaemonYml(env, os.Stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *domain == "" || *subnetStr == "" {
|
if *domain == "" {
|
||||||
return errors.New("--domain and --subnet are required")
|
return errors.New("--domain is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
*domain = strings.TrimRight(strings.TrimLeft(*domain, "."), ".")
|
*domain = strings.TrimRight(strings.TrimLeft(*domain, "."), ".")
|
||||||
|
|
||||||
ip, subnet, err := net.ParseCIDR(*subnetStr)
|
var (
|
||||||
|
ipCIDRs []*net.IPNet
|
||||||
|
thisIP net.IP
|
||||||
|
)
|
||||||
|
|
||||||
|
for _, ipCIDRStr := range strings.Split(*ipCIDRStrs, ",") {
|
||||||
|
|
||||||
|
ipCIDRStr = strings.TrimSpace(ipCIDRStr)
|
||||||
|
if ipCIDRStr == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ip, ipCIDR, err := net.ParseCIDR(ipCIDRStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parsing %q as a CIDR: %w", *subnetStr, err)
|
return fmt.Errorf("could not parse CIDR %q: %w", ipCIDRStr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hostName := "genesis"
|
thisIP = ip // we just need one IP from a CIDR, it doesn't matter which.
|
||||||
|
ipCIDRs = append(ipCIDRs, ipCIDR)
|
||||||
adminCreationParams := admin.CreationParams{
|
|
||||||
ID: randStr(32),
|
|
||||||
Domain: *domain,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
garageRPCSecret := randStr(32)
|
|
||||||
|
|
||||||
{
|
|
||||||
runtimeDirPath := env.RuntimeDirPath
|
runtimeDirPath := env.RuntimeDirPath
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "will use runtime directory %q for temporary state\n", runtimeDirPath)
|
fmt.Fprintf(os.Stderr, "will use runtime directory %q for temporary state\n", runtimeDirPath)
|
||||||
@ -117,7 +110,6 @@ var subCmdAdminCreateNetwork = subCmd{
|
|||||||
fmt.Fprintf(os.Stderr, "error removing temporary directory %q: %v", runtimeDirPath, err)
|
fmt.Fprintf(os.Stderr, "error removing temporary directory %q: %v", runtimeDirPath, err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
|
||||||
|
|
||||||
if err := writeMergedDaemonYml(env, *daemonYmlPath); err != nil {
|
if err := writeMergedDaemonYml(env, *daemonYmlPath); err != nil {
|
||||||
return fmt.Errorf("merging and writing daemon.yml file: %w", err)
|
return fmt.Errorf("merging and writing daemon.yml file: %w", err)
|
||||||
@ -129,68 +121,6 @@ var subCmdAdminCreateNetwork = subCmd{
|
|||||||
return fmt.Errorf("daemon.yml with at least 3 allocations was not provided")
|
return fmt.Errorf("daemon.yml with at least 3 allocations was not provided")
|
||||||
}
|
}
|
||||||
|
|
||||||
nebulaCACert, err := nebula.NewCACert(*domain, subnet)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("creating nebula CA cert: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
nebulaHostCert, err := nebula.NewHostCert(nebulaCACert, hostName, ip)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("creating nebula cert for host: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
host := bootstrap.Host{
|
|
||||||
Name: hostName,
|
|
||||||
Nebula: bootstrap.NebulaHost{
|
|
||||||
IP: ip.String(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
env.Bootstrap = bootstrap.Bootstrap{
|
|
||||||
AdminCreationParams: adminCreationParams,
|
|
||||||
Hosts: map[string]bootstrap.Host{
|
|
||||||
hostName: host,
|
|
||||||
},
|
|
||||||
HostName: hostName,
|
|
||||||
NebulaHostCert: nebulaHostCert,
|
|
||||||
GarageRPCSecret: garageRPCSecret,
|
|
||||||
}
|
|
||||||
|
|
||||||
// this will also write the bootstrap file
|
|
||||||
if err := mergeDaemonIntoBootstrap(env); err != nil {
|
|
||||||
return fmt.Errorf("merging daemon.yml into bootstrap data: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, val := range env.ToMap() {
|
|
||||||
if err := os.Setenv(key, val); err != nil {
|
|
||||||
return fmt.Errorf("failed to set %q to %q: %w", key, val, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
garageChildrenPmuxProcConfigs, err := garageChildrenPmuxProcConfigs(env)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("generating garage children configs: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pmuxConfig := pmuxlib.Config{
|
|
||||||
Processes: append(
|
|
||||||
[]pmuxlib.ProcessConfig{
|
|
||||||
nebulaEntrypointPmuxProcConfig(),
|
|
||||||
garageApplyLayoutDiffPmuxProcConfig(env),
|
|
||||||
},
|
|
||||||
garageChildrenPmuxProcConfigs...,
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(env.Context)
|
|
||||||
defer cancel()
|
|
||||||
pmuxDoneCh := make(chan struct{})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
pmuxlib.Run(ctx, pmuxConfig)
|
|
||||||
close(pmuxDoneCh)
|
|
||||||
}()
|
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -38,6 +42,34 @@ import (
|
|||||||
//
|
//
|
||||||
// * (On exit) cleans up the runtime directory.
|
// * (On exit) cleans up the runtime directory.
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// creates a new bootstrap file using available information from the network. If
|
// creates a new bootstrap file using available information from the network. If
|
||||||
// the new bootstrap file is different than the existing one, the existing one
|
// the new bootstrap file is different than the existing one, the existing one
|
||||||
// is overwritten, ReloadBootstrap is called on env, true is returned.
|
// is overwritten, ReloadBootstrap is called on env, true is returned.
|
||||||
@ -84,31 +116,65 @@ func runDaemonPmuxOnce(env *crypticnet.Env, s3Client garage.S3APIClient) error {
|
|||||||
fmt.Fprintf(os.Stderr, "host name is %q, ip is %q\n", thisHost.Name, thisHost.Nebula.IP)
|
fmt.Fprintf(os.Stderr, "host name is %q, ip is %q\n", thisHost.Name, thisHost.Nebula.IP)
|
||||||
|
|
||||||
pmuxProcConfigs := []pmuxlib.ProcessConfig{
|
pmuxProcConfigs := []pmuxlib.ProcessConfig{
|
||||||
nebulaEntrypointPmuxProcConfig(),
|
{
|
||||||
|
Name: "nebula",
|
||||||
|
Cmd: "cryptic-net-main",
|
||||||
|
Args: []string{
|
||||||
|
"nebula-entrypoint",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "dnsmasq",
|
Name: "dnsmasq",
|
||||||
Cmd: "bash",
|
Cmd: "bash",
|
||||||
Args: waitForNebulaArgs(env, "dnsmasq-entrypoint"),
|
Args: []string{
|
||||||
|
"wait-for-ip",
|
||||||
|
thisHost.Nebula.IP,
|
||||||
|
"dnsmasq-entrypoint",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(thisDaemon.Storage.Allocations) > 0 {
|
{
|
||||||
|
var args []string
|
||||||
|
|
||||||
garageChildrenPmuxProcConfigs, err := garageChildrenPmuxProcConfigs(env)
|
if allocs := thisDaemon.Storage.Allocations; len(allocs) > 0 {
|
||||||
if err != nil {
|
for _, alloc := range allocs {
|
||||||
return fmt.Errorf("generating garage children configs: %w", err)
|
args = append(
|
||||||
|
args,
|
||||||
|
"wait-for",
|
||||||
|
net.JoinHostPort(thisHost.Nebula.IP, strconv.Itoa(alloc.RPCPort)),
|
||||||
|
"--",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
args = []string{
|
||||||
|
"wait-for-ip",
|
||||||
|
thisHost.Nebula.IP,
|
||||||
}
|
}
|
||||||
|
|
||||||
pmuxProcConfigs = append(pmuxProcConfigs, garageChildrenPmuxProcConfigs...)
|
|
||||||
pmuxProcConfigs = append(pmuxProcConfigs, garageApplyLayoutDiffPmuxProcConfig(env))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pmuxProcConfigs = append(pmuxProcConfigs, pmuxlib.ProcessConfig{
|
pmuxProcConfigs = append(pmuxProcConfigs, pmuxlib.ProcessConfig{
|
||||||
Name: "update-global-bucket",
|
Name: "update-global-bucket",
|
||||||
Cmd: "bash",
|
Cmd: "bash",
|
||||||
Args: waitForGarageArgs(env, "update-global-bucket"),
|
Args: append(args, "update-global-bucket"),
|
||||||
NoRestartOn: []int{0},
|
NoRestartOn: []int{0},
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(thisDaemon.Storage.Allocations) > 0 {
|
||||||
|
pmuxProcConfigs = append(pmuxProcConfigs, pmuxlib.ProcessConfig{
|
||||||
|
Name: "garage",
|
||||||
|
Cmd: "bash",
|
||||||
|
Args: []string{
|
||||||
|
"wait-for-ip",
|
||||||
|
thisHost.Nebula.IP,
|
||||||
|
"cryptic-net-main", "garage-entrypoint",
|
||||||
|
},
|
||||||
|
|
||||||
|
// garage can take a while to clean up
|
||||||
|
SigKillWait: (1 * time.Minute) + (10 * time.Second),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pmuxConfig := pmuxlib.Config{Processes: pmuxProcConfigs}
|
pmuxConfig := pmuxlib.Config{Processes: pmuxProcConfigs}
|
||||||
|
|
||||||
@ -238,13 +304,44 @@ var subCmdDaemon = subCmd{
|
|||||||
return fmt.Errorf("merging and writing daemon.yml file: %w", err)
|
return fmt.Errorf("merging and writing daemon.yml file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
// we update this Host's data using whatever configuration has been
|
// we update this Host's data using whatever configuration has been
|
||||||
// provided by daemon.yml. This way the daemon has the most
|
// provided by daemon.yml. This way the daemon has the most
|
||||||
// up-to-date possible bootstrap. This updated bootstrap will later
|
// up-to-date possible bootstrap. This updated bootstrap will later
|
||||||
// get updated in garage using update-global-bucket, so other hosts
|
// get updated in garage using update-global-bucket, so other hosts
|
||||||
// will see it as well.
|
// will see it as well.
|
||||||
if err := mergeDaemonIntoBootstrap(env); err != nil {
|
|
||||||
return fmt.Errorf("merging daemon.yml into bootstrap data: %w", err)
|
// ThisDaemon can only be called after writeDaemonYml.
|
||||||
|
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,
|
||||||
|
WebPort: alloc.WebPort,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, val := range env.ToMap() {
|
for key, val := range env.ToMap() {
|
||||||
|
@ -1,205 +0,0 @@
|
|||||||
package entrypoint
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
crypticnet "cryptic-net"
|
|
||||||
"cryptic-net/bootstrap"
|
|
||||||
"cryptic-net/garage"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"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,
|
|
||||||
WebPort: alloc.WebPort,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 waitForGarageArgs(env *crypticnet.Env, args ...string) []string {
|
|
||||||
|
|
||||||
thisHost := env.Bootstrap.ThisHost()
|
|
||||||
allocs := env.ThisDaemon().Storage.Allocations
|
|
||||||
|
|
||||||
if len(allocs) == 0 {
|
|
||||||
return waitForNebulaArgs(env, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
var preArgs []string
|
|
||||||
|
|
||||||
for _, alloc := range allocs {
|
|
||||||
preArgs = append(
|
|
||||||
preArgs,
|
|
||||||
"wait-for",
|
|
||||||
net.JoinHostPort(thisHost.Nebula.IP, strconv.Itoa(alloc.RPCPort)),
|
|
||||||
"--",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return append(preArgs, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func nebulaEntrypointPmuxProcConfig() pmuxlib.ProcessConfig {
|
|
||||||
return pmuxlib.ProcessConfig{
|
|
||||||
Name: "nebula",
|
|
||||||
Cmd: "cryptic-net-main",
|
|
||||||
Args: []string{
|
|
||||||
"nebula-entrypoint",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func garageWriteChildConf(
|
|
||||||
env *crypticnet.Env,
|
|
||||||
alloc crypticnet.DaemonYmlStorageAllocation,
|
|
||||||
) (
|
|
||||||
string, error,
|
|
||||||
) {
|
|
||||||
|
|
||||||
if err := os.MkdirAll(alloc.MetaPath, 0750); err != nil {
|
|
||||||
return "", fmt.Errorf("making directory %q: %w", alloc.MetaPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
thisHost := env.Bootstrap.ThisHost()
|
|
||||||
|
|
||||||
peer := garage.Peer{
|
|
||||||
IP: thisHost.Nebula.IP,
|
|
||||||
RPCPort: alloc.RPCPort,
|
|
||||||
S3APIPort: alloc.S3APIPort,
|
|
||||||
}
|
|
||||||
|
|
||||||
pubKey, privKey := peer.RPCPeerKey()
|
|
||||||
|
|
||||||
nodeKeyPath := filepath.Join(alloc.MetaPath, "node_key")
|
|
||||||
nodeKeyPubPath := filepath.Join(alloc.MetaPath, "node_keypub")
|
|
||||||
|
|
||||||
if err := os.WriteFile(nodeKeyPath, privKey, 0400); err != nil {
|
|
||||||
return "", fmt.Errorf("writing private key to %q: %w", nodeKeyPath, err)
|
|
||||||
|
|
||||||
} else if err := os.WriteFile(nodeKeyPubPath, pubKey, 0440); err != nil {
|
|
||||||
return "", fmt.Errorf("writing public key to %q: %w", nodeKeyPubPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
garageTomlPath := filepath.Join(
|
|
||||||
env.RuntimeDirPath, fmt.Sprintf("garage-%d.toml", alloc.RPCPort),
|
|
||||||
)
|
|
||||||
|
|
||||||
err := garage.WriteGarageTomlFile(garageTomlPath, garage.GarageTomlData{
|
|
||||||
MetaPath: alloc.MetaPath,
|
|
||||||
DataPath: alloc.DataPath,
|
|
||||||
|
|
||||||
RPCSecret: env.Bootstrap.GarageRPCSecret,
|
|
||||||
|
|
||||||
RPCAddr: net.JoinHostPort(thisHost.Nebula.IP, strconv.Itoa(alloc.RPCPort)),
|
|
||||||
APIAddr: net.JoinHostPort(thisHost.Nebula.IP, strconv.Itoa(alloc.S3APIPort)),
|
|
||||||
WebAddr: net.JoinHostPort(thisHost.Nebula.IP, strconv.Itoa(alloc.WebPort)),
|
|
||||||
|
|
||||||
BootstrapPeers: env.Bootstrap.GarageRPCPeerAddrs(),
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("creating garage.toml file at %q: %w", garageTomlPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return garageTomlPath, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func garageChildrenPmuxProcConfigs(env *crypticnet.Env) ([]pmuxlib.ProcessConfig, error) {
|
|
||||||
|
|
||||||
var pmuxProcConfigs []pmuxlib.ProcessConfig
|
|
||||||
|
|
||||||
for _, alloc := range env.ThisDaemon().Storage.Allocations {
|
|
||||||
|
|
||||||
childConfPath, err := garageWriteChildConf(env, alloc)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("writing child config file for alloc %+v: %w", alloc, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pmuxProcConfigs = append(pmuxProcConfigs, pmuxlib.ProcessConfig{
|
|
||||||
Name: fmt.Sprintf("garage-%d", alloc.RPCPort),
|
|
||||||
Cmd: "garage",
|
|
||||||
Args: []string{"-c", childConfPath, "server"},
|
|
||||||
SigKillWait: 1 * time.Minute,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return pmuxProcConfigs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func garageApplyLayoutDiffPmuxProcConfig(env *crypticnet.Env) pmuxlib.ProcessConfig {
|
|
||||||
return pmuxlib.ProcessConfig{
|
|
||||||
Name: "garage-apply-layout-diff",
|
|
||||||
Cmd: "bash",
|
|
||||||
Args: waitForGarageArgs(env, "bash", "garage-apply-layout-diff"),
|
|
||||||
NoRestartOn: []int{0},
|
|
||||||
}
|
|
||||||
}
|
|
129
go-workspace/src/cmd/garage-entrypoint/main.go
Normal file
129
go-workspace/src/cmd/garage-entrypoint/main.go
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
package garage_entrypoint
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
crypticnet "cryptic-net"
|
||||||
|
"cryptic-net/garage"
|
||||||
|
|
||||||
|
"github.com/cryptic-io/pmux/pmuxlib"
|
||||||
|
)
|
||||||
|
|
||||||
|
func writeChildConf(
|
||||||
|
env *crypticnet.Env,
|
||||||
|
alloc crypticnet.DaemonYmlStorageAllocation,
|
||||||
|
) (string, error) {
|
||||||
|
|
||||||
|
if err := os.MkdirAll(alloc.MetaPath, 0750); err != nil {
|
||||||
|
return "", fmt.Errorf("making directory %q: %w", alloc.MetaPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
thisHost := env.Bootstrap.ThisHost()
|
||||||
|
|
||||||
|
peer := garage.Peer{
|
||||||
|
IP: thisHost.Nebula.IP,
|
||||||
|
RPCPort: alloc.RPCPort,
|
||||||
|
S3APIPort: alloc.S3APIPort,
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKey, privKey := peer.RPCPeerKey()
|
||||||
|
|
||||||
|
nodeKeyPath := filepath.Join(alloc.MetaPath, "node_key")
|
||||||
|
nodeKeyPubPath := filepath.Join(alloc.MetaPath, "node_keypub")
|
||||||
|
|
||||||
|
if err := os.WriteFile(nodeKeyPath, privKey, 0400); err != nil {
|
||||||
|
return "", fmt.Errorf("writing private key to %q: %w", nodeKeyPath, err)
|
||||||
|
|
||||||
|
} else if err := os.WriteFile(nodeKeyPubPath, pubKey, 0440); err != nil {
|
||||||
|
return "", fmt.Errorf("writing public key to %q: %w", nodeKeyPubPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
garageTomlPath := filepath.Join(
|
||||||
|
env.RuntimeDirPath, fmt.Sprintf("garage-%d.toml", alloc.RPCPort),
|
||||||
|
)
|
||||||
|
|
||||||
|
err := garage.WriteGarageTomlFile(garageTomlPath, garage.GarageTomlData{
|
||||||
|
MetaPath: alloc.MetaPath,
|
||||||
|
DataPath: alloc.DataPath,
|
||||||
|
|
||||||
|
RPCSecret: env.Bootstrap.GarageRPCSecret,
|
||||||
|
|
||||||
|
RPCAddr: net.JoinHostPort(thisHost.Nebula.IP, strconv.Itoa(alloc.RPCPort)),
|
||||||
|
APIAddr: net.JoinHostPort(thisHost.Nebula.IP, strconv.Itoa(alloc.S3APIPort)),
|
||||||
|
WebAddr: net.JoinHostPort(thisHost.Nebula.IP, strconv.Itoa(alloc.WebPort)),
|
||||||
|
|
||||||
|
BootstrapPeers: env.Bootstrap.GarageRPCPeerAddrs(),
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("creating garage.toml file at %q: %w", garageTomlPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return garageTomlPath, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func waitForArgs(env *crypticnet.Env, bin string, binArgs ...string) []string {
|
||||||
|
|
||||||
|
thisHost := env.Bootstrap.ThisHost()
|
||||||
|
|
||||||
|
var args []string
|
||||||
|
|
||||||
|
for _, alloc := range env.ThisDaemon().Storage.Allocations {
|
||||||
|
args = append(
|
||||||
|
args,
|
||||||
|
"wait-for",
|
||||||
|
net.JoinHostPort(thisHost.Nebula.IP, strconv.Itoa(alloc.RPCPort)),
|
||||||
|
"--",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
args = append(args, bin)
|
||||||
|
args = append(args, binArgs...)
|
||||||
|
|
||||||
|
return args
|
||||||
|
}
|
||||||
|
|
||||||
|
func Main() {
|
||||||
|
|
||||||
|
env, err := crypticnet.ReadEnv()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("reading envvars: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var pmuxProcConfigs []pmuxlib.ProcessConfig
|
||||||
|
|
||||||
|
for _, alloc := range env.ThisDaemon().Storage.Allocations {
|
||||||
|
|
||||||
|
childConfPath, err := writeChildConf(env, alloc)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("writing child config file for alloc %+v: %v", alloc, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("wrote config file %q", childConfPath)
|
||||||
|
|
||||||
|
pmuxProcConfigs = append(pmuxProcConfigs, pmuxlib.ProcessConfig{
|
||||||
|
Name: fmt.Sprintf("garage-%d", alloc.RPCPort),
|
||||||
|
Cmd: "garage",
|
||||||
|
Args: []string{"-c", childConfPath, "server"},
|
||||||
|
SigKillWait: 1 * time.Minute,
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
pmuxProcConfigs = append(pmuxProcConfigs, pmuxlib.ProcessConfig{
|
||||||
|
Name: "garage-apply-layout-diff",
|
||||||
|
Cmd: "bash",
|
||||||
|
Args: waitForArgs(env, "bash", "garage-apply-layout-diff"),
|
||||||
|
NoRestartOn: []int{0},
|
||||||
|
})
|
||||||
|
|
||||||
|
pmuxlib.Run(env.Context, pmuxlib.Config{Processes: pmuxProcConfigs})
|
||||||
|
}
|
@ -32,7 +32,7 @@ type CACert struct {
|
|||||||
// NewHostCert generates a new key/cert for a nebula host using the CA key
|
// NewHostCert generates a new key/cert for a nebula host using the CA key
|
||||||
// which will be found in the adminFS.
|
// which will be found in the adminFS.
|
||||||
func NewHostCert(
|
func NewHostCert(
|
||||||
caCert CACert, hostName string, ip net.IP,
|
caCert CACert, hostName, hostIP string,
|
||||||
) (
|
) (
|
||||||
HostCert, error,
|
HostCert, error,
|
||||||
) {
|
) {
|
||||||
@ -57,9 +57,14 @@ func NewHostCert(
|
|||||||
|
|
||||||
expireAt := caCrt.Details.NotAfter.Add(-1 * time.Second)
|
expireAt := caCrt.Details.NotAfter.Add(-1 * time.Second)
|
||||||
|
|
||||||
subnet := caCrt.Details.Subnets[0]
|
ip := net.ParseIP(hostIP)
|
||||||
if !subnet.Contains(ip) {
|
if ip == nil {
|
||||||
return HostCert{}, fmt.Errorf("invalid ip %q, not contained by network subnet %q", ip, subnet)
|
return HostCert{}, fmt.Errorf("invalid host ip %q", hostIP)
|
||||||
|
}
|
||||||
|
|
||||||
|
ipNet := &net.IPNet{
|
||||||
|
IP: ip,
|
||||||
|
Mask: ipCIDRMask,
|
||||||
}
|
}
|
||||||
|
|
||||||
var hostPub, hostKey []byte
|
var hostPub, hostKey []byte
|
||||||
@ -75,10 +80,7 @@ func NewHostCert(
|
|||||||
hostCrt := cert.NebulaCertificate{
|
hostCrt := cert.NebulaCertificate{
|
||||||
Details: cert.NebulaCertificateDetails{
|
Details: cert.NebulaCertificateDetails{
|
||||||
Name: hostName,
|
Name: hostName,
|
||||||
Ips: []*net.IPNet{{
|
Ips: []*net.IPNet{ipNet},
|
||||||
IP: ip,
|
|
||||||
Mask: subnet.Mask,
|
|
||||||
}},
|
|
||||||
NotBefore: time.Now(),
|
NotBefore: time.Now(),
|
||||||
NotAfter: expireAt,
|
NotAfter: expireAt,
|
||||||
PublicKey: hostPub,
|
PublicKey: hostPub,
|
||||||
@ -111,7 +113,7 @@ func NewHostCert(
|
|||||||
|
|
||||||
// NewCACert generates a CACert. The domain should be the network's root domain,
|
// NewCACert generates a CACert. The domain should be the network's root domain,
|
||||||
// and is included in the signing certificate's Name field.
|
// and is included in the signing certificate's Name field.
|
||||||
func NewCACert(domain string, subnet *net.IPNet) (CACert, error) {
|
func NewCACert(domain string, ipCIDRS []*net.IPNet) (CACert, error) {
|
||||||
|
|
||||||
pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
|
pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -124,7 +126,7 @@ func NewCACert(domain string, subnet *net.IPNet) (CACert, error) {
|
|||||||
caCrt := cert.NebulaCertificate{
|
caCrt := cert.NebulaCertificate{
|
||||||
Details: cert.NebulaCertificateDetails{
|
Details: cert.NebulaCertificateDetails{
|
||||||
Name: fmt.Sprintf("%s cryptic-net root cert", domain),
|
Name: fmt.Sprintf("%s cryptic-net root cert", domain),
|
||||||
Subnets: []*net.IPNet{subnet},
|
Ips: ipCIDRS,
|
||||||
NotBefore: now,
|
NotBefore: now,
|
||||||
NotAfter: expireAt,
|
NotAfter: expireAt,
|
||||||
PublicKey: pubKey,
|
PublicKey: pubKey,
|
||||||
|
Loading…
Reference in New Issue
Block a user