Compare commits

...

5 Commits

Author SHA1 Message Date
amery 3ad6adec3b jpictl: introduce --config-file/-f as alternative to scanning m/
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-10 23:38:46 +00:00
amery a02b7574b0 cluster: NewFromConfig() trying JSON and YAML
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-10 23:38:17 +00:00
amery 6fdf1fe0c7 vscode: add Wrapf to the dictionary
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-10 22:30:16 +00:00
amery db3c476930 DNS [WIP]
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-10 21:38:53 +00:00
amery b0dec3f445 vscode: add Lookuper to the dictionary
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-10 21:38:53 +00:00
10 changed files with 364 additions and 1 deletions
+2
View File
@@ -4,6 +4,8 @@
"darvaza",
"gofrs",
"jpictl",
"Lookuper",
"Wrapf",
"zerolog"
]
}
+37 -1
View File
@@ -1,20 +1,56 @@
package main
import "git.jpi.io/amery/jpictl/pkg/cluster"
import (
"os"
"darvaza.org/core"
"git.jpi.io/amery/jpictl/pkg/cluster"
)
const (
// DefaultConfigFile is read if -f/--config-file isn't specified.
// If it doesn't exist, m/ will be scanned
DefaultConfigFile = "cloud.yaml"
)
// Config describes the repository
type Config struct {
Base string
Domain string
ConfigFile *string
}
var cfg = &Config{
Base: "m",
Domain: "jpi.cloud",
ConfigFile: rootCmd.PersistentFlags().
StringP("config-file", "f", DefaultConfigFile, "config file (JSON or YAML)"),
}
// LoadZones loads all zones and machines in the config directory
// or file
func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
configFile := *cfg.ConfigFile
// try config file first
zones, err := cluster.NewFromConfig(configFile,
cluster.ResolvePublicAddresses(resolve),
cluster.WithLogger(log),
)
switch {
case err == nil:
// file was good
return zones, nil
case !os.IsNotExist(err) || configFile != DefaultConfigFile:
// file was bad
return nil, core.Wrapf(err, "NewFromConfig(%q)", configFile)
}
// default file doesn't exist. scan instead.
return cluster.NewFromDirectory(cfg.Base, cfg.Domain,
cluster.ResolvePublicAddresses(resolve),
cluster.WithLogger(log),
+48
View File
@@ -0,0 +1,48 @@
package main
import (
"github.com/spf13/cobra"
)
// Command
var dnsCmd = &cobra.Command{
Use: "dns",
}
var dnsSyncCmd = &cobra.Command{
Use: "sync",
RunE: func(_ *cobra.Command, _ []string) error {
_, err := cfg.LoadZones(true)
return err
},
}
var dnsWriteCmd = &cobra.Command{
Use: "write",
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
}
var dnsAddCmd = &cobra.Command{
Use: "add",
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
}
var dnsRemoveCmd = &cobra.Command{
Use: "remove",
RunE: func(_ *cobra.Command, _ []string) error {
return nil
},
}
func init() {
rootCmd.AddCommand(dnsCmd)
dnsCmd.AddCommand(dnsSyncCmd)
dnsCmd.AddCommand(dnsWriteCmd)
dnsCmd.AddCommand(dnsAddCmd)
dnsCmd.AddCommand(dnsRemoveCmd)
}
+2
View File
@@ -11,6 +11,8 @@ require (
darvaza.org/slog/handlers/discard v0.4.5
github.com/gofrs/uuid/v5 v5.0.0
github.com/hack-pad/hackpadfs v0.2.1
github.com/libdns/cloudflare v0.1.0
github.com/libdns/libdns v0.2.1
github.com/mgechev/revive v1.3.3
github.com/spf13/cobra v1.7.0
golang.org/x/crypto v0.12.0
+5
View File
@@ -36,6 +36,11 @@ github.com/hack-pad/hackpadfs v0.2.1 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMI
github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/libdns/cloudflare v0.1.0 h1:93WkJaGaiXCe353LHEP36kAWCUw0YjFqwhkBkU2/iic=
github.com/libdns/cloudflare v0.1.0/go.mod h1:a44IP6J1YH6nvcNl1PverfJviADgXUnsozR3a7vBKN8=
github.com/libdns/libdns v0.2.0/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis=
github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
+96
View File
@@ -0,0 +1,96 @@
package cluster
import (
"encoding/json"
"os"
"gopkg.in/yaml.v3"
)
func (m *Cluster) init(opts *ScanOptions) error {
for _, fn := range []func(*ScanOptions) error{
m.initZones,
m.scanZoneIDs,
m.scanSort,
m.scanGateways,
} {
if err := fn(opts); err != nil {
return err
}
}
return nil
}
func (m *Cluster) initZones(opts *ScanOptions) error {
var err error
m.ForEachZone(func(z *Zone) bool {
z.zones = m
z.logger = m
z.ForEachMachine(func(p *Machine) bool {
p.zone = z
p.logger = z
return err != nil
})
return err != nil
})
return err
}
func decodeConfigData(data []byte) (out *Cluster, err error) {
// try JSON first
out = new(Cluster)
err = json.Unmarshal(data, out)
if err == nil {
// good json
return out, nil
} else if _, ok := err.(*json.SyntaxError); !ok {
// bad json
return nil, err
}
out = new(Cluster)
err = yaml.Unmarshal(data, out)
if err != nil {
// bad yaml too
return nil, err
}
// good yaml
return out, nil
}
// NewFromConfig loads the cluster data from the given file
func NewFromConfig(filename string, opts ...ScanOption) (*Cluster, error) {
var scanOptions ScanOptions
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
m, err := decodeConfigData(data)
if err != nil {
return nil, err
}
for _, opt := range opts {
if err = opt(m, &scanOptions); err != nil {
return nil, err
}
}
if err = m.setScanDefaults(&scanOptions); err != nil {
return nil, err
}
if err := m.init(&scanOptions); err != nil {
return nil, err
}
return m, nil
}
+5
View File
@@ -15,3 +15,8 @@ func DefaultLogger() slog.Logger {
func DefaultLookuper() resolver.Lookuper {
return resolver.NewCloudflareLookuper()
}
// DefaultResolver returns a [resolver.Resolver] using Cloudflare's 1.1.1.1
func DefaultResolver() resolver.Resolver {
return resolver.NewResolver(DefaultLookuper())
}
+12
View File
@@ -0,0 +1,12 @@
// Package dns manages DNS entries for the cluster
package dns
// A Region describes where a Zone is
type Region struct {
// Name is the identifier of this Region
Name string
// Regions are a list of (sub)regions that belong to this Region
Regions []string
// Zones are a list of Zones that directly belong to this Region
Zones []string
}
+121
View File
@@ -0,0 +1,121 @@
package dns
import (
"darvaza.org/core"
"darvaza.org/resolver"
"darvaza.org/slog"
"git.jpi.io/amery/jpictl/pkg/cluster"
)
// Manager is a DNS Manager instance
type Manager struct {
p Provider
r resolver.Resolver
l slog.Logger
}
// ManagerOption configures a Manager
type ManagerOption func(*Manager) error
func newErrorManagerOption(err error, hint string) ManagerOption {
return func(*Manager) error {
if hint != "" {
err = core.Wrap(err, hint)
}
return err
}
}
// WithProvider attaches a libdns Provider to the Manager
func WithProvider(p Provider) ManagerOption {
var err error
if p == nil {
p, err = DefaultDNSProvider()
}
if err != nil {
return newErrorManagerOption(err, "WithProvider")
}
return func(mgr *Manager) error {
mgr.p = p
return nil
}
}
// WithLookuper attaches a resolver.Lookuper to the Manager
func WithLookuper(h resolver.Lookuper) ManagerOption {
if h == nil {
h = cluster.DefaultLookuper()
}
return func(mgr *Manager) error {
mgr.r = resolver.NewResolver(h)
return nil
}
}
// WithResolver attaches a resolver.Resolver to the Manager
func WithResolver(r resolver.Resolver) ManagerOption {
if r == nil {
r = cluster.DefaultResolver()
}
return func(mgr *Manager) error {
mgr.r = r
return nil
}
}
// WithLogger attaches a logger to the Manager
func WithLogger(log slog.Logger) ManagerOption {
if log == nil {
log = cluster.DefaultLogger()
}
return func(mgr *Manager) error {
mgr.l = log
return nil
}
}
func (mgr *Manager) setDefaults() error {
var opts []ManagerOption
if mgr.p == nil {
opts = append(opts, WithProvider(nil))
}
if mgr.r == nil {
opts = append(opts, WithResolver(nil))
}
if mgr.l == nil {
opts = append(opts, WithLogger(nil))
}
for _, opt := range opts {
if err := opt(mgr); err != nil {
return err
}
}
return nil
}
// NewManager creates a DNS manager with the
func NewManager(opts ...ManagerOption) (*Manager, error) {
mgr := new(Manager)
for _, opt := range opts {
if err := opt(mgr); err != nil {
return nil, err
}
}
if err := mgr.setDefaults(); err != nil {
return nil, err
}
return mgr, nil
}
+36
View File
@@ -0,0 +1,36 @@
package dns
import (
"fmt"
"os"
"github.com/libdns/cloudflare"
"github.com/libdns/libdns"
)
const (
// CloudflareAPIToken is the environment variable
// containing the API Token
CloudflareAPIToken = "CLOUDFLARE_DNS_API_TOKEN"
)
// Provider manages DNS entries
type Provider interface {
libdns.RecordGetter
libdns.RecordDeleter
}
// DefaultDNSProvider returns a cloudflare DNS provider
// using an API Token from env [CloudflareAPIToken]
func DefaultDNSProvider() (*cloudflare.Provider, error) {
s := os.Getenv(CloudflareAPIToken)
if s == "" {
return nil, fmt.Errorf("%q: %s", CloudflareAPIToken, "not found")
}
p := &cloudflare.Provider{
APIToken: s,
}
return p, nil
}