|
1 | 1 | package cli
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "github.com/cli/safeexec" |
4 | 12 | "github.com/spf13/cobra"
|
| 13 | + "golang.org/x/sync/errgroup" |
| 14 | + "golang.org/x/xerrors" |
| 15 | + |
| 16 | + "github.com/coder/coder/cli/cliflag" |
| 17 | + "github.com/coder/coder/cli/cliui" |
| 18 | + "github.com/coder/coder/codersdk" |
5 | 19 | )
|
6 | 20 |
|
7 |
| -// const sshStartToken = "# ------------START-CODER-----------" |
8 |
| -// const sshStartMessage = `# This was generated by "coder config-ssh". |
9 |
| -// # |
10 |
| -// # To remove this blob, run: |
11 |
| -// # |
12 |
| -// # coder config-ssh --remove |
13 |
| -// # |
14 |
| -// # You should not hand-edit this section, unless you are deleting it.` |
15 |
| -// const sshEndToken = "# ------------END-CODER------------" |
| 21 | +const sshStartToken = "# ------------START-CODER-----------" |
| 22 | +const sshStartMessage = `# This was generated by "coder config-ssh". |
| 23 | +# |
| 24 | +# To remove this blob, run: |
| 25 | +# |
| 26 | +# coder config-ssh --remove |
| 27 | +# |
| 28 | +# You should not hand-edit this section, unless you are deleting it.` |
| 29 | +const sshEndToken = "# ------------END-CODER------------" |
16 | 30 |
|
17 | 31 | func configSSH() *cobra.Command {
|
| 32 | + var ( |
| 33 | + sshConfigFile string |
| 34 | + ) |
18 | 35 | cmd := &cobra.Command{
|
19 | 36 | Use: "config-ssh",
|
20 | 37 | RunE: func(cmd *cobra.Command, args []string) error {
|
| 38 | + client, err := createClient(cmd) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + if strings.HasPrefix(sshConfigFile, "~/") { |
| 43 | + dirname, _ := os.UserHomeDir() |
| 44 | + sshConfigFile = filepath.Join(dirname, sshConfigFile[2:]) |
| 45 | + } |
| 46 | + // Doesn't matter if this fails, because we write the file anyways. |
| 47 | + sshConfigContentRaw, _ := os.ReadFile(sshConfigFile) |
| 48 | + sshConfigContent := string(sshConfigContentRaw) |
| 49 | + startIndex := strings.Index(sshConfigContent, sshStartToken) |
| 50 | + endIndex := strings.Index(sshConfigContent, sshEndToken) |
| 51 | + if startIndex != -1 && endIndex != -1 { |
| 52 | + sshConfigContent = sshConfigContent[:startIndex-1] + sshConfigContent[endIndex+len(sshEndToken):] |
| 53 | + } |
| 54 | + |
| 55 | + workspaces, err := client.WorkspacesByUser(cmd.Context(), "") |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + binPath, err := currentBinPath(cmd) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + sshConfigContent += "\n" + sshStartToken + "\n" + sshStartMessage + "\n\n" |
| 65 | + sshConfigContentMutex := sync.Mutex{} |
| 66 | + var errGroup errgroup.Group |
| 67 | + for _, workspace := range workspaces { |
| 68 | + workspace := workspace |
| 69 | + errGroup.Go(func() error { |
| 70 | + resources, err := client.WorkspaceResourcesByBuild(cmd.Context(), workspace.LatestBuild.ID) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + resourcesWithAgents := make([]codersdk.WorkspaceResource, 0) |
| 75 | + for _, resource := range resources { |
| 76 | + if resource.Agent == nil { |
| 77 | + continue |
| 78 | + } |
| 79 | + resourcesWithAgents = append(resourcesWithAgents, resource) |
| 80 | + } |
| 81 | + sshConfigContentMutex.Lock() |
| 82 | + defer sshConfigContentMutex.Unlock() |
| 83 | + if len(resourcesWithAgents) == 1 { |
| 84 | + sshConfigContent += strings.Join([]string{ |
| 85 | + "Host coder." + workspace.Name, |
| 86 | + "\tHostName coder." + workspace.Name, |
| 87 | + fmt.Sprintf("\tProxyCommand %q ssh --stdio %s", binPath, workspace.Name), |
| 88 | + "\tConnectTimeout=0", |
| 89 | + "\tStrictHostKeyChecking=no", |
| 90 | + }, "\n") + "\n" |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | + }) |
| 95 | + } |
| 96 | + err = errGroup.Wait() |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + sshConfigContent += "\n" + sshEndToken |
| 101 | + err = os.MkdirAll(filepath.Dir(sshConfigFile), os.ModePerm) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + err = os.WriteFile(sshConfigFile, []byte(sshConfigContent), os.ModePerm) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + _, _ = fmt.Printf("An auto-generated ssh config was written to \"%s\"\n", sshConfigFile) |
| 110 | + _, _ = fmt.Println("You should now be able to ssh into your workspace") |
| 111 | + _, _ = fmt.Printf("For example, try running\n\n\t$ ssh coder.%s\n\n", workspaces[0].Name) |
21 | 112 | return nil
|
22 | 113 | },
|
23 | 114 | }
|
| 115 | + cliflag.StringVarP(cmd.Flags(), &sshConfigFile, "ssh-config-file", "", "CODER_SSH_CONFIG_FILE", "~/.ssh/config", "Specifies the path to an SSH config.") |
24 | 116 |
|
25 | 117 | return cmd
|
26 | 118 | }
|
| 119 | + |
| 120 | +// currentBinPath returns the path to the coder binary suitable for use in ssh |
| 121 | +// ProxyCommand. |
| 122 | +func currentBinPath(cmd *cobra.Command) (string, error) { |
| 123 | + exePath, err := os.Executable() |
| 124 | + if err != nil { |
| 125 | + return "", xerrors.Errorf("get executable path: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + binName := filepath.Base(exePath) |
| 129 | + // We use safeexec instead of os/exec because os/exec returns paths in |
| 130 | + // the current working directory, which we will run into very often when |
| 131 | + // looking for our own path. |
| 132 | + pathPath, err := safeexec.LookPath(binName) |
| 133 | + // On Windows, the coder-cli executable must be in $PATH for both Msys2/Git |
| 134 | + // Bash and OpenSSH for Windows (used by Powershell and VS Code) to function |
| 135 | + // correctly. Check if the current executable is in $PATH, and warn the user |
| 136 | + // if it isn't. |
| 137 | + if err != nil && runtime.GOOS == "windows" { |
| 138 | + cliui.Warn(cmd.OutOrStdout(), |
| 139 | + "The current executable is not in $PATH.", |
| 140 | + "This may lead to problems connecting to your workspace via SSH.", |
| 141 | + fmt.Sprintf("Please move %q to a location in your $PATH (such as System32) and run `%s config-ssh` again.", binName, binName), |
| 142 | + ) |
| 143 | + // Return the exePath so SSH at least works outside of Msys2. |
| 144 | + return exePath, nil |
| 145 | + } |
| 146 | + |
| 147 | + // Warn the user if the current executable is not the same as the one in |
| 148 | + // $PATH. |
| 149 | + if filepath.Clean(pathPath) != filepath.Clean(exePath) { |
| 150 | + cliui.Warn(cmd.OutOrStdout(), |
| 151 | + "The current executable path does not match the executable path found in $PATH.", |
| 152 | + "This may cause issues connecting to your workspace via SSH.", |
| 153 | + fmt.Sprintf("\tCurrent executable path: %q", exePath), |
| 154 | + fmt.Sprintf("\tExecutable path in $PATH: %q", pathPath), |
| 155 | + ) |
| 156 | + } |
| 157 | + |
| 158 | + return binName, nil |
| 159 | +} |
0 commit comments