logadopt.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package main
  4. import (
  5. "flag"
  6. "io"
  7. "log"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "strings"
  12. )
  13. func main() {
  14. collection := flag.String("c", "", "logtail collection name")
  15. publicID := flag.String("m", "", "machine public identifier")
  16. apiKey := flag.String("p", "", "logtail API key")
  17. flag.Parse()
  18. if len(flag.Args()) != 0 {
  19. flag.Usage()
  20. os.Exit(1)
  21. }
  22. log.SetFlags(0)
  23. req, err := http.NewRequest("POST", "https://log.tailscale.io/instances", strings.NewReader(url.Values{
  24. "collection": []string{*collection},
  25. "instances": []string{*publicID},
  26. "adopt": []string{"true"},
  27. }.Encode()))
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  32. req.SetBasicAuth(*apiKey, "")
  33. resp, err := http.DefaultClient.Do(req)
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. b, err := io.ReadAll(resp.Body)
  38. resp.Body.Close()
  39. if err != nil {
  40. log.Fatalf("logadopt: response read failed %d: %v", resp.StatusCode, err)
  41. }
  42. if resp.StatusCode != 200 {
  43. log.Fatalf("adoption failed: %d: %s", resp.StatusCode, string(b))
  44. }
  45. log.Printf("%s", string(b))
  46. }