Revert "fix(IPsecUtil): Delete keywords no longer used"
[csit.git] / resources / libraries / bash / function / artifacts.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2023 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 set -exuo pipefail
17
18 function download_artifacts () {
19
20     # Download 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     # Functions conditionally called (see their documentation for side effects):
27     # - download_ubuntu_artifacts
28
29     set -exuo pipefail
30
31     os_id=$(grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g') || {
32         die "Get OS release failed."
33     }
34
35     repo_url_path="${CSIT_DIR}/VPP_REPO_URL"
36     if [ -e "${repo_url_path}" ]; then
37         REPO_URL="$(<${repo_url_path})" || {
38             die "Read repo URL from ${repo_url_path} failed."
39         }
40     else
41         REPO_URL="https://packagecloud.io/install/repositories/fdio/master"
42     fi
43
44     if [ "${os_id}" == "ubuntu" ]; then
45         download_ubuntu_artifacts || die
46     else
47         die "${os_id} is not yet supported."
48     fi
49 }
50
51 function download_ubuntu_artifacts () {
52
53     # Download or install Ubuntu VPP artifacts from packagecloud.io.
54     #
55     # Variables read:
56     # - REPO_URL - FD.io Packagecloud repository.
57     # - VPP_VERSION - VPP version.
58     # - INSTALL - Whether install packages (if set to "true") or download only.
59     #             Default: "false".
60
61     set -exuo pipefail
62
63     curl -s "${REPO_URL}"/script.deb.sh | sudo -E 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     pkgs=$(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 | grep vpp) || {
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 -o Dir::Etc::SourceList=${apt_fdio_repo_file} \
90                       -o Dir::Etc::SourceParts=${apt_fdio_repo_file} \
91                       --no-all-versions show vpp | grep Version: | \
92                       cut -d " " -f 2) || {
93                           die "Retrieval of most recent VPP version failed."
94                       }
95     fi
96
97     set +x
98     for package in ${pkgs}; do
99         # Filter packages with given version
100         pkg_info=$(apt-cache show -- ${package}) || {
101             die "apt-cache show on ${package} failed."
102         }
103         ver=$(echo ${pkg_info} | grep -o "Version: ${VPP_VERSION-}[^ ]*" | \
104               head -1) || true
105         if [ -n "${ver-}" ]; then
106             echo "Found '${VPP_VERSION-}' among '${package}' versions."
107             ver=$(echo "$ver" | cut -d " " -f 2)
108             artifacts+=(${package[@]/%/=${ver-}})
109         else
110             echo "Didn't find '${VPP_VERSION-}' among '${package}' versions."
111         fi
112     done
113     set -x
114
115     if [[ "${INSTALL:-false}" == "true" ]]; then
116         sudo apt-get -y install "${artifacts[@]}" || {
117             die "Install VPP artifacts failed."
118         }
119     else
120         apt-get -y download "${artifacts[@]}" || {
121             die "Download VPP artifacts failed."
122         }
123     fi
124 }