-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstartup
executable file
·304 lines (275 loc) · 9.69 KB
/
startup
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash
# If you are looking for docs on this script, consult https://github.com/jshook/simple-startup
# or, preferably, just run ./startup
# functions
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
options=$(getopt -o brg --long list -- "$@")
[ $? -eq 0 ] || {
echo "Incorrect options provided"
exit 1
}
CMD="list"
eval set -- "$options"
while true; do
case "$1" in
--list)
CMD="list"
;;
--)
shift
break
;;
esac
shift
printf "CMD:%s\n" ${CMD} 1>&2
done
# locate non-relative script dir
MAIN_SCRIPT_BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# normalize path to script
MAIN_SCRIPT="${MAIN_SCRIPT_BASEDIR}/"$( basename $0 )
EXECUTION_DIR=$(dirname ${MAIN_SCRIPT})
##
## locate .startup directory
##
MAIN_SCRIPT_DIRNAME=$(dirname $MAIN_SCRIPT)
if [ "${MAIN_SCRIPT_DIRNAME}" = ".startup" ]
then export COMPONENTS_DIRNAME="${MAIN_SCRIPT_DIRNAME}"
else export COMPONENTS_DIRNAME="${MAIN_SCRIPT_DIRNAME}/.startup"
fi
MANAGED_COMPONENTS_DIRNAME="${COMPONENTS_DIRNAME}/managed-scripts"
##
## sanity check that there is only one startup script
##
if [ -f "${COMPONENTS_DIRNAME}/startup" -a -f "${COMPONENTS_DIRNAME}/../startup" ]
then
printf "# ERROR: You must choose where to put your one and only 'startup' script.\n"
printf "You have one in both locations below:\n%s\n%s\n" "${COMPONENTS_DIRNAME}/startup" "${COMPONENTS_DIRNAME}/../startup"
exit 2
fi
if [ ! -d "${COMPONENTS_DIRNAME}" ]
then
printf "# ERROR: component scripts directory '${COMPONENTS_DIRNAME}' does not exist.\n"
exit 2
fi
##
## Initialize known component scripts array
##
component_scriptnames_ary=()
SCRIPT_DATA=$(cd $COMPONENTS_DIRNAME; /bin/ls)
while read -r line
do
if [ "$line" = "startup.order" ] ; then continue ; fi
if [ "$line" = "startup" ] ; then continue ; fi
if [ "$line" = "managed-scripts" ] ; then continue ; fi
if [ ! -x "${COMPONENTS_DIRNAME}/$line" -o ! -f "${COMPONENTS_DIRNAME}/$line" ] ; then continue ; fi
if [ -z "$line" ] ; then continue; fi
component_scriptnames_ary+=("$line")
done <<< "$SCRIPT_DATA"
#printf "# DEBUG: component_scriptname: %s\n" "${component_scriptnames_ary[@]}" 1>&2
managed_scripts_ary=()
if [ -d "${MANAGED_COMPONENTS_DIRNAME}" ]
then
# printf "# INFO: reading managed scripts in %s ...\n" "${MANAGED_COMPONENTS_DIRNAME}" 1>&2
SCRIPT_DATA=$(cd ${MANAGED_COMPONENTS_DIRNAME}; /bin/ls)
while read -r line
do
if [ ! -x "${MANAGED_COMPONENTS_DIRNAME}/$line" -o ! -f "${MANAGED_COMPONENTS_DIRNAME}/$line" ] ; then continue ; fi
if [ -z "$line" ] ; then continue; fi
# printf "# INFO: read managed script: %s\n" "$line" 1>&2
managed_scripts_ary+=("$line")
done <<< "$SCRIPT_DATA"
#printf "# DEBUG: component_scriptname: %s\n" "${component_scriptnames_ary[@]}" 1>&2
fi
##
## Initialize ordered components array
##
component_order_from_file=()
export ORDER_FILE="${COMPONENTS_DIRNAME}/startup.order"
if [ ! -f "${ORDER_FILE}" ]
then
component_order_from_file=("${component_scriptnames_ary[@]}")
else
# printf "# DEBUG: loading order file: %s\n" "${ORDER_FILE}" 1>&2
##
## Verify that component scripts referenced in the startup order are present and executable
##
ORDER_DATA=$(cat ${ORDER_FILE})
while read -r line
do
if [ "$line" = "startup.order" ] ; then continue ; fi
if [ "$line" = "startup" ] ; then continue ; fi
# if [ "${line:0:1}" = "3" ] ; then line= ${line#\#}; fi
component_order_from_file+=("$line")
line=${line#\#}
if [ -x "${COMPONENTS_DIRNAME}/${line}" ]
then COMPONENT_SCRIPT="${COMPONENTS_DIRNAME}/$line"
elif [ -x "${MANAGED_COMPONENTS_DIRNAME}/${line}" ]
then COMPONENT_SCRIPT="${MANAGED_COMPONENTS_DIRNAME}/${line}"
else
printf "# ERROR: component startup script '${line}' is not executable or it is missing.\n";
printf "# ERROR: (it was referenced in the startup order at ${COMPONENTS_DIRNAME}/startup.order )\n";
printf "# ERROR: (it was not found in either ${COMPONENTS_DIRNAME} or ${MANAGED_COMPONENTS_DIRNAME}\n";
exit 2
fi
done <<< "${ORDER_DATA}"
##
## verify that all named components found in script form exist in the startup.order file
##
for component_scriptname in "${component_scriptnames_ary[@]}"
do
# printf "checking %s for ordering membership.\n" "$component_scriptname" 1>&2
if ! containsElement "${component_scriptname}" "${component_order_from_file[@]}" && ! containsElement "#${component_scriptname}" "${component_order_from_file[@]}"
then
printf "# ERROR: There is a startup.order file, but script '${component_scriptname}' is not listed in it.\n"
exit 2
fi
done
fi
#printf "# DEBUG: component_order: %s\n" "${component_order_from_file[@]}" 1>&2
##
## warn if no component scripts found
##
if [ "${#component_order_from_file[@]}" -eq 0 ]
then
printf "# ERROR: There are no executable scripts in ${COMPONENTS_DIRNAME}\n";
exit 2
fi
##
## warn if order is not explicit
##
if [ ! -f "${ORDER_FILE}" ]
then
printf "# WARNING: Component order is based on filenames.\n" 1>&2
printf "# WARNING: Consider adding a startup.order file to control order of startup.\n" 1>&2
fi
##
## Initialize selected components array
##
selected_components_ary=()
# printf "# DEBUG: arg:[%s]\n" $* 1>&2
if [ "all" = "$1" ]
then
printf "# INFO: selecting all components\n"; 1>&2
selected_components_ary=("${component_order_from_file[@]}")
printf "# DEBUG: selected from file: %s\n" "${selected_components_ary[@]}"
elif [ ! -z "$1" ]
then
printf "# INFO: selecting specific components: $*\n"; 1>&2
selected_components_ary=( "$@" )
#do selected_components_ary+=("$line")
else
printf "# INFO: You must select from the following available components, or specify 'all':\n" 1>&2
printf "%s\n" "${component_order_from_file[@]}" 1>&2
if [ "${#managed_scripts_ary[@]}" -gt "0" ]
then
managed_overrides=( "$@" )
managed_available=( "$@" )
for managed in "${managed_scripts_ary[@]}"
do
if containsElement "$managed" "${component_order_from_file[@]}"
then managed_overrides+=("${managed}")
else managed_available+=("${managed}")
fi
done
if [ "${#managed_available}" -gt "0" ]
then
printf "# INFO: You may also select from managed-scripts, but they are not included with 'all':\n" 1>&2
printf "%s\n" "${managed_available[@]}" 1>&2
fi
if [ "${#managed_overrides}" -gt "0" ]
then
printf "# INFO: The following scripts are not available from managed-scripts because they are overridden above:\n" 1>&2
printf "%s\n" "${managed_overrides[@]}" 1>&2
fi
fi
exit 0
fi
#printf "# DEBUG: selected_component: %s\n" "${selected_components_ary[@]}" 1>&2
##
## Error if selected components can not be found
##
for selected in "${selected_components_ary[@]}"
do
if ! containsElement "${selected}" "${component_order_from_file[@]}" && \
! containsElement "#${selected}" "${component_order_from_file[@]}" && \
! containsElement "${selected}" "${managed_scripts_ary}" && \
! [ -x "${COMPONENTS_DIRNAME}/${selected}" ]
then
printf "%s was specified, but no such component could be found.\n" "${selected}"
exit 2
fi
done
##
## Add selected components in configured order, and log which ones are skipped
##
ordered_components_ary=()
for component in "${component_order_from_file[@]}"
do
component=${component#\#}
if containsElement "${component}" "${selected_components_ary[@]}"
then
ordered_components_ary+=("${component}")
else
printf "# INFO: component '%s' is being skipped\n" "${component}" 1>&2
fi
done
for component in "${managed_scripts_ary[@]}"
do
component=${component#\#}
# printf "# DEBUG: component: %s\n" "${component}"
# printf "# DEBUG: ordered_components_ary: %s\n" "${ordered_components_ary[@]}"
# printf "# DEBUG: selected_components_ary: %s\n" "${ordered_components_ary[@]}"
if ! containsElement "${component}" "${ordered_components_ary[@]}" && containsElement "${component}" "${selected_components_ary[@]}"
then ordered_components_ary+=("${component}")
printf "# INFO: including managed script after ordered scripts: %s\n" "${component}" 1>&2
fi
done
remaining_ary=()
for selected in "${selected_components_ary[@]}"
do
if ! containsElement "${selected}" "${ordered_components_ary[@]}"
then remaining_ary+=("${selected}")
fi
done
selected_components_ary=(${remaining_ary[@]})
for component in "${selected_components_ary[@]}"
do
if [ -x "${COMPONENTS_DIRNAME}/${component}" ]
then
printf "# INFO: adding raw script: %s\n" "${component}"
ordered_components_ary+=("${component}")
fi
done
#printf "# ordered components: %s\n" "${ordered_components_ary[@]}" 1>&2
#printf "component_order_from_file: %s\n" "${component_order_from_file[@]}" 1>&2
#printf "ordered_components_ary: %s\n" "${ordered_components_ary[@]}" 1>&2
# execute components in order
for selected in "${ordered_components_ary[@]}"
do
printf "# DEBUG: selected: %s\n" "${selected}"
FULL_SCRIPT_PATH="UNKNOWN"
#printf "# DEBUG: executing selected component: %s\n" ${selected}
if [ -x "${COMPONENTS_DIRNAME}/${selected}" ]
then FULL_SCRIPT_PATH="${COMPONENTS_DIRNAME}/${selected}"
elif [ -x "${MANAGED_COMPONENTS_DIRNAME}/${selected}" ]
then FULL_SCRIPT_PATH="${MANAGED_COMPONENTS_DIRNAME}/${selected}"
else
printf "# ERROR: selected script %s was not found.\n" "${selected}"
exit 2
fi
# printf "# DEBUG: executing: %s at path: %s\n" "${selected}" "${FULL_SCRIPT_PATH}"
export PROJECT_DIRECTORY=$(dirname ${COMPONENTS_DIRNAME})
export SELECTED_COMPONENTS="${ordered_components_ary[@]}"
export CURRENT_COMPONENT="${selected}"
export COMPONENT_SCRIPT="${FULL_SCRIPT_PATH}"
( cd ${EXECUTION_DIR} && bash ${FULL_SCRIPT_PATH})
last_status=$?
if [[ last_status -ne 0 ]]
then
printf "# ERROR: script %s failed with status: %s\n" "${FULL_SCRIPT_PATH}" "${last_status}"
fi
done