Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d1f7d225ae | |||
| dfbb358187 | |||
| 26c49dff72 | |||
| 2043708949 | |||
| 311ae572da | |||
| 4ca77b0ac0 | |||
| 1859c8e04b | |||
| 202f2e6dfc | |||
| 20484a5061 | |||
| 45b25c63d4 | |||
| c0e2ae9bf1 | |||
| 080021b427 | |||
| 4514b44211 | |||
| 49b82ace71 | |||
| 2207e4a4a4 | |||
| 7ca01aa1e4 | |||
| 8b72667f4d |
@@ -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)
|
||||
}
|
||||
|
||||
+54
-5
@@ -2,27 +2,76 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/burntSushi/toml"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// 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
|
||||
var dumpCmd = &cobra.Command{
|
||||
Use: "dump",
|
||||
Short: "generates a toml representation of the config",
|
||||
Short: "generates a text representation of the config",
|
||||
RunE: func(_ *cobra.Command, _ []string) error {
|
||||
var buf bytes.Buffer
|
||||
var enc Encoder
|
||||
|
||||
m, err := zones.New(cfg.Base, cfg.Domain)
|
||||
m, err := cfg.LoadZones()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enc := toml.NewEncoder(&buf)
|
||||
switch encoding {
|
||||
case JSONEncoding:
|
||||
enc = NewJSONEncoder(&buf)
|
||||
case YAMLEncoding:
|
||||
enc = NewYAMLEncoder(&buf)
|
||||
default:
|
||||
enc = NewTOMLEncoder(&buf)
|
||||
}
|
||||
|
||||
if err = enc.Encode(m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-3
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -13,6 +13,7 @@ require (
|
||||
github.com/spf13/cobra v1.7.0
|
||||
golang.org/x/crypto v0.12.0
|
||||
gopkg.in/gcfg.v1 v1.2.3
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
@@ -90,6 +90,7 @@ 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/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
||||
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/gcfg.v1 v1.2.3 h1:m8OOJ4ccYHnx2f4gQwpno8nAX5OGOh7RLaaz0pj3Ogs=
|
||||
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/crypto/curve25519"
|
||||
)
|
||||
@@ -50,6 +51,43 @@ 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
|
||||
func (key PrivateKey) IsZero() bool {
|
||||
var zero PrivateKey
|
||||
|
||||
@@ -5,16 +5,20 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// revive:disable:line-length-limit
|
||||
|
||||
// A Machine is a machine on a Zone
|
||||
type Machine struct {
|
||||
zone *Zone
|
||||
ID int
|
||||
Name string `toml:"name"`
|
||||
ID int `toml:"id"`
|
||||
Name string `toml:"-" json:"-" yaml:"-"`
|
||||
|
||||
PublicAddresses []netip.Addr `toml:"public,omitempty"`
|
||||
Rings []*RingInfo `toml:"rings,omitempty"`
|
||||
PublicAddresses []netip.Addr `toml:"public,omitempty" json:"public,omitempty" yaml:"public,omitempty"`
|
||||
Rings []*RingInfo `toml:"rings,omitempty" json:"rings,omitempty" yaml:"rings,omitempty"`
|
||||
}
|
||||
|
||||
// revive:enable:line-length-limit
|
||||
|
||||
func (m *Machine) String() string {
|
||||
return m.Name
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -105,6 +105,28 @@ func (m *Machine) WriteWireguardKeys(ring int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
|
||||
// the machine's config directory
|
||||
func (m *Machine) RemoveWireguardKeys(ring int) error {
|
||||
var err error
|
||||
|
||||
err = m.RemoveFile("wg%v.pub", ring)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
// ignore
|
||||
case err != nil:
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.RemoveFile("wg%v.key", ring)
|
||||
if os.IsNotExist(err) {
|
||||
// ignore
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// GetWireguardConfig reads a wgN.conf file
|
||||
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
|
||||
data, err := m.ReadFile("wg%v.conf", ring)
|
||||
@@ -228,3 +250,14 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveWireguardConfig deletes wgN.conf from the machine's
|
||||
// config directory.
|
||||
func (m *Machine) RemoveWireguardConfig(ring int) error {
|
||||
err := m.RemoveFile("wg%v.conf", ring)
|
||||
if os.IsNotExist(err) {
|
||||
err = nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-1
@@ -175,7 +175,7 @@ func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
|
||||
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
|
||||
return netip.Addr{}, false
|
||||
default:
|
||||
a4 := [4]uint8{10, 0, uint8(zoneID << 4), uint8(nodeID)}
|
||||
a4 := [4]uint8{10, uint8(zoneID << 4), 0, uint8(nodeID)}
|
||||
return netip.AddrFrom4(a4), true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.PruneWireguardConfig(ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.WriteWireguardKeys(ring)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
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
|
||||
}
|
||||
+9
-3
@@ -3,6 +3,7 @@ package zones
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hack-pad/hackpadfs/os"
|
||||
|
||||
@@ -13,8 +14,8 @@ import (
|
||||
type Zone struct {
|
||||
zones *Zones
|
||||
|
||||
ID int
|
||||
Name string
|
||||
ID int `toml:"id"`
|
||||
Name string `toml:"name"`
|
||||
|
||||
Machines []*Machine `toml:"machines"`
|
||||
}
|
||||
@@ -105,7 +106,12 @@ func NewFS(dir fs.FS, domain string) (*Zones, error) {
|
||||
|
||||
// New builds a [Zones] tree using the given directory
|
||||
func New(dir, domain string) (*Zones, error) {
|
||||
base, err := os.NewFS().Sub(dir)
|
||||
dir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
base, err := os.NewFS().Sub(dir[1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user