Add ARM Taishan testbed33 perftest
[csit.git] / resources / libraries / bash / function / artifacts.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2019 Cisco and/or its affiliates.
4 # Copyright (c) 2019 PANTHEON.tech and/or its affiliates.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 set -exuo pipefail
18
19 function download_artifacts () {
20     # Get and/or install VPP artifacts from packagecloud.io.
21     #
22     # Variables read:
23     # - CSIT_DIR - Path to existing root of local CSIT git repository.
24     # Variables set:
25     # - REPO_URL - FD.io Packagecloud repository.
26
27     set -exuo pipefail
28
29     os_id=$(grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g') || {
30         die "Get OS release failed."
31     }
32
33     repo_url_path="${CSIT_DIR}/VPP_REPO_URL"
34     if [ -e "${repo_url_path}" ]; then
35         REPO_URL="$(<${repo_url_path})" || {
36             die "Read repo URL from ${repo_url_path} failed."
37         }
38     else
39         REPO_URL="https://packagecloud.io/install/repositories/fdio/master"
40     fi
41
42     if [ "${os_id}" == "ubuntu" ]; then
43         download_ubuntu_artifacts || die
44     elif [ "${os_id}" == "centos" ]; then
45         download_centos_artifacts || die
46     elif [ "${os_id}" == "opensuse" ]; then
47         download_opensuse_artifacts || die
48     else
49         die "${os_id} is not yet supported."
50     fi
51 }
52
53 function download_ubuntu_artifacts () {
54     # Get and/or install Ubuntu VPP artifacts from packagecloud.io.
55     #
56     # Variables read:
57     # - REPO_URL - FD.io Packagecloud repository.
58     # - VPP_VERSION - VPP version.
59     # - INSTALL - If install packages or download only. Default: download
60
61     set -exuo pipefail
62
63     curl -s "${REPO_URL}"/script.deb.sh | sudo bash || {
64         die "Packagecloud FD.io repo fetch failed."
65     }
66     # If version is set we will add suffix.
67     artifacts=()
68     BOTH_QUOTES='"'"'"
69     MATCH="[^${BOTH_QUOTES}]*"
70     QMATCH="[${BOTH_QUOTES}]\?"
71     SED_COMMAND="s#.*apt_source_path=${QMATCH}\(${MATCH}\)${QMATCH}#\1#p"
72     APT_FDIO_REPO_FILE=$(curl -s "${REPO_URL}"/script.deb.sh | \
73                          sed -n ${SED_COMMAND}) || {
74                              die "Local fdio repo file path fetch failed."
75                          }
76
77     if [ ! -f ${APT_FDIO_REPO_FILE} ]; then
78         die "${APT_FDIO_REPO_FILE} not found, \
79             repository installation was not successful."
80     fi
81
82     packages=$(apt-cache -o Dir::Etc::SourceList=${APT_FDIO_REPO_FILE} \
83                -o Dir::Etc::SourceParts=${APT_FDIO_REPO_FILE} dumpavail \
84                | grep Package: | cut -d " " -f 2) || {
85                    die "Retrieval of available VPP packages failed."
86                }
87     if [ -z "${VPP_VERSION-}" ]; then
88         # If version is not specified, find out the most recent version
89         VPP_VERSION=$(apt-cache --no-all-versions show vpp | grep Version: | \
90                       cut -d " " -f 2) || {
91                           die "Retrieval of most recent VPP version failed."
92                       }
93     fi
94
95     set +x
96     for package in ${packages}; do
97         # Filter packages with given version
98         pkg_info=$(apt-cache show ${package}) || {
99             die "apt-cache show on ${package} failed."
100         }
101         ver=$(echo ${pkg_info} | grep -o "Version: ${VPP_VERSION-}[^ ]*") || true
102         if [ -n "${ver-}" ]; then
103             echo "Found '${VPP_VERSION-}' among '${package}' versions."
104             ver=$(echo "$ver" | cut -d " " -f 2)
105             artifacts+=(${package[@]/%/=${ver-}})
106         else
107             echo "Didn't find '${VPP_VERSION-}' among '${package}' versions."
108         fi
109     done
110     set -x
111
112     if [ "${INSTALL:-false}" = true ]; then
113         sudo apt-get -y install "${artifacts[@]}" || {
114             die "Install VPP artifacts failed."
115         }
116     else
117         apt-get -y download "${artifacts[@]}" || {
118             die "Download VPP artifacts failed."
119         }
120     fi
121 }
122
123 function download_centos_artifacts () {
124     # Get and/or install CentOS VPP artifacts from packagecloud.io.
125     #
126     # Variables read:
127     # - REPO_URL - FD.io Packagecloud repository.
128     # - VPP_VERSION - VPP version.
129     # - INSTALL - If install packages or download only. Default: download
130
131     set -exuo pipefail
132
133     curl -s "${REPO_URL}"/script.rpm.sh | sudo bash || {
134         die "Packagecloud FD.io repo fetch failed."
135     }
136     # If version is set we will add suffix.
137     artifacts=()
138     packages=(vpp vpp-selinux-policy vpp-devel vpp-lib vpp-plugins vpp-api-python)
139     if [ -z "${VPP_VERSION-}" ]; then
140         artifacts+=(${packages[@]})
141     else
142         artifacts+=(${packages[@]/%/-${VPP_VERSION-}})
143     fi
144
145     if [ "${INSTALL:-false}" = true ]; then
146         sudo yum -y install "${artifacts[@]}" || {
147             die "Install VPP artifact failed."
148         }
149     else
150         sudo yum -y install --downloadonly --downloaddir=. "${artifacts[@]}" || {
151             die "Download VPP artifacts failed."
152         }
153     fi
154 }
155
156 function download_opensuse_artifacts () {
157     # Get and/or install OpenSuSE VPP artifacts from packagecloud.io.
158     #
159     # Variables read:
160     # - REPO_URL - FD.io Packagecloud repository.
161     # - VPP_VERSION - VPP version.
162     # - INSTALL - If install packages or download only. Default: download
163
164     set -exuo pipefail
165
166     curl -s "${REPO_URL}"/script.rpm.sh | sudo bash || {
167         die "Packagecloud FD.io repo fetch failed."
168     }
169     # If version is set we will add suffix.
170     artifacts=()
171     packages=(vpp vpp-devel vpp-lib vpp-plugins libvpp0)
172     if [ -z "${VPP_VERSION-}" ]; then
173         artifacts+=(${packages[@]})
174     else
175         artifacts+=(${packages[@]/%/-${VPP_VERSION-}})
176     fi
177
178     if [ "${INSTALL:-false}" = true ]; then
179         sudo yum -y install "${artifacts[@]}" || {
180             die "Install VPP artifact failed."
181         }
182     else
183         sudo yum -y install --downloadonly --downloaddir=. "${artifacts[@]}" || {
184             die "Download VPP artifacts failed."
185         }
186     fi
187 }