Compare commits
2 Commits
v0.4.5
..
6a2cbdffd0
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a2cbdffd0 | |||
| 81aafbc083 |
@@ -1,7 +1,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "git.jpi.io/amery/jpictl/pkg/zones"
|
|
||||||
|
|
||||||
// Config describes the repository
|
// Config describes the repository
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Base string
|
Base string
|
||||||
@@ -12,8 +10,3 @@ var cfg = &Config{
|
|||||||
Base: "./m",
|
Base: "./m",
|
||||||
Domain: "m.jpi.cloud",
|
Domain: "m.jpi.cloud",
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadZones loads all zones and machines in the config directory
|
|
||||||
func (cfg *Config) LoadZones() (*zones.Zones, error) {
|
|
||||||
return zones.New(cfg.Base, cfg.Domain)
|
|
||||||
}
|
|
||||||
|
|||||||
+5
-54
@@ -2,76 +2,27 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/burntSushi/toml"
|
"github.com/burntSushi/toml"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Encoder represents an object that encodes another internally
|
|
||||||
type Encoder interface {
|
|
||||||
Encode(any) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encoding represents a type of [Encoder]
|
|
||||||
type Encoding int
|
|
||||||
|
|
||||||
const (
|
|
||||||
// TOMLEncoding represents TOML encoding
|
|
||||||
TOMLEncoding Encoding = iota
|
|
||||||
// JSONEncoding represents JSON encoding
|
|
||||||
JSONEncoding
|
|
||||||
// YAMLEncoding represents YAML encoding
|
|
||||||
YAMLEncoding
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewJSONEncoder returns a JSON [Encoder] to work on the given [io.Writer]
|
|
||||||
func NewJSONEncoder(w io.Writer) Encoder {
|
|
||||||
enc := json.NewEncoder(w)
|
|
||||||
enc.SetIndent(``, ` `)
|
|
||||||
return enc
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewYAMLEncoder returns a YAML [Encoder] to work on the given [io.Writer]
|
|
||||||
func NewYAMLEncoder(w io.Writer) Encoder {
|
|
||||||
enc := yaml.NewEncoder(w)
|
|
||||||
enc.SetIndent(2)
|
|
||||||
return enc
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewTOMLEncoder returns a TOML [Encoder] to work on the given [io.Writer]
|
|
||||||
func NewTOMLEncoder(w io.Writer) Encoder {
|
|
||||||
enc := toml.NewEncoder(w)
|
|
||||||
return enc
|
|
||||||
}
|
|
||||||
|
|
||||||
const encoding = YAMLEncoding
|
|
||||||
|
|
||||||
// Command
|
// Command
|
||||||
var dumpCmd = &cobra.Command{
|
var dumpCmd = &cobra.Command{
|
||||||
Use: "dump",
|
Use: "dump",
|
||||||
Short: "generates a text representation of the config",
|
Short: "generates a toml representation of the config",
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var enc Encoder
|
|
||||||
|
|
||||||
m, err := cfg.LoadZones()
|
m, err := zones.New(cfg.Base, cfg.Domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch encoding {
|
enc := toml.NewEncoder(&buf)
|
||||||
case JSONEncoding:
|
|
||||||
enc = NewJSONEncoder(&buf)
|
|
||||||
case YAMLEncoding:
|
|
||||||
enc = NewYAMLEncoder(&buf)
|
|
||||||
default:
|
|
||||||
enc = NewTOMLEncoder(&buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = enc.Encode(m); err != nil {
|
if err = enc.Encode(m); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -4,6 +4,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Command
|
// Command
|
||||||
@@ -11,7 +13,7 @@ var envCmd = &cobra.Command{
|
|||||||
Use: "env",
|
Use: "env",
|
||||||
Short: "generates environment variables for shell scripts",
|
Short: "generates environment variables for shell scripts",
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
m, err := cfg.LoadZones()
|
m, err := zones.New(cfg.Base, cfg.Domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Command
|
|
||||||
var writeCmd = &cobra.Command{
|
|
||||||
Use: "write",
|
|
||||||
Short: "rewrites all config files",
|
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
m, err := cfg.LoadZones()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return m.SyncAll()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
rootCmd.AddCommand(writeCmd)
|
|
||||||
}
|
|
||||||
@@ -13,7 +13,6 @@ require (
|
|||||||
github.com/spf13/cobra v1.7.0
|
github.com/spf13/cobra v1.7.0
|
||||||
golang.org/x/crypto v0.12.0
|
golang.org/x/crypto v0.12.0
|
||||||
gopkg.in/gcfg.v1 v1.2.3
|
gopkg.in/gcfg.v1 v1.2.3
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@@ -90,7 +90,6 @@ golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
|
|||||||
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
||||||
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
|
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/gcfg.v1 v1.2.3 h1:m8OOJ4ccYHnx2f4gQwpno8nAX5OGOh7RLaaz0pj3Ogs=
|
gopkg.in/gcfg.v1 v1.2.3 h1:m8OOJ4ccYHnx2f4gQwpno8nAX5OGOh7RLaaz0pj3Ogs=
|
||||||
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
|
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/curve25519"
|
"golang.org/x/crypto/curve25519"
|
||||||
)
|
)
|
||||||
@@ -51,43 +50,6 @@ func (pub PublicKey) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON encodes the key for JSON, omiting empty.
|
|
||||||
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
|
||||||
return encodeKeyJSON(key.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalJSON encodes the key for JSON, omiting empty.
|
|
||||||
func (pub PublicKey) MarshalJSON() ([]byte, error) {
|
|
||||||
return encodeKeyJSON(pub.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeKeyJSON(s string) ([]byte, error) {
|
|
||||||
var out []byte
|
|
||||||
if s != "" {
|
|
||||||
out = []byte(fmt.Sprintf("%q", s))
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalYAML encodes the key for YAML, omiting empty.
|
|
||||||
func (key PrivateKey) MarshalYAML() (any, error) {
|
|
||||||
return encodeKeyYAML(key.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalYAML encodes the key for YAML, omiting empty.
|
|
||||||
func (pub PublicKey) MarshalYAML() (any, error) {
|
|
||||||
return encodeKeyYAML(pub.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeKeyYAML(s string) (any, error) {
|
|
||||||
if s == "" {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return s, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsZero tells if the key hasn't been set
|
// IsZero tells if the key hasn't been set
|
||||||
func (key PrivateKey) IsZero() bool {
|
func (key PrivateKey) IsZero() bool {
|
||||||
var zero PrivateKey
|
var zero PrivateKey
|
||||||
|
|||||||
+9
-15
@@ -32,11 +32,14 @@ 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 *Zones) writeEnvVarFn(w io.Writer, fn func(*Zones) string, name string, args ...any) {
|
||||||
@@ -79,13 +82,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 +92,5 @@ func getRingZeroGatewayID(z *Zone) int {
|
|||||||
return gatewayID != 0
|
return gatewayID != 0
|
||||||
})
|
})
|
||||||
|
|
||||||
switch {
|
return gatewayID
|
||||||
case gatewayID == 0:
|
|
||||||
return firstNodeID
|
|
||||||
default:
|
|
||||||
return gatewayID
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-8
@@ -5,20 +5,16 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// revive:disable:line-length-limit
|
|
||||||
|
|
||||||
// A Machine is a machine on a Zone
|
// A Machine is a machine on a Zone
|
||||||
type Machine struct {
|
type Machine struct {
|
||||||
zone *Zone
|
zone *Zone
|
||||||
ID int `toml:"id"`
|
ID int
|
||||||
Name string `toml:"-" json:"-" yaml:"-"`
|
Name string `toml:"name"`
|
||||||
|
|
||||||
PublicAddresses []netip.Addr `toml:"public,omitempty" json:"public,omitempty" yaml:"public,omitempty"`
|
PublicAddresses []netip.Addr `toml:"public,omitempty"`
|
||||||
Rings []*RingInfo `toml:"rings,omitempty" json:"rings,omitempty" yaml:"rings,omitempty"`
|
Rings []*RingInfo `toml:"rings,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// revive:enable:line-length-limit
|
|
||||||
|
|
||||||
func (m *Machine) String() string {
|
func (m *Machine) String() string {
|
||||||
return m.Name
|
return m.Name
|
||||||
}
|
}
|
||||||
@@ -43,6 +39,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.syncRingConfig(0)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
|
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
|
||||||
return m.zone.zones.GetMachineByName(name)
|
return m.zone.zones.GetMachineByName(name)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,12 +92,12 @@ func (m *Machine) WriteWireguardKeys(ring int) error {
|
|||||||
pub = ri.Keys.PrivateKey.Public().String()
|
pub = ri.Keys.PrivateKey.Public().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.WriteStringFile(key+"\n", "wg%v.key", ring)
|
err = m.WriteStringFile(key, "wg%v.key", ring)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.WriteStringFile(pub+"\n", "wg%v.pub", ring)
|
err = m.WriteStringFile(pub, "wg%v.pub", ring)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -261,3 +261,57 @@ func (m *Machine) RemoveWireguardConfig(ring int) error {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*Machine) syncRingConfig(_ int) error {
|
||||||
|
// _, err := m.getRingNodes(ring)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) writeRingInfo(ri *RingInfo) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if m == nil || ri == nil {
|
||||||
|
return fs.ErrInvalid
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.WriteWireguardKeys(ri.Ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ri.Enabled {
|
||||||
|
return m.RemoveWireguardConfig(ri.Ring)
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.writeRingInfoConf(ri.Ring, ri.Keys.PrivateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Machine) writeRingInfoConf(ring int, _ wireguard.PrivateKey) error {
|
||||||
|
f, err := m.CreateTruncFile("wg%v.conf", ring)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
package zones
|
|
||||||
|
|
||||||
// SyncAll updates all config files
|
|
||||||
func (m *Zones) SyncAll() error {
|
|
||||||
for _, fn := range []func() error{
|
|
||||||
m.SyncAllWireguard,
|
|
||||||
} {
|
|
||||||
if err := fn(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SyncAllWireguard updates all wireguard config files
|
|
||||||
func (m *Zones) SyncAllWireguard() error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
for ring := 0; ring < RingsCount; ring++ {
|
|
||||||
err = m.PruneWireguardConfig(ring)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = m.WriteWireguardKeys(ring)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,61 +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 {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
|
||||||
_, ok := p.getRingInfo(ring)
|
|
||||||
if !ok {
|
|
||||||
err = p.RemoveWireguardConfig(ring)
|
|
||||||
}
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
|
|
||||||
func (z *Zone) WriteWireguardKeys(ring int) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
z.ForEachMachine(func(p *Machine) bool {
|
|
||||||
err = p.WriteWireguardKeys(ring)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
// ignore
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// PruneWireguardConfig removes wgN.conf files of machines with
|
|
||||||
// the corresponding ring disabled on all zones
|
|
||||||
func (m *Zones) PruneWireguardConfig(ring int) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
err = z.PruneWireguardConfig(ring)
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteWireguardKeys rewrites all wgN.{key,pub} files
|
|
||||||
func (m *Zones) WriteWireguardKeys(ring int) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
|
||||||
err = z.WriteWireguardKeys(ring)
|
|
||||||
return err != nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
+27
-2
@@ -14,8 +14,8 @@ import (
|
|||||||
type Zone struct {
|
type Zone struct {
|
||||||
zones *Zones
|
zones *Zones
|
||||||
|
|
||||||
ID int `toml:"id"`
|
ID int
|
||||||
Name string `toml:"name"`
|
Name string
|
||||||
|
|
||||||
Machines []*Machine `toml:"machines"`
|
Machines []*Machine `toml:"machines"`
|
||||||
}
|
}
|
||||||
@@ -34,6 +34,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