Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client tools auto update logic for multiple platforms #46587

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/client/webclient/webclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ type PingResponse struct {
ServerVersion string `json:"server_version"`
// MinClientVersion is the minimum client version required by the server.
MinClientVersion string `json:"min_client_version"`
// ToolsVersion defines the version of {tsh, tctl} for client auto-upgrade.
ToolsVersion string `json:"tools_version"`
// ToolsAutoupdate enables client autoupdate feature.
ToolsAutoupdate bool `json:"tools_autoupdate"`
// ClusterName contains the name of the Teleport cluster.
ClusterName string `json:"cluster_name"`

Expand Down
28 changes: 28 additions & 0 deletions lib/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import (
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/teleport/lib/utils/agentconn"
"github.com/gravitational/teleport/tool/common/update"
)

const (
Expand Down Expand Up @@ -684,6 +685,33 @@ func RetryWithRelogin(ctx context.Context, tc *TeleportClient, fn func() error,
return trace.Wrap(err)
}

// The user has typed a command like `tsh ssh ...` without being logged in,
// if the running binary needs to be updated, update and re-exec.
//
// If needed, download the new version of {tsh, tctl} and re-exec. Make
// sure to exit this process with the same exit code as the child process.
//
// TODO(russjones): Consolidate this with updateAndRun.
fmt.Printf("--> Will check remote and if needed, will update binary and re-exec.\n")

toolsVersion, reexec, err := update.CheckRemote(ctx, tc.WebProxyAddr)
if err != nil {
return trace.Wrap(err)
}
if reexec {
// Download the version of client tools required by the cluster.
if err := update.Download(toolsVersion); err != nil {
return trace.Wrap(err)
}

// Re-execute client tools with the correct version of client tools.
code, err := update.Exec()
if err != nil {
return trace.Wrap(err)
}
os.Exit(code)
}

if opt.afterLoginHook != nil {
if err := opt.afterLoginHook(); err != nil {
return trace.Wrap(err)
Expand Down
45 changes: 45 additions & 0 deletions tool/common/update/integration/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"errors"
"fmt"
"os"

log "github.com/sirupsen/logrus"

"github.com/gravitational/teleport/tool/common/update"
)

var version = "development"

func main() {
// At process startup, check if a version has already been downloaded to
// $TELEPORT_HOME/bin or if the user has set the TELEPORT_TOOLS_VERSION
// environment variable. If so, re-exec that version of {tsh, tctl}.
toolsVersion, reExec := update.CheckLocal()
if reExec {
// Download the version of client tools required by the cluster. This
// is required if the user passed in the TELEPORT_TOOLS_VERSION
// explicitly.
err := update.Download(toolsVersion)
if errors.Is(err, update.ErrCancelled) {
os.Exit(0)
return
}
if err != nil {
log.Fatalf("Failed to download version (%v): %v", toolsVersion, err)
return
}

// Re-execute client tools with the correct version of client tools.
code, err := update.Exec()
if err != nil {
log.Fatalf("Failed to re-exec client tool: %v", err)
} else {
os.Exit(code)
}
}
if len(os.Args) > 1 && os.Args[1] == "version" {
fmt.Printf("Teleport v%v git", version)
}
}
80 changes: 80 additions & 0 deletions tool/common/update/progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package update

import (
"fmt"
"io"
"os"
"os/signal"
"strings"
)

var (
ErrCancelled = fmt.Errorf("cancelled")
)

// cancelableTeeReader is a copy of TeeReader with ability to react on signal notifier
// to cancel reading process.
func cancelableTeeReader(r io.Reader, w io.Writer, signals ...os.Signal) io.Reader {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, signals...)

return &teeReader{r, w, sigs}
}

type teeReader struct {
r io.Reader
w io.Writer
sigs chan os.Signal
}

func (t *teeReader) Read(p []byte) (n int, err error) {
select {
case <-t.sigs:
return 0, ErrCancelled
default:
n, err = t.r.Read(p)
if n > 0 {
if n, err := t.w.Write(p[:n]); err != nil {
return n, err
}
}
}
return
}

type progressWriter struct {
n int64
limit int64
}

func (w *progressWriter) Write(p []byte) (int, error) {
w.n = w.n + int64(len(p))

n := int((w.n*100)/w.limit) / 10
bricks := strings.Repeat("▒", n) + strings.Repeat(" ", 10-n)
fmt.Printf("\rUpdate progress: [" + bricks + "] (Ctrl-C to cancel update)")

if w.n == w.limit {
fmt.Printf("\n")
}

return len(p), nil
}
Loading
Loading