3 # Copyright (c) 2021 Cisco Systems 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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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)"/
24 cd "${STATS_FS_DIR}"/../../
26 BUILD_ROOT=${VPP_DIR}build-root/
27 BINARY_DIR=${BUILD_ROOT}install-vpp-native/vpp/bin/
28 DEBUG_DIR=${BUILD_ROOT}install-vpp_debug-native/vpp/bin/
34 [ -z "${GOROOT}" ] && GOROOT="${HOME}/.go" && PATH=$GOROOT/bin:$PATH
35 [ -z "${GOPATH}" ] && GOPATH="${HOME}/go" && PATH=$GOPATH/bin:$PATH
37 function install_tools() {
38 echo "Installing downloading tools"
40 apt-get install git wget curl -y
43 # Install latest GO version
44 function install_go() {
47 echo "Installing latest GO"
48 if [[ -x "$(command -v go)" ]]; then
49 local installed_ver installed_ver_fmt
50 installed_ver=$(go version)
51 installed_ver_fmt=${installed_ver#"go version go"}
52 echo "Found installed version ${installed_ver_fmt}"
57 mkdir -p "${GOPATH}/"{src,pkg,bin}
59 wget "https://dl.google.com/go/$(curl https://golang.org/VERSION?m=text).linux-amd64.tar.gz" -O "${TMP}/go.tar.gz"
60 tar -C "$GOROOT" --strip-components=1 -xzf "${TMP}/go.tar.gz"
62 rm -f "${TMP}/go.tar.gz"
64 # export path for current session to install vpp_stast_fs
65 export GOROOT=${GOROOT}
66 export PATH=$GOROOT/bin:$PATH
68 export PATH=$GOPATH/bin:$PATH
70 echo "Installed $(go version)"
73 function install_fuse() {
74 echo "Installing Fuse"
76 apt-get install fuse -y
79 function install_nohup() {
80 echo "Installing nohup"
82 apt-get install nohup -y
85 function install_go_dep() {
86 echo "Installing Go dependencies"
87 if [[ ! -x "$(command -v go)" ]]; then
88 echo "GO is not installed"
92 if [ ! -e "go.mod" ]; then
94 # We require a specific govpp commit for compatibility
95 go get git.fd.io/govpp.git@da95997338b77811bc2ea850db393c652b3bd18e
96 go get git.fd.io/govpp.git/adapter/statsclient@da95997338b77811bc2ea850db393c652b3bd18e
97 go get github.com/hanwen/go-fuse/v2
101 # Resolve stats_fs dependencies and builds the binary
102 function build_statfs() {
103 echo "Installing statfs"
105 if [ -d "${BINARY_DIR}" ]; then
106 mv stats_fs "${BINARY_DIR}"/stats_fs
107 elif [ -d "${DEBUG_DIR}" ]; then
108 mv stats_fs "${DEBUG_DIR}"/stats_fs
110 echo "${BINARY_DIR} and ${DEBUG_DIR} directories does not exist, the binary is installed at ${STATS_FS_DIR}stats_fs instead"
114 function install_statfs() {
115 if [[ ! -x "$(command -v go)" ]]; then
120 if [[ ! -x "$(command -v fusermount)" ]]; then
124 if [[ ! -x "$(command -v nohup)" ]]; then
128 if [ ! -d "${STATS_FS_DIR}" ]; then
129 echo "${STATS_FS_DIR} directory does not exist"
134 if [[ ! -x "$(command -v ${STATS_FS_DIR}stats_fs)" ]]; then
138 echo "stats_fs already installed at path ${STATS_FS_DIR}stats_fs"
142 # Starts the statfs binary
143 function start_statfs() {
144 EXE_DIR=$STATS_FS_DIR
145 if [ -d "${BINARY_DIR}" ]; then
147 elif [ -d "${DEBUG_DIR}" ]; then
151 if [[ $(pidof "${EXE_DIR}"stats_fs) ]]; then
152 echo "The service stats_fs has already been launched"
156 mountpoint="${RUN_DIR}stats_fs_dir"
158 if [[ -x "$(command -v ${EXE_DIR}stats_fs)" ]] ; then
159 if [ ! -d "$mountpoint" ] ; then
162 nohup "${EXE_DIR}"stats_fs $mountpoint 0<&- &>/dev/null &
166 echo "stats_fs is not installed, use 'make stats-fs-install' first"
170 function stop_statfs() {
171 EXE_DIR=$STATS_FS_DIR
172 if [ -d "${BINARY_DIR}" ]; then
174 elif [ -d "${DEBUG_DIR}" ]; then
177 if [[ ! $(pidof "${EXE_DIR}"stats_fs) ]]; then
178 echo "The service stats_fs is not running"
182 PID=$(pidof "${EXE_DIR}"stats_fs)
184 if [[ $(pidof "${EXE_DIR}"stats_fs) ]]; then
185 echo "Check your syslog file (default is /var/log/syslog)."
186 echo "It may be that the file system is busy."
190 if [ -d "${RUN_DIR}stats_fs_dir" ] ; then
191 rm -df "${RUN_DIR}stats_fs_dir"
195 function force_unmount() {
196 if (( $(mount | grep "${RUN_DIR}stats_fs_dir" | wc -l) == 1 )) ; then
197 fusermount -uz "${RUN_DIR}stats_fs_dir"
199 echo "The default directory ${RUN_DIR}stats_fs_dir is not mounted."
202 if [ -d "${RUN_DIR}stats_fs_dir" ] ; then
203 rm -df "${RUN_DIR}stats_fs_dir"
207 # Remove stats_fs Go module
209 echo "Cleaning up stats_fs"
210 if [ ! -d "${STATS_FS_DIR}" ]; then
211 echo "${STATS_FS_DIR} directory does not exist"
217 if [ -e "stats_fs" ]; then
221 if [ -d "${BINARY_DIR}" ]; then
222 if [ -e "${BINARY_DIR}stats_fs" ]; then
223 rm -f ${BINARY_DIR}stats_fs
225 elif [ -d "${DEBUG_DIR}" ]; then
226 if [ -e "${DEBUG_DIR}stats_fs" ]; then
227 rm -f ${DEBUG_DIR}stats_fs
231 if [ -d "${RUN_DIR}stats_fs_dir" ] ; then
232 rm -df "${RUN_DIR}stats_fs_dir"
236 # Show available commands
241 install - Installs requirements (Go, GoVPP, GoFUSE) and builds stats_fs
242 start - Launches the stats_fs binary and creates a mountpoint
243 clean - Removes stats_fs binary
244 stop - Stops the executable, unmounts the file system
245 and removes the mountpoint directory
246 force-unmount - Forces the unmount of the filesystem even if it is busy
251 # Resolve chosen option and call appropriate functions
252 function resolve_option() {
273 *) echo invalid option ;;
277 if [[ -n ${OPT_ARG} ]]; then
278 resolve_option "${OPT_ARG}"
281 options=("install" "cleanup" "help" "start" "unmount")
282 select option in "${options[@]}"; do
283 resolve_option "${option}"