Skip to content

Commit e66da20

Browse files
committed
add main function to make control flow easier to follow
1 parent 25a0874 commit e66da20

File tree

1 file changed

+71
-64
lines changed

1 file changed

+71
-64
lines changed

src/nixos-anywhere.sh

Lines changed: 71 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -330,34 +330,37 @@ ssh_settings=$(ssh "${ssh_args[@]}" -G "${ssh_connection}")
330330
ssh_user=$(echo "$ssh_settings" | awk '/^user / { print $2 }')
331331
ssh_host=$(echo "$ssh_settings" | awk '/^hostname / { print $2 }')
332332

333-
step Uploading install SSH keys
334-
until
335-
if [[ -n ${env_password-} ]]; then
336-
sshpass -e \
333+
uploadSSHKey() {
334+
step Uploading install SSH keys
335+
until
336+
if [[ -n ${env_password-} ]]; then
337+
sshpass -e \
338+
ssh-copy-id \
339+
-i "$ssh_key_dir"/nixos-anywhere.pub \
340+
-o ConnectTimeout=10 \
341+
-o UserKnownHostsFile=/dev/null \
342+
-o IdentitiesOnly=yes \
343+
-o StrictHostKeyChecking=no \
344+
"${ssh_copy_id_args[@]}" \
345+
"${ssh_args[@]}" \
346+
"$ssh_connection"
347+
else
337348
ssh-copy-id \
338-
-i "$ssh_key_dir"/nixos-anywhere.pub \
339-
-o ConnectTimeout=10 \
340-
-o UserKnownHostsFile=/dev/null \
341-
-o IdentitiesOnly=yes \
342-
-o StrictHostKeyChecking=no \
343-
"${ssh_copy_id_args[@]}" \
344-
"${ssh_args[@]}" \
345-
"$ssh_connection"
346-
else
347-
ssh-copy-id \
348-
-i "$ssh_key_dir"/nixos-anywhere.pub \
349-
-o ConnectTimeout=10 \
350-
-o UserKnownHostsFile=/dev/null \
351-
-o StrictHostKeyChecking=no \
352-
"${ssh_copy_id_args[@]}" \
353-
"${ssh_args[@]}" \
354-
"$ssh_connection"
355-
fi
356-
do
357-
sleep 3
358-
done
349+
-i "$ssh_key_dir"/nixos-anywhere.pub \
350+
-o ConnectTimeout=10 \
351+
-o UserKnownHostsFile=/dev/null \
352+
-o StrictHostKeyChecking=no \
353+
"${ssh_copy_id_args[@]}" \
354+
"${ssh_args[@]}" \
355+
"$ssh_connection"
356+
fi
357+
do
358+
sleep 3
359+
done
360+
}
359361

