Improve test tag string parsing
[csit.git] / resources / libraries / bash / function / artifacts_hc.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2019 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_hc () {
19
20     # Download or install HC 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_hc
28     # - download_centos_artifacts_hc
29
30     set -exuo pipefail
31
32     os_id=$(grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g') || {
33         die "Get OS release failed."
34     }
35
36     repo_url_path="${CSIT_DIR}/VPP_REPO_URL"
37     if [ -e "${repo_url_path}" ]; then
38         REPO_URL="$(<${repo_url_path})" || {
39             die "Read repo URL from ${repo_url_path} failed."
40         }
41     else
42         REPO_URL="https://packagecloud.io/install/repositories/fdio/master"
43     fi
44
45     if [ "${os_id}" == "ubuntu" ]; then
46         download_ubuntu_artifacts_hc || die
47     elif [ "${os_id}" == "centos" ]; then
48         download_centos_artifacts_hc || die
49     else
50         die "${os_id} is not yet supported."
51     fi
52 }
53
54 function download_ubuntu_artifacts_hc () {
55
56     # Download or install Ubuntu HC artifacts from packagecloud.io.
57     #
58     # Variables read:
59     # - REPO_URL - FD.io Packagecloud repository.
60     # - HC_VERSION - HC version.
61     # - INSTALL - Whether install packages (if set to "true") or download only.
62     #             Default: "false".
63
64     set -exuo pipefail
65
66     curl -s "${REPO_URL}"/script.deb.sh | sudo bash || {
67         die "Packagecloud FD.io repo fetch failed."
68     }
69     # If version is set we will add suffix.
70     artifacts=()
71     hc=(honeycomb)
72     if [ -z "${HC_VERSION-}" ]; then
73         artifacts+=(${hc[@]})
74     else
75         artifacts+=(${hc[@]/%/=${HC_VERSION-}})
76     fi
77
78     if [[ "${INSTALL:-false}" == "true" ]]; then
79         sudo apt-get -y install "${artifacts[@]}" || {
80             die "Install HC artifacts failed."
81         }
82     else
83         apt-get -y download "${artifacts[@]}" || {
84             die "Download HC artifacts failed."
85         }
86     fi
87 }
88
89 function download_centos_artifacts_hc () {
90
91     # Download or install CentOS HC artifacts from packagecloud.io.
92     #
93     # Variables read:
94     # - REPO_URL - FD.io Packagecloud repository.
95     # - HC_VERSION - HC version.
96     # - INSTALL - Whether install packages (if set to "true") or download only.
97     #             Default: "false".
98
99     set -exuo pipefail
100
101     curl -s "${REPO_URL}"/script.rpm.sh | sudo bash || {
102         die "Packagecloud FD.io repo fetch failed."
103     }
104     # If version is set we will add suffix.
105     artifacts=()
106     hc=(honeycomb)
107     if [ -z "${HC_VERSION-}" ]; then
108         artifacts+=(${hc[@]})
109     else
110         artifacts+=(${hc[@]/%/-${HC_VERSION-}})
111     fi
112
113     if [[ "${INSTALL:-false}" == "true" ]]; then
114         sudo yum -y install "${artifacts[@]}" || {
115             die "Install HC artifact failed."
116         }
117     else
118         sudo yum -y install --downloadonly --downloaddir=. "${artifacts[@]}" || {
119             die "Download HC artifacts failed."
120         }
121     fi
122 }