Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9c4f6d987d | |||
| fb82a7f358 | |||
| 6ee848e6ca | |||
| 864eb02f9d | |||
| 9da2f8711f | |||
| 2a14205e7e | |||
| a5d9466fb8 | |||
| 3d638e9a85 | |||
| 60d3a2c290 | |||
| af90825f13 | |||
| b4f1d2e4d9 | |||
| acf9e0e81d | |||
| 3b43e0c9ea | |||
| 9762e78f5e | |||
| 3534e7b755 | |||
| b80dc84a26 | |||
| c0ef6ae9c4 | |||
| 58867031ea |
+10
-1
@@ -16,10 +16,19 @@ var envCmd = &cobra.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.WriteEnv(os.Stdout)
|
_, err = m.Env(*envExport).WriteTo(os.Stdout)
|
||||||
|
return err
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Command Flags
|
||||||
|
var (
|
||||||
|
envExport *bool
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(envCmd)
|
rootCmd.AddCommand(envCmd)
|
||||||
|
|
||||||
|
envExport = envCmd.PersistentFlags().BoolP("export", "e", false,
|
||||||
|
"export generated variables")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,12 +23,16 @@ var configTemplate = template.Must(template.New("config").Funcs(template.FuncMap
|
|||||||
return strings.Join(s, sep)
|
return strings.Join(s, sep)
|
||||||
},
|
},
|
||||||
}).Parse(`[Interface]
|
}).Parse(`[Interface]
|
||||||
|
{{if .Interface.Name}}# Name: {{.Interface.Name}}
|
||||||
|
{{end -}}
|
||||||
Address = {{.Interface.Address}}
|
Address = {{.Interface.Address}}
|
||||||
PrivateKey = {{.Interface.PrivateKey}}
|
PrivateKey = {{.Interface.PrivateKey}}
|
||||||
ListenPort = {{.Interface.ListenPort}}
|
ListenPort = {{.Interface.ListenPort}}
|
||||||
{{- range .Peer }}
|
{{- range .Peer }}
|
||||||
|
|
||||||
[Peer]
|
[Peer]
|
||||||
|
{{if .Name}}# Name: {{.Name}}
|
||||||
|
{{end -}}
|
||||||
PublicKey = {{.PublicKey}}
|
PublicKey = {{.PublicKey}}
|
||||||
Endpoint = {{.Endpoint}}
|
Endpoint = {{.Endpoint}}
|
||||||
AllowedIPs = {{ PrefixJoin .AllowedIPs ", "}}
|
AllowedIPs = {{ PrefixJoin .AllowedIPs ", "}}
|
||||||
@@ -64,6 +68,7 @@ func (f *Config) WriteTo(w io.Writer) (int64, error) {
|
|||||||
|
|
||||||
// InterfaceConfig represents the [Interface] section
|
// InterfaceConfig represents the [Interface] section
|
||||||
type InterfaceConfig struct {
|
type InterfaceConfig struct {
|
||||||
|
Name string
|
||||||
Address netip.Addr
|
Address netip.Addr
|
||||||
PrivateKey PrivateKey
|
PrivateKey PrivateKey
|
||||||
ListenPort uint16
|
ListenPort uint16
|
||||||
@@ -71,6 +76,7 @@ type InterfaceConfig struct {
|
|||||||
|
|
||||||
// PeerConfig represents a [Peer] section
|
// PeerConfig represents a [Peer] section
|
||||||
type PeerConfig struct {
|
type PeerConfig struct {
|
||||||
|
Name string
|
||||||
PublicKey PublicKey
|
PublicKey PublicKey
|
||||||
Endpoint EndpointAddress
|
Endpoint EndpointAddress
|
||||||
AllowedIPs []netip.Prefix
|
AllowedIPs []netip.Prefix
|
||||||
|
|||||||
@@ -51,12 +51,12 @@ func (pub PublicKey) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON encodes the key for JSON, omiting empty.
|
// MarshalJSON encodes the key for JSON, omitting empty.
|
||||||
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
||||||
return encodeKeyJSON(key.String())
|
return encodeKeyJSON(key.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON encodes the key for JSON, omiting empty.
|
// MarshalJSON encodes the key for JSON, omitting empty.
|
||||||
func (pub PublicKey) MarshalJSON() ([]byte, error) {
|
func (pub PublicKey) MarshalJSON() ([]byte, error) {
|
||||||
return encodeKeyJSON(pub.String())
|
return encodeKeyJSON(pub.String())
|
||||||
}
|
}
|
||||||
@@ -70,12 +70,12 @@ func encodeKeyJSON(s string) ([]byte, error) {
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalYAML encodes the key for YAML, omiting empty.
|
// MarshalYAML encodes the key for YAML, omitting empty.
|
||||||
func (key PrivateKey) MarshalYAML() (any, error) {
|
func (key PrivateKey) MarshalYAML() (any, error) {
|
||||||
return encodeKeyYAML(key.String())
|
return encodeKeyYAML(key.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalYAML encodes the key for YAML, omiting empty.
|
// MarshalYAML encodes the key for YAML, omitting empty.
|
||||||
func (pub PublicKey) MarshalYAML() (any, error) {
|
func (pub PublicKey) MarshalYAML() (any, error) {
|
||||||
return encodeKeyYAML(pub.String())
|
return encodeKeyYAML(pub.String())
|
||||||
}
|
}
|
||||||
|
|||||||
+43
-27
@@ -7,8 +7,23 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WriteEnv generates environment variables for shell scripts
|
// Env is a shell environment factory for this cluster
|
||||||
func (m *Zones) WriteEnv(w io.Writer) error {
|
type Env struct {
|
||||||
|
ZoneIterator
|
||||||
|
|
||||||
|
export bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Env returns a shell environment factory
|
||||||
|
func (m *Zones) Env(export bool) *Env {
|
||||||
|
return &Env{
|
||||||
|
ZoneIterator: m,
|
||||||
|
export: export,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo generates environment variables for shell scripts
|
||||||
|
func (m *Env) WriteTo(w io.Writer) (int64, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
|
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
|
||||||
@@ -17,11 +32,10 @@ func (m *Zones) WriteEnv(w io.Writer) error {
|
|||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
_, err := buf.WriteTo(w)
|
return buf.WriteTo(w)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
|
func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
|
||||||
zoneID := z.ID
|
zoneID := z.ID
|
||||||
|
|
||||||
// ZONE{zoneID}
|
// ZONE{zoneID}
|
||||||
@@ -32,14 +46,17 @@ func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
|
|||||||
|
|
||||||
// ZONE{zoneID}_GW
|
// ZONE{zoneID}_GW
|
||||||
gatewayID := getRingZeroGatewayID(z)
|
gatewayID := getRingZeroGatewayID(z)
|
||||||
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW")
|
if gatewayID > 0 {
|
||||||
|
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW")
|
||||||
|
|
||||||
// ZONE{zoneID}_IP
|
// ZONE{zoneID}_IP
|
||||||
ip, _ := RingZeroAddress(zoneID, gatewayID)
|
if ip, ok := RingZeroAddress(zoneID, gatewayID); ok {
|
||||||
m.writeEnvVar(w, ip.String(), "ZONE%v_%s", zoneID, "IP")
|
m.writeEnvVar(w, ip.String(), "ZONE%v_%s", zoneID, "IP")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Zones) writeEnvVarFn(w io.Writer, fn func(*Zones) string, name string, args ...any) {
|
func (m *Env) writeEnvVarFn(w io.Writer, fn func(*Env) string, name string, args ...any) {
|
||||||
var value string
|
var value string
|
||||||
|
|
||||||
if fn != nil {
|
if fn != nil {
|
||||||
@@ -49,7 +66,13 @@ func (m *Zones) writeEnvVarFn(w io.Writer, fn func(*Zones) string, name string,
|
|||||||
m.writeEnvVar(w, value, name, args...)
|
m.writeEnvVar(w, value, name, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*Zones) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
||||||
|
var prefix string
|
||||||
|
|
||||||
|
if m.export {
|
||||||
|
prefix = "export "
|
||||||
|
}
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
name = fmt.Sprintf(name, args...)
|
name = fmt.Sprintf(name, args...)
|
||||||
}
|
}
|
||||||
@@ -57,15 +80,17 @@ func (*Zones) writeEnvVar(w io.Writer, value string, name string, args ...any) {
|
|||||||
if name != "" {
|
if name != "" {
|
||||||
value = strings.TrimSpace(value)
|
value = strings.TrimSpace(value)
|
||||||
|
|
||||||
_, _ = fmt.Fprintf(w, "%s=%q\n", name, value)
|
_, _ = fmt.Fprintf(w, "%s%s=%q\n", prefix, name, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func genEnvZones(m *Zones) string {
|
func genEnvZones(m *Env) string {
|
||||||
s := make([]string, 0, len(m.Zones))
|
var s []string
|
||||||
for _, z := range m.Zones {
|
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
s = append(s, fmt.Sprintf("%v", z.ID))
|
s = append(s, fmt.Sprintf("%v", z.ID))
|
||||||
}
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
return strings.Join(s, " ")
|
return strings.Join(s, " ")
|
||||||
}
|
}
|
||||||
@@ -79,13 +104,9 @@ func genEnvZoneNodes(z *Zone) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getRingZeroGatewayID(z *Zone) int {
|
func getRingZeroGatewayID(z *Zone) int {
|
||||||
var firstNodeID, gatewayID int
|
var gatewayID int
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
if firstNodeID == 0 {
|
|
||||||
firstNodeID = p.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.IsGateway() {
|
if p.IsGateway() {
|
||||||
gatewayID = p.ID
|
gatewayID = p.ID
|
||||||
}
|
}
|
||||||
@@ -93,10 +114,5 @@ func getRingZeroGatewayID(z *Zone) int {
|
|||||||
return gatewayID != 0
|
return gatewayID != 0
|
||||||
})
|
})
|
||||||
|
|
||||||
switch {
|
return gatewayID
|
||||||
case gatewayID == 0:
|
|
||||||
return firstNodeID
|
|
||||||
default:
|
|
||||||
return gatewayID
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,24 @@ func (m *Machine) IsGateway() bool {
|
|||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetGateway enables/disables a Machine ring0 integration
|
||||||
|
func (m *Machine) SetGateway(enabled bool) error {
|
||||||
|
ri, found := m.getRingInfo(0)
|
||||||
|
switch {
|
||||||
|
case !found && !enabled:
|
||||||
|
return nil
|
||||||
|
case !found:
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if ri, err = m.createRingInfo(0, false); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ri.Enabled = enabled
|
||||||
|
return m.SyncWireguardConfig(0)
|
||||||
|
}
|
||||||
|
|
||||||
// Zone indicates the [Zone] this machine belongs to
|
// Zone indicates the [Zone] this machine belongs to
|
||||||
func (m *Machine) Zone() int {
|
func (m *Machine) Zone() int {
|
||||||
return m.zone.ID
|
return m.zone.ID
|
||||||
|
|||||||
+20
-33
@@ -3,7 +3,6 @@ package zones
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
@@ -73,38 +72,6 @@ func (m *Machine) tryReadWireguardKeys(ring int) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteWireguardKeys writes the wgN.key/wgN.pub files
|
|
||||||
func (m *Machine) WriteWireguardKeys(ring int) error {
|
|
||||||
var err error
|
|
||||||
var key, pub string
|
|
||||||
var ri *RingInfo
|
|
||||||
|
|
||||||
ri, _ = m.getRingInfo(ring)
|
|
||||||
if ri != nil {
|
|
||||||
key = ri.Keys.PrivateKey.String()
|
|
||||||
pub = ri.Keys.PublicKey.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case key == "":
|
|
||||||
return fs.ErrNotExist
|
|
||||||
case pub == "":
|
|
||||||
pub = ri.Keys.PrivateKey.Public().String()
|
|
||||||
}
|
|
||||||
|
|
||||||
err = m.WriteStringFile(key+"\n", "wg%v.key", ring)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = m.WriteStringFile(pub+"\n", "wg%v.pub", ring)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
|
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
|
||||||
// the machine's config directory
|
// the machine's config directory
|
||||||
func (m *Machine) RemoveWireguardKeys(ring int) error {
|
func (m *Machine) RemoveWireguardKeys(ring int) error {
|
||||||
@@ -261,3 +228,23 @@ func (m *Machine) RemoveWireguardConfig(ring int) error {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Machine) createRingInfo(ring int, enabled bool) (*RingInfo, error) {
|
||||||
|
keys, err := wireguard.NewKeyPair()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ri := &RingInfo{
|
||||||
|
Ring: ring,
|
||||||
|
Enabled: enabled,
|
||||||
|
Keys: keys,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.applyRingInfo(ring, ri)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ri, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -210,6 +210,7 @@ func (r *Ring) AddPeer(p *Machine) bool {
|
|||||||
Address: addr,
|
Address: addr,
|
||||||
PrivateKey: ri.Keys.PrivateKey,
|
PrivateKey: ri.Keys.PrivateKey,
|
||||||
PeerConfig: wireguard.PeerConfig{
|
PeerConfig: wireguard.PeerConfig{
|
||||||
|
Name: fmt.Sprintf("%s-%v", p.Name, r.ID),
|
||||||
PublicKey: ri.Keys.PublicKey,
|
PublicKey: ri.Keys.PublicKey,
|
||||||
Endpoint: wireguard.EndpointAddress{
|
Endpoint: wireguard.EndpointAddress{
|
||||||
Host: p.FullName(),
|
Host: p.FullName(),
|
||||||
@@ -300,6 +301,7 @@ func (r *Ring) ExportConfig(p *Machine) (*wireguard.Config, error) {
|
|||||||
case pp.Node == p:
|
case pp.Node == p:
|
||||||
// current
|
// current
|
||||||
found = true
|
found = true
|
||||||
|
out.Interface.Name = pp.PeerConfig.Name
|
||||||
out.Interface.Address = pp.Address
|
out.Interface.Address = pp.Address
|
||||||
out.Interface.PrivateKey = pp.PrivateKey
|
out.Interface.PrivateKey = pp.PrivateKey
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ func (m *Zones) scan() error {
|
|||||||
m.scanMachines,
|
m.scanMachines,
|
||||||
m.scanZoneIDs,
|
m.scanZoneIDs,
|
||||||
m.scanSort,
|
m.scanSort,
|
||||||
|
m.scanGateways,
|
||||||
} {
|
} {
|
||||||
if err := fn(); err != nil {
|
if err := fn(); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -115,6 +116,16 @@ func (m *Zones) scanSort() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Zones) scanGateways() error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
_, _, err = z.GetGateway()
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (z *Zone) scan() error {
|
func (z *Zone) scan() error {
|
||||||
// each directory is a machine
|
// each directory is a machine
|
||||||
entries, err := fs.ReadDir(z.zones.dir, z.Name)
|
entries, err := fs.ReadDir(z.zones.dir, z.Name)
|
||||||
@@ -139,3 +150,41 @@ func (z *Zone) scan() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetGateway returns the first gateway found, if none
|
||||||
|
// files will be created to enable the first [Machine] to
|
||||||
|
// be one
|
||||||
|
func (z *Zone) GetGateway() (*Machine, bool, error) {
|
||||||
|
var first *Machine
|
||||||
|
var gateway *Machine
|
||||||
|
|
||||||
|
z.zones.ForEachMachine(func(p *Machine) bool {
|
||||||
|
switch {
|
||||||
|
case p.IsGateway():
|
||||||
|
// found
|
||||||
|
gateway = p
|
||||||
|
case first == nil:
|
||||||
|
// remember
|
||||||
|
first = p
|
||||||
|
default:
|
||||||
|
// keep looking
|
||||||
|
}
|
||||||
|
|
||||||
|
return gateway != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case gateway != nil:
|
||||||
|
// found one
|
||||||
|
return gateway, false, nil
|
||||||
|
case first != nil:
|
||||||
|
// make one
|
||||||
|
if err := first.SetGateway(true); err != nil {
|
||||||
|
return first, false, err
|
||||||
|
}
|
||||||
|
return first, true, nil
|
||||||
|
default:
|
||||||
|
// Zone without nodes?
|
||||||
|
panic("unreachable")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+2
-2
@@ -18,12 +18,12 @@ func (m *Zones) SyncAllWireguard() error {
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
for ring := 0; ring < RingsCount; ring++ {
|
for ring := 0; ring < RingsCount; ring++ {
|
||||||
err = m.PruneWireguardConfig(ring)
|
err = m.WriteWireguardKeys(ring)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.WriteWireguardKeys(ring)
|
err = m.SyncWireguardConfig(ring)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,272 @@
|
|||||||
|
package zones
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ WireguardConfigPruner = (*Zones)(nil)
|
||||||
|
_ WireguardConfigPruner = (*Zone)(nil)
|
||||||
|
_ WireguardConfigPruner = (*Machine)(nil)
|
||||||
|
|
||||||
|
_ WireguardConfigWriter = (*Zones)(nil)
|
||||||
|
_ WireguardConfigWriter = (*Zone)(nil)
|
||||||
|
_ WireguardConfigWriter = (*Machine)(nil)
|
||||||
|
|
||||||
|
_ WireguardConfigSyncer = (*Zones)(nil)
|
||||||
|
_ WireguardConfigSyncer = (*Zone)(nil)
|
||||||
|
_ WireguardConfigSyncer = (*Machine)(nil)
|
||||||
|
|
||||||
|
_ WireguardKeysWriter = (*Zones)(nil)
|
||||||
|
_ WireguardKeysWriter = (*Zone)(nil)
|
||||||
|
_ WireguardKeysWriter = (*Machine)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
// A WireguardConfigPruner deletes wgN.conf on all machines under
|
||||||
|
// its scope with the specified ring disabled
|
||||||
|
type WireguardConfigPruner interface {
|
||||||
|
PruneWireguardConfig(ring int) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
||||||
|
// the corresponding ring disabled on all zones
|
||||||
|
func (m *Zones) PruneWireguardConfig(ring int) error {
|
||||||
|
return pruneWireguardConfig(m, ring)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PruneWireguardConfig removes wgN.conf files of machines with
|
||||||
|
// the corresponding ring disabled.
|
||||||
|
func (z *Zone) PruneWireguardConfig(ring int) error {
|
||||||
|
return pruneWireguardConfig(z, ring)
|
||||||
|
}
|
||||||
|
|
||||||
|
func pruneWireguardConfig(m MachineIterator, ring int) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
|
err = p.PruneWireguardConfig(ring)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
// ignore
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// PruneWireguardConfig deletes the wgN.conf file if its
|
||||||
|
// presence on the ring is disabled
|
||||||
|
func (m *Machine) PruneWireguardConfig(ring int) error {
|
||||||
|
_, ok := m.getRingInfo(ring)
|
||||||
|
if !ok {
|
||||||
|
return m.RemoveWireguardConfig(ring)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// A WireguardConfigWriter rewrites all wgN.conf on all machines under
|
||||||
|
// its scope attached to that ring
|
||||||
|
type WireguardConfigWriter interface {
|
||||||
|
WriteWireguardConfig(ring int) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
||||||
|
// attached to that ring
|
||||||
|
func (m *Zones) WriteWireguardConfig(ring int) error {
|
||||||
|
switch ring {
|
||||||
|
case 0:
|
||||||
|
return writeWireguardConfig(m, m, ring)
|
||||||
|
case 1:
|
||||||
|
var err error
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
err = writeWireguardConfig(m, z, ring)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteWireguardConfig rewrites all wgN.conf on all machines
|
||||||
|
// on the Zone attached to that ring
|
||||||
|
func (z *Zone) WriteWireguardConfig(ring int) error {
|
||||||
|
switch ring {
|
||||||
|
case 0:
|
||||||
|
return writeWireguardConfig(z.zones, z.zones, ring)
|
||||||
|
case 1:
|
||||||
|
return writeWireguardConfig(z.zones, z, ring)
|
||||||
|
default:
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error {
|
||||||
|
r, err := NewRing(z, m, ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
r.ForEachMachine(func(p *Machine) bool {
|
||||||
|
err = p.writeWireguardRingConfig(r)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteWireguardConfig rewrites the wgN.conf file of this Machine
|
||||||
|
// if enabled
|
||||||
|
func (m *Machine) WriteWireguardConfig(ring int) error {
|
||||||
|
r, err := NewRing(m.zone.zones, m.zone, ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.writeWireguardRingConfig(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) writeWireguardRingConfig(r *Ring) error {
|
||||||
|
wg, err := r.ExportConfig(m)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := m.CreateTruncFile("wg%v.conf", r.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = wg.WriteTo(f)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// A WireguardConfigSyncer updates all wgN.conf on all machines under
|
||||||
|
// its scope reflecting the state of the ring
|
||||||
|
type WireguardConfigSyncer interface {
|
||||||
|
SyncWireguardConfig(ring int) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
||||||
|
// ring
|
||||||
|
func (m *Zones) SyncWireguardConfig(ring int) error {
|
||||||
|
switch ring {
|
||||||
|
case 0:
|
||||||
|
return syncWireguardConfig(m, m, ring)
|
||||||
|
case 1:
|
||||||
|
var err error
|
||||||
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
|
err = syncWireguardConfig(m, z, ring)
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
||||||
|
// ring
|
||||||
|
func (z *Zone) SyncWireguardConfig(ring int) error {
|
||||||
|
switch ring {
|
||||||
|
case 0:
|
||||||
|
return syncWireguardConfig(z.zones, z.zones, ring)
|
||||||
|
case 1:
|
||||||
|
return syncWireguardConfig(z.zones, z, ring)
|
||||||
|
default:
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func syncWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error {
|
||||||
|
r, err := NewRing(z, m, ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
r.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if _, ok := p.getRingInfo(ring); ok {
|
||||||
|
err = p.writeWireguardRingConfig(r)
|
||||||
|
} else {
|
||||||
|
err = p.RemoveWireguardConfig(ring)
|
||||||
|
}
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SyncWireguardConfig updates all wgN.conf files for the specified
|
||||||
|
// ring
|
||||||
|
func (m *Machine) SyncWireguardConfig(ring int) error {
|
||||||
|
return m.zone.SyncWireguardConfig(ring)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A WireguardKeysWriter writes the Wireguard Keys for all machines
|
||||||
|
// under its scope for the specified ring
|
||||||
|
type WireguardKeysWriter interface {
|
||||||
|
WriteWireguardKeys(ring int) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
||||||
|
func (m *Zones) WriteWireguardKeys(ring int) error {
|
||||||
|
return writeWireguardKeys(m, ring)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
||||||
|
func (z *Zone) WriteWireguardKeys(ring int) error {
|
||||||
|
return writeWireguardKeys(z, ring)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeWireguardKeys(m MachineIterator, ring int) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
m.ForEachMachine(func(p *Machine) bool {
|
||||||
|
err = p.WriteWireguardKeys(ring)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
// ignore
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return err != nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteWireguardKeys writes the wgN.key/wgN.pub files
|
||||||
|
func (m *Machine) WriteWireguardKeys(ring int) error {
|
||||||
|
var err error
|
||||||
|
var key, pub string
|
||||||
|
var ri *RingInfo
|
||||||
|
|
||||||
|
ri, _ = m.getRingInfo(ring)
|
||||||
|
if ri != nil {
|
||||||
|
key = ri.Keys.PrivateKey.String()
|
||||||
|
pub = ri.Keys.PublicKey.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case key == "":
|
||||||
|
return fs.ErrNotExist
|
||||||
|
case pub == "":
|
||||||
|
pub = ri.Keys.PrivateKey.Public().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.WriteStringFile(key+"\n", "wg%v.key", ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.WriteStringFile(pub+"\n", "wg%v.pub", ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
package zones
|
|
||||||
|
|
||||||
import "os"
|
|
||||||
|
|
||||||
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
||||||
// the corresponding ring disabled.
|
|
||||||
func (z *Zone) PruneWireguardConfig(ring int) error {
|
|
||||||
return pruneWireguardConfig(z, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
|
||||||
func (z *Zone) WriteWireguardKeys(ring int) error {
|
|
||||||
return writeWireguardKeys(z, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
||||||
// the corresponding ring disabled on all zones
|
|
||||||
func (m *Zones) PruneWireguardConfig(ring int) error {
|
|
||||||
return pruneWireguardConfig(m, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
|
||||||
func (m *Zones) WriteWireguardKeys(ring int) error {
|
|
||||||
return writeWireguardKeys(m, ring)
|
|
||||||
}
|
|
||||||
|
|
||||||
func pruneWireguardConfig(m MachineIterator, ring int) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
|
||||||
_, ok := p.getRingInfo(ring)
|
|
||||||
if !ok {
|
|
||||||
err = p.RemoveWireguardConfig(ring)
|
|
||||||
}
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeWireguardKeys(m MachineIterator, ring int) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
m.ForEachMachine(func(p *Machine) bool {
|
|
||||||
err = p.WriteWireguardKeys(ring)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
// ignore
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
@@ -50,6 +50,31 @@ func (z *Zone) ForEachMachine(fn func(*Machine) bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetGateway configures a machine to be the zone's ring0 gateway
|
||||||
|
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
||||||
|
var err error
|
||||||
|
var found bool
|
||||||
|
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
if p.ID == gatewayID {
|
||||||
|
found = true
|
||||||
|
err = p.SetGateway(enabled)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
case !found:
|
||||||
|
return fs.ErrNotExist
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Zones represents all zones in a cluster
|
// Zones represents all zones in a cluster
|
||||||
type Zones struct {
|
type Zones struct {
|
||||||
dir fs.FS
|
dir fs.FS
|
||||||
|
|||||||
Reference in New Issue
Block a user