-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun
206 lines (188 loc) · 8.7 KB
/
run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
# run command on image
# @example
# 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..."'
# )
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
plugin_load remove || return 1
plugin_load chown || return 1
function rpi_run_prerun() {
{
[[ -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() {
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_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"
}
# ---------------------------------------------------------------------
# run command on login
function rpi_run_on_login() {
log "run on login:"
for cmd in "$@" ; do
# got path to script file?
if [[ -f "${cmd}" ]] ; then
log " (login) installing script: \"${cmd}\""
# create script dir
[[ -d "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")" ]] || mkdir -p "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")"
# copy script
sudo cp "${cmd}" "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")" || error "cp ${cmd} ${RPI_ROOT}/${RPI_IMG_DISTDIR}/"
rpi_chown_pi "${RPI_IMG_DISTDIR}/${cmd}" || error "rpi_chown_pi"
rpi_append_to_file "${RPI_IMG_DISTDIR}/${cmd} || exit 1" "${RPI_ROOT}/home/pi/.bashrc" || error "rpi_append_to_file"
# got command string
else
log " (login) installing cmd: \"${cmd}\""
rpi_append_to_file "${cmd}" "${RPI_ROOT}/home/pi/.bashrc" || error "rpi_append_to_file"
fi
done
}
# run command once upon first login
function rpi_run_on_first_login() {
[[ -n "$*" ]] || error "missing argument"
local once_script="/home/pi/.bootstrap_run_on_first_login"
log "run on first login:"
# prepare script
if ! [[ -f "${RPI_ROOT}/${once_script}" ]] ; then
# call script from .bashrc
rpi_run_on_login "if [[ -f \"${once_script}\" ]] ; then echo \"executing first-time setup...\" ; time /bin/bash -c \"${once_script} && sudo rm -f ${once_script} && sudo rm -rf ${RPI_IMG_DISTDIR}\" ; echo \"Done. Please reboot now.\" ; fi"
# once-script dir existing?
[[ -d "${RPI_ROOT}/$(dirname "${once_script}")" ]] || mkdir -p "${RPI_ROOT}/$(dirname "${once_script}")"
# create once-script
sudo touch "${RPI_ROOT}/${once_script}" || error "touch"
sudo chmod +x "${RPI_ROOT}/${once_script}" || error "sudo chmod +x"
sudo chown root:root "${RPI_ROOT}/${once_script}" || error "chown"
fi
for cmd in "$@" ; do
# got path to script file?
if [[ -f "${cmd}" ]] ; then
log " (first login) installing script: \"${cmd}\""
rpi_append_to_file "echo -e '------\nexecuting script: ${cmd}\n------'" "${RPI_ROOT}/${once_script}"
rpi_append_to_file "${RPI_IMG_DISTDIR}/${cmd} || exit 1" "${RPI_ROOT}/${once_script}"
[[ -d "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")" ]] || mkdir -p "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")"
sudo cp "${cmd}" "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")" || error "cp ${cmd} ${RPI_ROOT}/${RPI_IMG_DISTDIR}/"
rpi_chown_pi "${RPI_IMG_DISTDIR}/${cmd}" || error "rpi_chown_pi"
# got command string
else
log " (first login) installing command: \"${cmd}\""
rpi_append_to_file "echo -e '------\nexecuting: ${cmd}\n------'" "${RPI_ROOT}/${once_script}"
rpi_append_to_file "${cmd} || exit 1" "${RPI_ROOT}/${once_script}"
fi
done
}
# run on every boot
function rpi_run_on_boot() {
log "run on boot:"
# remove "exit 0" at the end if it's there, so we
# can simply append commands
rpi_remove_pattern_from_file "exit 0" "${RPI_ROOT}/etc/rc.local" || error "remove exit from rc.local"
for cmd in "$@" ; do
# got path to script file?
if [[ -f "${cmd}" ]] ; then
log " (boot) installing script: \"${cmd}\""
rpi_append_to_file "echo -e '------\nexecuting script: ${cmd}\n------'" "${RPI_ROOT}/etc/rc.local"
rpi_append_to_file "${RPI_IMG_DISTDIR}/${cmd} || exit 1" "${RPI_ROOT}/etc/rc.local"
[[ -d "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")" ]] || mkdir -p "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")"
sudo cp "${cmd}" "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")" || error "cp ${cmd} ${RPI_ROOT}/${RPI_IMG_DISTDIR}"
rpi_chown_pi "${RPI_IMG_DISTDIR}/${cmd}" || error "rpi_chown_pi"
# got command string
else
log " (boot) installing cmd: \"${cmd}\""
rpi_append_to_file "${cmd}" "${RPI_ROOT}/etc/rc.local" || error "append ${cmd} to rc.local"
fi
done
}
# run command once upon first boot
function rpi_run_on_first_boot() {
[[ -n "$*" ]] || error "missing argument"
local once_script="/home/pi/.bootstrap_run_on_first_boot"
log "run on first boot:"
# prepare script
if ! [[ -f "${RPI_ROOT}/${once_script}" ]] ; then
# call script from /etc/rc.local
rpi_run_on_boot "if [[ -f \"${once_script}\" ]] ; then echo \"executing first-time setup...\" ; time /bin/bash -c \"${once_script} && rm ${once_script} && rm -rf /home/pi/bootstrap-dist\" ; echo \"Done. Please reboot now.\" ; fi"
# once-script dir existing?
[[ -d "${RPI_ROOT}/$(dirname "${once_script}")" ]] || mkdir -p "${RPI_ROOT}/$(dirname "${once_script}")"
# create script
sudo touch "${RPI_ROOT}/${once_script}" || error "touch ${RPI_ROOT}/${once_script}"
sudo chmod +x "${RPI_ROOT}/${once_script}" || error "sudo chmod +x"
sudo chown root:root "${RPI_ROOT}/${once_script}" || error "chown"
fi
for cmd in "$@" ; do
# got path to script file?
if [[ -f "${cmd}" ]] ; then
log " (first boot) installing script: \"${cmd}\""
rpi_append_to_file "echo -e '------\nexecuting script: ${cmd}\n------'" "${RPI_ROOT}/${once_script}"
rpi_append_to_file "${RPI_IMG_DISTDIR}/${cmd} || exit 1" "${RPI_ROOT}/${once_script}"
[[ -d "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")" ]] || mkdir -p "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")"
sudo cp "${cmd}" "${RPI_ROOT}/${RPI_IMG_DISTDIR}/$(dirname "${cmd}")" || error "cp ${cmd} ${RPI_ROOT}/${RPI_IMG_DISTDIR}/"
rpi_chown_pi "${RPI_IMG_DISTDIR}/${cmd}" || error "rpi_chown_pi"
# got command string
else
log " (first boot) installing cmd: \"${cmd}\""
# append to script
rpi_append_to_file "echo -e '------\nexecuting: ${cmd}\n------'" "${RPI_ROOT}/${once_script}"
rpi_append_to_file "${cmd} || exit 1" "${RPI_ROOT}/${once_script}"
fi
done
}
# output command to once-script
function rpi_run_on_bake() {
log "run on bake:"
for cmd in "$@" ; do
if [[ -f "${cmd}" ]] ; then
# execute script
log " (now) running script: \"${cmd}\""
# (shellcheck cannot source non-constant source)
# shellcheck disable=SC1090
( . "${cmd}" || error "${cmd}" )
else
# execute command
log " (now) running: \"${cmd}\""
eval "${cmd}" || error "${cmd}"
fi
done
}