360362
importFacts() {
363+
step Gathering machine facts
361364
local facts filtered_facts
362365
if ! facts=$(ssh_ -o ConnectTimeout=10 enable_debug=$enable_debug sh -- <"$here"/get-facts.sh); then
363366
exit 1
@@ -371,27 +374,26 @@ importFacts() {
371374
export $(echo "$filtered_facts" | xargs)
372375
}
373376

374-
step Gathering machine facts
375-
importFacts
376-
377-
if [[ ${has_tar-n} == "n" ]]; then
378-
abort "no tar command found, but required to unpack kexec tarball"
379-
fi
377+
checkFacts() {
378+
if [[ ${has_tar-n} == "n" ]]; then
379+
abort "no tar command found, but required to unpack kexec tarball"
380+
fi
380381

381-
if [[ ${has_setsid-n} == "n" ]]; then
382-
abort "no setsid command found, but required to run the kexec script under a new session"
383-
fi
382+
if [[ ${has_setsid-n} == "n" ]]; then
383+
abort "no setsid command found, but required to run the kexec script under a new session"
384+
fi
384385

385-
maybe_sudo=""
386-
if [[ ${has_sudo-n} == "y" ]]; then
387-
maybe_sudo="sudo"
388-
elif [[ ${has_doas-n} == "y" ]]; then
389-
maybe_sudo="doas"
390-
fi
386+
maybe_sudo=""
387+
if [[ ${has_sudo-n} == "y" ]]; then
388+
maybe_sudo="sudo"
389+
elif [[ ${has_doas-n} == "y" ]]; then
390+
maybe_sudo="doas"
391+
fi
391392

392-
if [[ ${is_os-n} != "Linux" ]]; then
393-
abort "This script requires Linux as the operating system, but got $is_os"
394-
fi
393+
if [[ ${is_os-n} != "Linux" ]]; then
394+
abort "This script requires Linux as the operating system, but got $is_os"
395+
fi
396+
}
395397

396398
runKexec() {
397399
if [[ ${is_kexec-n} == "n" ]] && [[ ${is_installer-n} == "n" ]]; then
@@ -502,6 +504,8 @@ nixosInstall() {
502504
step Installing NixOS
503505
maybeReboot=""
504506
if [[ ${phases[reboot]-} == 1 ]]; then
507+
# We will reboot in background so we can cleanly finish the script before the hosts go down.
508+
# This makes integration into scripts easier
505509
maybeReboot="nohup sh -c 'sleep 6 && reboot' >/dev/null &"
506510
fi
507511
ssh_ sh <<SSH
@@ -535,29 +539,32 @@ SSH
535539

536540
}
537541

538-
if [[ ${phases[kexec]-} == 1 ]]; then
539-
runKexec
540-
fi
542+
main() {
543+
uploadSSHKey
544+
importFacts
545+
checkFacts
541546

542-
# Installation will fail if non-root user is used for installer.
543-
# Switch to root user by copying authorized_keys.
544-
if [[ ${is_installer-n} == "y" ]] && [[ ${ssh_user} != "root" ]]; then
545-
# Allow copy to fail if authorized_keys does not exist, like if using /etc/ssh/authorized_keys.d/
546-
ssh_ "${maybe_sudo} mkdir -p /root/.ssh; ${maybe_sudo} cp ~/.ssh/authorized_keys /root/.ssh || true"
547-
ssh_connection="root@${ssh_host}"
548-
fi
547+
if [[ ${phases[kexec]-} == 1 ]]; then
548+
runKexec
549+
fi
549550

550-
if [[ ${phases[disko]-} == 1 ]]; then
551-
runDisko "$disko_script"
552-
fi
551+
# Installation will fail if non-root user is used for installer.
552+
# Switch to root user by copying authorized_keys.
553+
if [[ ${is_installer-n} == "y" ]] && [[ ${ssh_user} != "root" ]]; then
554+
# Allow copy to fail if authorized_keys does not exist, like if using /etc/ssh/authorized_keys.d/
555+
ssh_ "${maybe_sudo} mkdir -p /root/.ssh; ${maybe_sudo} cp ~/.ssh/authorized_keys /root/.ssh || true"
556+
ssh_connection="root@${ssh_host}"
557+
fi
553558

554-
if [[ ${phases[install]-} == 1 ]]; then
555-
nixosInstall
556-
fi
559+
if [[ ${phases[disko]-} == 1 ]]; then
560+
runDisko "$disko_script"
561+
fi
557562

558-
if [[ ${phases[reboot]-} == 1 ]]; then
559-
step Waiting for the machine to become unreachable due to reboot
560-
while timeout_ssh_ -- exit 0; do sleep 1; done
561-
fi
563+
if [[ ${phases[reboot]-} == 1 ]]; then
564+
step Waiting for the machine to become unreachable due to reboot
565+
while timeout_ssh_ -- exit 0; do sleep 1; done
566+
fi
567+
step "Done!"
568+
}
562569

563-
step "Done!"
570+
main

0 commit comments

Comments
 (0)