diff --git a/cmd/waifud/metadata-server.go b/cmd/waifud-metadatad/main.go similarity index 86% rename from cmd/waifud/metadata-server.go rename to cmd/waifud-metadatad/main.go index 63b883b..009cbfa 100644 --- a/cmd/waifud/metadata-server.go +++ b/cmd/waifud-metadatad/main.go @@ -11,20 +11,24 @@ import ( "net/http" "os" "os/exec" + "os/signal" "path" "path/filepath" "strings" "text/template" "github.com/Xe/waifud/key2hex" + "github.com/facebookgo/flagenv" "github.com/go-redis/redis/v8" "golang.zx2c4.com/wireguard/conn" "golang.zx2c4.com/wireguard/device" "golang.zx2c4.com/wireguard/tun/netstack" "within.website/ln" + "within.website/ln/opname" ) var ( + redisURL = flag.String("redis-url", "redis://chrysalis", "the url to dial out to Redis") wgPort = flag.Int("wireguard-sever-port", 28139, "what port to have the kernel listen on wireguard") wgHostAddr = flag.String("wireguard-host-addr", "169.254.169.253/30", "what IP range to have for the metadata service") wgGuestAddr = flag.String("wireguard-guest-addr", "169.254.169.254", "the IP address for the metadata service") @@ -35,6 +39,40 @@ var ( wgGuestPubkey = flag.String("wireguard-guest-public-key", "./var/waifud-guest.pubkey", "wireguard guest public key path (b64)") ) +func main() { + flagenv.Parse() + flag.Parse() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + ctx = opname.With(ctx, "main") + + rOptions, err := redis.ParseURL(*redisURL) + if err != nil { + ln.FatalErr(ctx, err, ln.Action("parsing redis url")) + } + + rdb := redis.NewClient(rOptions) + defer rdb.Close() + + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + defer func() { + signal.Stop(c) + cancel() + }() + + ms := metadataServer{cli: rdb} + go ms.Run(ctx) + + select { + case <-c: + cancel() + case <-ctx.Done(): + } +} + func run(args ...string) error { log.Println("running command:", strings.Join(args, " ")) cmd := exec.Command(args[0], args[1:]...) @@ -87,7 +125,7 @@ func (ms metadataServer) listen(ctx context.Context) error { }() mux := http.NewServeMux() - mux.Handle("/metadata/", ms) + mux.Handle("/instance/", ms) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "waifud metadata service - https://github.com/Xe/waifud") }) diff --git a/cmd/waifud/main.go b/cmd/waifud/main.go index dc88211..7924864 100644 --- a/cmd/waifud/main.go +++ b/cmd/waifud/main.go @@ -3,7 +3,10 @@ package main import ( "context" "flag" + "os" + "os/signal" + "github.com/facebookgo/flagenv" "github.com/go-redis/redis/v8" "within.website/ln" "within.website/ln/opname" @@ -17,6 +20,7 @@ var ( ) func main() { + flagenv.Parse() flag.Parse() ctx, cancel := context.WithCancel(context.Background()) @@ -24,6 +28,20 @@ func main() { ctx = opname.With(ctx, "main") + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + defer func() { + signal.Stop(c) + cancel() + }() + go func() { + select { + case <-c: + cancel() + case <-ctx.Done(): + } + }() + rOptions, err := redis.ParseURL(*redisURL) if err != nil { ln.FatalErr(ctx, err, ln.Action("parsing redis url")) @@ -31,14 +49,4 @@ func main() { rdb := redis.NewClient(rOptions) defer rdb.Close() - - err = rdb.Set(ctx, "fa3d4df4-2f3e-45f7-a2c9-c006f406b68a/meta-data", `instance-id: fa3d4df4-2f3e-45f7-a2c9-c006f406b68a -local-hostname: ezscape`, 0).Err() - if err != nil { - ln.FatalErr(ctx, err) - } - - ms := metadataServer{rdb} - - ln.FatalErr(ctx, ms.listen(ctx)) } diff --git a/go.mod b/go.mod index 56c5508..db5e650 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/digitalocean/go-libvirt v0.0.0-20210513152157-a898af65b5cc + github.com/facebookgo/flagenv v0.0.0-20160425205200-fcd59fca7456 // indirect github.com/go-redis/redis/v8 v8.8.3 // indirect github.com/google/uuid v1.2.0 github.com/philandstuff/dhall-golang/v5 v5.0.0 diff --git a/go.sum b/go.sum index c875db5..ddc4892 100644 --- a/go.sum +++ b/go.sum @@ -108,6 +108,8 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/facebookgo/flagenv v0.0.0-20160425205200-fcd59fca7456 h1:CkmB2l68uhvRlwOTPrwnuitSxi/S3Cg4L5QYOcL9MBc= +github.com/facebookgo/flagenv v0.0.0-20160425205200-fcd59fca7456/go.mod h1:zFhibDvPDWmtk4dAQ05sRobtyoffEHygEt3wSNuAzz8= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fxamacker/cbor/v2 v2.2.1-0.20200511212021-28e39be4a84f h1:lvGFo/tDOSQ4FKu0d2694s8XyOfAL6FLR9DCD5BIUW4=