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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # A simple script that installs stats_fs, a Fuse file system
17 # for the stats segment
23 STATS_FS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && 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/
33 [ -z "${GOROOT}" ] && GOROOT="${HOME}/.go" && PATH=$GOROOT/bin:$PATH
34 [ -z "${GOPATH}" ] && GOPATH="${HOME}/go" && PATH=$GOPATH/bin:$PATH
36 function install_tools() {
37 echo "Installing downloading tools"
39 apt-get install git wget curl -y
42 # Install latest GO version
43 function install_go() {
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}"
56 mkdir -p "${GOPATH}/"{src,pkg,bin}
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"
61 rm -f "${TMP}/go.tar.gz"
63 # export path for current session to install vpp_stast_fs
64 export GOROOT=${GOROOT}
65 export PATH=$GOROOT/bin:$PATH
67 export PATH=$GOPATH/bin:$PATH
69 echo "Installed $(go version)"
72 function install_fuse() {
73 echo "Installing Fuse"
75 apt-get install fuse -y
78 function install_go_dep() {
79 echo "Installing Go dependencies"
80 if [[ ! -x "$(command -v go)" ]]; then
81 echo "GO is not installed"
85 if [ ! -e "go.mod" ]; then
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
94 # Resolve stats_fs dependencies and builds the binary
95 function build_statfs() {
96 echo "Installing statfs"
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
103 echo "${BINARY_DIR} and ${DEBUG_DIR} directories does not exist, the binary is installed at ${STATS_FS_DIR}stats_fs instead"
107 function install_statfs() {
108 if [[ ! -x "$(command -v go)" ]]; then
113 if [[ ! -x "$(command -v fusermount)" ]]; then
117 if [ ! -d "${STATS_FS_DIR}" ]; then
118 echo "${STATS_FS_DIR} directory does not exist"
123 if [[ ! -x "$(command -v ${STATS_FS_DIR}stats_fs)" ]]; then
127 echo "stats_fs already installed at path ${STATS_FS_DIR}stats_fs"
131 # Starts the statfs binary
132 function start_statfs() {
133 EXE_DIR=$STATS_FS_DIR
134 if [ -d "${BINARY_DIR}" ]; then
136 elif [ -d "${DEBUG_DIR}" ]; then
140 mountpoint="${RUN_DIR}stats_fs_dir"
142 if [[ -x "$(command -v ${EXE_DIR}stats_fs)" ]] ; then
143 if [ ! -d "$mountpoint" ] ; then
146 nohup "${EXE_DIR}"stats_fs $mountpoint 0<&- &>/dev/null &
150 echo "stats_fs is not installed, use 'make stats-fs-install' first"
153 function stop_statfs() {
154 EXE_DIR=$STATS_FS_DIR
155 if [ -d "${BINARY_DIR}" ]; then
157 elif [ -d "${DEBUG_DIR}" ]; then
160 if [[ ! $(pidof "${EXE_DIR}"stats_fs) ]]; then
161 echo "The service stats_fs is not running"
165 PID=$(pidof "${EXE_DIR}"stats_fs)
167 if [[ $(pidof "${EXE_DIR}"stats_fs) ]]; then
168 echo "Can't unmount the file system: Device or resource busy"
172 if [ -d "${RUN_DIR}stats_fs_dir" ] ; then
173 rm -df "${RUN_DIR}stats_fs_dir"
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"
181 echo "The default directory ${RUN_DIR}stats_fs_dir is not mounted."
184 if [ -d "${RUN_DIR}stats_fs_dir" ] ; then
185 rm -df "${RUN_DIR}stats_fs_dir"
189 # Remove stats_fs Go module
191 echo "Cleaning up stats_fs"
192 if [ ! -d "${STATS_FS_DIR}" ]; then
193 echo "${STATS_FS_DIR} directory does not exist"
199 if [ -e "go.mod" ]; then
202 if [ -e "go.sum" ]; then
205 if [ -e "stats_fs" ]; then
209 if [ -d "${BINARY_DIR}" ]; then
210 if [ -e "${BINARY_DIR}stats_fs" ]; then
211 rm -f ${BINARY_DIR}stats_fs
213 elif [ -d "${DEBUG_DIR}" ]; then
214 if [ -e "${DEBUG_DIR}stats_fs" ]; then
215 rm -f ${DEBUG_DIR}stats_fs
219 if [ -d "${RUN_DIR}stats_fs_dir" ] ; then
220 rm -df "${RUN_DIR}stats_fs_dir"
224 # Show available commands
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
239 # Resolve chosen option and call appropriate functions
240 function resolve_option() {
261 *) echo invalid option ;;
265 if [[ -n ${OPT_ARG} ]]; then
266 resolve_option "${OPT_ARG}"
269 options=("install" "cleanup" "help" "start" "unmount")
270 select option in "${options[@]}"; do
271 resolve_option "${option}"