Skip to content

Commit b4ca95d

Browse files
committed
Make proxysever cmd
1 parent a2c6d28 commit b4ca95d

File tree

6 files changed

+90
-9
lines changed

6 files changed

+90
-9
lines changed

cli/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,7 @@ type CommonServerCmd struct {
11281128
closeFuncs []func()
11291129
}
11301130

1131+
// Close closes all the resources associated with the CommonServerCmd.
11311132
func (c *CommonServerCmd) Close() {
11321133
for _, f := range c.closeFuncs {
11331134
f()
@@ -1138,6 +1139,8 @@ func (c *CommonServerCmd) addClose(f func()) {
11381139
c.closeFuncs = append(c.closeFuncs, f)
11391140
}
11401141

1142+
// SetupServerCmd sets up the common elements of starting a server daemon.
1143+
// This is used by both coderd and workspace proxies.
11411144
func SetupServerCmd(inv *clibase.Invocation, cfg *codersdk.DeploymentValues) (_ *CommonServerCmd, err error) {
11421145
c := &CommonServerCmd{}
11431146

enterprise/cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type RootCmd struct {
1212
func (r *RootCmd) enterpriseOnly() []*clibase.Cmd {
1313
return []*clibase.Cmd{
1414
r.server(),
15+
r.workspaceProxy(),
1516
r.features(),
1617
r.licenses(),
1718
r.groups(),

enterprise/cli/workspaceproxy.go

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !slim
2+
13
package cli
24

35
import (
@@ -7,6 +9,7 @@ import (
79
"log"
810
"net"
911
"net/http"
12+
"net/url"
1013
"runtime/pprof"
1114
"time"
1215

@@ -34,12 +37,44 @@ func (r *RootCmd) workspaceProxy() *clibase.Cmd {
3437
},
3538
Children: []*clibase.Cmd{
3639
r.proxyServer(),
40+
r.registerProxy(),
3741
},
3842
}
3943

4044
return cmd
4145
}
4246

47+
func (r *RootCmd) registerProxy() *clibase.Cmd {
48+
client := new(codersdk.Client)
49+
cmd := &clibase.Cmd{
50+
Use: "register",
51+
Short: "Register a workspace proxy",
52+
Middleware: clibase.Chain(
53+
clibase.RequireNArgs(1),
54+
r.InitClient(client),
55+
),
56+
Handler: func(i *clibase.Invocation) error {
57+
ctx := i.Context()
58+
name := i.Args[0]
59+
// TODO: Fix all this
60+
resp, err := client.CreateWorkspaceProxy(ctx, codersdk.CreateWorkspaceProxyRequest{
61+
Name: name,
62+
DisplayName: name,
63+
Icon: "whocares.png",
64+
URL: "http://localhost:6005",
65+
WildcardHostname: "",
66+
})
67+
if err != nil {
68+
return xerrors.Errorf("create workspace proxy: %w", err)
69+
}
70+
71+
fmt.Println(resp.ProxyToken)
72+
return nil
73+
},
74+
}
75+
return cmd
76+
}
77+
4378
func (r *RootCmd) proxyServer() *clibase.Cmd {
4479
var (
4580
// TODO: Remove options that we do not need
@@ -50,8 +85,9 @@ func (r *RootCmd) proxyServer() *clibase.Cmd {
5085

5186
client := new(codersdk.Client)
5287
cmd := &clibase.Cmd{
53-
Use: "server",
54-
Short: "Start a workspace proxy server",
88+
Use: "server",
89+
Short: "Start a workspace proxy server",
90+
Options: opts,
5591
Middleware: clibase.Chain(
5692
cli.WriteConfigMW(cfg),
5793
cli.PrintDeprecatedOptions(),
@@ -67,14 +103,16 @@ func (r *RootCmd) proxyServer() *clibase.Cmd {
67103
defer scd.Close()
68104
ctx := scd.Ctx
69105

106+
pu, _ := url.Parse("http://localhost:3000")
70107
proxy, err := wsproxy.New(&wsproxy.Options{
71108
Logger: scd.Logger,
72109
// TODO: PrimaryAccessURL
73-
PrimaryAccessURL: nil,
74-
AccessURL: cfg.AccessURL.Value(),
75-
AppHostname: scd.AppHostname,
76-
AppHostnameRegex: scd.AppHostnameRegex,
77-
RealIPConfig: scd.RealIPConfig,
110+
PrimaryAccessURL: pu,
111+
AccessURL: cfg.AccessURL.Value(),
112+
AppHostname: scd.AppHostname,
113+
AppHostnameRegex: scd.AppHostnameRegex,
114+
RealIPConfig: scd.RealIPConfig,
115+
// TODO: AppSecurityKey
78116
AppSecurityKey: workspaceapps.SecurityKey{},
79117
Tracing: scd.Tracer,
80118
PrometheusRegistry: scd.PrometheusRegistry,
@@ -85,6 +123,9 @@ func (r *RootCmd) proxyServer() *clibase.Cmd {
85123
// TODO: ProxySessionToken
86124
ProxySessionToken: "",
87125
})
126+
if err != nil {
127+
return xerrors.Errorf("create workspace proxy: %w", err)
128+
}
88129

89130
shutdownConnsCtx, shutdownConns := context.WithCancel(ctx)
90131
defer shutdownConns()
@@ -110,7 +151,7 @@ func (r *RootCmd) proxyServer() *clibase.Cmd {
110151

111152
// TODO: So this obviously is not going to work well.
112153
errCh := make(chan error, 1)
113-
pprof.Do(ctx, pprof.Labels("service", "workspace-proxy"), func(ctx context.Context) {
154+
go pprof.Do(ctx, pprof.Labels("service", "workspace-proxy"), func(ctx context.Context) {
114155
errCh <- scd.HTTPServers.Serve(httpServer)
115156
})
116157

@@ -128,7 +169,6 @@ func (r *RootCmd) proxyServer() *clibase.Cmd {
128169
var exitErr error
129170
select {
130171
case exitErr = <-errCh:
131-
fmt.Println("As")
132172
case <-scd.NotifyCtx.Done():
133173
exitErr = scd.NotifyCtx.Err()
134174
_, _ = fmt.Fprintln(inv.Stdout, cliui.Styles.Bold.Render(

enterprise/cli/workspaceproxy_slim.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build slim
2+
3+
package cli
4+
5+
import (
6+
"fmt"
7+
"io"
8+
"os"
9+
10+
"github.com/coder/coder/cli/clibase"
11+
"github.com/coder/coder/cli/cliui"
12+
)
13+
14+
func (r *RootCmd) workspaceProxy() *clibase.Cmd {
15+
root := &clibase.Cmd{
16+
Use: "workspace-proxy",
17+
Short: "Manage workspace proxies",
18+
Aliases: []string{"proxy"},
19+
// We accept RawArgs so all commands and flags are accepted.
20+
RawArgs: true,
21+
Hidden: true,
22+
Handler: func(inv *clibase.Invocation) error {
23+
serverUnsupported(inv.Stderr)
24+
return nil
25+
},
26+
}
27+
28+
return root
29+
}
30+
31+
func serverUnsupported(w io.Writer) {
32+
_, _ = fmt.Fprintf(w, "You are using a 'slim' build of Coder, which does not support the %s subcommand.\n", cliui.Styles.Code.Render("server"))
33+
_, _ = fmt.Fprintln(w, "")
34+
_, _ = fmt.Fprintln(w, "Please use a build of Coder from GitHub releases:")
35+
_, _ = fmt.Fprintln(w, " https://github.com/coder/coder/releases")
36+
os.Exit(1)
37+
}

0 commit comments

Comments
 (0)