Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 0c4351f

Browse files
committed
Use env.ssh_available field in config-ssh
1 parent db06ecc commit 0c4351f

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

coder-sdk/env.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Environment struct {
3232
LastOpenedAt time.Time `json:"last_opened_at" table:"-"`
3333
LastConnectionAt time.Time `json:"last_connection_at" table:"-"`
3434
AutoOffThreshold Duration `json:"auto_off_threshold" table:"-"`
35+
SSHAvailable bool `json:"ssh_available" table:"-"`
3536
}
3637

3738
// RebuildMessage defines the message shown when an Environment requires a rebuild for it can be accessed.

internal/cmd/configssh.go

+16-20
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"context"
55
"fmt"
66
"io/ioutil"
7-
"net"
87
"net/url"
98
"os"
109
"os/user"
1110
"path/filepath"
1211
"strings"
13-
"time"
1412

1513
"cdr.dev/coder-cli/pkg/clog"
1614

@@ -93,10 +91,6 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
9391
return err
9492
}
9593

96-
if !isSSHAvailable(ctx) {
97-
return xerrors.New("SSH is disabled or not available for your Coder Enterprise deployment.")
98-
}
99-
10094
user, err := client.Me(ctx)
10195
if err != nil {
10296
return xerrors.Errorf("fetch username: %w", err)
@@ -109,6 +103,18 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
109103
if len(envs) < 1 {
110104
return xerrors.New("no environments found")
111105
}
106+
107+
sshAvailable := false
108+
for _, env := range envs {
109+
if env.SSHAvailable {
110+
sshAvailable = true
111+
break
112+
}
113+
}
114+
if !sshAvailable {
115+
return xerrors.New("SSH is disabled or not available any environments in your Coder Enterprise deployment.")
116+
}
117+
112118
newConfig, err := makeNewConfigs(user.Username, envs, startToken, startMessage, endToken, privateKeyFilepath)
113119
if err != nil {
114120
return xerrors.Errorf("make new ssh configurations: %w", err)
@@ -161,6 +167,10 @@ func makeNewConfigs(userName string, envs []coder.Environment, startToken, start
161167

162168
newConfig := fmt.Sprintf("\n%s\n%s\n\n", startToken, startMsg)
163169
for _, env := range envs {
170+
if !env.SSHAvailable {
171+
continue
172+
}
173+
164174
newConfig += makeSSHConfig(hostname, userName, env.Name, privateKeyFilepath)
165175
}
166176
newConfig += fmt.Sprintf("\n%s\n", endToken)
@@ -181,20 +191,6 @@ func makeSSHConfig(host, userName, envName, privateKeyFilepath string) string {
181191
`, envName, host, userName, envName, privateKeyFilepath)
182192
}
183193

184-
func isSSHAvailable(ctx context.Context) bool {
185-
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
186-
defer cancel()
187-
188-
host, err := configuredHostname()
189-
if err != nil {
190-
return false
191-
}
192-
193-
var dialer net.Dialer
194-
_, err = dialer.DialContext(ctx, "tcp", net.JoinHostPort(host, "22"))
195-
return err == nil
196-
}
197-
198194
func configuredHostname() (string, error) {
199195
u, err := config.URL.Read()
200196
if err != nil {

0 commit comments

Comments
 (0)