Bash functions style cleanup
[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
21     # Download or install VPP artifacts from packagecloud.io.
22     #
23     # Variables read:
24     # - CSIT_DIR - Path to existing root of local CSIT git repository.
25     # Variables set:
26     # - REPO_URL - FD.io Packagecloud repository.
27     # Functions conditionally called (see their documentation for side effects):
28     # - download_ubuntu_artifacts
29     # - download_centos_artifacts
30     # - download_opensuse_artifacts
31
32     set -exuo pipefail
33
34     os_id=$(grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g') || {
35         die "Get OS release failed."
36     }
37
38     repo_url_path="${CSIT_DIR}/VPP_REPO_URL"
39     if [ -e "${repo_url_path}" ]; then
40         REPO_URL="$(<${repo_url_path})" || {
41             die "Read repo URL from ${repo_url_path} failed."
42         }
43     else
44         REPO_URL="https://packagecloud.io/install/repositories/fdio/master"
45     fi
46
47     if [ "${os_id}" == "ubuntu" ]; then
48         download_ubuntu_artifacts || die
49     elif [ "${os_id}" == "centos" ]; then
50         download_centos_artifacts || die
51     elif [ "${os_id}" == "opensuse" ]; then
52         download_opensuse_artifacts || die
53     else
54         die "${os_id} is not yet supported."
55     fi
56 }
57
58 function download_ubuntu_artifacts () {
59
60     # Download or install Ubuntu VPP artifacts from packagecloud.io.
61     #
62     # Variables read:
63     # - REPO_URL - FD.io Packagecloud repository.
64     # - VPP_VERSION - VPP version.
65     # - INSTALL - Whether install packages (if set to "true") or download only.
66     #             Default: "false".
67
68     set -exuo pipefail
69
70     curl -s "${REPO_URL}"/script.deb.sh | sudo bash || {
71         die "Packagecloud FD.io repo fetch failed."
72     }
73     # If version is set we will add suffix.
74     artifacts=()
75     both_quotes='"'"'"
76     match="[^${both_quotes}]*"
77     qmatch="[${both_quotes}]\?"
78     sed_command="s#.*apt_source_path=${qmatch}\(${match}\)${qmatch}#\1#p"
79     apt_fdio_repo_file=$(curl -s "${REPO_URL}"/script.deb.sh | \
80                          sed -n ${sed_command}) || {
81                              die "Local fdio repo file path fetch failed."
82                          }
83
84     if [ ! -f ${apt_fdio_repo_file} ]; then
85         die "${apt_fdio_repo_file} not found, \
86             repository installation was not successful."
87     fi
88
89     packages=$(apt-cache -o Dir::Etc::SourceList=${apt_fdio_repo_file} \
90                -o Dir::Etc::SourceParts=${apt_fdio_repo_file} dumpavail \
91                | grep Package: | cut -d " " -f 2) || {
92                    die "Retrieval of available VPP packages failed."
93                }
94     if [ -z "${VPP_VERSION-}" ]; then
95         # If version is not specified, find out the most recent version
96         VPP_VERSION=$(apt-cache -o Dir::Etc::SourceList=${apt_fdio_repo_file} \
97                       -o Dir::Etc::SourceParts=${apt_fdio_repo_file} \
98                       --no-all-versions show vpp | grep Version: | \
99                       cut -d " " -f 2) || {
100                           die "Retrieval of most recent VPP version failed."
101                       }
102     fi
103
104     set +x
105     for package in ${packages}; do
106         # Filter packages with given version
107         pkg_info=$(apt-cache show ${package}) || {
108             die "apt-cache show on ${package} failed."
109         }
110         ver=$(echo ${pkg_info} | grep -o "Version: ${VPP_VERSION-}[^ ]*" | \
111               head -1) || true
112         if [ -n "${ver-}" ]; then
113             echo "Found '${VPP_VERSION-}' among '${package}' versions."
114             ver=$(echo "$ver" | cut -d " " -f 2)
115             artifacts+=(${package[@]/%/=${ver-}})
116         else
117             echo "Didn't find '${VPP_VERSION-}' among '${package}' versions."
118         fi
119     done
120     set -x
121
122     if [[ "${INSTALL:-false}" == "true" ]]; then
123         sudo apt-get -y install "${artifacts[@]}" || {
124             die "Install VPP artifacts failed."
125         }
126     else
127         apt-get -y download "${artifacts[@]}" || {
128             die "Download VPP artifacts failed."
129         }
130     fi
131 }
132
133 function download_centos_artifacts () {
134
135     # Download or install CentOS VPP artifacts from packagecloud.io.
136     #
137     # Variables read:
138     # - REPO_URL - FD.io Packagecloud repository.
139     # - VPP_VERSION - VPP version.
140     # - INSTALL - Whether install packages (if set to "true") or download only.
141     #             Default: "false".
142
143     set -exuo pipefail
144
145     curl -s "${REPO_URL}"/script.rpm.sh | sudo bash || {
146         die "Packagecloud FD.io repo fetch failed."
147     }
148     # If version is set we will add suffix.
149     artifacts=()
150     packages=(vpp vpp-selinux-policy vpp-devel vpp-lib vpp-plugins vpp-api-python)
151     if [ -z "${VPP_VERSION-}" ]; then
152         artifacts+=(${packages[@]})
153     else
154         artifacts+=(${packages[@]/%/-${VPP_VERSION-}})
155     fi
156
157     if [[ "${INSTALL:-false}" == "true" ]]; then
158         sudo yum -y install "${artifacts[@]}" || {
159             die "Install VPP artifact failed."
160         }
161     else
162         sudo yum -y install --downloadonly --downloaddir=. "${artifacts[@]}" || {
163             die "Download VPP artifacts failed."
164         }
165     fi
166 }
167
168 function download_opensuse_artifacts () {
169
170     # Download or install OpenSuSE VPP artifacts from packagecloud.io.
171     #
172     # Variables read:
173     # - REPO_URL - FD.io Packagecloud repository.
174     # - VPP_VERSION - VPP version.
175     # - INSTALL - Whether install packages (if set to "true") or download only.
176     #             Default: "false".
177
178     set -exuo pipefail
179
180     curl -s "${REPO_URL}"/script.rpm.sh | sudo bash || {
181         die "Packagecloud FD.io repo fetch failed."
182     }
183     # If version is set we will add suffix.
184     artifacts=()
185     packages=(vpp vpp-devel vpp-lib vpp-plugins libvpp0)
186     if [ -z "${VPP_VERSION-}" ]; then
187         artifacts+=(${packages[@]})
188     else
189         artifacts+=(${packages[@]/%/-${VPP_VERSION-}})
190     fi
191
192     if [[ "${INSTALL:-false}" == "true" ]]; then
193         sudo yum -y install "${artifacts[@]}" || {
194             die "Install VPP artifact failed."
195         }
196     else
197         sudo yum -y install --downloadonly --downloaddir=. "${artifacts[@]}" || {
198             die "Download VPP artifacts failed."
199         }
200     fi
201 }