-
Notifications
You must be signed in to change notification settings - Fork 9
/
attach.go
185 lines (154 loc) · 5.23 KB
/
attach.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cmd
import (
"context"
"net"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/cli/keyboard"
"namespacelabs.dev/foundation/internal/devsession"
"namespacelabs.dev/foundation/internal/executor"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/logs/logtail"
"namespacelabs.dev/foundation/internal/observers"
"namespacelabs.dev/foundation/internal/planning/deploy/view"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/schema/storage"
"namespacelabs.dev/foundation/std/cfg"
)
func NewAttachCmd() *cobra.Command {
var res hydrateResult
var servingAddr string
var useWebUI bool
return fncobra.
Cmd(&cobra.Command{
Use: "attach [path/to/server]...",
Short: "Attaches to the specified environment, of the specified servers.",
Args: cobra.ArbitraryArgs}).
WithFlags(func(flags *pflag.FlagSet) {
flags.StringVarP(&servingAddr, "listen", "H", "", "webui: listen on the specified address.")
flags.BoolVar(&useWebUI, "webui", useWebUI, "If true, exposes a web UI as well.")
}).
With(parseHydrationWithDeps(&res, &fncobra.ParseLocationsOpts{ReturnAllIfNoneSpecified: true}, &hydrateOpts{rehydrate: true})...).
Do(func(ctx context.Context) error {
event := &observers.StackUpdateEvent{
Env: res.Env.Environment(),
Stack: res.Stack,
Focus: schema.Strs(res.Focus...),
Deployed: true,
DeployedRevision: 1,
}
observer := observers.Static()
observer.PushUpdate(event)
cluster, err := runtime.NamespaceFor(ctx, res.Env)
if err != nil {
return err
}
return keyboard.Handle(ctx, keyboard.HandleOpts{
Provider: observer,
Keybindings: []keyboard.Handler{
view.NetworkPlanKeybinding{Name: "ingress"},
logtail.Keybinding{
DefaultPaused: useWebUI,
LoadEnvironment: func(name string) (cfg.Context, error) {
if name == res.Env.Environment().Name {
return res.Env, nil
}
return nil, fnerrors.InternalError("requested invalid environment: %s", name)
},
},
},
Handler: func(ctx context.Context) error {
pfwd := devsession.NewPortFwd(ctx, nil, res.Env, cluster, "localhost")
pfwd.OnUpdate = func(plan *storage.NetworkPlan) {
event := protos.Clone(event)
event.NetworkPlan = plan
observer.PushUpdate(event)
}
pfwd.Update(res.Stack, res.Focus, res.Ingress)
if !useWebUI {
return nil
}
lis, err := startListener(servingAddr)
if err != nil {
return fnerrors.InternalError("Failed to start listener at %q: %w", servingAddr, err)
}
defer lis.Close()
r := mux.NewRouter()
fncobra.RegisterPprof(r)
devsession.RegisterSomeEndpoints(attachSessionLike{cluster, res.Stack, event}, r)
mux, err := devsession.PrebuiltWebUI(ctx)
if err != nil {
return err
}
r.PathPrefix("/").Handler(mux)
srv := &http.Server{
Handler: r,
Addr: servingAddr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
BaseContext: func(l net.Listener) context.Context { return ctx },
}
eg := executor.New(ctx, "attach")
eg.Go(func(ctx context.Context) error {
// On cancelation, i.e. Ctrl-C, ask the server to shutdown. This will lead to the next go-routine below, actually returns.
<-ctx.Done()
ctxT, cancelT := context.WithTimeout(ctx, 1*time.Second)
defer cancelT()
return srv.Shutdown(ctxT)
})
eg.Go(func(ctx context.Context) error {
updateWebUISticky(ctx, "running at: http://%s", lis.Addr())
return srv.Serve(lis)
})
return eg.Wait()
},
})
})
}
type attachSessionLike struct {
cluster runtime.ClusterNamespace
stack *schema.Stack
event *observers.StackUpdateEvent
}
func (s attachSessionLike) ResolveServer(ctx context.Context, serverID string) (runtime.ClusterNamespace, runtime.Deployable, error) {
entry := s.stack.GetServerByID(serverID)
if entry != nil {
return s.cluster, entry.Server, nil
}
return nil, nil, fnerrors.New("%s: no such server in the current session", serverID)
}
func (s attachSessionLike) NewClient(needsHistory bool) (devsession.ObserverLike, error) {
ch := make(chan *devsession.Update, 1)
ch <- &devsession.Update{
StackUpdate: &devsession.Stack{
Revision: s.event.DeployedRevision,
Env: s.event.Env,
Stack: s.event.Stack,
Focus: s.event.Focus,
NetworkPlan: s.event.NetworkPlan,
Deployed: s.event.Deployed,
},
}
return attachedObserver{ch}, nil
}
func (s attachSessionLike) DeferRequest(req *devsession.DevWorkflowRequest) {
// Ignoring.
}
type attachedObserver struct {
ch chan *devsession.Update
}
func (a attachedObserver) Events() chan *devsession.Update {
return a.ch
}
func (a attachedObserver) Close() {
// XXX
}