-
Notifications
You must be signed in to change notification settings - Fork 26
/
package_check.sh
executable file
·234 lines (204 loc) · 7.04 KB
/
package_check.sh
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
#!/bin/bash
cd "$(dirname "$(realpath "$0")")" || exit 1
source "./lib/common.sh"
source "./lib/tests_coordination.sh"
source "./lib/build_base_lxc.sh"
print_help() {
cat << EOF
Usage: package_check.sh [OPTION]... PACKAGE_TO_CHECK
-b, --branch=BRANCH Specify a branch to check.
-D, --dry-run Show a JSON representing which tests are going to be ran (meant for debugging)
-i, --interactive Wait for the user to continue before each remove
-e, --interactive-on-errors Wait for the user to continue on errors
-s, --force-stop Force the stop of running package_check
-r, --rebuild (Re)Build the base container
(N.B.: you're not supposed to use this option,
images are supposed to be fetch from
repo.yunohost.org/incus automatically)
-S, --storage-dir DIRECTORY Where to store temporary test files like yunohost backups
-h, --help Display this help
Pass YNHDEV_BACKEND=incus|lxd to use a specific LXD-compatible backend.
Pass DIST=bullseye|bookworm to use a specific distribution version
Pass YNH_BRANCH=stable|unstable to use a specific Yunohost branch
EOF
exit 0
}
#=================================================
# Pase CLI arguments
#=================================================
# If no arguments provided
# Print the help and exit
[ "$#" -eq 0 ] && print_help
gitbranch=""
dry_run=0
interactive=0
interactive_on_errors=0
rebuild=0
force_stop=0
storage_dir="${YNH_PACKAGE_CHECK_STORAGE_DIR:-}"
function parse_args() {
local getopts_built_arg=()
# Read the array value per value
for i in $(seq 0 $(( ${#arguments[@]} -1 )))
do
if [[ "${arguments[i]}" =~ "--branch=" ]]
then
getopts_built_arg+=(-b)
arguments[i]=${arguments[i]//--branch=/}
fi
# For each argument in the array, reduce to short argument for getopts
arguments[i]=${arguments[i]//--interactive/-i}
arguments[i]=${arguments[i]//--dry-run/-D}
arguments[i]=${arguments[i]//--rebuild/-r}
arguments[i]=${arguments[i]//--force-stop/-s}
arguments[i]=${arguments[i]//--storage-dir/-s}
arguments[i]=${arguments[i]//--help/-h}
getopts_built_arg+=("${arguments[i]}")
done
# Read and parse all the arguments
# Use a function here, to use standart arguments $@ and be able to use shift.
parse_arg () {
while [ $# -ne 0 ]
do
# If the paramater begins by -, treat it with getopts
if [ "${1:0:1}" == "-" ]
then
# Initialize the index of getopts
OPTIND=1
# Parse with getopts only if the argument begin by -
getopts ":b:Diresh" parameter || true
case $parameter in
b)
# --branch=branch-name
gitbranch="-b $OPTARG"
shift_value=2
;;
i)
# --interactive
interactive=1
shift_value=1
;;
D)
# --dry-run
dry_run=1
shift_value=1
;;
e)
# --interactive-on-errors
interactive_on_errors=1
shift_value=1
;;
r)
# --rebuild
rebuild=1
shift_value=1
;;
s)
# --force-stop
force_stop=1
shift_value=1
;;
S)
# --storage-dir
storage_dir=$OPTARG
shift_value=2
;;
h)
# --help
print_help
;;
\?)
echo "Invalid argument: -${OPTARG:-}"
print_help
;;
:)
echo "-$OPTARG parameter requires an argument."
print_help
;;
esac
# Otherwise, it's not an option, it's an operand
else
path_to_package_to_test="$1"
shift_value=1
fi
# Shift the parameter and its argument
shift "$shift_value"
done
}
# Call parse_arg and pass the modified list of args as a array of arguments.
parse_arg "${getopts_built_arg[@]}"
}
arguments=("$@")
parse_args
#=================================================
# Cleanup / force-stop
#=================================================
function cleanup()
{
trap '' SIGINT # Disable ctrl+c in this function
LXC_RESET
[ -n "$TEST_CONTEXT" ] && rm -rf "$TEST_CONTEXT"
[ -n "$lock_file" ] && rm -f "$lock_file"
}
if [[ $force_stop == 1 ]]
then
package_check_pid="$(cut -d: -f3 "$lock_file")"
if [ -n "$package_check_pid" ]; then
kill -15 "$package_check_pid"
fi
cleanup
exit 0
fi
#=================================================
# Check if the lock file exist
#=================================================
# If the lock file exist and corresponding process still exists
if test -e "$lock_file" && ps --pid "$(cut -d: -f3 "$lock_file")" | grep --quiet "$(cut -d: -f3 "$lock_file")"
then
if [ $interactive -eq 1 ]; then
echo "The lock file $lock_file already exists."
read -r -p "Do you want to continue anyway? (y/n) : " answer
else
log_critical "The lock file $lock_file already exists. Package check won't continue."
fi
# Set the answer at lowercase only
answer=${answer,,}
if [ "${answer:0:1}" != "y" ]
then
log_critical "Package check cancelled"
fi
fi
# Create the lock file
# $$ is the PID of package_check itself.
echo "start:$(date +%s):$$" > "$lock_file"
#==========================
# Cleanup
# N.B. the traps are added AFTER the lock is taken
# because we don't want to mess with the lock and LXC
# it we ain't the process with the lock...
#==========================
trap cleanup EXIT
trap 'exit 2' TERM
#==========================
# Main code
#==========================
assert_we_are_connected_to_the_internets
assert_we_have_all_dependencies
if [[ $rebuild == 1 ]]
then
rebuild_base_lxc 2>&1 | tee -a "./build_base_lxc.log"
exit 0
fi
self_upgrade
fetch_or_upgrade_package_linter
if [[ -z "${TEST_CONTEXT:-}" ]]; then
if [[ -n "$storage_dir" ]]; then
TEST_CONTEXT=$(mktemp -d "$storage_dir/package_check.XXXXXX")
else
TEST_CONTEXT=$(mktemp -d "/tmp/package_check.XXXXXX")
fi
fi
readonly TEST_CONTEXT
fetch_package_to_test "$path_to_package_to_test"
run_all_tests
exit 0