Skip to content

Commit

Permalink
don't use RPI_RUN_ONCE and RPI_RUN_PHASE anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Hiepler committed Aug 30, 2023
1 parent 3b01a0b commit 1de655a
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 116 deletions.
2 changes: 1 addition & 1 deletion bootstrap-plugins/adduser
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ rpi_adduser_prerun() {

rpi_adduser_run() {
for g in "${RPI_ADDUSER_GROUP[@]}" ; do
RPI_RUN_ONCE=true rpi_run "sudo adduser pi ${g}" || error "add to group ${g}"
rpi_run_on_first_boot "sudo adduser pi ${g}" || error "add to group ${g}"
done
}

Expand Down
29 changes: 27 additions & 2 deletions bootstrap-plugins/apt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,38 @@

# default options
RPI_APT_INTERACTIVE="${RPI_APT_INTERACTIVE:=false}"
RPI_APT_ON="${RPI_APT_ON:=login}"
to_array RPI_APT_CMDS

# load dependencies
plugin_load run || return 1


function run() {
case "${RPI_APT_ON}" in
"login")
rpi_run_on_first_login "$@"
;;

"boot")
rpi_run_on_first_boot "$@"
;;

?|*)
error "invalid RPI_APT_ON=${RPI_APT_ON}"
esac
}

rpi_apt_prerun() {
[[ -n "${RPI_APT_CMDS}" ]] || return 1
case "${RPI_APT_ON}" in
"login")
;;
"boot")
;;
?|*)
error "invalid RPI_APT_ON=${RPI_APT_ON} - valid: login, boot"
esac
return 0
}

Expand All @@ -25,10 +49,10 @@ rpi_apt_run() {
for cmd in "${RPI_APT_CMDS[@]}" ; do
# run in interactive mode?
if [[ "${RPI_APT_INTERACTIVE}" == "true" ]] ; then
RPI_RUN_ONCE=true rpi_run "sudo apt ${cmd}" || error "apt"
run "sudo apt ${cmd}" || error "apt"
# run in non-interactive mode
else
RPI_RUN_ONCE=true rpi_run "sudo DEBIAN_FRONTEND=noninteractive apt --yes --quiet ${cmd}" || error "install apt update"
run "sudo DEBIAN_FRONTEND=noninteractive apt --yes --quiet ${cmd}" || error "install apt update"
fi
done
}
Expand All @@ -40,4 +64,5 @@ function rpi_apt_description() {
function rpi_apt_help_params() {
help_param "RPI_APT_CMDS" "array of apt commands to execute"
help_param "RPI_APT_INTERACTIVE" "run in interactive mode"
help_param "RPI_APT_ON" "login=run on login, boot=run on boot"
}
29 changes: 25 additions & 4 deletions bootstrap-plugins/dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# RPI_DIST_COPY=( "/etc", "/home", "/usr" )

# default options
RPI_DIST_ON="${RPI_DIST_ON:=bake}"
to_array RPI_DIST_COPY
to_array RPI_DIST_APPEND

Expand All @@ -21,6 +22,25 @@ plugin_load chmod || return 1
plugin_load chown || return 1


function run() {
case "${RPI_DIST_ON}" in
"bake")
rpi_run_on_bake "$@"
;;

"login")
rpi_run_on_first_login "$@"
;;

"boot")
rpi_run_on_first_boot "$@"
;;

?|*)
error "invalid RPI_DIST_ON=${RPI_DIST_ON}"
esac
}

function rpi_dist_prerun() {
true
}
Expand Down Expand Up @@ -50,6 +70,7 @@ function rpi_dist_help_params() {
help_param "RPI_DIST_COPY" "array of files to copy to disk image"
help_param "RPI_DIST_COPY_BOOT" "array of files to copy when the pi is booting"
help_param "RPI_DIST_APPEND" "array of files to append to files on disk image"
help_param "RPI_DIST_ON" "copy on \"bake\", first \"boot\" or first \"login\""
}

