Skip to content

Commit 67dda0d

Browse files
committed
Fix Windows support
1 parent 1c753dc commit 67dda0d

File tree

8 files changed

+36
-17
lines changed

8 files changed

+36
-17
lines changed

cli/cliui/prompt.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cliui
22

33
import (
44
"bufio"
5+
"bytes"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -62,7 +63,11 @@ func Prompt(cmd *cobra.Command, opts PromptOptions) (string, error) {
6263
var rawMessage json.RawMessage
6364
err := json.NewDecoder(pipeReader).Decode(&rawMessage)
6465
if err == nil {
65-
line = string(rawMessage)
66+
var buf bytes.Buffer
67+
err = json.Compact(&buf, rawMessage)
68+
if err == nil {
69+
line = buf.String()
70+
}
6671
}
6772
}
6873
}

cli/cliui/prompt_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ func TestPrompt(t *testing.T) {
9393
ptty.WriteLine(`{
9494
"test": "wow"
9595
}`)
96-
require.Equal(t, `{
97-
"test": "wow"
98-
}`, <-doneChan)
96+
require.Equal(t, `{"test":"wow"}`, <-doneChan)
9997
})
10098
}
10199

cli/ssh.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
"github.com/pion/webrtc/v3"
1212
"github.com/spf13/cobra"
1313
gossh "golang.org/x/crypto/ssh"
14-
"golang.org/x/crypto/ssh/terminal"
1514
"golang.org/x/xerrors"
1615

1716
"github.com/coder/coder/cli/cliflag"
1817
"github.com/coder/coder/cli/cliui"
1918
"github.com/coder/coder/coderd/database"
2019
"github.com/coder/coder/codersdk"
20+
"golang.org/x/crypto/ssh/terminal"
2121
)
2222

