xref: /spdk/test/common/config/autotest_setup.sh (revision 66bb5d465bb6a03bce77320d997be835b2323d3f)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2017 Intel Corporation
4#  All rights reserved.
5#
6
7# The purpose of this script is to provide a simple procedure for spinning up a new
8# test environment capable of running our whole test suite. This script will install
9# all of the necessary dependencies to run almost the complete test suite.
10
11sudo() {
12	"$(type -P sudo)" -E "$@"
13}
14
15set -e
16shopt -s extglob nullglob
17
18UPGRADE=false
19INSTALL=false
20CONF="rocksdb,fio,flamegraph,tsocks,qemu,libiscsi,nvmecli,qat,spdk,refspdk,vagrant,igb_uio,ice"
21package_manager=
22
23function pre_install() { :; }
24function install() { :; }
25function upgrade() { :; }
26
27function usage() {
28	echo "This script is intended to automate the environment setup for a linux virtual machine."
29	echo "Please run this script as your regular user. The script will make calls to sudo as needed."
30	echo ""
31	echo "${0##*/}"
32	echo "  -h --help"
33	echo "  -u --upgrade Run $package_manager upgrade"
34	echo "  -i --install-deps Install $package_manager based dependencies"
35	echo "  -t --test-conf List of test configurations to enable (${CONF},irdma,lcov,bpftrace,doxygen)"
36	echo "  -c --conf-path Path to configuration file"
37	echo "  -d --dir-git Path to where git sources should be saved"
38	echo "  -s --disable-tsocks Disable use of tsocks"
39}
40
41function error() {
42	printf "%s\n\n" "$1" >&2
43	usage
44	return 1
45}
46
47function set_os_id_version() {
48	if [[ -f /etc/os-release ]]; then
49		source /etc/os-release
50	elif [[ -f /usr/local/etc/os-release ]]; then
51		# On FreeBSD file is located under /usr/local if etc_os-release package is installed
52		source /usr/local/etc/os-release
53	elif [[ $(uname -s) == FreeBSD ]]; then
54		ID=freebsd
55		VERSION_ID=$(freebsd-version)
56		VERSION_ID=${VERSION_ID//.*/}
57	else
58		echo "File os-release not found" >&2
59		return 1
60	fi
61
62	OSID=$ID
63	OSVERSION=$VERSION_ID
64
65	echo "OS-ID: $OSID | OS-Version: $OSVERSION"
66}
67
68function detect_package_manager() {
69	local manager_scripts
70	manager_scripts=("$vmsetupdir/pkgdep/"!(git))
71
72	local package_manager_lib
73	for package_manager_lib in "${manager_scripts[@]}"; do
74		package_manager=${package_manager_lib##*/}
75		if hash "${package_manager}" &> /dev/null; then
76			source "${package_manager_lib}"
77			return
78		fi
79	done
80}
81
82vmsetupdir=$(readlink -f "$(dirname "$0")")
83rootdir=$(readlink -f "$vmsetupdir/../../../")
84source "$rootdir/scripts/common.sh"
85
86set_os_id_version
87source "$vmsetupdir/pkgdep/git"
88detect_package_manager
89
90if [[ -e $vmsetupdir/pkgdep/os/$OSID ]]; then
91	source "$vmsetupdir/pkgdep/os/$OSID"
92fi
93
94# Parse input arguments #
95while getopts 'd:siuht:c:-:' optchar; do
96	case "$optchar" in
97		-)
98			case "$OPTARG" in
99				help)
100					usage
101					exit 0
102					;;
103				upgrade) UPGRADE=true ;;
104				install-deps) INSTALL=true ;;
105				test-conf=*) CONF="${OPTARG#*=}" ;;
106				conf-path=*) CONF_PATH="${OPTARG#*=}" ;;
107				dir-git=*) GIT_REPOS="${OPTARG#*=}" ;;
108				disable-tsocks) NO_TSOCKS=true ;;
109				*) error "Invalid argument '$OPTARG'" ;;
110			esac
111			;;
112		h)
113			usage
114			exit 0
115			;;
116		u) UPGRADE=true ;;
117		i) INSTALL=true ;;
118		t) CONF="$OPTARG" ;;
119		c) CONF_PATH="$OPTARG" ;;
120		d) GIT_REPOS="$OPTARG" ;;
121		s) NO_TSOCKS=true ;;
122		*) error "Invalid argument '$OPTARG'" ;;
123	esac
124done
125
126if [[ -z $package_manager ]]; then
127	echo "Supported package manager not found. Script supports:"
128	printf " * %s\n" "${manager_scripts[@]##*/}"
129	exit 1
130fi
131
132if [[ -n $CONF_PATH ]]; then
133	if [[ ! -f $CONF_PATH ]]; then
134		error "Configuration file does not exist: '$CONF_PATH'"
135	fi
136	source "$CONF_PATH"
137fi
138
139if $UPGRADE; then
140	upgrade
141fi
142
143if $INSTALL; then
144	sudo "$rootdir/scripts/pkgdep.sh" --all
145	pre_install
146	install "${packages[@]}"
147fi
148
149install_sources
150