@@ -11,7 +11,6 @@ buildArgs=(
1111)
1212profile=/nix/var/nix/profiles/system
1313# will be set later
14- controlPath=
1514sshOpts=(
1615 -o " ControlMaster=auto"
1716 -o " ControlPersist=60"
@@ -32,7 +31,7 @@ outPath="$2"
3231targetHost=" $3 "
3332targetPort=" $4 "
3433buildOnTarget=" $5 "
35- sshPrivateKeyFile =" $6 "
34+ sshPrivateKey =" $6 "
3635action=" $7 "
3736shift 7
3837
@@ -42,8 +41,13 @@ buildArgs+=("$@")
4241
4342sshOpts+=( -p " ${targetPort} " )
4443
45- if [[ -n " ${sshPrivateKeyFile} " && " ${sshPrivateKeyFile} " != " -" ]]; then
46- sshOpts+=( -o " IdentityFile=${sshPrivateKeyFile} " )
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} " )
4751fi
4852
4953# ## Functions ###
@@ -62,16 +66,17 @@ targetHostCmd() {
6266 # `ssh` did not properly maintain the array nature of the command line,
6367 # erroneously splitting arguments with internal spaces, even when using `--`.
6468 # Tested with OpenSSH_7.9p1.
69+ #
70+ # shellcheck disable=SC2029
6571 ssh " ${sshOpts[@]} " " $targetHost " " ./maybe-sudo.sh ${*@ Q} "
6672}
6773
6874# Setup a temporary ControlPath for this session. This speeds-up the
6975# operations by not re-creating SSH sessions between each command. At the end
7076# of the run, the session is forcefully terminated.
7177setupControlPath () {
72- controlPath=$( mktemp)
7378 sshOpts+=(
74- -o " ControlPath=$controlPath "
79+ -o " ControlPath=$workDir /ssh_control "
7580 )
7681 cleanupControlPath () {
7782 local ret=$?
@@ -80,7 +85,7 @@ setupControlPath() {
8085 # Close ssh multiplex-master process gracefully
8186 log " closing persistent ssh-connection"
8287 ssh " ${sshOpts[@]} " -O stop " $targetHost "
83- rm -f " $controlPath "
88+ rm -rf " $workDir "
8489 exit " $ret "
8590 }
8691 trap cleanupControlPath EXIT
0 commit comments