-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Fix remote daemon upload without SFTP #8434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e254283
test: cover daemon upload without SFTP
34bbdf7
fix: upload remote daemon through SSH exec
beb9da1
test: avoid remote session helper name collisions
8a15cae
test: assert remote daemon upload transaction
0ff4f29
test: cover SSH uploads with StdinNull configured
2514438
fix: preserve file stdin across SSH configuration
3e17c3a
Merge remote-tracking branch 'origin/main' into issue-7851-synology-s…
87f68d6
test: cover cleanup after daemon finalization failure
c32014d
fix: clean failed remote daemon finalization
e2443e5
Merge remote-tracking branch 'origin/main' into issue-7851-synology-s…
8d64e47
Organize remote upload test fixture type
21d8376
Merge remote-tracking branch 'origin/main' into issue-7851-synology-s…
b8dd1b8
test: cover sanitized daemon upload errors
8e79c8f
fix: sanitize daemon upload launch errors
01ad6ca
Merge remote-tracking branch 'origin/main' into issue-7851-synology-s…
4c6bfb0
Merge remote-tracking branch 'origin/main' into issue-7851-synology-s…
austinywang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
107 changes: 107 additions & 0 deletions
107
...moteSession/Sources/CmuxRemoteSession/Session/RemoteSessionCoordinator+DaemonUpload.swift
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,107 @@ | ||
| internal import CmuxFoundation | ||
| internal import Foundation | ||
|
|
||
| // Installs cmuxd-remote through the same SSH exec channel used by bootstrap. | ||
| // No SFTP subsystem or remote scp executable is required: the local binary is | ||
| // streamed to `cat`, then the existing chmod-and-rename step publishes it | ||
| // atomically at the versioned destination. | ||
| extension RemoteSessionCoordinator { | ||
| func uploadRemoteDaemonBinaryLocked(localBinary: URL, location: RemoteDaemonInstallLocation) throws { | ||
| let remotePath = location.absolutePath | ||
| let remoteDirectory = location.directory | ||
| let remoteTempPath = "\(remotePath).tmp-\(UUID().uuidString.prefix(8))" | ||
| debugLog( | ||
| "remote.upload.begin transport=ssh-stdin local=\(localBinary.path) " + | ||
| "remoteTemp=\(remoteTempPath) remote=\(remotePath)" | ||
| ) | ||
|
|
||
| let mkdirScript = "mkdir -p \(remoteDirectory.shellSingleQuoted)" | ||
| let mkdirCommand = "sh -c \(mkdirScript.shellSingleQuoted)" | ||
| let mkdirResult: RemoteCommandResult | ||
| do { | ||
| mkdirResult = try sshExec( | ||
| arguments: sshCommonArguments(batchMode: true) + [configuration.destination, mkdirCommand], | ||
| timeout: 12 | ||
| ) | ||
| } catch { | ||
| throw NSError(domain: "cmux.remote.daemon", code: 30, userInfo: [ | ||
| NSLocalizedDescriptionKey: String( | ||
| localized: "remoteDaemon.upload.createDirectoryFailed", | ||
| defaultValue: "failed to create remote daemon directory" | ||
| ), | ||
| ]) | ||
| } | ||
| guard mkdirResult.status == 0 else { | ||
| let detail = Self.bestErrorLine(stderr: mkdirResult.stderr, stdout: mkdirResult.stdout) ?? | ||
| "ssh exited \(mkdirResult.status)" | ||
| throw NSError(domain: "cmux.remote.daemon", code: 30, userInfo: [ | ||
| NSLocalizedDescriptionKey: String( | ||
| localized: "remoteDaemon.upload.createDirectoryFailedWithDetail", | ||
| defaultValue: "failed to create remote daemon directory: \(detail)" | ||
| ), | ||
| ]) | ||
| } | ||
|
|
||
| let uploadScript = "cat > \(remoteTempPath.shellSingleQuoted)" | ||
| let uploadCommand = "sh -c \(uploadScript.shellSingleQuoted)" | ||
| let uploadResult: RemoteCommandResult | ||
| do { | ||
| uploadResult = try sshExec( | ||
| arguments: sshCommonArguments(batchMode: true) + [configuration.destination, uploadCommand], | ||
| stdinFile: localBinary, | ||
| timeout: 45 | ||
| ) | ||
| } catch { | ||
| cleanupUploadedRemotePaths([remoteTempPath]) | ||
| throw NSError(domain: "cmux.remote.daemon", code: 31, userInfo: [ | ||
| NSLocalizedDescriptionKey: String( | ||
| localized: "remoteDaemon.upload.transferFailed", | ||
| defaultValue: "failed to upload cmuxd-remote" | ||
| ), | ||
| ]) | ||
| } | ||
| guard uploadResult.status == 0 else { | ||
| cleanupUploadedRemotePaths([remoteTempPath]) | ||
| let detail = Self.bestErrorLine(stderr: uploadResult.stderr, stdout: uploadResult.stdout) ?? | ||
| "ssh exited \(uploadResult.status)" | ||
| throw NSError(domain: "cmux.remote.daemon", code: 31, userInfo: [ | ||
| NSLocalizedDescriptionKey: String( | ||
| localized: "remoteDaemon.upload.transferFailedWithDetail", | ||
| defaultValue: "failed to upload cmuxd-remote: \(detail)" | ||
| ), | ||
| ]) | ||
| } | ||
|
|
||
| let finalizeScript = """ | ||
| chmod 755 \(remoteTempPath.shellSingleQuoted) && \ | ||
| mv \(remoteTempPath.shellSingleQuoted) \(remotePath.shellSingleQuoted) | ||
| """ | ||
| let finalizeCommand = "sh -c \(finalizeScript.shellSingleQuoted)" | ||
| let finalizeResult: RemoteCommandResult | ||
| do { | ||
| finalizeResult = try sshExec( | ||
| arguments: sshCommonArguments(batchMode: true) + [configuration.destination, finalizeCommand], | ||
| timeout: 12 | ||
| ) | ||
| } catch { | ||
| cleanupUploadedRemotePaths([remoteTempPath]) | ||
| throw NSError(domain: "cmux.remote.daemon", code: 32, userInfo: [ | ||
| NSLocalizedDescriptionKey: String( | ||
| localized: "remoteDaemon.upload.installFailed", | ||
| defaultValue: "failed to install remote daemon binary" | ||
| ), | ||
| ]) | ||
| } | ||
| guard finalizeResult.status == 0 else { | ||
| cleanupUploadedRemotePaths([remoteTempPath]) | ||
| let detail = Self.bestErrorLine(stderr: finalizeResult.stderr, stdout: finalizeResult.stdout) ?? | ||
| "ssh exited \(finalizeResult.status)" | ||
| throw NSError(domain: "cmux.remote.daemon", code: 32, userInfo: [ | ||
| NSLocalizedDescriptionKey: String( | ||
| localized: "remoteDaemon.upload.installFailedWithDetail", | ||
| defaultValue: "failed to install remote daemon binary: \(detail)" | ||
| ), | ||
| ]) | ||
| } | ||
| } | ||
| } | ||
78 changes: 78 additions & 0 deletions
78
...Session/Sources/CmuxRemoteSession/Session/RemoteSessionCoordinator+ProcessExecution.swift
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,78 @@ | ||
| internal import Foundation | ||
|
|
||
| // Blocking SSH/SCP/dev-build execution through the injected process runner. | ||
| // Calls stay on the coordinator's serial utility queue; file-backed stdin lets | ||
| // large helper binaries flow through SSH without buffering them in memory. | ||
| extension RemoteSessionCoordinator { | ||
| func sshExec( | ||
| arguments: [String], | ||
| stdin: Data? = nil, | ||
| timeout: TimeInterval = 15 | ||
| ) throws -> RemoteCommandResult { | ||
| try runProcess( | ||
| executable: "/usr/bin/ssh", | ||
| arguments: arguments, | ||
| environment: configuration.sshProcessEnvironment, | ||
| stdin: stdin, | ||
| timeout: timeout | ||
| ) | ||
| } | ||
|
|
||
| func sshExec( | ||
| arguments: [String], | ||
| stdinFile: URL, | ||
| timeout: TimeInterval = 15 | ||
| ) throws -> RemoteCommandResult { | ||
| // A host or caller can configure StdinNull=yes; OpenSSH would then | ||
| // discard this file while `cat` still exits successfully. Its first | ||
| // option value wins, so pin file-backed execs before caller options. | ||
| let fileInputArguments = ["-o", "StdinNull=no"] + arguments | ||
| return try processRunner.run( | ||
| RemoteProcessRequest( | ||
| executable: "/usr/bin/ssh", | ||
| arguments: fileInputArguments, | ||
| environment: configuration.sshProcessEnvironment, | ||
| stdinFile: stdinFile, | ||
| timeout: timeout | ||
| ), | ||
| operation: nil | ||
| ) | ||
| } | ||
|
|
||
| func scpExec( | ||
| arguments: [String], | ||
| timeout: TimeInterval = 30, | ||
| operation: (any RemoteTransferCancelling)? = nil | ||
| ) throws -> RemoteCommandResult { | ||
| try runProcess( | ||
| executable: "/usr/bin/scp", | ||
| arguments: arguments, | ||
| environment: configuration.sshProcessEnvironment, | ||
| stdin: nil, | ||
| timeout: timeout, | ||
| operation: operation | ||
| ) | ||
| } | ||
|
|
||
| func runProcess( | ||
| executable: String, | ||
| arguments: [String], | ||
| environment: [String: String]? = nil, | ||
| currentDirectory: URL? = nil, | ||
| stdin: Data?, | ||
| timeout: TimeInterval, | ||
| operation: (any RemoteTransferCancelling)? = nil | ||
| ) throws -> RemoteCommandResult { | ||
| try processRunner.run( | ||
| RemoteProcessRequest( | ||
| executable: executable, | ||
| arguments: arguments, | ||
| environment: environment, | ||
| currentDirectory: currentDirectory, | ||
| stdin: stdin, | ||
| timeout: timeout | ||
| ), | ||
| operation: operation | ||
| ) | ||
| } | ||
| } |
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
7 changes: 7 additions & 0 deletions
7
Packages/macOS/CmuxRemoteSession/Tests/CmuxRemoteSessionTests/RemoteDaemonUploadStep.swift
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,7 @@ | ||
| enum RemoteDaemonUploadStep: Equatable { | ||
| case createDirectory | ||
| case upload | ||
| case finalize | ||
| case cleanup | ||
| case unknown | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.