-
Notifications
You must be signed in to change notification settings - Fork 290
/
up.go
173 lines (143 loc) · 4.54 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
package cli
import (
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/fatih/color"
opentracing "github.com/opentracing/opentracing-go"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"github.com/windmilleng/tilt/internal/build"
"github.com/windmilleng/tilt/internal/engine"
"github.com/windmilleng/tilt/internal/hud"
"github.com/windmilleng/tilt/internal/logger"
"github.com/windmilleng/tilt/internal/model"
"github.com/windmilleng/tilt/internal/store"
"github.com/windmilleng/tilt/internal/tiltfile"
"github.com/windmilleng/tilt/internal/tracer"
)
var updateModeFlag string = string(engine.UpdateModeAuto)
var logActionsFlag bool = false
type upCmd struct {
watch bool
browserMode string
traceTags string
hud bool
autoDeploy bool
port int
fileName string
}
func (c *upCmd) register() *cobra.Command {
cmd := &cobra.Command{
Use: "up [<name>] [<name2>] [...]",
Short: "stand up one or more manifests",
}
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(&c.browserMode, "browser", "", "deprecated. TODO(nick): remove this flag")
cmd.Flags().StringVar(&updateModeFlag, "update-mode", string(engine.UpdateModeAuto),
fmt.Sprintf("Control the strategy Tilt uses for updating instances. Possible values: %v", engine.AllUpdateModes))
cmd.Flags().StringVar(&c.traceTags, "traceTags", "", "tags to add to spans for easy querying, of the form: key1=val1,key2=val2")
cmd.Flags().StringVar(&build.ImageTagPrefix, "image-tag-prefix", build.ImageTagPrefix,
"For integration tests. Customize the image tag prefix so tests can write to a public registry")
cmd.Flags().BoolVar(&c.hud, "hud", true, "If true, tilt will open in HUD mode.")
cmd.Flags().BoolVar(&c.autoDeploy, "auto-deploy", true, "If false, tilt will wait on <spacebar> to trigger builds")
cmd.Flags().BoolVar(&logActionsFlag, "logactions", false, "log all actions and state changes")
cmd.Flags().IntVar(&c.port, "port", 0, "Port for the Tilt HTTP server")
cmd.Flags().Lookup("logactions").Hidden = true
cmd.Flags().StringVar(&c.fileName, "file", tiltfile.FileName, "Path to Tiltfile")
err := cmd.Flags().MarkHidden("image-tag-prefix")
if err != nil {
panic(err)
}
err = cmd.Flags().MarkHidden("browser")
if err != nil {
panic(err)
}
return cmd
}
func (c *upCmd) run(ctx context.Context, args []string) error {
analyticsService.Incr("cmd.up", map[string]string{
"watch": fmt.Sprintf("%v", c.watch),
"mode": string(updateModeFlag),
})
defer analyticsService.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)
}
threads, err := wireThreads(ctx)
if err != nil {
return err
}
upper := threads.upper
h := threads.hud
server := threads.server
l := engine.NewLogActionLogger(ctx, upper.Dispatch)
ctx = logger.WithLogger(ctx, l)
log.SetOutput(l.Writer(logger.InfoLvl))
logOutput(fmt.Sprintf("Starting Tilt (%s)…", buildStamp()))
if isAnalyticsDisabledFromEnv() {
logOutput("Tilt analytics manually disabled by environment")
}
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 c.hud {
g.Go(func() error {
return h.Run(ctx, upper.Dispatch, hud.DefaultRefreshInterval)
})
}
if c.port != 0 {
http.Handle("/", server.Router())
httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", c.port),
Handler: http.DefaultServeMux,
}
g.Go(func() error {
<-ctx.Done()
return httpServer.Shutdown(context.Background())
})
g.Go(func() error {
err := httpServer.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
return err
}
return nil
})
}
triggerMode := model.TriggerAuto
if !c.autoDeploy {
triggerMode = model.TriggerManual
}
g.Go(func() error {
defer cancel()
return upper.Start(ctx, args, c.watch, triggerMode, c.fileName, c.hud)
})
err = g.Wait()
if err != context.Canceled {
return err
} else {
return nil
}
}
func logOutput(s string) {
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
log.Printf(color.GreenString(s))
}
func provideUpdateModeFlag() engine.UpdateModeFlag {
return engine.UpdateModeFlag(updateModeFlag)
}
func provideLogActions() store.LogActionsFlag {
return store.LogActionsFlag(logActionsFlag)
}