Skip to content

Commit 45925cf

Browse files
authored
also put logs into kexec directory (#592)
2 parents 3f514df + 5748958 commit 45925cf

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

src/get-facts.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ hasWget=$(has wget)
2020
hasCurl=$(has curl)
2121
hasSetsid=$(has setsid)
2222
hasNixOSFacter=$(command -v nixos-facter >/dev/null && echo "y" || echo "n")
23-
remoteHomeDir=$HOME
2423
FACTS

src/nixos-anywhere.sh

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ hasWget=
5858
hasCurl=
5959
hasSetsid=
6060
hasNixOSFacter=
61-
remoteHomeDir=
6261

6362
tempDir=$(mktemp -d)
6463
trap 'rm -rf "$tempDir"' EXIT
@@ -586,16 +585,13 @@ importFacts() {
586585

587586
# Necessary to prevent Bash erroring before printing out which fact had an issue
588587
set +u
589-
for var in isOs isArch isInstaller isContainer isRoot hasIpv6Only hasTar hasCpio hasSudo hasDoas hasWget hasCurl hasSetsid remoteHomeDir; do
588+
for var in isOs isArch isInstaller isContainer isRoot hasIpv6Only hasTar hasCpio hasSudo hasDoas hasWget hasCurl hasSetsid; do
590589
if [[ -z ${!var} ]]; then
591590
abort "Failed to retrieve fact $var from host"
592591
fi
593592
done
594593
set -u
595594

596-
# Compute the log file path from the home directory
597-
remoteLogFile="${remoteHomeDir}/.nixos-anywhere.log"
598-
599595
if [[ -n ${enableDebug} ]]; then
600596
set -x
601597
fi
@@ -722,10 +718,6 @@ runKexec() {
722718
kexecUrl=${kexecUrl/"github.com"/"gh-v6.com"}
723719
fi
724720
725-
if [[ -z $remoteLogFile ]]; then
726-
abort "Could not create a temporary log file for $sshUser"
727-
fi
728-
729721
# Handle kexec operation failures
730722
handleKexecFailure() {
731723
local operation=$1
@@ -734,7 +726,8 @@ runKexec() {
734726
local logContent=""
735727
if logContent=$(
736728
set +x
737-
runSsh "cat \"$remoteLogFile\" 2>/dev/null" 2>/dev/null
729+
# shellcheck disable=SC2016 # We want $HOME to expand on the remote server
730+
runSsh 'cat "$HOME/kexec/nixos-anywhere.log" 2>/dev/null' 2>/dev/null
738731
); then
739732
echo "Remote output log:" >&2
740733
echo "$logContent" >&2
@@ -743,28 +736,21 @@ runKexec() {
743736
exit 1
744737
}
745738
746-
# Extract directly to the user's home directory
747-
if [[ -z $remoteHomeDir ]]; then
748-
abort "Could not determine home directory for user $sshUser"
749-
fi
750-
751739
# Define common remote commands template
752740
local remoteCommandTemplate
753741
remoteCommandTemplate="
754742
# Run kexec commands with sudo if needed
755743
{
756744
set -eu ${enableDebug}
757-
${maybeSudo} rm -rf \"$remoteHomeDir/kexec\"
758-
mkdir -p \"$remoteHomeDir/kexec\"
759-
cd \"$remoteHomeDir/kexec\"
745+
cd \"\$HOME/kexec\"
760746
echo Downloading kexec tarball, this may take a moment...
761747
# Execute tar command
762748
%TAR_COMMAND%
763-
TMPDIR=\"$remoteHomeDir/kexec\" ${maybeSudo} setsid --wait \"$remoteHomeDir/kexec/kexec/run\" --kexec-extra-flags $(printf '%q' "$kexecExtraFlags")
764-
} 2>&1 | tee \"$remoteLogFile\" || true
749+
TMPDIR=\"\$HOME/kexec\" ${maybeSudo} setsid --wait \"\$HOME/kexec/kexec/run\" --kexec-extra-flags $(printf '%q' "$kexecExtraFlags")
750+
} 2>&1 | tee \"\$HOME/kexec/nixos-anywhere.log\" || true
765751
766752
# The script will likely disconnect us, so we consider it successful if we see the kexec message
767-
if ! grep -q 'machine will boot into nixos' \"$remoteLogFile\"; then
753+
if ! grep -q 'machine will boot into nixos' \"\$HOME/kexec/nixos-anywhere.log\"; then
768754
echo 'Kexec may have failed - check output above'
769755
exit 1
770756
fi
@@ -803,20 +789,25 @@ fi
803789
# Use remote command for download
804790
tarCommand="$(printf '%q ' "${remoteUploadCommand[@]}") | tar -xv ${tarDecomp}"
805791
else
806-
# Upload the kexec tarball first
807-
"${localUploadCommand[@]}" | runSsh "cat > \"$remoteHomeDir\"/kexec-tarball.tar.gz"
808792
# Use local file for extraction
809-
tarCommand="cat \"$remoteHomeDir\"/kexec-tarball.tar.gz | tar -xv ${tarDecomp}"
793+
tarCommand="cat \"\$HOME/kexec/kexec-tarball.tar.gz\" | tar -xv ${tarDecomp}"
810794
fi
811795
812796
local remoteCommands
813797
remoteCommands=${remoteCommandTemplate//'%TAR_COMMAND%'/$tarCommand}
814798
815799
# Create and execute the script on the remote system
816-
runSsh "mkdir -p \"$remoteHomeDir/kexec\" && cat > \"$remoteHomeDir/kexec/unpack.sh\"" <<EOF
800+
# shellcheck disable=SC2016 # We want $HOME to expand on the remote server
801+
runSsh 'mkdir -p "$HOME/kexec" && cat > "$HOME/kexec/nixos-anywhere-kexec.sh"' <<EOF
817802
$remoteCommands
818803
EOF
819-
runSsh "bash $remoteHomeDir/kexec/unpack.sh" || handleKexecFailure "Kexec"
804+
if [[ ${#localUploadCommand[@]} -gt 0 ]]; then
805+
# Upload the kexec tarball first
806+
# shellcheck disable=SC2016 # We want $HOME to expand on the remote server
807+
"${localUploadCommand[@]}" | runSsh 'cat > "$HOME/kexec/kexec-tarball.tar.gz"'
808+
fi
809+
# shellcheck disable=SC2016 # We want $HOME to expand on the remote server
810+
runSsh 'bash "$HOME/kexec/nixos-anywhere-kexec.sh"' || handleKexecFailure "Kexec"
820811
821812
# use the default SSH port to connect at this point
822813
local i

0 commit comments

Comments
 (0)