FIX: Correct the packagecloud.io URL path
[csit.git] / resources / libraries / bash / function / artifacts.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2018 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     # Get and/or install VPP artifacts from packagecloud.io.
20     #
21     # Variables read:
22     # - CSIT_DIR - Path to existing root of local CSIT git repository.
23     # Variables set:
24     # - REPO_URL - FD.io Packagecloud repository.
25
26     set -exuo pipefail
27
28     os_id=$(grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g') || {
29         die "Get OS release failed."
30     }
31
32     repo_url_path="${CSIT_DIR}/VPP_REPO_URL"
33     if [ -e "${repo_url_path}" ]; then
34         REPO_URL="$(<${repo_url_path})" || {
35             die "Read repo URL from ${repo_url_path} failed."
36         }
37     else
38         REPO_URL="https://packagecloud.io/install/repositories/fdio/master"
39     fi
40
41     if [ "${os_id}" == "ubuntu" ]; then
42         download_ubuntu_artifacts || die
43     elif [ "${os_id}" == "centos" ]; then
44         download_centos_artifacts || die
45     elif [ "${os_id}" == "opensuse" ]; then
46         download_opensuse_artifacts || die
47     else
48         die "${os_id} is not yet supported."
49     fi
50 }
51
52 function download_ubuntu_artifacts () {
53     # Get and/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     # - DKMS_VERSION - DKMS 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     vpp=(vpp vpp-dbg vpp-dev vpp-lib vpp-plugins)
69     if [ -z "${VPP_VERSION-}" ]; then
70         artifacts+=(${vpp[@]})
71     else
72         artifacts+=(${vpp[@]/%/=${VPP_VERSION-}})
73     fi
74     dkms=(vpp-ext-deps)
75     if [ -z "${DKMS_VERSION-}" ]; then
76         artifacts+=(${dkms[@]})
77     else
78         artifacts+=(${dkms[@]/%/=${DKMS_VERSION-}})
79     fi
80
81     if [ "${INSTALL:-false}" = true ]; then
82         sudo apt-get -y install "${artifacts[@]}" || {
83             die "Install VPP artifacts failed."
84         }
85     else
86         apt-get -y download "${artifacts[@]}" || {
87             die "Download VPP artifacts failed."
88         }
89     fi
90 }
91
92 function download_centos_artifacts () {
93     # Get and/or install CentOS VPP artifacts from packagecloud.io.
94     #
95     # Variables read:
96     # - REPO_URL - FD.io Packagecloud repository.
97     # - VPP_VERSION - VPP version.
98     # - INSTALL - If install packages or download only. Default: download
99
100     set -exuo pipefail
101
102     curl -s "${REPO_URL}"/script.rpm.sh | sudo bash || {
103         die "Packagecloud FD.io repo fetch failed."
104     }
105     # If version is set we will add suffix.
106     artifacts=()
107     vpp=(vpp vpp-selinux-policy vpp-devel vpp-lib vpp-plugins)
108     if [ -z "${VPP_VERSION-}" ]; then
109         artifacts+=(${vpp[@]})
110     else
111         artifacts+=(${vpp[@]/%/-${VPP_VERSION-}})
112     fi
113
114     if [ "${INSTALL:-false}" = true ]; then
115         sudo yum -y install "${artifacts[@]}" || {
116             die "Install VPP artifact failed."
117         }
118     else
119         sudo yum -y install --downloadonly --downloaddir=. "${artifacts[@]}" || {
120             die "Download VPP artifacts failed."
121         }
122     fi
123 }
124
125 function download_opensuse_artifacts () {
126     # Get and/or install OpenSuSE VPP artifacts from packagecloud.io.
127     #
128     # Variables read:
129     # - REPO_URL - FD.io Packagecloud repository.
130     # - VPP_VERSION - VPP version.
131     # - INSTALL - If install packages or download only. Default: download
132
133     set -exuo pipefail
134
135     curl -s "${REPO_URL}"/script.rpm.sh | sudo bash || {
136         die "Packagecloud FD.io repo fetch failed."
137     }
138     # If version is set we will add suffix.
139     artifacts=()
140     vpp=(vpp vpp-devel vpp-lib vpp-plugins libvpp0)
141     if [ -z "${VPP_VERSION-}" ]; then
142         artifacts+=(${vpp[@]})
143     else
144         artifacts+=(${vpp[@]/%/-${VPP_VERSION-}})
145     fi
146
147     if [ "${INSTALL:-false}" = true ]; then
148         sudo yum -y install "${artifacts[@]}" || {
149             die "Install VPP artifact failed."
150         }
151     else
152         sudo yum -y install --downloadonly --downloaddir=. "${artifacts[@]}" || {
153             die "Download VPP artifacts failed."
154         }
155     fi
156 }
157