This repository was archived by the owner on Aug 30, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork18
chore: use access url from env resource pool#216
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
c669eb5
chore: use access url from env resource pool
cmoogad6830a
fixup! chore: use access url from env resource pool
cmoog6adc01b
fixup! chore: use access url from env resource pool
cmoogb646d95
fixup! chore: use access url from env resource pool
cmoog9010f77
fixup! chore: use access url from env resource pool
cmoog19e53c7
fix: use proper SSH enabled bool flag
cmoog8cd4a49
fixup! fix: use proper SSH enabled bool flag
cmoog4df1a57
fixup! fix: use proper SSH enabled bool flag
cmoog8f039a9
fixup! fix: use proper SSH enabled bool flag
cmoog4dea056
fixup! fix: use proper SSH enabled bool flag
cmoogFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
14 changes: 7 additions & 7 deletionscoder-sdk/env.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
13 changes: 11 additions & 2 deletionscoder-sdk/request.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletionscoder-sdk/resourcepools.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
23 changes: 22 additions & 1 deletioncoder-sdk/ws.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
73 changes: 23 additions & 50 deletionsinternal/cmd/configssh.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -4,17 +4,16 @@ import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/url" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
"strings" | ||
"cdr.dev/coder-cli/pkg/clog" | ||
"cdr.dev/coder-cli/coder-sdk" | ||
"cdr.dev/coder-cli/internal/coderutil" | ||
"cdr.dev/coder-cli/internal/config" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
@@ -103,20 +102,17 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st | ||
return xerrors.New("no environments found") | ||
} | ||
envsWithPools, err := coderutil.EnvsWithPool(ctx, client, envs) | ||
if err != nil { | ||
return xerrors.Errorf("resolve env pools: %w", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Should we keep canConnectSSH? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. It was kinda dumb anyway.... | ||
} | ||
if !sshAvailable(envsWithPools) { | ||
return xerrors.New("SSH is disabled or not available for any environments in your Coder Enterprise deployment.") | ||
} | ||
newConfig := makeNewConfigs(user.Username, envsWithPools, privateKeyFilepath) | ||
err = os.MkdirAll(filepath.Dir(*configpath), os.ModePerm) | ||
if err != nil { | ||
return xerrors.Errorf("make configuration directory: %w", err) | ||
@@ -159,42 +155,15 @@ func removeOldConfig(config string) (string, bool) { | ||
} | ||
// sshAvailable returns true if SSH is available for at least one environment. | ||
func sshAvailable(envs []coderutil.EnvWithPool) bool { | ||
for _, env := range envs { | ||
if env.Pool.SSHEnabled { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
func writeSSHKey(ctx context.Context, client *coder.Client, privateKeyPath string) error { | ||
key, err := client.SSHKey(ctx) | ||
if err != nil { | ||
@@ -203,23 +172,26 @@ func writeSSHKey(ctx context.Context, client *coder.Client, privateKeyPath strin | ||
return ioutil.WriteFile(privateKeyPath, []byte(key.PrivateKey), 0400) | ||
} | ||
func makeNewConfigs(userName string, envs []coderutil.EnvWithPool, privateKeyFilepath string) string { | ||
newConfig := fmt.Sprintf("\n%s\n%s\n\n", sshStartToken, sshStartMessage) | ||
for _, env := range envs { | ||
if !env.Pool.SSHEnabled { | ||
clog.LogWarn(fmt.Sprintf("SSH is not enabled for pool %q", env.Pool.Name), | ||
clog.BlankLine, | ||
clog.Tipf("ask an infrastructure administrator to enable SSH for this resource pool"), | ||
) | ||
continue | ||
} | ||
u, err := url.Parse(env.Pool.AccessURL) | ||
if err != nil { | ||
clog.LogWarn("invalid access url", clog.Causef("malformed url: %q", env.Pool.AccessURL)) | ||
continue | ||
} | ||
newConfig += makeSSHConfig(u.Host, userName, env.Env.Name, privateKeyFilepath) | ||
} | ||
newConfig += fmt.Sprintf("\n%s\n", sshEndToken) | ||
return newConfig | ||
} | ||
func makeSSHConfig(host, userName, envName, privateKeyFilepath string) string { | ||
@@ -235,6 +207,7 @@ func makeSSHConfig(host, userName, envName, privateKeyFilepath string) string { | ||
`, envName, host, userName, envName, privateKeyFilepath) | ||
} | ||
//nolint:deadcode,unused | ||
func configuredHostname() (string, error) { | ||
u, err := config.URL.Read() | ||
if err != nil { | ||
5 changes: 3 additions & 2 deletionsinternal/cmd/shell.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletionsinternal/coderutil/doc.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package coderutil providers utilities for high-level operations on coder-sdk entities. | ||
package coderutil |
59 changes: 59 additions & 0 deletionsinternal/coderutil/env.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package coderutil | ||
import ( | ||
"context" | ||
"net/url" | ||
"cdr.dev/coder-cli/coder-sdk" | ||
"golang.org/x/xerrors" | ||
"nhooyr.io/websocket" | ||
) | ||
// DialEnvWsep dials the executor endpoint using the https://github.com/cdr/wsep message protocol. | ||
// The proper resource pool access URL is used. | ||
func DialEnvWsep(ctx context.Context, client *coder.Client, env *coder.Environment) (*websocket.Conn, error) { | ||
resourcePool, err := client.ResourcePoolByID(ctx, env.ResourcePoolID) | ||
if err != nil { | ||
return nil, xerrors.Errorf("get env resource pool: %w", err) | ||
} | ||
accessURL, err := url.Parse(resourcePool.AccessURL) | ||
if err != nil { | ||
return nil, xerrors.Errorf("invalid resource pool access url: %w", err) | ||
} | ||
conn, err := client.DialWsep(ctx, accessURL, env.ID) | ||
if err != nil { | ||
return nil, xerrors.Errorf("dial websocket: %w", err) | ||
} | ||
return conn, nil | ||
} | ||
// EnvWithPool composes an Environment entity with its associated ResourcePool. | ||
type EnvWithPool struct { | ||
Env coder.Environment | ||
Pool coder.ResourcePool | ||
} | ||
// EnvsWithPool performs the composition of each Environment with its associated ResourcePool. | ||
func EnvsWithPool(ctx context.Context, client *coder.Client, envs []coder.Environment) ([]EnvWithPool, error) { | ||
pooledEnvs := make([]EnvWithPool, len(envs)) | ||
pools, err := client.ResourcePools(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
poolMap := make(map[string]coder.ResourcePool, len(pools)) | ||
for _, p := range pools { | ||
poolMap[p.ID] = p | ||
} | ||
for _, e := range envs { | ||
envPool, ok := poolMap[e.ResourcePoolID] | ||
if !ok { | ||
return nil, xerrors.Errorf("fetch env resource pool: %w", coder.ErrNotFound) | ||
} | ||
pooledEnvs = append(pooledEnvs, EnvWithPool{ | ||
Env: e, | ||
Pool: envPool, | ||
}) | ||
} | ||
return pooledEnvs, nil | ||
} |
3 changes: 2 additions & 1 deletioninternal/sync/singlefile.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletionsinternal/sync/sync.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.