Merge "Delete remains of DMM jobs"
[ci-management.git] / jjb / scripts / post_build_deploy_archives.sh
1 #!/bin/bash
2
3 # Copyright (c) 2021 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 echo "---> jjb/scripts/post_build_deploy_archives.sh"
17
18 set +e  # Do not affect the build result if some part of archiving fails.
19 WS_ARCHIVES_DIR="$WORKSPACE/archives"
20 BUILD_ENV_LOG="$WS_ARCHIVES_DIR/_build-enviroment-variables.log"
21
22 # Generate gdb-command script to output vpp stack traceback from core files.
23 gdb_cmdfile="/tmp/gdb-commands"
24 cat >$gdb_cmdfile <<'__END__'
25 # Usage:
26 # gdb $BINFILE $CORE -ex 'source -v gdb-commands' -ex quit
27
28 set pagination off
29 thread apply all bt
30
31 define printstack
32   set $i=0
33   while $i < 15
34       frame $i
35       x/i $pc
36       info locals
37       info reg
38       set $i = $i + 1
39   end
40 end
41 thread apply all printstack
42
43 # info proc mappings
44
45 __END__
46
47 STACKTRACE=""
48 # Returns stacktrace filename in STACKTRACE
49 generate_vpp_stacktrace_and_delete_core() {
50     local corefile="$1"
51     if grep -qe 'debug' <<< "$WORKSPACE" ; then
52         local binfile="$WORKSPACE/build-root/install-vpp_debug-native/vpp/bin/vpp"
53     else
54         local binfile="$WORKSPACE/build-root/install-vpp-native/vpp/bin/vpp"
55     fi
56
57     echo "Generating stack trace from core file: $corefile"
58     STACKTRACE="${corefile}.stacktrace"
59     gdb "$binfile" $corefile -ex 'source -v /tmp/gdb-commands' -ex quit > $STACKTRACE
60     # remove the core to save space
61     echo "Removing core file: $corefile"
62     rm -f "$corefile"
63     # Dump stacktrace to console log
64     if [ -f $STACKTRACE ] ; then
65         echo -e "\n=====[ $STACKTRACE ]=====\n$(cat $STACKTRACE)\n=====[ $STACKTRACE ]=====\n"
66     else
67         echo "Stacktrace file not generated!"
68         STACKTRACE=""
69     fi
70 }
71
72 # Delete existing archives dir to ensure current artifact upload
73 rm -rf "$WS_ARCHIVES_DIR"
74 mkdir -p "$WS_ARCHIVES_DIR"
75
76 # Log the build environment variables
77 echo "Logging build environment variables in '$BUILD_ENV_LOG'..."
78 env > $BUILD_ENV_LOG
79
80 echo "WS_ARCHIVE_ARTIFACTS = '$WS_ARCHIVE_ARTIFACTS'"
81 if [ -n "${WS_ARCHIVE_ARTIFACTS}" ]; then
82     pushd $WORKSPACE
83     shopt -s globstar  # Enable globstar to copy archives
84     archive_artifacts=$(echo ${WS_ARCHIVE_ARTIFACTS})
85     shopt -u globstar  # Disable globstar
86     for file in $archive_artifacts; do
87         if [ -f "$file" ] ; then
88             fname="$(basename $file)"
89             # Decompress core.gz file
90             if grep -qe '^core.*\.gz$' <<<"$fname" ; then
91                 echo "Uncompressing core file $file"
92                 gunzip "$file"
93                 file="${file::(-3)}"
94             fi
95             # Convert core file to stacktrace
96             if [ "${fname::4}" = "core" ] ; then
97                 generate_vpp_stacktrace_and_delete_core $file
98                 [ -z "$STACKTRACE" ] && continue
99                 file=$STACKTRACE
100             fi
101             # Set destination filename
102             if [ "${file::26}" = "/tmp/vpp-failed-unittests/" ] ; then
103                 destfile=$WS_ARCHIVES_DIR${file:25}
104             else
105                 destfile=$WS_ARCHIVE_DIR$file
106             fi
107             echo "Archiving '$file' to '$destfile'"
108             destdir="$(dirname $destfile)"
109             mkdir -p $destdir
110             mv $file $destfile
111         else
112             echo "Not archiving '$file'"
113         fi
114     done
115     popd
116 fi
117
118 # find and gzip any 'text' files
119 find $WS_ARCHIVES_DIR -type f -print0 \
120                 | xargs -0r file \
121                 | egrep -e ':.*text.*' \
122                 | cut -d: -f1 \
123                 | xargs -d'\n' -r gzip
124
125 echo "Workspace archived artifacts:"
126 ls -alR $WS_ARCHIVES_DIR