hs-test: more debug output in http3 test
[vpp.git] / extras / vpptop / vpptop.sh
1 #!/bin/bash
2
3 # A simple script that installs VPPTop utility including
4 # all requirements. The binary API is built from the local
5 # vpp data. 'make install-dep' is recommended to call first.
6
7 set -eo pipefail
8
9 OPT_ARG=${1:-}
10
11 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
12 cd "${SCRIPT_DIR}"/../../
13 VPP_DIR=$(pwd)
14 BUILD_DIR=${VPP_DIR}/extras/vpptop/build
15
16 GOROOT=${GOROOT:-}
17 GOPATH=${GOPATH:-}
18
19 [ -z "${GOROOT}" ] && GOROOT="${HOME}/.go"
20 [ -z "${GOPATH}" ] && GOPATH="${HOME}/go"
21
22 function dep_install() {
23   echo "Installing VPPTop dependencies"
24   apt-get update
25   apt-get install git wget curl -y
26 }
27
28 # Install latest GO version
29 function go_install() {
30   local TMP="/tmp"
31
32   # Search for existing golang installation
33   echo "Looking for pre-installed GO.."
34   local installed_ver installed_ver_fmt
35   if [[ -f "${GOROOT}/bin/go" ]]; then
36     installed_ver=$(cd ${GOROOT}/bin && ./go version)
37     installed_ver_fmt=${installed_ver#"go version go"}
38     export PATH=$GOROOT/bin:$PATH
39     export PATH=$GOPATH/bin:$PATH
40     echo "Found installed version ${installed_ver_fmt}"
41     return
42   fi
43
44   # install golang when missing
45   echo ".. none was found. Installing the latest one"
46   mkdir -p "${GOROOT}"
47   mkdir -p "${GOPATH}/"{src,pkg,bin}
48
49   wget "https://dl.google.com/go/$(curl https://golang.org/VERSION?m=text).linux-amd64.tar.gz" -O "${TMP}/go.tar.gz"
50   tar -C "$GOROOT" --strip-components=1 -xzf "${TMP}/go.tar.gz"
51
52   rm -f "${TMP}/go.tar.gz"
53
54   # export for current session so the VPPTop can be installed
55   export GOROOT=${GOROOT}
56   export GOPATH=$GOPATH
57   export PATH=$GOROOT/bin:$PATH
58   export PATH=$GOPATH/bin:$PATH
59
60   cat << EOF
61 Installed $(go version)
62 Note: following variables were exported for the current session:
63 GOROOT=${GOROOT}
64 GOPATH=${GOPATH}
65 Both were added to PATH
66 EOF
67 }
68
69 # Install GoVPP binary API generator. GoLang required
70 # to be installed in version 1.13 or higher
71 function install_binapi_gen() {
72   echo "Installing GoVPP binary API generator"
73
74   export GO111MODULE=on
75   # master required for latest VPPTop
76   if [[ ! -f "${GOROOT}/bin/go" ]]; then
77     echo "GO is not installed"
78     exit 1
79   fi
80   cd ${GOROOT}/bin && ./go get git.fd.io/govpp.git/cmd/binapi-generator@master
81
82   local installed_ver installed_ver_fmt
83   installed_ver=$(cd ${GOPATH}/bin && ./binapi-generator -version)
84   installed_ver_fmt=${installed_ver#"govpp "}
85   echo "Binary API generator ${installed_ver_fmt} installed"
86 }
87
88 # Generate binary API files in the VPPTop directory using
89 # the local VPP sources
90 function generate_binary_api() {
91   # note: build-root dir is planned to be removed, update the path by then
92   local api_dir=${VPP_DIR}/build-root/install-vpp-native/vpp/share/vpp/api
93   local out_dir=${BUILD_DIR}/vpptop/stats/local/binapi
94
95   if [[ ! -f "${GOPATH}/bin/binapi-generator" ]]; then
96     install_binapi_gen
97   fi
98   if [ ! -d "${BUILD_DIR}" ]; then
99     echo "VPPTop directory does not exist"
100     exit 1
101   elif [ ! -d "${out_dir}" ]; then
102     mkdir -p "${out_dir}"
103   fi
104   if [ ! -d "${api_dir}" ]; then
105     echo "JSON API files missing, call 'make json-api-files' first"
106     exit 1
107   fi
108
109   echo "Generating API"
110   cd ${GOPATH}/bin && ./binapi-generator --output-dir="${out_dir}" -input-dir="${api_dir}" \
111     "${api_dir}"/plugins/dhcp.api.json \
112     "${api_dir}"/core/interface.api.json \
113     "${api_dir}"/core/ip.api.json \
114     "${api_dir}"/core/vpe.api.json
115 }
116
117 # Retrieve VPPTop repository
118 function get_vpptop() {
119   echo "Fetching VPPTop"
120   if [ ! -d "${BUILD_DIR}/vpptop" ]; then
121     mkdir "${BUILD_DIR}"
122     cd "${BUILD_DIR}" && git clone https://github.com/PANTHEONtech/vpptop.git
123   else
124     echo "VPPTop directory already exists"
125   fi
126 }
127
128 # Resolve VPPTop dependencies and install the binary
129 function vpptop_install() {
130   get_vpptop
131   generate_binary_api
132
133   echo "Installing VPPTop"
134   if [ ! -d "${BUILD_DIR}" ]; then
135     echo "VPPTop directory does not exist"
136     exit 1
137   fi
138
139   gopath=${GOROOT}/bin/go
140
141   cd "${BUILD_DIR}"/vpptop && go mod download
142   cd "${BUILD_DIR}"/vpptop && make install
143
144   if [[ ! -x "$(command -v vpptop)" ]] && [[ ! -f "${GOPATH}/bin/vpptop" ]] ; then
145     echo "VPPTop was not successfully installed"
146     exit 1
147   fi
148   if [[ ! -x "$(command -v vpptop)" ]] ; then
149     echo "VPPTop was installed to ${GOPATH}/bin/vpptop"
150   fi
151
152     cat << EOF
153 -----
154 $(vpptop --help)
155 -----
156
157 Following binaries were installed:
158 ${GOPATH}/bin/binapi-generator
159 ${GOPATH}/bin/vpptop
160 EOF
161 }
162
163 # Starts the vpptop binary
164 function vpptop_start() {
165   if [[ -f "${GOPATH}/bin/vpptop"  ]] ; then
166     cd ${GOPATH}/bin && ./vpptop
167     return
168   fi
169
170   echo "VPPTop is not installed, use 'make vpptop-install' first"
171 }
172
173 # Remove VPPTop repository from extras
174 function cleanup() {
175   echo "Cleaning up VPPTop"
176   rm -rf "${BUILD_DIR}"
177 }
178
179 # Show available commands
180 function help() {
181   cat <<__EOF__
182   VPPTop installer
183
184   Run 'make install-dep' before the installation
185
186   <install>:
187   Installs VPPTop requirements (latest GO, GoVPP binary API generator),
188   retrieves VPPTop repository, generates binary API and installs the
189   VPPTop binary.
190
191   <cleanup>:
192   Removes VPPTop repository
193
194   <start>:
195   Runs vpptop binary
196
197 __EOF__
198 }
199
200 # Resolve chosen option and call appropriate functions
201 function resolve_option() {
202   local option=$1
203   case ${option} in
204   "start")
205     vpptop_start
206     ;;
207   "install")
208     dep_install
209     go_install
210     vpptop_install
211     ;;
212   "cleanup")
213     cleanup
214     ;;
215   "help")
216     help
217     ;;
218   *) echo invalid option ;;
219   esac
220 }
221
222 if [[ -n ${OPT_ARG} ]]; then
223   resolve_option "${OPT_ARG}"
224 else
225   PS3="--> "
226   options=("install" "cleanup" "help")
227   select option in "${options[@]}"; do
228     resolve_option "${option}"
229     break
230   done
231 fi