Skip to content

Commit

Permalink
Make client a service with configurable registration -interval (#22)
Browse files Browse the repository at this point in the history
* Turn autojoin client into a service with configurable -interval

* Remove unnecessary fmt

* Don't spawn a goroutine

* Use memoryless
  • Loading branch information
robertodauria authored Jun 13, 2024
1 parent f5bc5ff commit 735d907
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions cmd/register/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"time"

v0 "github.com/m-lab/autojoin/api/v0"
"github.com/m-lab/go/memoryless"
"github.com/m-lab/go/rtx"
v2 "github.com/m-lab/locate/api/v2"
"github.com/m-lab/uuid-annotator/annotator"
Expand All @@ -24,14 +27,17 @@ const (
)

var (
endpoint = flag.String("endpoint", registerEndpoint, "Endpoint of the autojoin service")
apiKey = flag.String("key", "", "API key for the autojoin service")
service = flag.String("service", "ndt", "Service name to register with the autojoin service")
org = flag.String("organization", "", "Organization to register with the autojoin service")
iata = flag.String("iata", "", "IATA code to register with the autojoin service")
ipv4 = flag.String("ipv4", "", "IPv4 address to register with the autojoin service")
ipv6 = flag.String("ipv6", "", "IPv6 address to register with the autojoin service")
outputPath = flag.String("output", "", "Output folder")
endpoint = flag.String("endpoint", registerEndpoint, "Endpoint of the autojoin service")
apiKey = flag.String("key", "", "API key for the autojoin service")
service = flag.String("service", "ndt", "Service name to register with the autojoin service")
org = flag.String("organization", "", "Organization to register with the autojoin service")
iata = flag.String("iata", "", "IATA code to register with the autojoin service")
ipv4 = flag.String("ipv4", "", "IPv4 address to register with the autojoin service")
ipv6 = flag.String("ipv6", "", "IPv6 address to register with the autojoin service")
interval = flag.Duration("interval.expected", 1*time.Hour, "Expected registration interval")
intervalMin = flag.Duration("interval.min", 55*time.Minute, "Minimum registration interval")
intervalMax = flag.Duration("interval.max", 65*time.Minute, "Maximum registration interval")
outputPath = flag.String("output", "", "Output folder")
)

func main() {
Expand All @@ -41,6 +47,25 @@ func main() {
panic("-key, -service, -organization, and -iata are required.")
}

register()

// Keep retrying registration every configured interval.
t, err := memoryless.NewTicker(context.Background(), memoryless.Config{
Expected: *interval,
Min: *intervalMin,
Max: *intervalMax,
})
rtx.Must(err, "Failed to create ticker")

for range t.C {
register()
}
}

// Make a call to the register endpoint and write the resulting config files to
// disk. If the node is registered already, this is effectively a no-op for the
// autojoin API and will just touch the output files' last-modified time.
func register() {
// Make a HTTP call to the autojoin service to register this node.
registerURL, err := url.Parse(*endpoint)
rtx.Must(err, "Failed to parse autojoin service URL")
Expand All @@ -53,12 +78,13 @@ func main() {
q.Add("ipv6", *ipv6)
registerURL.RawQuery = q.Encode()

log.Printf("Registering with %s", registerURL)

resp, err := http.Post(registerURL.String(), "application/json", nil)
rtx.Must(err, "POST autojoin/v0/node/register failed")
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
fmt.Println(resp.Status)
panic("Failed to register with autojoin service")
}

Expand Down Expand Up @@ -88,4 +114,6 @@ func main() {
rtx.Must(err, "Failed to write heartbeat file")
err = os.WriteFile(path.Join(*outputPath, annotationFilename), annotationJSON, 0644)
rtx.Must(err, "Failed to write annotation file")

log.Printf("Registration successful with hostname: %s", r.Registration.Hostname)
}

0 comments on commit 735d907

Please sign in to comment.