Skip to content

Commit 7b5a78c

Browse files
committed
Merge remote-tracking branch 'origin/main' into template-settings-fixes/kira-pilot
2 parents 8b8882f + 7a71180 commit 7b5a78c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+505
-52
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ build: site/out/index.html $(shell find . -not -path './vendor/*' -type f -name
5656
.PHONY: build
5757

5858
# Runs migrations to output a dump of the database.
59-
coderd/database/dump.sql: $(wildcard coderd/database/migrations/*.sql)
59+
coderd/database/dump.sql: coderd/database/dump/main.go $(wildcard coderd/database/migrations/*.sql)
6060
go run coderd/database/dump/main.go
6161

6262
# Generates Go code for querying the database.

agent/agent.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,15 @@ func (a *agent) createCommand(ctx context.Context, rawCommand string, env []stri
404404
unixExecutablePath := strings.ReplaceAll(executablePath, "\\", "/")
405405
cmd.Env = append(cmd.Env, fmt.Sprintf(`GIT_SSH_COMMAND=%s gitssh --`, unixExecutablePath))
406406

407+
// Set SSH connection environment variables (these are also set by OpenSSH
408+
// and thus expected to be present by SSH clients). Since the agent does
409+
// networking in-memory, trying to provide accurate values here would be
410+
// nonsensical. For now, we hard code these values so that they're present.
411+
srcAddr, srcPort := "0.0.0.0", "0"
412+
dstAddr, dstPort := "0.0.0.0", "0"
413+
cmd.Env = append(cmd.Env, fmt.Sprintf("SSH_CLIENT=%s %s %s", srcAddr, srcPort, dstPort))
414+
cmd.Env = append(cmd.Env, fmt.Sprintf("SSH_CONNECTION=%s %s %s %s", srcAddr, srcPort, dstAddr, dstPort))
415+
407416
// Load environment variables passed via the agent.
408417
// These should override all variables we manually specify.
409418
for envKey, value := range metadata.EnvironmentVariables {
@@ -441,6 +450,8 @@ func (a *agent) handleSSHSession(session ssh.Session) (retErr error) {
441450
sshPty, windowSize, isPty := session.Pty()
442451
if isPty {
443452
cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", sshPty.Term))
453+
454+
// The pty package sets `SSH_TTY` on supported platforms.
444455
ptty, process, err := pty.Start(cmd)
445456
if err != nil {
446457
return xerrors.Errorf("start command: %w", err)
@@ -801,7 +812,9 @@ func (r *reconnectingPTY) Close() {
801812
_ = conn.Close()
802813
}
803814
_ = r.ptty.Close()
815+
r.circularBufferMutex.Lock()
804816
r.circularBuffer.Reset()
817+
r.circularBufferMutex.Unlock()
805818
r.timeout.Stop()
806819
}
807820

agent/agent_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,29 @@ func TestAgent(t *testing.T) {
252252
}
253253
})
254254

255+
t.Run("SSH connection env vars", func(t *testing.T) {
256+
t.Parallel()
257+
258+
// Note: the SSH_TTY environment variable should only be set for TTYs.
259+
// For some reason this test produces a TTY locally and a non-TTY in CI
260+
// so we don't test for the absence of SSH_TTY.
261+
for _, key := range []string{"SSH_CONNECTION", "SSH_CLIENT"} {
262+
key := key
263+
t.Run(key, func(t *testing.T) {
264+
t.Parallel()
265+
266+
session := setupSSHSession(t, agent.Metadata{})
267+
command := "sh -c 'echo $" + key + "'"
268+
if runtime.GOOS == "windows" {
269+
command = "cmd.exe /c echo %" + key + "%"
270+
}
271+
output, err := session.Output(command)
272+
require.NoError(t, err)
273+
require.NotEmpty(t, strings.TrimSpace(string(output)))
274+
})
275+
}
276+
})
277+
255278
t.Run("StartupScript", func(t *testing.T) {
256279
t.Parallel()
257280
tempPath := filepath.Join(t.TempDir(), "content.txt")

cli/clitest/clitest.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ import (
2121
// New creates a CLI instance with a configuration pointed to a
2222
// temporary testing directory.
2323
func New(t *testing.T, args ...string) (*cobra.Command, config.Root) {
24-
cmd := cli.Root(cli.AGPL())
24+
return NewWithSubcommands(t, cli.AGPL(), args...)
25+
}
26+
27+
func NewWithSubcommands(
28+
t *testing.T, subcommands []*cobra.Command, args ...string,
29+
) (*cobra.Command, config.Root) {
30+
cmd := cli.Root(subcommands)
2531
dir := t.TempDir()
2632
root := config.Root(dir)
2733
cmd.SetArgs(append([]string{"--global-config", dir}, args...))

cli/configssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func configSSH() *cobra.Command {
158158
),
159159
Args: cobra.ExactArgs(0),
160160
RunE: func(cmd *cobra.Command, _ []string) error {
161-
client, err := createClient(cmd)
161+
client, err := CreateClient(cmd)
162162
if err != nil {
163163
return err
164164
}

cli/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func create() *cobra.Command {
2727
Use: "create [name]",
2828
Short: "Create a workspace from a template",
2929
RunE: func(cmd *cobra.Command, args []string) error {
30-
client, err := createClient(cmd)
30+
client, err := CreateClient(cmd)
3131
if err != nil {
3232
return err
3333
}

cli/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func deleteWorkspace() *cobra.Command {
2828
return err
2929
}
3030

31-
client, err := createClient(cmd)
31+
client, err := CreateClient(cmd)
3232
if err != nil {
3333
return err
3434
}

cli/features.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func featuresList() *cobra.Command {
3636
Use: "list",
3737
Aliases: []string{"ls"},
3838
RunE: func(cmd *cobra.Command, args []string) error {
39-
client, err := createClient(cmd)
39+
client, err := CreateClient(cmd)
4040
if err != nil {
4141
return err
4242
}

cli/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func list() *cobra.Command {
6565
Aliases: []string{"ls"},
6666
Args: cobra.ExactArgs(0),
6767
RunE: func(cmd *cobra.Command, args []string) error {
68-
client, err := createClient(cmd)
68+
client, err := CreateClient(cmd)
6969
if err != nil {
7070
return err
7171
}

cli/logout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func logout() *cobra.Command {
1616
Use: "logout",
1717
Short: "Remove the local authenticated session",
1818
RunE: func(cmd *cobra.Command, args []string) error {
19-
client, err := createClient(cmd)
19+
client, err := CreateClient(cmd)
2020
if err != nil {
2121
return err
2222
}

cli/parameterslist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func parameterList() *cobra.Command {
2222
RunE: func(cmd *cobra.Command, args []string) error {
2323
scope, name := args[0], args[1]
2424

25-
client, err := createClient(cmd)
25+
client, err := CreateClient(cmd)
2626
if err != nil {
2727
return err
2828
}

cli/portforward.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func portForward() *cobra.Command {
7070
return xerrors.New("no port-forwards requested")
7171
}
7272

73-
client, err := createClient(cmd)
73+
client, err := CreateClient(cmd)
7474
if err != nil {
7575
return err
7676
}

cli/publickey.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func publickey() *cobra.Command {
2020
Aliases: []string{"pubkey"},
2121
Short: "Output your public key for Git operations",
2222
RunE: func(cmd *cobra.Command, args []string) error {
23-
client, err := createClient(cmd)
23+
client, err := CreateClient(cmd)
2424
if err != nil {
2525
return xerrors.Errorf("create codersdk client: %w", err)
2626
}

cli/root.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func Root(subcommands []*cobra.Command) *cobra.Command {
114114
return nil
115115
}
116116

117-
client, err := createClient(cmd)
117+
client, err := CreateClient(cmd)
118118
// If the client is unauthenticated we can ignore the check.
119119
// The child commands should handle an unauthenticated client.
120120
if xerrors.Is(err, errUnauthenticated) {
@@ -190,9 +190,9 @@ func isTest() bool {
190190
return flag.Lookup("test.v") != nil
191191
}
192192

193-
// createClient returns a new client from the command context.
193+
// CreateClient returns a new client from the command context.
194194
// It reads from global configuration files if flags are not set.
195-
func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
195+
func CreateClient(cmd *cobra.Command) (*codersdk.Client, error) {
196196
root := createConfig(cmd)
197197
rawURL, err := cmd.Flags().GetString(varURL)
198198
if err != nil || rawURL == "" {
@@ -226,7 +226,7 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
226226
}
227227

228228
// createAgentClient returns a new client from the command context.
229-
// It works just like createClient, but uses the agent token and URL instead.
229+
// It works just like CreateClient, but uses the agent token and URL instead.
230230
func createAgentClient(cmd *cobra.Command) (*codersdk.Client, error) {
231231
rawURL, err := cmd.Flags().GetString(varAgentURL)
232232
if err != nil {

cli/schedule.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func scheduleShow() *cobra.Command {
7777
Long: scheduleShowDescriptionLong,
7878
Args: cobra.ExactArgs(1),
7979
RunE: func(cmd *cobra.Command, args []string) error {
80-
client, err := createClient(cmd)
80+
client, err := CreateClient(cmd)
8181
if err != nil {
8282
return err
8383
}
@@ -106,7 +106,7 @@ func scheduleStart() *cobra.Command {
106106
Long: scheduleStartDescriptionLong,
107107
Args: cobra.RangeArgs(2, 4),
108108
RunE: func(cmd *cobra.Command, args []string) error {
109-
client, err := createClient(cmd)
109+
client, err := CreateClient(cmd)
110110
if err != nil {
111111
return err
112112
}
@@ -156,7 +156,7 @@ func scheduleStop() *cobra.Command {
156156
Short: "Edit workspace stop schedule",
157157
Long: scheduleStopDescriptionLong,
158158
RunE: func(cmd *cobra.Command, args []string) error {
159-
client, err := createClient(cmd)
159+
client, err := CreateClient(cmd)
160160
if err != nil {
161161
return err
162162
}
@@ -207,7 +207,7 @@ func scheduleOverride() *cobra.Command {
207207
return err
208208
}
209209

210-
client, err := createClient(cmd)
210+
client, err := CreateClient(cmd)
211211
if err != nil {
212212
return xerrors.Errorf("create client: %w", err)
213213
}

cli/show.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func show() *cobra.Command {
1414
Short: "Show details of a workspace's resources and agents",
1515
Args: cobra.ExactArgs(1),
1616
RunE: func(cmd *cobra.Command, args []string) error {
17-
client, err := createClient(cmd)
17+
client, err := CreateClient(cmd)
1818
if err != nil {
1919
return err
2020
}

cli/ssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func ssh() *cobra.Command {
5454
ctx, cancel := context.WithCancel(cmd.Context())
5555
defer cancel()
5656

57-
client, err := createClient(cmd)
57+
client, err := CreateClient(cmd)
5858
if err != nil {
5959
return err
6060
}

cli/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func start() *cobra.Command {
1717
Short: "Build a workspace with the start state",
1818
Args: cobra.ExactArgs(1),
1919
RunE: func(cmd *cobra.Command, args []string) error {
20-
client, err := createClient(cmd)
20+
client, err := CreateClient(cmd)
2121
if err != nil {
2222
return err
2323
}

cli/state.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func statePull() *cobra.Command {
2727
Use: "pull <workspace> [file]",
2828
Args: cobra.MinimumNArgs(1),
2929
RunE: func(cmd *cobra.Command, args []string) error {
30-
client, err := createClient(cmd)
30+
client, err := CreateClient(cmd)
3131
if err != nil {
3232
return err
3333
}
@@ -68,7 +68,7 @@ func statePush() *cobra.Command {
6868
Use: "push <workspace> <file>",
6969
Args: cobra.ExactArgs(2),
7070
RunE: func(cmd *cobra.Command, args []string) error {
71-
client, err := createClient(cmd)
71+
client, err := CreateClient(cmd)
7272
if err != nil {
7373
return err
7474
}

cli/stop.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func stop() *cobra.Command {
2525
return err
2626
}
2727

28-
client, err := createClient(cmd)
28+
client, err := CreateClient(cmd)
2929
if err != nil {
3030
return err
3131
}

cli/templatecreate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func templateCreate() *cobra.Command {
3232
Short: "Create a template from the current directory or as specified by flag",
3333
Args: cobra.MaximumNArgs(1),
3434
RunE: func(cmd *cobra.Command, args []string) error {
35-
client, err := createClient(cmd)
35+
client, err := CreateClient(cmd)
3636
if err != nil {
3737
return err
3838
}

cli/templatedelete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func templateDelete() *cobra.Command {
2323
templates = []codersdk.Template{}
2424
)
2525

26-
client, err := createClient(cmd)
26+
client, err := CreateClient(cmd)
2727
if err != nil {
2828
return err
2929
}

cli/templateedit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func templateEdit() *cobra.Command {
2525
Args: cobra.ExactArgs(1),
2626
Short: "Edit the metadata of a template by name.",
2727
RunE: func(cmd *cobra.Command, args []string) error {
28-
client, err := createClient(cmd)
28+
client, err := CreateClient(cmd)
2929
if err != nil {
3030
return xerrors.Errorf("create client: %w", err)
3131
}

cli/templateinit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func templateInit() *cobra.Command {
3636

3737
_, _ = fmt.Fprintln(cmd.OutOrStdout(), cliui.Styles.Wrap.Render(
3838
"A template defines infrastructure as code to be provisioned "+
39-
"for individual developer workspaces. Select an example to get started:\n"))
39+
"for individual developer workspaces. Select an example to be copied to the active directory:\n"))
4040
option, err := cliui.Select(cmd, cliui.SelectOptions{
4141
Options: exampleNames,
4242
})

cli/templatelist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func templateList() *cobra.Command {
1616
Short: "List all the templates available for the organization",
1717
Aliases: []string{"ls"},
1818
RunE: func(cmd *cobra.Command, args []string) error {
19-
client, err := createClient(cmd)
19+
client, err := CreateClient(cmd)
2020
if err != nil {
2121
return err
2222
}

cli/templatepull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func templatePull() *cobra.Command {
2929
dest = args[1]
3030
}
3131

32-
client, err := createClient(cmd)
32+
client, err := CreateClient(cmd)
3333
if err != nil {
3434
return xerrors.Errorf("create client: %w", err)
3535
}

cli/templatepush.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func templatePush() *cobra.Command {
2929
Args: cobra.MaximumNArgs(1),
3030
Short: "Push a new template version from the current directory or as specified by flag",
3131
RunE: func(cmd *cobra.Command, args []string) error {
32-
client, err := createClient(cmd)
32+
client, err := CreateClient(cmd)
3333
if err != nil {
3434
return err
3535
}

cli/templateversions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func templateVersionsList() *cobra.Command {
3838
Args: cobra.ExactArgs(1),
3939
Short: "List all the versions of the specified template",
4040
RunE: func(cmd *cobra.Command, args []string) error {
41-
client, err := createClient(cmd)
41+
client, err := CreateClient(cmd)
4242
if err != nil {
4343
return xerrors.Errorf("create client: %w", err)
4444
}

cli/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func update() *cobra.Command {
2222
Args: cobra.ExactArgs(1),
2323
Short: "Update a workspace to the latest template version",
2424
RunE: func(cmd *cobra.Command, args []string) error {
25-
client, err := createClient(cmd)
25+
client, err := CreateClient(cmd)
2626
if err != nil {
2727
return err
2828
}

cli/usercreate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func userCreate() *cobra.Command {
2121
cmd := &cobra.Command{
2222
Use: "create",
2323
RunE: func(cmd *cobra.Command, args []string) error {
24-
client, err := createClient(cmd)
24+
client, err := CreateClient(cmd)
2525
if err != nil {
2626
return err
2727
}

cli/userlist.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func userList() *cobra.Command {
2626
Use: "list",
2727
Aliases: []string{"ls"},
2828
RunE: func(cmd *cobra.Command, args []string) error {
29-
client, err := createClient(cmd)
29+
client, err := CreateClient(cmd)
3030
if err != nil {
3131
return err
3232
}
@@ -76,7 +76,7 @@ func userSingle() *cobra.Command {
7676
),
7777
Args: cobra.ExactArgs(1),
7878
RunE: func(cmd *cobra.Command, args []string) error {
79-
client, err := createClient(cmd)
79+
client, err := CreateClient(cmd)
8080
if err != nil {
8181
return err
8282
}

0 commit comments

Comments
 (0)