Pass ssh flags to rsync · coder/sshcode@53fc8fa

@@ -13,6 +13,7 @@ import (

1313

"path/filepath"

1414

"runtime"

1515

"strconv"

16+

"strings"

1617

"time"

17181819

"github.com/pkg/browser"

@@ -58,41 +59,42 @@ More info: https://github.com/codercom/sshcode

58595960

const codeServerPath = "/tmp/codessh-code-server"

606161-

// Downloads the latest code-server and allows it to be executed.

62-

sshCmd := exec.Command("ssh",

63-

"-tt",

64-

host,

65-

`/bin/bash -c 'set -euxo pipefail || exit 1

66-

# Make sure any currently running code-server is gone so we can overwrite

67-

# the binary.

68-

pkill -9 `+filepath.Base(codeServerPath)+` || true

69-

wget -q https://codesrv-ci.cdr.sh/latest-linux -O `+codeServerPath+`

62+

downloadScript := `set -euxo pipefail || exit 1

63+7064

mkdir -p ~/.local/share/code-server

71-

cd `+filepath.Dir(codeServerPath)+`

65+

cd ` + filepath.Dir(codeServerPath) + `

7266

wget -N https://codesrv-ci.cdr.sh/latest-linux

73-

[ -f `+codeServerPath+` ] && rm `+codeServerPath+`

74-

ln latest-linux `+codeServerPath+`

75-

chmod +x `+codeServerPath+`

76-

'`,

67+

[ -f ` + codeServerPath + ` ] && rm ` + codeServerPath + `

68+

ln latest-linux ` + codeServerPath + `

69+

chmod +x ` + codeServerPath

70+

// Downloads the latest code-server and allows it to be executed.

71+

sshCmdStr := fmt.Sprintf("ssh" +

72+

" " + *sshFlags + " " +

73+

host + " /bin/bash",

7774

)

75+

sshCmd := exec.Command("sh", "-c", sshCmdStr)

7876

sshCmd.Stdout = os.Stdout

7977

sshCmd.Stderr = os.Stderr

78+

sshCmd.Stdin = strings.NewReader(downloadScript)

8079

err := sshCmd.Run()

8180

if err != nil {

82-

flog.Fatal("failed to update code-server: %v", err)

81+

flog.Fatal("failed to update code-server: %v\n---ssh cmd---\n%s\n---download script---\n%s", err,

82+

sshCmdStr,

83+

downloadScript,

84+

)

8385

}

84868587

if !*skipSyncFlag {

8688

start := time.Now()

8789

flog.Info("syncing settings")

88-

err = syncUserSettings(host, false)

90+

err = syncUserSettings(*sshFlags, host, false)

8991

if err != nil {

9092

flog.Fatal("failed to sync settings: %v", err)

9193

}

9294

flog.Info("synced settings in %s", time.Since(start))

93959496

flog.Info("syncing extensions")

95-

err = syncExtensions(host, false)

97+

err = syncExtensions(*sshFlags, host, false)

9698

if err != nil {

9799

flog.Fatal("failed to sync extensions: %v", err)

98100

}

@@ -105,7 +107,7 @@ chmod +x `+codeServerPath+`

105107

flog.Fatal("failed to find available port: %v", err)

106108

}

107109108-

sshCmdStr := fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'",

110+

sshCmdStr = fmt.Sprintf("ssh -tt -q -L %v %v %v 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'",

109111

localPort+":localhost:"+localPort, *sshFlags, host, dir, codeServerPath, localPort,

110112

)

111113

@@ -162,12 +164,12 @@ chmod +x `+codeServerPath+`

162164163165

flog.Info("synchronizing VS Code back to local")

164166165-

err = syncExtensions(host, true)

167+

err = syncExtensions(*sshFlags, host, true)

166168

if err != nil {

167169

flog.Fatal("failed to sync extensions back: %v", err)

168170

}

169171170-

err = syncUserSettings(host, true)

172+

err = syncUserSettings(*sshFlags, host, true)

171173

if err != nil {

172174

flog.Fatal("failed to user settigns extensions back: %v", err)

173175

}

@@ -235,7 +237,7 @@ func randomPort() (string, error) {

235237

return "", xerrors.Errorf("max number of tries exceeded: %d", maxTries)

236238

}

237239238-

func syncUserSettings(host string, back bool) error {

240+

func syncUserSettings(sshFlags string, host string, back bool) error {

239241

localConfDir, err := configDir()

240242

if err != nil {

241243

return err

@@ -252,10 +254,10 @@ func syncUserSettings(host string, back bool) error {

252254

}

253255254256

// Append "/" to have rsync copy the contents of the dir.

255-

return rsync(src, dest, "workspaceStorage", "logs", "CachedData")

257+

return rsync(src, dest, sshFlags, "workspaceStorage", "logs", "CachedData")

256258

}

257259258-

func syncExtensions(host string, back bool) error {

260+

func syncExtensions(sshFlags string, host string, back bool) error {

259261

localExtensionsDir, err := extensionsDir()

260262

if err != nil {

261263

return err

@@ -270,16 +272,17 @@ func syncExtensions(host string, back bool) error {

270272

dest, src = src, dest

271273

}

272274273-

return rsync(src, dest)

275+

return rsync(src, dest, sshFlags)

274276

}

275277276-

func rsync(src string, dest string, excludePaths ...string) error {

278+

func rsync(src string, dest string, sshFlags string, excludePaths ...string) error {

277279

excludeFlags := make([]string, len(excludePaths))

278280

for i, path := range excludePaths {

279281

excludeFlags[i] = "--exclude=" + path

280282

}

281283282284

cmd := exec.Command("rsync", append(excludeFlags, "-azvr",

285+

"-e", "ssh "+sshFlags,

283286

// Only update newer directories, and sync times

284287

// to keep things simple.

285288

"-u", "--times",