2323
func ssh() *cobra.Command {
@@ -121,18 +121,16 @@ func ssh() *cobra.Command {
121121
}
122122

123123
if isatty.IsTerminal(os.Stdout.Fd()) {
124-
state, err := terminal.MakeRaw(int(os.Stdout.Fd()))
124+
state, err := terminal.MakeRaw(int(os.Stdin.Fd()))
125125
if err != nil {
126126
return err
127127
}
128128
defer func() {
129-
_ = terminal.Restore(int(os.Stdout.Fd()), state)
129+
_ = terminal.Restore(int(os.Stdin.Fd()), state)
130130
}()
131131
}
132132

133-
err = sshSession.RequestPty("xterm-256color", 128, 128, gossh.TerminalModes{
134-
gossh.OCRNL: 1,
135-
})
133+
err = sshSession.RequestPty("xterm-256color", 128, 128, gossh.TerminalModes{})
136134
if err != nil {
137135
return err
138136
}

cli/start.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func start() *cobra.Command {
314314
cliflag.StringVarP(root.Flags(), &accessURL, "access-url", "", "CODER_ACCESS_URL", "", "Specifies the external URL to access Coder")
315315
cliflag.StringVarP(root.Flags(), &address, "address", "a", "CODER_ADDRESS", "127.0.0.1:3000", "The address to serve the API and dashboard")
316316
// systemd uses the CACHE_DIRECTORY environment variable!
317-
cliflag.StringVarP(root.Flags(), &cacheDir, "cache-dir", "", "CACHE_DIRECTORY", filepath.Join(os.TempDir(), ".coder-cache"), "Specifies a directory to cache binaries for provision operations.")
317+
cliflag.StringVarP(root.Flags(), &cacheDir, "cache-dir", "", "CACHE_DIRECTORY", filepath.Join(os.TempDir(), "coder-cache"), "Specifies a directory to cache binaries for provision operations.")
318318
cliflag.BoolVarP(root.Flags(), &dev, "dev", "", "CODER_DEV_MODE", false, "Serve Coder in dev mode for tinkering")
319319
cliflag.StringVarP(root.Flags(), &postgresURL, "postgres-url", "", "CODER_PG_CONNECTION_URL", "", "URL of a PostgreSQL database to connect to")
320320
cliflag.Uint8VarP(root.Flags(), &provisionerDaemonCount, "provisioner-daemons", "", "CODER_PROVISIONER_DAEMONS", 1, "The amount of provisioner daemons to create on start.")
@@ -370,6 +370,11 @@ func createFirstUser(cmd *cobra.Command, client *codersdk.Client, cfg config.Roo
370370
}
371371

372372
func newProvisionerDaemon(ctx context.Context, client *codersdk.Client, logger slog.Logger, cacheDir string) (*provisionerd.Server, error) {
373+
err := os.MkdirAll(cacheDir, 0700)
374+
if err != nil {
375+
return nil, xerrors.Errorf("mkdir %q: %w", cacheDir, err)
376+
}
377+
373378
terraformClient, terraformServer := provisionersdk.TransportPipe()
374379
go func() {
375380
err := terraform.Serve(ctx, &terraform.ServeOptions{

examples/gcp-linux/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Coder requires a Google Cloud Service Account to provision workspaces.
1818
- Service Account User
1919
3. Click on the created key, and navigate to the "Keys" tab.
2020
4. Click "Add key", then "Create new key".
21-
5. Generate a JSON private key, and paste the contents in \'\' quotes below.
21+
5. Generate a JSON private key, and paste the contents below.
2222
EOF
2323
sensitive = true
2424
}

examples/gcp-windows/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Coder requires a Google Cloud Service Account to provision workspaces.
1818
- Service Account User
1919
3. Click on the created key, and navigate to the "Keys" tab.
2020
4. Click "Add key", then "Create new key".
21-
5. Generate a JSON private key, and paste the contents in \'\' quotes below.
21+
5. Generate a JSON private key, and paste the contents below.
2222
EOF
2323
sensitive = true
2424
}

provisioner/terraform/provision.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12+
"runtime"
1213
"strings"
1314

1415
"github.com/awalterschulze/gographviz"
@@ -87,7 +88,9 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
8788
})
8889
}
8990
}()
90-
if t.cachePath != "" {
91+
// Windows doesn't work with a plugin cache directory.
92+
// The cause is unknown, but it should work.
93+
if t.cachePath != "" && runtime.GOOS != "windows" {
9194
err = terraform.SetEnv(map[string]string{
9295
"TF_PLUGIN_CACHE_DIR": t.cachePath,
9396
})

provisioner/terraform/serve.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package terraform
22

33
import (
44
"context"
5-
"os/exec"
5+
"path/filepath"
66

77
"github.com/hashicorp/go-version"
88
"golang.org/x/xerrors"
99

1010
"cdr.dev/slog"
1111

12+
"github.com/cli/safeexec"
1213
"github.com/coder/coder/provisionersdk"
1314

1415
"github.com/hashicorp/hc-install/product"
@@ -41,7 +42,7 @@ type ServeOptions struct {
4142
// Serve starts a dRPC server on the provided transport speaking Terraform provisioner.
4243
func Serve(ctx context.Context, options *ServeOptions) error {
4344
if options.BinaryPath == "" {
44-
binaryPath, err := exec.LookPath("terraform")
45+
binaryPath, err := safeexec.LookPath("terraform")
4546
if err != nil {
4647
installer := &releases.ExactVersion{
4748
InstallDir: options.CachePath,
@@ -55,7 +56,16 @@ func Serve(ctx context.Context, options *ServeOptions) error {
5556
}
5657
options.BinaryPath = execPath
5758
} else {
58-
options.BinaryPath = binaryPath
59+
// If the "coder" binary is in the same directory as
60+
// the "terraform" binary, "terraform" is returned.
61+
//
62+
// We must resolve the absolute path for other processes
63+
// to execute this properly!
64+
absoluteBinary, err := filepath.Abs(binaryPath)
65+
if err != nil {
66+
return xerrors.Errorf("absolute: %w", err)
67+
}
68+
options.BinaryPath = absoluteBinary
5969
}
6070
}
6171
return provisionersdk.Serve(ctx, &terraform{

0 commit comments

Comments
 (0)