@@ -11,6 +11,7 @@ buildArgs=(
1111)
1212profile=/nix/var/nix/profiles/system
1313# will be set later
14+ controlPath=
1415sshOpts=(
1516 -o " ControlMaster=auto"
1617 -o " ControlPersist=60"
@@ -31,7 +32,7 @@ outPath="$2"
3132targetHost=" $3 "
3233targetPort=" $4 "
3334buildOnTarget=" $5 "
34- sshPrivateKey =" $6 "
35+ sshPrivateKeyFile =" $6 "
3536action=" $7 "
3637shift 7
3738
@@ -41,13 +42,8 @@ buildArgs+=("$@")
4142
4243sshOpts+=( -p " ${targetPort} " )
4344
44- workDir=$( mktemp -d)
45- trap ' rm -rf "$workDir"' EXIT
46-
47- if [[ -n " ${sshPrivateKey} " ]]; then
48- sshPrivateKeyFile=" $workDir /ssh_key"
49- echo " $sshPrivateKey " > " $sshPrivateKeyFile "
50- sshOpts+=( -o " IdentityFile=${sshPrivateKeyFile} " )
45+ if [[ -n " ${sshPrivateKeyFile} " && " ${sshPrivateKeyFile} " != " -" ]]; then
46+ sshOpts+=( -o " IdentityFile=${sshPrivateKeyFile} " )
5147fi
5248
5349# ## Functions ###
@@ -66,17 +62,16 @@ targetHostCmd() {
6662 # `ssh` did not properly maintain the array nature of the command line,
6763 # erroneously splitting arguments with internal spaces, even when using `--`.
6864 # Tested with OpenSSH_7.9p1.
69- #
70- # shellcheck disable=SC2029
7165 ssh " ${sshOpts[@]} " " $targetHost " " ./maybe-sudo.sh ${*@ Q} "
7266}
7367
7468# Setup a temporary ControlPath for this session. This speeds-up the
7569# operations by not re-creating SSH sessions between each command. At the end
7670# of the run, the session is forcefully terminated.
7771setupControlPath () {
72+ controlPath=$( mktemp)
7873 sshOpts+=(
79- -o " ControlPath=$workDir /ssh_control "
74+ -o " ControlPath=$controlPath "
8075 )
8176 cleanupControlPath () {
8277 local ret=$?
@@ -85,7 +80,7 @@ setupControlPath() {
8580 # Close ssh multiplex-master process gracefully
8681 log " closing persistent ssh-connection"
8782 ssh " ${sshOpts[@]} " -O stop " $targetHost "
88- rm -rf " $workDir "
83+ rm -f " $controlPath "
8984 exit " $ret "
9085 }
9186 trap cleanupControlPath EXIT
0 commit comments