6249e63c6eb02ace9232f01da5ebad62b208724b
[vpp.git] / extras / vpp_stats_fs / install.sh
1 # Copyright (c) 2021 Cisco Systems and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 #!/bin/bash
15
16 # A simple script that installs stats_fs, a Fuse file system
17 # for the stats segment
18
19 set -eo pipefail
20
21 OPT_ARG=${1:-}
22
23 STATS_FS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"/
24 VPP_DIR=$(pwd)/
25 BUILD_ROOT=${VPP_DIR}build-root/
26 BINARY_DIR=${BUILD_ROOT}install-vpp-native/vpp/bin/
27 DEBUG_DIR=${BUILD_ROOT}install-vpp_debug-native/vpp/bin/
28 RUN_DIR=/run/vpp/
29
30 GOROOT=${GOROOT:-}
31 GOPATH=${GOPATH:-}
32
33 [ -z "${GOROOT}" ] && GOROOT="${HOME}/.go" && PATH=$GOROOT/bin:$PATH
34 [ -z "${GOPATH}" ] && GOPATH="${HOME}/go"  && PATH=$GOPATH/bin:$PATH
35
36 function install_tools() {
37   echo "Installing downloading tools"
38   apt-get update
39   apt-get install git wget curl -y
40 }
41
42 # Install latest GO version
43 function install_go() {
44   local TMP="/tmp"
45
46   echo "Installing latest GO"
47   if [[ -x "$(command -v go)" ]]; then
48     local installed_ver installed_ver_fmt
49     installed_ver=$(go version)
50     installed_ver_fmt=${installed_ver#"go version go"}
51     echo "Found installed version ${installed_ver_fmt}"
52     return
53   fi
54
55   mkdir -p "${GOROOT}"
56   mkdir -p "${GOPATH}/"{src,pkg,bin}
57
58   wget "https://dl.google.com/go/$(curl https://golang.org/VERSION?m=text).linux-amd64.tar.gz" -O "${TMP}/go.tar.gz"
59   tar -C "$GOROOT" --strip-components=1 -xzf "${TMP}/go.tar.gz"
60
61   rm -f "${TMP}/go.tar.gz"
62
63   # export path for current session to install vpp_stast_fs
64   export GOROOT=${GOROOT}
65   export PATH=$GOROOT/bin:$PATH
66   export GOPATH=$GOPATH
67   export PATH=$GOPATH/bin:$PATH
68
69   echo "Installed $(go version)"
70 }
71
72 function install_fuse() {
73   echo "Installing Fuse"
74   apt-get update
75   apt-get install fuse -y
76 }
77
78 function install_go_dep() {
79   echo "Installing Go dependencies"
80   if [[ ! -x "$(command -v go)" ]]; then
81     echo "GO is not installed"
82     exit 1
83   fi
84
85   if [ ! -e "go.mod" ]; then
86     go mod init stats_fs
87   fi
88   # master required
89   go get git.fd.io/govpp.git@master
90   go get git.fd.io/govpp.git/adapter/statsclient@master
91   go get github.com/hanwen/go-fuse/v2
92 }
93
94 # Resolve stats_fs dependencies and builds the binary
95 function build_statfs() {
96   echo "Installing statfs"
97   go build
98   if [ -d "${BINARY_DIR}" ]; then
99     mv stats_fs "${BINARY_DIR}"/stats_fs
100   elif [ -d "${DEBUG_DIR}" ]; then
101     mv stats_fs "${DEBUG_DIR}"/stats_fs
102   else
103     echo "${BINARY_DIR} and ${DEBUG_DIR} directories does not exist, the binary is installed at ${STATS_FS_DIR}stats_fs instead"
104   fi
105 }
106
107 function install_statfs() {
108   if [[ ! -x "$(command -v go)" ]]; then
109     install_tools
110     install_go
111   fi
112
113   if [[ ! -x "$(command -v fusermount)" ]]; then
114     install_fuse
115   fi
116
117   if [ ! -d "${STATS_FS_DIR}" ]; then
118     echo "${STATS_FS_DIR} directory does not exist"
119     exit 1
120   fi
121   cd "${STATS_FS_DIR}"
122
123   if [[ ! -x "$(command -v ${STATS_FS_DIR}stats_fs)" ]]; then
124     install_go_dep
125     build_statfs
126   else
127     echo "stats_fs already installed at path ${STATS_FS_DIR}stats_fs"
128   fi
129 }
130
131 # Starts the statfs binary
132 function start_statfs() {
133   EXE_DIR=$STATS_FS_DIR
134   if [ -d "${BINARY_DIR}" ]; then
135     EXE_DIR=$BINARY_DIR
136   elif [ -d "${DEBUG_DIR}" ]; then
137     EXE_DIR=$DEBUG_DIR
138   fi
139
140   mountpoint="${RUN_DIR}stats_fs_dir"
141
142   if [[ -x "$(command -v ${EXE_DIR}stats_fs)" ]] ; then
143     if [ ! -d "$mountpoint" ] ; then
144       mkdir "$mountpoint"
145     fi
146     nohup "${EXE_DIR}"stats_fs $mountpoint 0<&- &>/dev/null &
147     return
148   fi
149
150   echo "stats_fs is not installed, use 'make stats-fs-install' first"
151 }
152
153 function stop_statfs() {
154   EXE_DIR=$STATS_FS_DIR
155   if [ -d "${BINARY_DIR}" ]; then
156     EXE_DIR=$BINARY_DIR
157   elif [ -d "${DEBUG_DIR}" ]; then
158     EXE_DIR=$DEBUG_DIR
159   fi
160   if [[ ! $(pidof "${EXE_DIR}"stats_fs) ]]; then
161     echo "The service stats_fs is not running"
162     exit 1
163   fi
164
165   PID=$(pidof "${EXE_DIR}"stats_fs)
166   kill "$PID"
167   if [[ $(pidof "${EXE_DIR}"stats_fs) ]]; then
168     echo "Can't unmount the file system: Device or resource busy"
169     exit 1
170   fi
171
172   if [ -d "${RUN_DIR}stats_fs_dir" ] ; then
173     rm -df "${RUN_DIR}stats_fs_dir"
174   fi
175 }
176
177 function force_unmount() {
178   if (( $(mount | grep "${RUN_DIR}stats_fs_dir" | wc -l) == 1 )) ; then
179     fusermount -uz "${RUN_DIR}stats_fs_dir"
180   else
181     echo "The default directory ${RUN_DIR}stats_fs_dir is not mounted."
182   fi
183
184   if [ -d "${RUN_DIR}stats_fs_dir" ] ; then
185     rm -df "${RUN_DIR}stats_fs_dir"
186   fi
187 }
188
189 # Remove stats_fs Go module
190 function cleanup() {
191   echo "Cleaning up stats_fs"
192   if [ ! -d "${STATS_FS_DIR}" ]; then
193     echo "${STATS_FS_DIR} directory does not exist"
194     exit 1
195   fi
196
197   cd "${STATS_FS_DIR}"
198
199   if [ -e "go.mod" ]; then
200     rm -f go.mod
201   fi
202   if [ -e "go.sum" ]; then
203     rm -f go.sum
204   fi
205   if [ -e "stats_fs" ]; then
206     rm -f stats_fs
207   fi
208
209   if [ -d "${BINARY_DIR}" ]; then
210     if [ -e "${BINARY_DIR}stats_fs" ]; then
211       rm -f ${BINARY_DIR}stats_fs
212     fi
213   elif [ -d "${DEBUG_DIR}" ]; then
214     if [ -e "${DEBUG_DIR}stats_fs" ]; then
215       rm -f ${DEBUG_DIR}stats_fs
216     fi
217   fi
218
219   if [ -d "${RUN_DIR}stats_fs_dir" ] ; then
220     rm -df "${RUN_DIR}stats_fs_dir"
221   fi
222 }
223
224 # Show available commands
225 function help() {
226   cat <<__EOF__
227   Stats_fs installer
228
229   stats-fs-install        - Installs requirements (Go, GoVPP, GoFUSE) and builds stats_fs
230   stats-fs-start          - Launches the stats_fs binary and creates a mountpoint
231   stats-fs-cleanup        - Removes stats_fs binary and deletes go module
232   stats-fs-stop           - Stops the executable, unmounts the file system
233                             and removes the mountpoint directory
234   stats-fs-force-unmount  - Forces the unmount of the filesystem even if it is busy
235
236 __EOF__
237 }
238
239 # Resolve chosen option and call appropriate functions
240 function resolve_option() {
241   local option=$1
242   case ${option} in
243   "start")
244     start_statfs
245     ;;
246   "install")
247     install_statfs
248     ;;
249   "cleanup")
250     cleanup
251     ;;
252   "unmount")
253     force_unmount
254     ;;
255   "stop")
256     stop_statfs
257     ;;
258   "help")
259     help
260     ;;
261   *) echo invalid option ;;
262   esac
263 }
264
265 if [[ -n ${OPT_ARG} ]]; then
266   resolve_option "${OPT_ARG}"
267 else
268   PS3="--> "
269   options=("install" "cleanup" "help" "start" "unmount")
270   select option in "${options[@]}"; do
271     resolve_option "${option}"
272     break
273   done
274 fi