-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrelease.sh
executable file
·228 lines (195 loc) · 7.95 KB
/
release.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
#!/bin/bash
if test $# -ne 2
then
echo "Usage: $0 <release.csv> <path-to-beats-repo>" >&2
exit 1
fi
BATCH="$1"
BEATS_DIR="$2"
OUTPUT=generated.output
ARGS="-F whitespace -O deduplicate,globals"
FILESETS=""
DO_MODULE=${DO_MODULE:-1}
INSTALL_MODULE=${INSTALL_MODULE:-1}
DO_PACKAGE=${DO_PACKAGE:-1}
die() {
logerr "$@"
exit 1
}
logerr() {
echo "$0: $*" >&2
}
convert_device() {
ROW="$1"
DEV=$(echo $ROW | cut -d, -f1)
MOD=$(echo $ROW | cut -d, -f2)
FST=$(echo $ROW | cut -d, -f3)
VENDOR=$(echo $ROW | cut -d, -f4)
PROD=$(echo $ROW | cut -d, -f5)
TYPE=$(echo $ROW | cut -d, -f6)
VERSION=$(echo $ROW | cut -d, -f7)
CATEGORIES=$(echo $ROW | cut -d, -f9 | tr ';' ,)
PORT=$(echo $ROW | cut -d, -f10)
DIR=$OUTPUT/$DEV
PRODINFO="--vendor $VENDOR --product $PROD --type $TYPE"
test -d devices/$DEV || die "Device $DEV does not exists"
mkdir -p $DIR/module $DIR/package
if test "$DO_MODULE" = "1"
then
echo "$DEV: generating module [$LINE/$LINES]"
echo "$DEV: generating module [$LINE/$LINES]" >> $DIR/output.log 2>&1
./nwdevice2filebeat generate module --device devices/$DEV/ $ARGS $PRODINFO --output $DIR/module --port $PORT --module $MOD --fileset $FST >> $DIR/output.log 2>&1 || die "Failed generating module"
fi
echo "$DEV: generating package [$LINE/$LINES]"
echo "$DEV: generating package [$LINE/$LINES]" >> $DIR/output.log 2>&1
./nwdevice2filebeat generate package --device devices/$DEV/ $ARGS $PRODINFO --output $DIR/package --port $PORT \
--module $MOD --fileset $FST --categories "$CATEGORIES" --version $VERSION >> $DIR/output.log 2>&1 || die "Failed generating package"
echo "$DEV: generating logs [$LINE/$LINES]"
echo "$DEV: generating logs [$LINE/$LINES]" >> $DIR/output.log 2>&1
mkdir -p $DIR/module/$MOD/$FST/test
./nwdevice2filebeat generate logs --device devices/$DEV $ARGS --output $DIR/module/$MOD/$FST/test/generated.log --lines 100 >> $DIR/output.log 2>&1
if test $? -eq 0
then
echo "$DEV: testing logs [$LINE/$LINES]"
echo "$DEV: testing logs [$LINE/$LINES]" >> $DIR/output.log 2>&1
./nwdevice2filebeat run --device devices/$DEV $ARGS --logs $DIR/module/$MOD/$FST/test/generated.log >> $DIR/output.log 2>&1 || logerr "Failed testing logs"
else
logerr "Failed generating logs for $DEV"
fi
mkdir -p $DIR/package/$MOD/_dev/deploy/docker/sample_logs
cp $DIR/module/$MOD/$FST/test/generated.log $DIR/package/$MOD/_dev/deploy/docker/sample_logs/$MOD-$FST-generated.log
for log_file in $(ls samples/$DEV/*.log 2>/dev/null)
do
cp $log_file $DIR/module/$MOD/$FST/test/
cp $log_file $DIR/package/$MOD/_dev/deploy/docker/sample_logs/$MOD-$FST-$(basename $log_file)
done
}
module_to_fb() {
ROW="$1"
DEV=$(echo $ROW | cut -d, -f1)
MOD=$(echo $ROW | cut -d, -f2)
FST=$(echo $ROW | cut -d, -f3)
MODULES_DIR="$BEATS_DIR/x-pack/filebeat/module"
if test -d "$MODULES_DIR/$MOD"
then
# Merge in existing module
test -d "$MODULES_DIR/$MOD/$FST" && die "fileset $MOD/$FST already exists!"
cp -a "$OUTPUT/$DEV/module/$MOD/$FST" "$MODULES_DIR/$MOD/" || die "Failed copying fileset $MOD/$FST for $DEV"
# Merge config.yml
(cat "$OUTPUT/$DEV/module/$MOD/_meta/config.yml" | sed 's/^- module:.*//' >> "$MODULES_DIR/$MOD/_meta/config.yml") || die "Failed merging config.yml"
echo "Copied fileset $MOD/$FST"
# Merge docs
SRC="$OUTPUT/$DEV/module/$MOD/_meta/docs.asciidoc"
DST="$MODULES_DIR/$MOD/_meta/docs.asciidoc"
merge_docs "$SRC" "$DST" || die "Failed merging docs"
else
# Copy new module
cp -a "$OUTPUT/$DEV/module/$MOD" "$MODULES_DIR/" || die "Failed copying mod=$MOD for $DEV"
echo "Copied module $MOD"
fi
for test_file in $(ls "$MODULES_DIR/$MOD/$FST/test/"*.log 2> /dev/null)
do
touch "$test_file"-expected.json
done
}
merge_docs() {
SRC="$1"
DST="$2"
SRC_FROM_LINE=$(grep -n '^==== .* fileset settings' "$SRC" | cut -d: -f1)
SRC_TO_LINE=$(grep -n ':fileset_ex!:' "$SRC" | cut -d: -f1)
SRC_TO_LINE=$(expr $SRC_TO_LINE + 1)
test "$(echo $SRC_FROM_LINE | wc -l)" -eq 1 -a "$(echo $SRC_TO_LINE | wc -l)" -eq 1 || die "Unable to extract fileset from docs"
SRC_FROM_LINE=$(expr $SRC_FROM_LINE - 1)
SRC_NUM_LINES=$(expr $SRC_TO_LINE - $SRC_FROM_LINE + 1)
DST_LINE=$(grep -n ':fileset_ex!:' "$DST" | tail -1 | cut -d: -f1)
test "$DST_LINE" = "" && die "Unable to find last fileset in target docs"
DST_LINE=$(expr $DST_LINE + 1)
DST_NUM_LINES=$(expr $(cat "$DST" | wc -l) + 0)
head -$DST_LINE "$DST" > "$DST".tmp
head -$SRC_TO_LINE "$SRC" | tail -$SRC_NUM_LINES >> "$DST".tmp
tail -$(expr $DST_NUM_LINES - $DST_LINE) "$DST" >> "$DST".tmp
mv "$DST".tmp "$DST"
}
test_fileset() {
ROW="$1"
DEV=$(echo $ROW | cut -d, -f1)
MOD=$(echo $ROW | cut -d, -f2)
FST=$(echo $ROW | cut -d, -f3)
FILES="$(ls $OUTPUT/$DEV/module/$MOD/$FST/test/*.log 2>/dev/null)"
if test -z "$FILES"
then
echo ""
echo "No test files in $MOD/$FST"
return 0
fi
pushd "$BEATS_DIR/x-pack/filebeat"
echo ""
echo "Generating golden files for $MOD/$FST [$LINE/$LINES]"
MODULES_PATH="$BEATS_DIR/x-pack/filebeat/module" \
INTEGRATION_TESTS=1 \
TESTING_FILEBEAT_MODULES=$MOD \
TESTING_FILEBEAT_FILESETS=$FST \
GENERATE=1 \
pytest -v -s tests/system/test_xpack_modules.py || die "Generating golden files failed"
echo ""
echo "Testing $MOD/$FST [$LINE/$LINES]"
MODULES_PATH="$BEATS_DIR/x-pack/filebeat/module" \
INTEGRATION_TESTS=1 \
TESTING_FILEBEAT_MODULES=$MOD \
TESTING_FILEBEAT_FILESETS=$FST \
pytest -v -s tests/system/test_xpack_modules.py || die "Generating golden files failed"
popd
}
foreach_line() {
FILE="$1"
ACT="$2"
test -f "$FILE" || die "No such file $FILE"
LINES=$(cat "$FILE" | wc -l)
LINES=$(expr $LINES + 0)
test "$LINES" -gt 0 || die "Cannot read $FILE"
LINE=0
while test $LINE -lt $LINES
do
LINE=$(expr $LINE + 1)
ROW=$(head -$LINE "$FILE" | tail -1)
$ACT "$ROW"
done
}
if test $DO_MODULE -eq 1
then
test -d "$BEATS_DIR/libbeat" -a "$BEATS_DIR/.git" || die "Beats dir is not the beats repo"
echo "This will destroy all your changes in Beats repo ($BEATS_DIR)."
read -p "Continue? [y/n] " ANS
test $ANS = 'y' -o $ANS = 'Y' -o $ANS = 'yes' || die "cancelled by user."
fi
if test -d venv
then
. venv/bin/activate
else
python3 -mvenv venv || die "failed creating virtualenv"
. venv/bin/activate
pip install -r "$BEATS_DIR/libbeat/tests/system/requirements.txt" || die "Failed installing test requirements"
fi
rm -rf $OUTPUT
foreach_line "$BATCH" convert_device
if [ "$DO_MODULE" = 1 -a "$INSTALL_MODULE" = 1 ]
then
echo ''
echo 'Cleaning up beats repo'
pushd "$BEATS_DIR" && git clean -df && git reset --hard && popd || die "Failed cleaning beats repo"
foreach_line "$BATCH" module_to_fb
echo ''
echo 'Running mage update'
pushd "$BEATS_DIR/x-pack/filebeat" && mage update && popd || die "Failed mage update"
echo ''
echo 'Building filebeat'
pushd "$BEATS_DIR/x-pack/filebeat" && mage build && go test -c && popd || die "Failed mage update"
echo ''
echo 'Validating fields'
pushd "$BEATS_DIR/x-pack/filebeat" && ./filebeat export index-pattern > /dev/null && popd || die "Index-pattern export failed: Broken fields!"
pushd "$BEATS_DIR/x-pack/filebeat" && ./filebeat export template > /dev/null && popd || die "Template export failed: Broken fields!"
foreach_line "$BATCH" test_fileset
echo ''
echo 'Updating filebeat docs'
pushd "$BEATS_DIR/filebeat" && make update && popd || die "Failed cleaning beats repo"
fi