Include container id in console log.
[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 # Output executor runtime attributes [again] in case the job fails prior to
23 # running setup_executor_env.sh
24 long_line="************************************************************************"
25 OS_ID=$(grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g')
26 OS_VERSION_ID=$(grep '^VERSION_ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g')
27 OS_ARCH=$(uname -m)
28 echo "$long_line"
29 echo "Executor Runtime Attributes:"
30 echo "OS: $OS_ID-$OS_VERSION_ID"
31 echo "Arch: $OS_ARCH"
32 echo "Nomad Hostname: $(grep search /etc/resolv.conf | cut -d' ' -f2 | head -1)"
33 echo "Container ID: $(hostname)"
34 echo "$long_line"
35
36 # Generate gdb-command script to output vpp stack traceback from core files.
37 gdb_cmdfile="/tmp/gdb-commands"
38 cat >$gdb_cmdfile <<'__END__'
39 # Usage:
40 # gdb $BINFILE $CORE -ex 'source -v gdb-commands' -ex quit
41
42 set pagination off
43 thread apply all bt
44
45 define printstack
46   set $i=0
47   while $i < 15
48       frame $i
49       x/i $pc
50       info locals
51       info reg
52       set $i = $i + 1
53   end
54 end
55 thread apply all printstack
56
57 # info proc mappings
58
59 __END__
60
61 STACKTRACE=""
62 # Returns stacktrace filename in STACKTRACE
63 generate_vpp_stacktrace_and_delete_core() {
64     local corefile="$1"
65     if grep -qe 'debug' <<< "$WORKSPACE" ; then
66         local binfile="$WORKSPACE/build-root/install-vpp_debug-native/vpp/bin/vpp"
67     else
68         local binfile="$WORKSPACE/build-root/install-vpp-native/vpp/bin/vpp"
69     fi
70
71     echo "Generating stack trace from core file: $corefile"
72     STACKTRACE="${corefile}.stacktrace"
73     gdb "$binfile" $corefile -ex 'source -v /tmp/gdb-commands' -ex quit > $STACKTRACE
74     # remove the core to save space
75     echo "Removing core file: $corefile"
76     rm -f "$corefile"
77     # Dump stacktrace to console log
78     if [ -f $STACKTRACE ] ; then
79         echo -e "\n=====[ $STACKTRACE ]=====\n$(cat $STACKTRACE)\n=====[ $STACKTRACE ]=====\n"
80     else
81         echo "Stacktrace file not generated!"
82         STACKTRACE=""
83     fi
84 }
85
86 # Delete existing archives dir to ensure current artifact upload
87 rm -rf "$WS_ARCHIVES_DIR"
88 mkdir -p "$WS_ARCHIVES_DIR"
89
90 # Log the build environment variables
91 echo "Logging build environment variables in '$BUILD_ENV_LOG'..."
92 env > $BUILD_ENV_LOG
93
94 echo "WS_ARCHIVE_ARTIFACTS = '$WS_ARCHIVE_ARTIFACTS'"
95 if [ -n "${WS_ARCHIVE_ARTIFACTS}" ]; then
96     pushd $WORKSPACE
97     shopt -s globstar  # Enable globstar to copy archives
98     archive_artifacts=$(echo ${WS_ARCHIVE_ARTIFACTS})
99     shopt -u globstar  # Disable globstar
100     for file in $archive_artifacts; do
101         if [ -f "$file" ] ; then
102             fname="$(basename $file)"
103             # Decompress core.gz file
104             if grep -qe '^core.*\.gz$' <<<"$fname" ; then
105                 echo "Uncompressing core file $file"
106                 gunzip "$file"
107                 file="${file::(-3)}"
108             fi
109             # Convert core file to stacktrace
110             if [ "${fname::4}" = "core" ] ; then
111                 generate_vpp_stacktrace_and_delete_core $file
112                 [ -z "$STACKTRACE" ] && continue
113                 file=$STACKTRACE
114             fi
115             # Set destination filename
116             if [ "${file::26}" = "/tmp/vpp-failed-unittests/" ] ; then
117                 destfile=$WS_ARCHIVES_DIR${file:25}
118             else
119                 destfile=$WS_ARCHIVE_DIR$file
120             fi
121             echo "Archiving '$file' to '$destfile'"
122             destdir="$(dirname $destfile)"
123             mkdir -p $destdir
124             mv $file $destfile
125         else
126             echo "Not archiving '$file'"
127         fi
128     done
129     popd
130 fi
131
132 # find and gzip any 'text' files
133 find $WS_ARCHIVES_DIR -type f -print0 \
134                 | xargs -0r file \
135                 | egrep -e ':.*text.*' \
136                 | cut -d: -f1 \
137                 | xargs -d'\n' -r gzip
138
139 echo "Workspace archived artifacts:"
140 ls -alR $WS_ARCHIVES_DIR