Skip to content

Commit

Permalink
tailcfg,ipn/ipnlocal: add C2NUpdateRequest to c2n /update handler
Browse files Browse the repository at this point in the history
An optional (for now) request body for POST /update messages to carry
the suggested version. This removes the need from clients to figure out
what the latest available version is.

Updates #6907

Signed-off-by: Andrew Lytvynov <awly@tailscale.com>
  • Loading branch information
awly committed Aug 30, 2023
1 parent ce1e020 commit 48a1b31
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/tailscaled/depaware.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
tailscale.com/types/views from tailscale.com/ipn/ipnlocal+
tailscale.com/util/clientmetric from tailscale.com/control/controlclient+
tailscale.com/util/cloudenv from tailscale.com/net/dns/resolver+
LW tailscale.com/util/cmpver from tailscale.com/net/dns+
tailscale.com/util/cmpver from tailscale.com/net/dns+
tailscale.com/util/cmpx from tailscale.com/derp/derphttp+
💣 tailscale.com/util/deephash from tailscale.com/ipn/ipnlocal+
L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics+
Expand Down
25 changes: 24 additions & 1 deletion ipn/ipnlocal/c2n.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"tailscale.com/net/sockstats"
"tailscale.com/tailcfg"
"tailscale.com/util/clientmetric"
"tailscale.com/util/cmpver"
"tailscale.com/util/goroutines"
"tailscale.com/version"
)
Expand Down Expand Up @@ -141,6 +142,7 @@ func (b *LocalBackend) handleC2NUpdate(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
return
}

if !res.Enabled {
res.Err = "not enabled"
return
Expand All @@ -150,6 +152,22 @@ func (b *LocalBackend) handleC2NUpdate(w http.ResponseWriter, r *http.Request) {
return
}

var req tailcfg.C2NUpdateRequest
if r.ContentLength > 0 {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
b.logf("c2n: bad /update request body: %v", err)
http.Error(w, "failed to parse request body", http.StatusBadRequest)
return
}
if cmpver.Compare(req.Ver, version.Short()) > 0 {
b.logf("c2n: update to version %q requested", req.Ver)
// TODO(awly): store the requested version somewhere to surface in UI.
} else {
res.Err = fmt.Sprintf("requested update version %q is older or the same as current version %q", req.Ver, version.Short())
return
}
}

cmdTS, err := findCmdTailscale()
if err != nil {
res.Err = fmt.Sprintf("failed to find cmd/tailscale binary: %v", err)
Expand All @@ -171,7 +189,12 @@ func (b *LocalBackend) handleC2NUpdate(w http.ResponseWriter, r *http.Request) {
res.Err = "cmd/tailscale version mismatch"
return
}
cmd := exec.Command(cmdTS, "update", "--yes")
var cmd *exec.Cmd
if req.Ver != "" {
cmd = exec.Command(cmdTS, "update", "--yes", "--version", req.Ver)
} else {
cmd = exec.Command(cmdTS, "update", "--yes")
}
if err := cmd.Start(); err != nil {
res.Err = fmt.Sprintf("failed to start cmd/tailscale update: %v", err)
return
Expand Down
8 changes: 8 additions & 0 deletions tailcfg/c2ntypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ type C2NSSHUsernamesResponse struct {
Usernames []string
}

// C2NUpdateRequest is the request (from control to node) to the /update
// handler. It tells the node what version to upgrade to. The node may choose
// to ignore this update, notify the user, or apply it automatically.
type C2NUpdateRequest struct {
// Ver is the version to upgrade to.
Ver string
}

// C2NUpdateResponse is the response (from node to control) from the /update
// handler. It tells control the status of its request for the node to update
// its Tailscale installation.
Expand Down

0 comments on commit 48a1b31

Please sign in to comment.