Add ARM Taishan testbed33 perftest
[csit.git] / resources / libraries / bash / function / gather.sh
1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Copyright (c) 2019 PANTHEON.tech and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 set -exuo pipefail
16
17 # This library defines functions used mainly by "bootstrap" entry scripts.
18 # Generally, the functions assume "common.sh" library has been sourced already.
19
20 # Keep functions ordered alphabetically, please.
21
22 # TODO: Add a link to bash style guide.
23
24
25 function gather_build () {
26
27     set -exuo pipefail
28
29     # Variables read:
30     # - TEST_CODE - String affecting test selection, usually jenkins job name.
31     # - DOWNLOAD_DIR - Path to directory pybot takes the build to test from.
32     # Variables set:
33     # - DUT - CSIT test/ subdirectory containing suites to execute.
34     # Directories updated:
35     # - ${DOWNLOAD_DIR} - Files needed by tests are gathered here.
36     # Functions called:
37     # - die - Print to stderr and exit, defined in common.sh
38     # - gather_dpdk, gather_vpp, gather_ligato - See their definitions.
39     # Multiple other side effects are possible,
40     # see functions called from here for their current description.
41
42     # TODO: Separate DUT-from-TEST_CODE from gather-for-DUT,
43     #   when the first one becomes relevant for per_patch.
44
45     pushd "${DOWNLOAD_DIR}" || die "Pushd failed."
46     case "${TEST_CODE}" in
47         *"hc2vpp"*)
48             DUT="hc2vpp"
49             # FIXME: Avoid failing on empty ${DOWNLOAD_DIR}.
50             ;;
51         *"vpp"*)
52             DUT="vpp"
53             gather_vpp || die "The function should have died on error."
54             ;;
55         *"ligato"*)
56             DUT="kubernetes"
57             gather_ligato || die "The function should have died on error."
58             ;;
59         *"dpdk"*)
60             DUT="dpdk"
61             gather_dpdk || die "The function should have died on error."
62             ;;
63         *)
64             die "Unable to identify DUT type from: ${TEST_CODE}"
65             ;;
66     esac
67     popd || die "Popd failed."
68 }
69
70
71 function gather_dpdk () {
72
73     set -exuo pipefail
74
75     # Ensure latest DPDK archive is downloaded.
76     #
77     # Variables read:
78     # - TEST_CODE - The test selection string from environment or argument.
79     # Hardcoded:
80     # - dpdk archive name to download if TEST_CODE is not time based.
81     # Directories updated:
82     # - ./ - Assumed ${DOWNLOAD_DIR}, dpdk-*.tar.xz is downloaded if not there.
83     # Functions called:
84     # - die - Print to stderr and exit, defined in common.sh
85
86     dpdk_repo="https://fast.dpdk.org/rel"
87     # Use downloaded packages with specific version
88     if [[ "${TEST_CODE}" == *"daily"* ]] || \
89        [[ "${TEST_CODE}" == *"weekly"* ]] || \
90        [[ "${TEST_CODE}" == *"timed"* ]];
91     then
92         echo "Downloading latest DPDK packages from repo..."
93         # URL is not in quotes, calling command from variable keeps them.
94         wget_command=("wget" "--no-check-certificate" "-nv" "-O" "-")
95         wget_command+=("${dpdk_repo}")
96         dpdk_stable_ver="$("${wget_command[@]}" | grep -v "2015"\
97             | grep -Eo 'dpdk-[^\"]+xz' | tail -1)" || {
98             die "Composite piped command failed."
99         }
100     else
101         echo "Downloading DPDK package of specific version from repo ..."
102         # Downloading DPDK version based on what VPP is using. Currently
103         # it is not easy way to detect from VPP version automatically.
104         dpdk_stable_ver="$(< "${CSIT_DIR}/DPDK_VPP_VER")".tar.xz || {
105             die "Failed to read DPDK VPP version!"
106         }
107     fi
108     # TODO: Use "wget -N" instead checking for file presence?
109     if [[ ! -f "${dpdk_stable_ver}" ]]; then
110         wget -nv --no-check-certificate "${dpdk_repo}/${dpdk_stable_ver}" || {
111             die "Failed to get DPDK package from: ${dpdk_repo}"
112         }
113     fi
114 }
115
116
117 function gather_ligato () {
118
119     set -exuo pipefail
120
121     # Build docker image (with vpp, ligato and vpp-agent),
122     # and put it to ${DOWNLOAD_DIR}/.
123     #
124     # Access rights needed for:
125     # - "wget", "git clone", "dpdk -x", "cd" above ${CSIT_DIR}.
126     # - "sudo" without password.
127     # - With sudo:
128     #   - "dpdk -i" is allowed.
129     #   - "docker" commands have everything they needs.
130     # Variables read:
131     # - DOWNLOAD_DIR - Path to directory pybot takes the build to test from.
132     # - CSIT_DIR - Path to existing root of local CSIT git repository.
133     # Files read:
134     # - ${CSIT_DIR}/VPP_AGENT_STABLE_VER - Vpp agent version to use.
135     # Directories updated:
136     # - ${DOWNLOAD_DIR} - Docker image stored, VPP *.deb stored and deleted.
137     # - /tmp/vpp - VPP is unpacked there, not cleaned afterwards.
138     # - ${CSIT_DIR}/vpp-agent - Created, vpp-agent git repo si cloned there.
139     #   - Also, various temporary files are stored there.
140     # System consequences:
141     # - Docker package is installed.
142     # - Presumably dockerd process is started.
143     # - The ligato/dev-vpp-agent docker image is downloaded.
144     # - Results of subsequent image manipulation are probably left lingering.
145     # Other hardcoded values:
146     # - Docker .deb file name to download and install.
147     # Functions called:
148     # - die - Print to stderr and exit, defined in common_functions.sh
149     # - gather_vpp - See eponymous fragment file assumend to be sourced already.
150     # TODO: What is the best order of description items?
151
152     # TODO: Many of the following comments act as abstraction.
153     #   But the abstracted blocks are mostly one-liners (plus "|| die"),
154     #   so maybe it is not worth introducing fragments/functions for the blocks.
155     # TODO: This fragment is too long anyway, split it up.
156
157     gather_vpp || die "The function should have died on error."
158
159     mkdir -p /tmp/vpp && rm -f /tmp/vpp/* || {
160         die "Failed to create temporary directory!"
161     }
162     dpkg -x "${DOWNLOAD_DIR}/vpp_"*".deb" "/tmp/vpp" || {
163         die "Failed to extract VPP packages for kubernetes!"
164     }
165
166     ligato_repo_url="https://github.com/ligato/"
167     vpp_agent_stable_ver="$(< "${CSIT_DIR}/VPP_AGENT_STABLE_VER")" || {
168         die "Failed to read vpp-agent stable version!"
169     }
170
171     # Clone & checkout stable vpp-agent.
172     cd "${CSIT_DIR}" || die "Change directory failed!"
173     git clone -b master --single-branch \
174         "${ligato_repo_url}/vpp-agent" "vpp-agent" || {
175         die "Failed to run: git clone ${ligato_repo_url}/vpp-agent!"
176     }
177     cd "vpp-agent" || die "Change directory failed!"
178
179     # Install Docker.
180     curl -fsSL https://get.docker.com | sudo bash || {
181         die "Failed to install Docker package!"
182     }
183
184     # Pull ligato/dev_vpp_agent docker image and re-tag as local.
185     sudo docker pull "ligato/dev-vpp-agent:${vpp_agent_stable_ver}" || {
186         die "Failed to pull Docker image!"
187     }
188     params=(ligato/dev-vpp-agent:${vpp_agent_stable_ver} dev_vpp_agent:latest)
189     sudo docker tag "${params[@]}" || {
190         die "Failed to tag Docker image!"
191     }
192
193     # Start dev_vpp_agent container as daemon.
194     sudo docker run --rm -itd --name "agentcnt" "dev_vpp_agent" bash || {
195         die "Failed to run Docker image!"
196     }
197
198     # Copy latest vpp api into running container.
199     sudo docker exec agentcnt rm -rf "agentcnt:/usr/share/vpp/api" || {
200         die "Failed to remove previous API!"
201     }
202     sudo docker cp "/tmp/vpp/usr/share/vpp/api" "agentcnt:/usr/share/vpp" || {
203         die "Failed to copy files Docker image!"
204     }
205
206     # Recompile vpp-agent.
207     script_arg=". ~/.bashrc; cd /go/src/github.com/ligato/vpp-agent"
208     script_arg+=" && make generate && make install"
209     sudo docker exec -i agentcnt script -qec "${script_arg}" || {
210         die "Failed to recompile vpp-agent in Docker image!"
211     }
212     # Make sure .deb files of other version are not present.
213     rm_cmd="rm -vf /opt/vpp-agent/dev/vpp/build-root/vpp*.deb /opt/vpp/*.deb"
214     sudo docker exec agentcnt bash -c "${rm_cmd}" || {
215         die "Failed to remove VPP debian packages!"
216     }
217     for f in "${DOWNLOAD_DIR}"/*; do
218         sudo docker cp "$f" "agentcnt:/opt/vpp-agent/dev/vpp/build-root"/ || {
219             die "Failed to copy files to Docker image!"
220         }
221     done
222     # Save container state.
223     sudo docker commit "$(sudo docker ps -q)" "dev_vpp_agent:latest" || {
224         die "Failed to commit state of Docker image!"
225     }
226
227     # Build prod_vpp_agent docker image.
228     cd "docker/prod" || die "Change directory failed."
229     sudo docker build --tag "prod_vpp_agent" --no-cache "." || {
230         die "Failed to build Docker image!"
231     }
232     # Export Docker image.
233     sudo docker save "prod_vpp_agent" | gzip > "prod_vpp_agent.tar.gz" || {
234         die "Failed to save Docker image!"
235     }
236     docker_image="$(readlink -e "prod_vpp_agent.tar.gz")" || {
237         die "Failed to get Docker image path!"
238     }
239     rm -r "${DOWNLOAD_DIR}/vpp"* || die "Failed to remove VPP packages!"
240     mv "${docker_image}" "${DOWNLOAD_DIR}"/ || die "Failed to move image!"
241 }
242
243
244 function gather_vpp () {
245
246     set -exuo pipefail
247
248     # Variables read:
249     # - BASH_FUNCTION_DIR - Bash directory with functions.
250     # - TEST_CODE - The test selection string from environment or argument.
251     # - DOWNLOAD_DIR - Path to directory pybot takes the build to test from.
252     # - CSIT_DIR - Path to existing root of local CSIT git repository.
253     # Files read:
254     # - ${CSIT_DIR}/DPDK_STABLE_VER - DPDK version to use
255     #   by csit-vpp not-timed jobs.
256     # - ${CSIT_DIR}/VPP_STABLE_VER_UBUNTU - VPP version to use by those.
257     # - ../vpp*.deb - Relative to ${DOWNLOAD_DIR}, copied for vpp-csit jobs.
258     # Directories updated:
259     # - ${DOWNLOAD_DIR}, vpp-*.deb files are copied here for vpp-csit jobs.
260     # - ./ - Assumed ${DOWNLOAD_DIR}, vpp-*.deb files
261     #   are downloaded here for csit-vpp.
262     # Functions called:
263     # - die - Print to stderr and exit, defined in common_functions.sh
264     # Bash scripts executed:
265     # - ${CSIT_DIR}/resources/tools/scripts/download_install_vpp_pkgs.sh
266     #   - Should download and extract requested files to ./.
267
268     case "${TEST_CODE}" in
269         # Not csit-vpp as this code is re-used by ligato gathering.
270         "csit-"*)
271             # Use downloaded packages with specific version.
272             if [[ "${TEST_CODE}" == *"daily"* ]] || \
273                ([[ "${TEST_CODE}" == *"weekly"* ]] && \
274                 [[ "${TEST_CODE}" != *"device"* ]]) || \
275                [[ "${TEST_CODE}" == *"semiweekly"* ]];
276             then
277                 warn "Downloading latest VPP packages from Packagecloud."
278             else
279                 warn "Downloading stable VPP packages from Packagecloud."
280                 VPP_VERSION="$(<"${CSIT_DIR}/VPP_STABLE_VER_UBUNTU_BIONIC")" || {
281                     die "Read VPP stable version failed."
282                 }
283             fi
284             source "${BASH_FUNCTION_DIR}/artifacts.sh" || die "Source failed."
285             download_artifacts || die
286             ;;
287         "vpp-csit-"*)
288             # Use locally built packages.
289             mv "${DOWNLOAD_DIR}"/../"vpp"*".deb" "${DOWNLOAD_DIR}"/ || {
290                 die "Move command failed."
291             }
292             ;;
293         *)
294             die "Unable to identify job type from: ${TEST_CODE}"
295             ;;
296     esac
297 }