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

[gitpod-cli] Add auto-updating capabilities #19056

Merged
merged 10 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
16 changes: 7 additions & 9 deletions components/local-app/BUILD.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const generatePackage = function (goos, goarch, binaryName, mainFile) {
let pkg = {
name,
type: "go",
srcs: ["go.mod", "go.sum", "**/*.go"],
srcs: ["go.mod", "go.sum", "**/*.go", "version.txt"],
deps: [
"components/supervisor-api/go:lib",
"components/gitpod-protocol/go:lib",
Expand All @@ -19,14 +19,12 @@ const generatePackage = function (goos, goarch, binaryName, mainFile) {
packaging: "app",
dontTest: dontTest,
buildCommand: [
"go",
"build",
"-trimpath",
"-ldflags",
"-buildid= -w -s -X 'github.com/gitpod-io/local-app/pkg/constants.Version=commit-${__git_commit}'",
"-o",
binaryName,
mainFile,
"sh",
"-c",
'go build -trimpath -ldflags "-X github.com/gitpod-io/local-app/pkg/constants.GitCommit=${__git_commit} -X github.com/gitpod-io/local-app/pkg/constants.BuildTime=$(date +%s)" -o ' +
binaryName +
" " +
mainFile,
],
},
binaryName,
Expand Down
29 changes: 27 additions & 2 deletions components/local-app/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ packages:
- name: docker
type: docker
deps:
- :app
- :app-with-manifest
argdeps:
- imageRepoBase
config:
Expand All @@ -13,7 +13,32 @@ packages:
image:
- ${imageRepoBase}/local-app:${version}
- ${imageRepoBase}/local-app:commit-${__git_commit}

- name: update-manifest
type: go
srcs:
- go.mod
- go.sum
- "**/*.go"
- version.txt
deps:
- components/supervisor-api/go:lib
- components/gitpod-protocol/go:lib
- components/local-app-api/go:lib
- components/public-api/go:lib
config:
packaging: app
dontTest: true
buildCommand: ["go", "build", "-o", "update-manifest", "./main/update-manifest/main.go"]
- name: app-with-manifest
type: generic
deps:
- :app
- :update-manifest
config:
commands:
- ["sh", "-c", "mkdir -p bin && mv components-local-app--app/bin/* bin/"]
- ["sh", "-c", "components-local-app--update-manifest/update-manifest --cwd bin | tee bin/manifest.json"]
- ["rm", "-rf", "components-local-app--update-manifest", "components-local-app--app"]
scripts:
- name: install-cli
description: "Install gitpod-cli as `gitpod` command and add auto-completion. Usage: '. $(leeway run components/local-app:install-cli)'"
Expand Down
5 changes: 5 additions & 0 deletions components/local-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ leeway run components/local-app:install-cli
leeway run components/local-app:cli-completion
```

### Versioning and Release Management

The CLI is versioned independently of other Gitpod artifacts due to its auto-updating behaviour.
To create a new version that existing clients will consume increment the number in `version.txt`. Make sure to use semantic versioning. The minor version can be greater than 10, e.g. `0.342` is a valid version.

## local-app

**Beware**: this is very much work in progress and will likely break things.
Expand Down
37 changes: 37 additions & 0 deletions components/local-app/cmd/config-get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.

package cmd

import (
"github.com/gitpod-io/local-app/pkg/config"
"github.com/gitpod-io/local-app/pkg/prettyprint"
"github.com/spf13/cobra"
)

var configGetCmd = &cobra.Command{
Use: "get",
Short: "Get an individual config value in the config file",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

cfg := config.FromContext(cmd.Context())

return WriteTabular([]struct {
Telemetry bool `header:"Telemetry"`
Autoupdate bool `header:"Autoupdate"`
}{
{Telemetry: cfg.Telemetry.Enabled, Autoupdate: cfg.Autoupdate},
}, configGetOpts.Format, prettyprint.WriterFormatNarrow)
},
}

var configGetOpts struct {
Format formatOpts
}

func init() {
configCmd.AddCommand(configGetCmd)
addFormatFlags(configGetCmd, &configGetOpts.Format)
}
64 changes: 64 additions & 0 deletions components/local-app/cmd/config-set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.

package cmd

import (
"log/slog"

"github.com/gitpod-io/local-app/pkg/config"
"github.com/spf13/cobra"
)

var configSetCmd = &cobra.Command{
Use: "set",
Short: "Set an individual config value in the config file",
Long: `Set an individual config value in the config file.

Example:
# Disable telemetry
local-app config set --telemetry=false

# Disable autoupdate
local-app config set --autoupdate=false

# Enable telemetry and autoupdate
local-app config set --telemetry=true --autoupdate=true
`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

var update bool
cfg := config.FromContext(cmd.Context())
if cmd.Flags().Changed("autoupdate") {
cfg.Autoupdate = configSetOpts.Autoupdate
update = true
}
if cmd.Flags().Changed("telemetry") {
cfg.Telemetry.Enabled = configSetOpts.Telemetry
update = true
}
if !update {
return cmd.Help()
}

slog.Debug("updating config")
err := config.SaveConfig(cfg.Filename, cfg)
if err != nil {
return err
}
return nil
},
}

var configSetOpts struct {
Autoupdate bool
Telemetry bool
}

func init() {
configCmd.AddCommand(configSetCmd)
configSetCmd.Flags().BoolVar(&configSetOpts.Autoupdate, "autoupdate", true, "enable/disable autoupdate")
configSetCmd.Flags().BoolVar(&configSetOpts.Telemetry, "telemetry", true, "enable/disable telemetry")
}
4 changes: 4 additions & 0 deletions components/local-app/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"log/slog"
"net/url"
"os"
"strings"
"time"

"github.com/bufbuild/connect-go"
Expand All @@ -37,6 +38,9 @@ var loginCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

if !strings.HasPrefix(loginOpts.Host, "http") {
loginOpts.Host = "https://" + loginOpts.Host
}
csweichel marked this conversation as resolved.
Show resolved Hide resolved
host, err := url.Parse(loginOpts.Host)
if err != nil {
return fmt.Errorf("cannot parse host %s: %w", loginOpts.Host, err)
Expand Down
9 changes: 8 additions & 1 deletion components/local-app/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/gitpod-io/local-app/pkg/config"
"github.com/gitpod-io/local-app/pkg/constants"
"github.com/gitpod-io/local-app/pkg/prettyprint"
"github.com/gitpod-io/local-app/pkg/selfupdate"
"github.com/gitpod-io/local-app/pkg/telemetry"
"github.com/gookit/color"
"github.com/lmittmann/tint"
Expand Down Expand Up @@ -97,9 +98,15 @@ var rootCmd = &cobra.Command{
if gpctx, err := cfg.GetActiveContext(); err == nil && gpctx != nil {
telemetryEnabled = telemetryEnabled && gpctx.Host.String() == "https://gitpod.io"
}
telemetry.Init(telemetryEnabled, cfg.Telemetry.Identity, constants.Version)
telemetry.Init(telemetryEnabled, cfg.Telemetry.Identity, constants.Version.String())
telemetry.RecordCommand(cmd)

waitForUpdate := selfupdate.Autoupdate(cmd.Context(), cfg)
cmd.PostRunE = func(cmd *cobra.Command, args []string) error {
waitForUpdate()
return nil
}

return nil
},
}
Expand Down
35 changes: 35 additions & 0 deletions components/local-app/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.

package cmd

import (
"time"

"github.com/gitpod-io/local-app/pkg/constants"
"github.com/gitpod-io/local-app/pkg/prettyprint"
"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the CLI version",
RunE: func(cmd *cobra.Command, args []string) error {
type Version struct {
Version string `print:"version"`
GitCommit string `print:"commit"`
BuildTime string `print:"built at"`
}
v := Version{
Version: constants.Version.String(),
GitCommit: constants.GitCommit,
BuildTime: constants.MustParseBuildTime().Format(time.RFC3339),
}
return WriteTabular([]Version{v}, formatOpts{}, prettyprint.WriterFormatNarrow)
},
}

func init() {
rootCmd.AddCommand(versionCmd)
}
5 changes: 5 additions & 0 deletions components/local-app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ require (
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/binarydist v0.1.0 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/sftp v1.13.5 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sanbornm/go-selfupdate v0.0.0-20230714125711-e1c03e3d6ac7 // indirect
github.com/segmentio/backo-go v1.0.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.2.0 // indirect
Expand All @@ -70,6 +73,7 @@ require (
)

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/danieljoos/wincred v1.1.0 // indirect
Expand All @@ -81,6 +85,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/melbahja/goph v1.4.0
github.com/opencontainers/go-digest v1.0.0
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/segmentio/analytics-go/v3 v3.3.0
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37 // indirect
Expand Down
10 changes: 10 additions & 0 deletions components/local-app/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/local-app/leeway.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
FROM cgr.dev/chainguard/wolfi-base:latest@sha256:a8c9c2888304e62c133af76f520c9c9e6b3ce6f1a45e3eaa57f6639eb8053c90

WORKDIR /app
COPY components-local-app--app/bin/* ./
COPY components-local-app--app-with-manifest/bin/* ./

ARG __GIT_COMMIT
ARG VERSION
Expand Down
4 changes: 2 additions & 2 deletions components/local-app/main/gitpod-local-companion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
Usage: "connect your Gitpod workspaces",
Action: DefaultCommand("run"),
EnableBashCompletion: true,
Version: constants.Version,
Version: constants.Version.String(),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "gitpod-host",
Expand Down Expand Up @@ -283,7 +283,7 @@ func tryConnectToServer(gitpodUrl string, tkn string, reconnectionHandler func()
CloseHandler: closeHandler,
ExtraHeaders: map[string]string{
"User-Agent": "gitpod/local-companion",
"X-Client-Version": constants.Version,
"X-Client-Version": constants.Version.String(),
},
})
if err != nil {
Expand Down
Loading
Loading