# ---------------------------------------------------------------------
Expand Down Expand Up @@ -115,15 +136,15 @@ function rpi_dist_cp_after_boot() {
# copy to image
sudo cp -r "${RPI_USER_DISTDIR}/${path}"/* "${distdir}/$(dirname "${path}")" || sudo cp -r "${RPI_DISTDIR}/${path}"/* "${distdir}/$(dirname "${path}")" || error "cp"
# register copy command
RPI_RUN_ONCE=true rpi_run "sudo cp -r \"/home/pi/bootstrap-dist/${path}/\"* \"/$(dirname "${path}")" || error "rpi_run"
run "sudo cp -r \"/home/pi/bootstrap-dist/${path}/\"* \"/$(dirname "${path}")" || error "rpi_run"
# copy file?
else
sudo cp -r "${RPI_USER_DISTDIR}/${path}" "${distdir}/$(dirname "${path}")/" || sudo cp -r "${RPI_DISTDIR}/${path}" "${distdir}/$(dirname "${path}")/" || error "cp"
RPI_RUN_ONCE=true rpi_run "sudo cp \"/home/pi/bootstrap-dist/${path}\" \"/$(dirname "${path}")/\"" || error "rpi_run"
run "sudo cp \"/home/pi/bootstrap-dist/${path}\" \"/$(dirname "${path}")/\"" || error "rpi_run"
fi
# chmod?
[[ -n "${permissions}" ]] && { RPI_RUN_ONCE=true rpi_run "sudo chmod -R \"${permissions}\" \"${path}\"" || error "chmod" ; }
[[ -n "${permissions}" ]] && { run "sudo chmod -R \"${permissions}\" \"${path}\"" || error "chmod" ; }
# chown
RPI_RUN_ONCE=true rpi_run "sudo chown -R pi:pi \"${path}\"" || error "chown"
run "sudo chown -R pi:pi \"${path}\"" || error "chown"

}
101 changes: 45 additions & 56 deletions bootstrap-plugins/run
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
# run command on image

# @example
# print a custom message once
# RPI_RUN_ONCE=true
# RPI_RUN=(
# print a custom message once at login
# RPI_RUN_LOGIN_ONCE=(
# 'echo "*********************************************************"'
# 'echo "Welcome to this install."'
# 'echo "It is $(date) and we are about to bake this pi."'
# 'echo "Please standby..."'
# )


# default options
RPI_RUN_PHASE="${RPI_RUN_PHASE:=login}"

to_array RPI_RUN
to_array RPI_RUN_LOGIN
to_array RPI_RUN_LOGIN_ONCE
to_array RPI_RUN_BOOT
to_array RPI_RUN_BOOT_ONCE
to_array RPI_RUN_BAKE
to_array RPI_RUN_BAKE_ONCE

# load dependencies
plugin_load append || return 1
Expand All @@ -25,35 +26,48 @@ plugin_load chown || return 1


function rpi_run_prerun() {
[[ -n "${RPI_RUN_PHASE}" ]] || error "RPI_RUN_PHASE unset."
[[ -n "${RPI_RUN}" ]] || error "RPI_RUN unset."
case "${RPI_RUN_PHASE}" in
"bake")
;;

"login")
;;

"boot")
;;

?|*)
error "invalid RPI_RUN_PHASE. Use one of bake, boot, login"
esac
{
[[ -n "${RPI_RUN_LOGIN}" ]] ||
[[ -n "${RPI_RUN_LOGIN_ONCE}" ]] ||
[[ -n "${RPI_RUN_BOOT}" ]] ||
[[ -n "${RPI_RUN_BOOT_ONCE}" ]] ||
[[ -n "${RPI_RUN_BAKE}" ]]
} || error "got nothing to run."
}

function rpi_run_run() {
rpi_run "${RPI_RUN[@]}"
if [[ -n "${RPI_RUN_BAKE}" ]] ; then
rpi_run_on_bake "${RPI_RUN_BAKE[@]}"
unset RPI_RUN_BAKE
fi
if [[ -n "${RPI_RUN_LOGIN_ONCE}" ]] ; then
rpi_run_on_first_login "${RPI_RUN_LOGIN_ONCE[@]}"
unset RPI_RUN_LOGIN_ONCE
fi
if [[ -n "${RPI_RUN_LOGIN}" ]] ; then
rpi_run_on_login "${RPI_RUN_LOGIN[@]}"
unset RPI_RUN_LOGIN
fi
if [[ -n "${RPI_RUN_BOOT_ONCE}" ]] ; then
rpi_run_on_first_boot "${RPI_RUN_BOOT_ONCE}[@]"
unset RPI_RUN_BOOT_ONCE
fi
if [[ -n "${RPI_RUN_BOOT}" ]] ; then
rpi_run_on_boot "${RPI_RUN_BOOT[@]}"
unset RPI_RUN_BOOT
fi
}

function rpi_run_description() {
echo "run commands on image after boot, login or manually"
}

function rpi_run_help_params() {
help_param "RPI_RUN" "command or path to script to run"
help_param "RPI_RUN_PHASE" "boot (run on boot), login (run on login), bake (run on host during image generation)"
help_param "RPI_RUN_ONCE" "only run once if set"
help_param "RPI_RUN_LOGIN" "command or script to run on every login"
help_param "RPI_RUN_LOGIN_ONCE" "command or script to run on first login"
help_param "RPI_RUN_BOOT" "command or script to run on every boot"
help_param "RPI_RUN_BOOT_ONCE" "command or script to run on first boot"
help_param "RPI_RUN_BAKE" "command or script to run on host during image generation"
}

# ---------------------------------------------------------------------
Expand Down Expand Up @@ -122,35 +136,10 @@ function rpi_run_on_first_boot() {
function rpi_run_on_bake() {
for cmd in "$@" ; do
log "running: \"${cmd}\""
( eval "${cmd}" ) || error "${cmd}"
# export complete environment for command
set -a
bash -c "${cmd}" || error "${cmd}"
# don't export env for commands
set +a
done
}

# run command (either on boot or on login)
function rpi_run() {
[[ -n "$*" ]] || error "missing argument"
case "${RPI_RUN_PHASE}" in
"bake")
rpi_run_on_bake "$@"
;;

"login")
if [[ -n "${RPI_RUN_ONCE}" ]] ; then
rpi_run_on_first_login "$@"
else
rpi_run_on_login "$@"
fi
;;

"boot")
if [[ -n "${RPI_RUN_ONCE}" ]] ; then
rpi_run_on_first_boot "$@"
else
rpi_run_on_boot "$@"
fi
;;

?|*)
error "invalid RPI_RUN_PHASE."
esac
}
19 changes: 18 additions & 1 deletion bootstrap-plugins/ssh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ RPI_SSH_KEY_TYPE="${RPI_SSH_KEY_TYPE:=ed25519}"
RPI_SSH_KEY_ROUNDS="${RPI_SSH_KEY_ROUNDS:=100}"
RPI_SSH_KEYGEN="${RPI_SSH_KEYGEN:=false}"
RPI_SSH_KEYGEN_SERVER="${RPI_SSH_KEYGEN_SERVER:=false}"
RPI_SSH_ON="${RPI_SSH_ON:=login}"
to_array RPI_SSH_AUTHORIZE

# load dependencies
Expand All @@ -28,6 +29,21 @@ plugin_load chown || return 1
plugin_load run || return 1


function run() {
case "${RPI_SSH_ON}" in
"login")
rpi_run_on_first_login "$@"
;;

"boot")
rpi_run_on_first_boot "$@"
;;

?|*)
error "invalid RPI_SSH_ON=${RPI_SSH_ON}"
esac
}

function rpi_ssh_prerun() {
true
}
Expand Down Expand Up @@ -85,7 +101,7 @@ function rpi_ssh_run() {
if [[ "${RPI_SSH_KEYGEN_SERVER}" == "true" ]] ; then
log "regenerating /etc/ssh/ssh_host_*"
sudo rm -f "${RPI_ROOT}/etc/ssh/ssh_host_"*
RPI_RUN_ONCE=true rpi_run "sudo dpkg-reconfigure openssh-server"
run "sudo dpkg-reconfigure openssh-server"
fi
}

Expand All @@ -99,6 +115,7 @@ function rpi_ssh_help_params() {
help_param "RPI_SSH_KEYGEN" "use local ssh-keygen to generate a key for the pi user"
help_param "RPI_SSH_KEYGEN_SERVER" "(re)generate server keys on the pi"
help_param "RPI_SSH_AUTHORIZE" "array of public keys to add to authorized_keys for the pi user"
help_param "RPI_SSH_ON" "setup on first \"login\" or \"boot\""
}

function rpi_ssh_help_distfiles() {
Expand Down
19 changes: 18 additions & 1 deletion bootstrap-plugins/systemctl
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,36 @@


# default options
RPI_SYSTEMCTL_ON="${RPI_SYSTEMCTL_ON:=login}"
to_array RPI_SYSTEMCTL_CMDS

# load dependencies
plugin_load run || return 1


function run() {
case "${RPI_SYSTEMCTL_ON}" in
"login")
rpi_run_on_first_login "$@"
;;

"boot")
rpi_run_on_first_boot "$@"
;;

?|*)
error "invalid RPI_SYSTEMCTL_ON=${RPI_SYSTEMCTL_ON}"
esac
}

function rpi_systemctl_prerun() {
[[ -n "${RPI_SYSTEMCTL_CMDS}" ]] || { warn "RPI_SYSTEMCTL_CMDS must be set" ; return 1 ; }
return 0
}

function rpi_systemctl_run() {
for cmd in "${RPI_SYSTEMCTL_CMDS[@]}" ; do
RPI_RUN_ONCE=true rpi_run "sudo systemctl ${cmd}" || error "install systemctl ${cmd}"
run "sudo systemctl ${cmd}" || error "install systemctl ${cmd}"
done
}

Expand All @@ -31,4 +47,5 @@ function rpi_systemctl_description() {

function rpi_systemctl_help_params() {
help_param "RPI_SYSTEMCTL_CMDS" "array of systemctl commands"
help_param "RPI_SYSTEMCTL_ON" "run on first \"boot\" or first \"login\""
}
Loading

0 comments on commit 1de655a

Please sign in to comment.