Compare commits

..

11 Commits

Author SHA1 Message Date
amery 09678d8eb8 WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 22:15:54 +00:00
amery 3662fc7510 zones: change WriteEnv() to not fake gateways
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 22:15:54 +00:00
amery cade4b83bc zones: add Zone.SetGateway() to set one by ID
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 22:15:54 +00:00
amery 2bff61d6f2 zones: set first node of a Zone as ring0 gateway if it doesn't have one already
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 22:15:54 +00:00
amery b537a4438d wireguard: Config.WriteTo()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 22:15:52 +00:00
amery dfbb358187 jpictl: introduce write command rewriting all config files
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 21:20:34 +00:00
amery 26c49dff72 jpictl: refactor zones loading
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 21:18:55 +00:00
amery 2043708949 zones: Zones.WriteWireguardKeys() and Zone.WriteWireguardKeys()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 21:18:55 +00:00
amery 311ae572da zones: Zones.PruneWireguardConfig()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 21:18:55 +00:00
amery 4ca77b0ac0 zones: Zone.PruneWireguardConfig()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 21:18:55 +00:00
amery 1859c8e04b zones: inject trailing new lines on Machine.WriteWireguardKeys()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 21:18:55 +00:00
8 changed files with 130 additions and 15 deletions
+7
View File
@@ -1,5 +1,7 @@
package main
import "git.jpi.io/amery/jpictl/pkg/zones"
// Config describes the repository
type Config struct {
Base string
@@ -10,3 +12,8 @@ var cfg = &Config{
Base: "./m",
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)
}
+1 -3
View File
@@ -9,8 +9,6 @@ import (
"github.com/burntSushi/toml"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"git.jpi.io/amery/jpictl/pkg/zones"
)
// Encoder represents an object that encodes another internally
@@ -60,7 +58,7 @@ var dumpCmd = &cobra.Command{
var buf bytes.Buffer
var enc Encoder
m, err := zones.New(cfg.Base, cfg.Domain)
m, err := cfg.LoadZones()
if err != nil {
return err
}
+1 -3
View File
@@ -4,8 +4,6 @@ import (
"os"
"github.com/spf13/cobra"
"git.jpi.io/amery/jpictl/pkg/zones"
)
// Command
@@ -13,7 +11,7 @@ var envCmd = &cobra.Command{
Use: "env",
Short: "generates environment variables for shell scripts",
RunE: func(_ *cobra.Command, _ []string) error {
m, err := zones.New(cfg.Base, cfg.Domain)
m, err := cfg.LoadZones()
if err != nil {
return err
}
+23
View File
@@ -0,0 +1,23 @@
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)
}
+6 -6
View File
@@ -37,20 +37,20 @@ type Config struct {
}
// GetAddress is a shortcut to the interface's address
func (wg *Config) GetAddress() netip.Addr {
return wg.Interface.Address
func (f *Config) GetAddress() netip.Addr {
return f.Interface.Address
}
// Peers tells how many peers are described
func (wg *Config) Peers() int {
return len(wg.Peer)
func (f *Config) Peers() int {
return len(f.Peer)
}
// WriteTo writes a Wireguard [Config] onto the provided [io.Writer]
func (wg *Config) WriteTo(w io.Writer) (int64, error) {
func (f *Config) WriteTo(w io.Writer) (int64, error) {
var buf bytes.Buffer
if err := configTemplate.Execute(&buf, wg); err != nil {
if err := configTemplate.Execute(&buf, f); err != nil {
return 0, err
}
+2 -2
View File
@@ -92,12 +92,12 @@ func (m *Machine) WriteWireguardKeys(ring int) error {
pub = ri.Keys.PrivateKey.Public().String()
}
err = m.WriteStringFile(key, "wg%v.key", ring)
err = m.WriteStringFile(key+"\n", "wg%v.key", ring)
if err != nil {
return err
}
err = m.WriteStringFile(pub, "wg%v.pub", ring)
err = m.WriteStringFile(pub+"\n", "wg%v.pub", ring)
if err != nil {
return err
}
+33
View File
@@ -0,0 +1,33 @@
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.WriteWireguardKeys(ring)
if err != nil {
return err
}
err = m.SyncWireguardConfig(ring)
if err != nil {
return err
}
}
return nil
}
+57 -1
View File
@@ -1,6 +1,10 @@
package zones
import "git.jpi.io/amery/jpictl/pkg/wireguard"
import (
"os"
"git.jpi.io/amery/jpictl/pkg/wireguard"
)
type Ring struct {
Ring int
@@ -52,3 +56,55 @@ func (z *Zone) PruneWireguardConfig(ring int) error {
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
}
// SyncWireguardConfig updates all wgN.conf files for the specified
// ring
func (m *Zones) SyncWireguardConfig(ring int) error {
err := m.PruneWireguardConfig(ring)
if err != nil {
return err
}
}