Compare commits

...

4 Commits

Author SHA1 Message Date
amery 0c0cba6fb5 jpictl: introduce env command
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 22:27:11 +00:00
amery 75206e4fa5 zones: Zones.WriteEnv() writing env variables describing the cluster
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 22:26:48 +00:00
amery b084e103b9 zones: introduce Machine.getRingInfo()
and refactor Machine.applyRingInfo()

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 22:25:11 +00:00
amery 223edf846b zones: introduce Zone.ForEachMachine()
and refactor Zones.ForEachMachine() using it

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 22:25:09 +00:00
4 changed files with 154 additions and 14 deletions
+27
View File
@@ -0,0 +1,27 @@
package main
import (
"os"
"github.com/spf13/cobra"
"git.jpi.io/amery/jpictl/pkg/zones"
)
// Command
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)
if err != nil {
return err
}
return m.WriteEnv(os.Stdout)
},
}
func init() {
rootCmd.AddCommand(envCmd)
}
+97
View File
@@ -0,0 +1,97 @@
package zones
import (
"bytes"
"fmt"
"io"
"strings"
)
// WriteEnv generates environment variables for shell scripts
func (m *Zones) WriteEnv(w io.Writer) error {
var buf bytes.Buffer
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
m.ForEachZone(func(z *Zone) bool {
m.writeEnvZone(&buf, z)
return false
})
_, err := buf.WriteTo(w)
return err
}
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
zoneID := z.ID
// ZONE{zoneID}
m.writeEnvVar(w, genEnvZoneNodes(z), "ZONE%v", zoneID)
// ZONE{zoneID}_NAME
m.writeEnvVar(w, z.Name, "ZONE%v_%s", zoneID, "NAME")
// ZONE{zoneID}_GW
firstNodeID := 0
gatewayID := 0
z.ForEachMachine(func(p *Machine) bool {
nodeID := p.ID()
if firstNodeID == 0 {
firstNodeID = nodeID
}
if _, found := p.getRingInfo(0); found {
gatewayID = nodeID
}
return gatewayID != 0
})
if gatewayID == 0 {
gatewayID = firstNodeID
}
m.writeEnvVar(w, fmt.Sprintf("%v", gatewayID), "ZONE%v_%s", zoneID, "GW")
// ZONE{zoneID}_IP
ip, _ := RingZeroAddress(zoneID, gatewayID)
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) {
var value string
if fn != nil {
value = fn(m)
}
m.writeEnvVar(w, value, name, args...)
}
func (*Zones) writeEnvVar(w io.Writer, value string, name string, args ...any) {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
if name != "" {
value = strings.TrimSpace(value)
_, _ = fmt.Fprintf(w, "%s=%q\n", name, value)
}
}
func genEnvZones(m *Zones) string {
s := make([]string, 0, len(m.Zones))
for _, z := range m.Zones {
s = append(s, fmt.Sprintf("%v", z.ID))
}
return strings.Join(s, " ")
}
func genEnvZoneNodes(z *Zone) string {
s := make([]string, 0, len(z.Machines))
for _, p := range z.Machines {
s = append(s, p.Name)
}
return strings.Join(s, " ")
}
+8 -6
View File
@@ -60,17 +60,19 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
return nil
}
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
var cur *RingInfo
func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) {
for _, ri := range m.RingAddresses {
if ri.Ring == ring {
cur = ri
break
return ri, true
}
}
if cur == nil {
return nil, false
}
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
cur, found := m.getRingInfo(ring)
if !found {
// first, append
m.RingAddresses = append(m.RingAddresses, new)
return nil
+22 -8
View File
@@ -22,6 +22,16 @@ func (z *Zone) String() string {
return z.Name
}
// ForEachMachine calls a function for each Machine in the zone
// until instructed to terminate the loop
func (z *Zone) ForEachMachine(fn func(*Machine) bool) {
for _, p := range z.Machines {
if fn(p) {
return
}
}
}
// Zones represents all zones in a cluster
type Zones struct {
dir fs.FS
@@ -32,18 +42,22 @@ type Zones struct {
}
// ForEachMachine calls a function for each Machine in the cluster
// until instructed to terminate the loop
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
for _, z := range m.Zones {
for _, p := range z.Machines {
if fn(p) {
// terminate
return
}
}
}
m.ForEachZone(func(z *Zone) bool {
var term bool
z.ForEachMachine(func(p *Machine) bool {
term = fn(p)
return term
})
return term
})
}
// ForEachZone calls a function for each Zone in the cluster
// until instructed to terminate the loop
func (m *Zones) ForEachZone(fn func(*Zone) bool) {
for _, p := range m.Zones {
if fn(p) {