Skip to content

Commit

Permalink
[v17] Modulate install script when managed updates v2 are off (#52609)
Browse files Browse the repository at this point in the history
* Modulate install script when managed updates v2 are off

* fixup! Modulate install script when managed updates v2 are off

* Address Stephen's feedback
  • Loading branch information
hugoShaka authored Feb 28, 2025
1 parent 382d9da commit d79792f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
26 changes: 25 additions & 1 deletion lib/web/scripts/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ package scripts
import (
"context"
_ "embed"
"fmt"
"net/url"
"regexp"
"strings"

"github.com/google/safetext/shsprintf"
"github.com/gravitational/trace"

"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/utils/teleportassets"
"github.com/gravitational/teleport/lib/web/scripts/oneoff"
)
Expand Down Expand Up @@ -173,12 +176,33 @@ func GetInstallScript(ctx context.Context, opts InstallScriptOptions) (string, e
//go:embed install/install.sh
var legacyInstallScript string

var (
versionVar = regexp.MustCompile(`(?m)^TELEPORT_VERSION=""$`)
suffixVar = regexp.MustCompile(`(?m)^TELEPORT_SUFFIX=""$`)
editionVar = regexp.MustCompile(`(?m)^TELEPORT_EDITION=""$`)
)

// getLegacyInstallScript returns the installation script that we have been serving at
// "https://cdn.teleport.dev/install.sh". This script installs teleport via package manager
// or by unpacking the tarball. Its usage should be phased out in favor of the updater-based
// installation script served by getUpdaterInstallScript.
func getLegacyInstallScript(ctx context.Context, opts InstallScriptOptions) (string, error) {
return legacyInstallScript, nil
tunedScript := versionVar.ReplaceAllString(legacyInstallScript, fmt.Sprintf(`TELEPORT_VERSION="%s"`, opts.TeleportVersion))
if opts.TeleportFlavor == types.PackageNameEnt {
tunedScript = suffixVar.ReplaceAllString(tunedScript, `TELEPORT_SUFFIX="-ent"`)
}

var edition string
if opts.AutoupdateStyle == PackageManagerAutoupdate {
edition = "cloud"
} else if opts.TeleportFlavor == types.PackageNameEnt {
edition = "enterprise"
} else {
edition = "oss"
}
tunedScript = editionVar.ReplaceAllString(tunedScript, fmt.Sprintf(`TELEPORT_EDITION="%s"`, edition))

return tunedScript, nil
}

// getUpdaterInstallScript returns an installation script that downloads teleport-update
Expand Down
8 changes: 5 additions & 3 deletions lib/web/scripts/install/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,13 @@ TELEPORT_EDITION=""
if [ $# -ge 1 ] && [ -n "$1" ]; then
TELEPORT_VERSION=$1
else
echo "ERROR: Please provide the version you want to install (e.g., 10.1.9)."
exit 1
if [ -z "$TELEPORT_VERSION" ]; then
echo "ERROR: Please provide the version you want to install (e.g., 10.1.9)."
exit 1
fi
fi

if ! echo "$1" | grep -qE "[0-9]+\.[0-9]+\.[0-9]+"; then
if ! echo "$TELEPORT_VERSION" | grep -qE "[0-9]+\.[0-9]+\.[0-9]+"; then
echo "ERROR: The first parameter must be a version number, e.g., 10.1.9."
exit 1
fi
Expand Down
37 changes: 31 additions & 6 deletions lib/web/scripts/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,42 @@ func TestGetInstallScript(t *testing.T) {
assertFn func(t *testing.T, script string)
}{
{
name: "Legacy install, no autoupdate",
opts: InstallScriptOptions{AutoupdateStyle: NoAutoupdate},
name: "Legacy install, oss",
opts: InstallScriptOptions{
AutoupdateStyle: NoAutoupdate,
TeleportVersion: testVersion,
TeleportFlavor: types.PackageNameOSS,
},
assertFn: func(t *testing.T, script string) {
require.Contains(t, script, fmt.Sprintf(`TELEPORT_VERSION="%s"`, testVersion))
require.Contains(t, script, `TELEPORT_SUFFIX=""`)
require.Contains(t, script, `TELEPORT_EDITION="oss"`)
},
},
{
name: "Legacy install, enterprise",
opts: InstallScriptOptions{
AutoupdateStyle: NoAutoupdate,
TeleportVersion: testVersion,
TeleportFlavor: types.PackageNameEnt,
},
assertFn: func(t *testing.T, script string) {
require.Equal(t, legacyInstallScript, script)
require.Contains(t, script, fmt.Sprintf(`TELEPORT_VERSION="%s"`, testVersion))
require.Contains(t, script, `TELEPORT_SUFFIX="-ent"`)
require.Contains(t, script, `TELEPORT_EDITION="enterprise"`)
},
},
{
name: "Legacy install, package manager autoupdate",
opts: InstallScriptOptions{AutoupdateStyle: NoAutoupdate},
name: "Legacy install, cloud",
opts: InstallScriptOptions{
AutoupdateStyle: PackageManagerAutoupdate,
TeleportVersion: testVersion,
TeleportFlavor: types.PackageNameEnt,
},
assertFn: func(t *testing.T, script string) {
require.Equal(t, legacyInstallScript, script)
require.Contains(t, script, fmt.Sprintf(`TELEPORT_VERSION="%s"`, testVersion))
require.Contains(t, script, `TELEPORT_SUFFIX="-ent"`)
require.Contains(t, script, `TELEPORT_EDITION="cloud"`)
},
},
{
Expand Down

0 comments on commit d79792f

Please sign in to comment.