-
Notifications
You must be signed in to change notification settings - Fork 224
/
update_nomad.go
110 lines (88 loc) · 2.82 KB
/
update_nomad.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package image
import (
"context"
"errors"
"fmt"
"strings"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/internal/watch"
"github.com/superfly/flyctl/iostreams"
)
func updateImageForNomad(ctx context.Context) error {
var (
client = client.FromContext(ctx).API()
io = iostreams.FromContext(ctx)
appName = appconfig.NameFromContext(ctx)
autoConfirm = flag.GetBool(ctx, "yes")
)
app, err := client.GetImageInfo(ctx, appName)
if err != nil {
return fmt.Errorf("failed to get image info: %w", err)
}
if !app.ImageVersionTrackingEnabled {
return errors.New("image is not eligible for automated image updates")
}
if !app.ImageUpgradeAvailable {
return errors.New("image is already running the latest image")
}
cI := app.ImageDetails
lI := app.LatestImageDetails
current := cI.FullImageRef()
target := lI.FullImageRef()
if cI.Version != "" {
current = fmt.Sprintf("%s %s", current, cI.Version)
}
if lI.Version != "" {
target = fmt.Sprintf("%s %s", target, lI.Version)
}
if !autoConfirm {
switch confirmed, err := prompt.Confirmf(ctx, "Update `%s` from %s to %s?", appName, current, target); {
case err == nil:
if !confirmed {
return nil
}
case prompt.IsNonInteractive(err):
return prompt.NonInteractiveError("yes flag must be specified when not running interactively")
default:
return err
}
}
input := api.DeployImageInput{
AppID: appName,
Image: lI.FullImageRef(),
Strategy: api.StringPointer("ROLLING"),
}
// Set the deployment strategy
if val := flag.GetString(ctx, "strategy"); val != "" {
input.Strategy = api.StringPointer(strings.ReplaceAll(strings.ToUpper(val), "-", "_"))
}
release, releaseCommand, err := client.DeployImage(ctx, input)
if err != nil {
return err
}
fmt.Fprintf(io.Out, "Release v%d created\n", release.Version)
if releaseCommand != nil {
fmt.Fprintln(io.Out, "Release command detected: this new release will not be available until the command succeeds.")
}
fmt.Fprintln(io.Out)
tb := render.NewTextBlock(ctx)
tb.Detail("You can detach the terminal anytime without stopping the update")
if releaseCommand != nil {
// TODO: don't use text block here
tb := render.NewTextBlock(ctx, fmt.Sprintf("Release command detected: %s\n", releaseCommand.Command))
tb.Done("This release will not be available until the release command succeeds.")
if err := watch.ReleaseCommand(ctx, appName, releaseCommand.ID); err != nil {
return err
}
release, err = client.GetAppReleaseNomad(ctx, appName, release.ID)
if err != nil {
return err
}
}
return watch.Deployment(ctx, appName, release.EvaluationID)
}