|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/replicatedhq/embedded-cluster/pkg/metrics" |
| 14 | + "github.com/replicatedhq/embedded-cluster/pkg/prompts" |
| 15 | + "github.com/replicatedhq/embedded-cluster/pkg/release" |
| 16 | + kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1" |
| 17 | + "github.com/sirupsen/logrus" |
| 18 | + "github.com/urfave/cli/v2" |
| 19 | +) |
| 20 | + |
| 21 | +// maybePromptForAppUpdate warns the user if the embedded release is not the latest for the current |
| 22 | +// channel. If prompts are enabled, it will prompt the user to continue installing the out-of-date |
| 23 | +// release and return an error if the user chooses not to continue. |
| 24 | +func maybePromptForAppUpdate(c *cli.Context, prompt prompts.Prompt, license *kotsv1beta1.License) error { |
| 25 | + // It is not possible to check for app updates in airgap mode. |
| 26 | + if isAirgap := c.String("airgap-bundle") != ""; isAirgap { |
| 27 | + return nil |
| 28 | + } |
| 29 | + |
| 30 | + channelRelease, err := release.GetChannelRelease() |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf("unable to get channel release: %w", err) |
| 33 | + } else if channelRelease == nil { |
| 34 | + // It is possible to install without embedding the release data. In this case, we cannot |
| 35 | + // check for app updates. |
| 36 | + return nil |
| 37 | + } |
| 38 | + |
| 39 | + if license == nil { |
| 40 | + return errors.New("license required") |
| 41 | + } |
| 42 | + |
| 43 | + logrus.Debugf("Checking for pending app releases") |
| 44 | + |
| 45 | + currentRelease, err := getCurrentAppChannelRelease(c.Context, license, channelRelease.ChannelID) |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("get current app channel release: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + // In the dev and test environments, the channelSequence is set to 0 for all releases. |
| 51 | + if channelRelease.VersionLabel == currentRelease.VersionLabel { |
| 52 | + logrus.Debugf("Current app release is up-to-date") |
| 53 | + return nil |
| 54 | + } |
| 55 | + logrus.Debugf("Current app release is out-of-date") |
| 56 | + |
| 57 | + apiURL := metrics.BaseURL(license) |
| 58 | + releaseURL := fmt.Sprintf("%s/embedded/%s/%s", apiURL, channelRelease.AppSlug, channelRelease.ChannelSlug) |
| 59 | + logrus.Warnf("A newer version %s is available.", currentRelease.VersionLabel) |
| 60 | + logrus.Infof( |
| 61 | + "To download it, run:\n curl -fL \"%s\" \\\n -H \"Authorization: %s\" \\\n -o %s-%s.tgz", |
| 62 | + releaseURL, |
| 63 | + license.Spec.LicenseID, |
| 64 | + channelRelease.AppSlug, |
| 65 | + channelRelease.ChannelSlug, |
| 66 | + ) |
| 67 | + |
| 68 | + // if no-prompt is true, we don't prompt the user and continue by default. |
| 69 | + if !c.Bool("no-prompt") { |
| 70 | + text := fmt.Sprintf("Do you want to continue installing %s anyway?", channelRelease.VersionLabel) |
| 71 | + if !prompt.Confirm(text, true) { |
| 72 | + return ErrNothingElseToAdd |
| 73 | + } |
| 74 | + } |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +type apiChannelRelease struct { |
| 79 | + ChannelID string `json:"channelId"` |
| 80 | + ChannelSequence int64 `json:"channelSequence"` |
| 81 | + ReleaseSequence int64 `json:"releaseSequence"` |
| 82 | + VersionLabel string `json:"versionLabel"` |
| 83 | + IsRequired bool `json:"isRequired"` |
| 84 | + SemVer string `json:"semver,omitempty"` |
| 85 | + CreatedAt string `json:"createdAt"` |
| 86 | + ReleaseNotes string `json:"releaseNotes"` |
| 87 | + ReplicatedRegistryDomain string `json:"replicatedRegistryDomain"` |
| 88 | + ReplicatedProxyDomain string `json:"replicatedProxyDomain"` |
| 89 | +} |
| 90 | + |
| 91 | +func getCurrentAppChannelRelease(ctx context.Context, license *kotsv1beta1.License, channelID string) (*apiChannelRelease, error) { |
| 92 | + query := url.Values{} |
| 93 | + query.Set("selectedChannelId", channelID) |
| 94 | + query.Set("channelSequence", "") // sending an empty string will return the latest channel release |
| 95 | + query.Set("isSemverSupported", "true") |
| 96 | + |
| 97 | + apiURL := metrics.BaseURL(license) |
| 98 | + url := fmt.Sprintf("%s/release/%s/pending?%s", apiURL, license.Spec.AppSlug, query.Encode()) |
| 99 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("create request: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + auth := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", license.Spec.LicenseID, license.Spec.LicenseID)))) |
| 105 | + req.Header.Set("Authorization", auth) |
| 106 | + |
| 107 | + // This will use the proxy from the environment if set by the cli command. |
| 108 | + client := &http.Client{ |
| 109 | + Timeout: 10 * time.Second, |
| 110 | + } |
| 111 | + resp, err := client.Do(req) |
| 112 | + if err != nil { |
| 113 | + return nil, fmt.Errorf("get pending app releases: %w", err) |
| 114 | + } |
| 115 | + defer resp.Body.Close() |
| 116 | + |
| 117 | + if resp.StatusCode != http.StatusOK { |
| 118 | + return nil, fmt.Errorf("unexpected status code %s", resp.Status) |
| 119 | + } |
| 120 | + |
| 121 | + var releases struct { |
| 122 | + ChannelReleases []apiChannelRelease `json:"channelReleases"` |
| 123 | + } |
| 124 | + if err := json.NewDecoder(resp.Body).Decode(&releases); err != nil { |
| 125 | + return nil, fmt.Errorf("decode pending app releases: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + if len(releases.ChannelReleases) == 0 { |
| 129 | + return nil, errors.New("no app releases found") |
| 130 | + } |
| 131 | + |
| 132 | + return &releases.ChannelReleases[0], nil |
| 133 | +} |
0 commit comments