implement uncloudd daemon that starts a WireGuard network without peer probing

This commit is contained in:
Pavel Sviderski
2024-08-26 17:09:54 +10:00
parent 222edb7647
commit e074b6855f
13 changed files with 347 additions and 145 deletions
-45
View File
@@ -1,45 +0,0 @@
package daemon
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
const (
DefaultDataDir = "/var/lib/uncloud"
MachineConfigPath = "machine.json"
)
type Config struct {
UncloudID string
UncloudSecret string
// IPv4 network in CIDR format to use for the machine network.
Network string
}
func ReadConfig(path string) (Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return Config{}, fmt.Errorf("read config file %q: %w", path, err)
}
var config Config
if err = json.Unmarshal(data, &config); err != nil {
return Config{}, fmt.Errorf("parse config file %q: %w", path, err)
}
return config, nil
}
func (c *Config) Write(path string) error {
dir, _ := filepath.Split(path)
if err := os.MkdirAll(dir, 0700); err != nil {
return fmt.Errorf("create config directory %q: %w", dir, err)
}
data, err := json.Marshal(c)
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
return os.WriteFile(path, data, 0600)
}
+34
View File
@@ -0,0 +1,34 @@
package daemon
import (
"context"
"fmt"
"uncloud/internal/machine"
"uncloud/internal/machine/network"
)
func Run(ctx context.Context, dataDir string) error {
cfg, err := machine.ParseConfig(machine.ConfigPath(dataDir))
if err != nil {
return fmt.Errorf("load machine config: %w", err)
}
wgnet, err := network.NewWireGuardNetwork()
if err != nil {
return fmt.Errorf("create WireGuard network: %w", err)
}
if err = wgnet.Configure(*cfg.Network); err != nil {
return fmt.Errorf("configure WireGuard network: %w", err)
}
//ctx, cancel := context.WithCancel(context.Background())
//go wgnet.WatchEndpoints(ctx, peerEndpointChangeNotifier)
//addrs, err := network.ListRoutableAddresses()
//if err != nil {
// return err
//}
//fmt.Println("Addresses:", addrs)
return wgnet.Run(ctx)
}
-17
View File
@@ -1,17 +0,0 @@
package daemon
import (
"errors"
"fmt"
"path/filepath"
)
func Run(dataDir string) error {
cfg, err := ReadConfig(filepath.Join(dataDir, MachineConfigPath))
if err != nil {
return err
}
fmt.Println("Config", cfg)
return errors.New("Running daemon...")
}