Skip to content

Commit 55bec81

Browse files
committed
feat: autostart workspaces on ssh
1 parent 2e4e0b2 commit 55bec81

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

cli/configssh.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path/filepath"
1414
"runtime"
1515
"sort"
16+
"strconv"
1617
"strings"
1718

1819
"github.com/cli/safeexec"
@@ -46,9 +47,10 @@ const (
4647
// sshConfigOptions represents options that can be stored and read
4748
// from the coder config in ~/.ssh/coder.
4849
type sshConfigOptions struct {
49-
waitEnum string
50-
userHostPrefix string
51-
sshOptions []string
50+
waitEnum string
51+
userHostPrefix string
52+
sshOptions []string
53+
disableAutostart bool
5254
}
5355

5456
// addOptions expects options in the form of "option=value" or "option value".
@@ -106,7 +108,7 @@ func (o sshConfigOptions) equal(other sshConfigOptions) bool {
106108
if !slices.Equal(opt1, opt2) {
107109
return false
108110
}
109-
return o.waitEnum == other.waitEnum && o.userHostPrefix == other.userHostPrefix
111+
return o.waitEnum == other.waitEnum && o.userHostPrefix == other.userHostPrefix && o.disableAutostart == other.disableAutostart
110112
}
111113

112114
func (o sshConfigOptions) asList() (list []string) {
@@ -116,6 +118,9 @@ func (o sshConfigOptions) asList() (list []string) {
116118
if o.userHostPrefix != "" {
117119
list = append(list, fmt.Sprintf("ssh-host-prefix: %s", o.userHostPrefix))
118120
}
121+
if o.disableAutostart {
122+
list = append(list, fmt.Sprintf("disable-autostart: %v", o.disableAutostart))
123+
}
119124
for _, opt := range o.sshOptions {
120125
list = append(list, fmt.Sprintf("ssh-option: %s", opt))
121126
}
@@ -392,6 +397,9 @@ func (r *RootCmd) configSSH() *clibase.Cmd {
392397
if sshConfigOpts.waitEnum != "auto" {
393398
flags += " --wait=" + sshConfigOpts.waitEnum
394399
}
400+
if sshConfigOpts.disableAutostart {
401+
flags += " --disable-autostart=true"
402+
}
395403
defaultOptions = append(defaultOptions, fmt.Sprintf(
396404
"ProxyCommand %s --global-config %s ssh --stdio%s %s",
397405
escapedCoderBinary, escapedGlobalConfig, flags, workspaceHostname,
@@ -566,6 +574,13 @@ func (r *RootCmd) configSSH() *clibase.Cmd {
566574
Default: "auto",
567575
Value: clibase.EnumOf(&sshConfigOpts.waitEnum, "yes", "no", "auto"),
568576
},
577+
{
578+
Flag: "disable-autostart",
579+
Description: "Disable starting the workspace automatically when connecting via SSH.",
580+
Env: "CODER_CONFIGSSH_DISABLE_AUTOSTART",
581+
Value: clibase.BoolOf(&sshConfigOpts.disableAutostart),
582+
Default: "false",
583+
},
569584
{
570585
Flag: "force-unix-filepaths",
571586
Env: "CODER_CONFIGSSH_UNIX_FILEPATHS",
@@ -602,6 +617,9 @@ func sshConfigWriteSectionHeader(w io.Writer, addNewline bool, o sshConfigOption
602617
if o.userHostPrefix != "" {
603618
_, _ = fmt.Fprintf(&ow, "# :%s=%s\n", "ssh-host-prefix", o.userHostPrefix)
604619
}
620+
if o.disableAutostart {
621+
_, _ = fmt.Fprintf(&ow, "# :%s=%v\n", "disable-autostart", o.disableAutostart)
622+
}
605623
for _, opt := range o.sshOptions {
606624
_, _ = fmt.Fprintf(&ow, "# :%s=%s\n", "ssh-option", opt)
607625
}
@@ -634,6 +652,8 @@ func sshConfigParseLastOptions(r io.Reader) (o sshConfigOptions) {
634652
o.userHostPrefix = parts[1]
635653
case "ssh-option":
636654
o.sshOptions = append(o.sshOptions, parts[1])
655+
case "disable-autostart":
656+
o.disableAutostart, _ = strconv.ParseBool(parts[1])
637657
default:
638658
// Unknown option, ignore.
639659
}

cli/ssh.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ var (
4444

4545
func (r *RootCmd) ssh() *clibase.Cmd {
4646
var (
47-
stdio bool
48-
forwardAgent bool
49-
forwardGPG bool
50-
identityAgent string
51-
wsPollInterval time.Duration
52-
waitEnum string
53-
noWait bool
54-
logDirPath string
55-
remoteForward string
47+
stdio bool
48+
forwardAgent bool
49+
forwardGPG bool
50+
identityAgent string
51+
wsPollInterval time.Duration
52+
waitEnum string
53+
noWait bool
54+
logDirPath string
55+
remoteForward string
56+
disableAutostart bool
5657
)
5758
client := new(codersdk.Client)
5859
cmd := &clibase.Cmd{
@@ -459,6 +460,13 @@ func (r *RootCmd) ssh() *clibase.Cmd {
459460
FlagShorthand: "R",
460461
Value: clibase.StringOf(&remoteForward),
461462
},
463+
{
464+
Flag: "disable-autostart",
465+
Description: "Disable starting the workspace automatically when connecting via SSH.",
466+
Env: "CODER_SSH_DISABLE_AUTOSTART",
467+
Value: clibase.BoolOf(&disableAutostart),
468+
Default: "false",
469+
},
462470
}
463471
return cmd
464472
}

0 commit comments

Comments
 (0)