/
up.go
276 lines (230 loc) · 8.21 KB
/
up.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package cli
import (
"context"
"fmt"
"log"
"net/url"
"os"
"time"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
"github.com/opentracing/opentracing-go"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"k8s.io/klog"
"github.com/windmilleng/tilt/internal/analytics"
"github.com/windmilleng/tilt/internal/cloud"
engineanalytics "github.com/windmilleng/tilt/internal/engine/analytics"
"github.com/windmilleng/tilt/internal/engine/buildcontrol"
"github.com/windmilleng/tilt/internal/hud"
"github.com/windmilleng/tilt/internal/k8s"
"github.com/windmilleng/tilt/internal/store"
"github.com/windmilleng/tilt/internal/tracer"
"github.com/windmilleng/tilt/pkg/assets"
"github.com/windmilleng/tilt/pkg/logger"
"github.com/windmilleng/tilt/pkg/model"
"github.com/windmilleng/tilt/web"
)
const DefaultWebHost = "localhost"
const DefaultWebPort = 10350
const DefaultWebDevPort = 46764
var updateModeFlag string = string(buildcontrol.UpdateModeAuto)
var webModeFlag model.WebMode = model.DefaultWebMode
var webPort = 0
var webHost = DefaultWebHost
var webDevPort = 0
var noBrowser bool = false
var logActionsFlag bool = false
type upCmd struct {
watch bool
traceTags string
fileName string
outputSnapshotOnExit string
defaultTUI bool
hud bool
// whether hud was explicitly set or just got the default value
hudFlagExplicitlySet bool
}
func (c *upCmd) register() *cobra.Command {
cmd := &cobra.Command{
Use: "up [<tilt flags>] [-- <Tiltfile args>]",
DisableFlagsInUseLine: true,
Short: "Start Tilt with the given Tiltfile args",
Long: `
Starts Tilt and runs services defined in the Tiltfile.
There are two types of args:
1) Tilt flags, listed below, which are handled entirely by Tilt.
2) Tiltfile args, which can be anything, and are potentially accessed by config.parse in your Tiltfile.
By default:
1) Tiltfile args are interpreted as the list of services to start, e.g. tilt up frontend backend.
2) Running with no Tiltfile args starts all services defined in the Tiltfile
This default behavior does not apply if the Tiltfile uses config.parse or config.set_enabled_resources.
In that case, see https://tilt.dev/user_config.html and/or comments in your Tiltfile
When you exit Tilt (using Ctrl+C), Kubernetes resources and Docker Compose resources continue running;
you can use tilt down (https://docs.tilt.dev/cli/tilt_down.html) to delete these resources. Any long-running
local resources--i.e. those using serve_cmd--are terminated when you exit Tilt.
`,
}
cmd.Flags().BoolVar(&c.watch, "watch", true, "If true, services will be automatically rebuilt and redeployed when files change. Otherwise, each service will be started once.")
cmd.Flags().StringVar(&updateModeFlag, "update-mode", string(buildcontrol.UpdateModeAuto),
fmt.Sprintf("Control the strategy Tilt uses for updating instances. Possible values: %v", buildcontrol.AllUpdateModes))
cmd.Flags().StringVar(&c.traceTags, "traceTags", "", "tags to add to spans for easy querying, of the form: key1=val1,key2=val2")
cmd.Flags().BoolVar(&c.hud, "hud", true, "If true, tilt will open in HUD mode.")
cmd.Flags().BoolVar(&logActionsFlag, "logactions", false, "log all actions and state changes")
addWebServerFlags(cmd)
addTiltfileFlag(cmd, &c.fileName)
cmd.Flags().Lookup("logactions").Hidden = true
cmd.Flags().BoolVar(&noBrowser, "no-browser", false, "If true, web UI will not open on startup.")
cmd.Flags().StringVar(&c.outputSnapshotOnExit, "output-snapshot-on-exit", "", "If specified, Tilt will dump a snapshot of its state to the specified path when it exits")
// this is to test the new behavior before enabling it in Tilt 1.0
// https://app.clubhouse.io/windmill/epic/5549/make-tui-hard-to-find-in-tilt-1-0
cmd.Flags().BoolVar(&c.defaultTUI, "default-hud", true, "If false, we'll hide the TUI by default")
cmd.Flags().Lookup("default-hud").Hidden = true
cmd.PreRun = func(cmd *cobra.Command, args []string) {
c.hudFlagExplicitlySet = cmd.Flag("hud").Changed
}
return cmd
}
func (c *upCmd) isHudEnabledByConfig() bool {
ret := c.hud
// in non-default-TUI mode, we only show the hud if the user explicitly specified --hud
if !c.defaultTUI && !c.hudFlagExplicitlySet {
ret = false
}
return ret
}
func (c *upCmd) run(ctx context.Context, args []string) error {
a := analytics.Get(ctx)
cmdUpTags := engineanalytics.CmdTags(map[string]string{
"watch": fmt.Sprintf("%v", c.watch),
"mode": string(updateModeFlag),
})
a.Incr("cmd.up", cmdUpTags.AsMap())
defer a.Flush(time.Second)
span, ctx := opentracing.StartSpanFromContext(ctx, "Up")
defer span.Finish()
tags := tracer.TagStrToMap(c.traceTags)
for k, v := range tags {
span.SetTag(k, v)
}
deferred := logger.NewDeferredLogger(ctx)
ctx = redirectLogs(ctx, deferred)
logOutput(fmt.Sprintf("Starting Tilt (%s)…", buildStamp()))
if ok, reason := analytics.IsAnalyticsDisabledFromEnv(); ok {
log.Printf("Tilt analytics disabled: %s", reason)
}
hudEnabled := c.isHudEnabledByConfig() && isatty.IsTerminal(os.Stdout.Fd())
cmdUpDeps, err := wireCmdUp(ctx, hud.HudEnabled(hudEnabled), a, cmdUpTags)
if err != nil {
deferred.SetOutput(deferred.Original())
return err
}
upper := cmdUpDeps.Upper
h := cmdUpDeps.Hud
l := store.NewLogActionLogger(ctx, upper.Dispatch)
deferred.SetOutput(l)
ctx = redirectLogs(ctx, l)
if c.outputSnapshotOnExit != "" {
defer cloud.WriteSnapshot(ctx, cmdUpDeps.Store, c.outputSnapshotOnExit)
}
if trace {
traceID, err := tracer.TraceID(ctx)
if err != nil {
return err
}
logger.Get(ctx).Infof("TraceID: %s", traceID)
}
g, ctx := errgroup.WithContext(ctx)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if hudEnabled {
g.Go(func() error {
err := h.Run(ctx, upper.Dispatch, hud.DefaultRefreshInterval)
return err
})
}
engineMode := store.EngineModeUp
if !c.watch {
engineMode = store.EngineModeApply
}
g.Go(func() error {
defer cancel()
return upper.Start(ctx, args, cmdUpDeps.TiltBuild, engineMode,
c.fileName, hudEnabled, a.UserOpt(), cmdUpDeps.Token, string(cmdUpDeps.CloudAddress))
})
err = g.Wait()
if err != context.Canceled {
return err
} else {
return nil
}
}
func redirectLogs(ctx context.Context, l logger.Logger) context.Context {
ctx = logger.WithLogger(ctx, l)
log.SetOutput(l.Writer(logger.InfoLvl))
klog.SetOutput(l.Writer(logger.InfoLvl))
return ctx
}
func logOutput(s string) {
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
log.Print(color.GreenString(s))
}
func provideUpdateModeFlag() buildcontrol.UpdateModeFlag {
return buildcontrol.UpdateModeFlag(updateModeFlag)
}
func provideLogActions() store.LogActionsFlag {
return store.LogActionsFlag(logActionsFlag)
}
func provideKubectlLogLevel() k8s.KubectlLogLevel {
return k8s.KubectlLogLevel(klogLevel)
}
func provideWebMode(b model.TiltBuild) (model.WebMode, error) {
switch webModeFlag {
case model.LocalWebMode, model.ProdWebMode, model.PrecompiledWebMode:
return webModeFlag, nil
case model.DefaultWebMode:
if b.Dev {
return model.LocalWebMode, nil
} else {
return model.ProdWebMode, nil
}
}
return "", model.UnrecognizedWebModeError(string(webModeFlag))
}
func provideWebHost() model.WebHost {
return model.WebHost(webHost)
}
func provideWebPort() model.WebPort {
return model.WebPort(webPort)
}
func provideNoBrowserFlag() model.NoBrowser {
return model.NoBrowser(noBrowser)
}
func provideWebURL(webHost model.WebHost, webPort model.WebPort) (model.WebURL, error) {
if webPort == 0 {
return model.WebURL{}, nil
}
u, err := url.Parse(fmt.Sprintf("http://%s:%d/", webHost, webPort))
if err != nil {
return model.WebURL{}, err
}
return model.WebURL(*u), nil
}
func provideAssetServer(mode model.WebMode, version model.WebVersion) (assets.Server, error) {
if mode == model.ProdWebMode {
return assets.NewProdServer(assets.ProdAssetBucket, version)
}
if mode == model.PrecompiledWebMode || mode == model.LocalWebMode {
path, err := web.StaticPath()
if err != nil {
return nil, err
}
pkgDir := assets.PackageDir(path)
if mode == model.PrecompiledWebMode {
return assets.NewPrecompiledServer(pkgDir), nil
} else {
return assets.NewDevServer(pkgDir, model.WebDevPort(webDevPort))
}
}
return nil, model.UnrecognizedWebModeError(string(mode))
}