xref: /spdk/test/common/config/autotest_setup.sh (revision 1078198e78653b2f39414c1566740018d76ee73d)
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
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)"
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
86if [[ ${0##*/} == vm_setup.sh ]]; then
87	cat <<- DEPRECATED >&2
88		Running this script as ${0##*/} is deprecated and support for it will be removed in the future.
89
90		Please, use $vmsetupdir/autotest_setup.sh instead.
91
92	DEPRECATED
93fi
94
95set_os_id_version
96source "$vmsetupdir/pkgdep/git"
97detect_package_manager
98
99if [[ -e $vmsetupdir/pkgdep/os/$OSID ]]; then
100	source "$vmsetupdir/pkgdep/os/$OSID"
101fi
102
103# Parse input arguments #
104while getopts 'd:siuht:c:-:' optchar; do
105	case "$optchar" in
106		-)
107			case "$OPTARG" in
108				help)
109					usage
110					exit 0
111					;;
112				upgrade) UPGRADE=true ;;
113				install-deps) INSTALL=true ;;
114				test-conf=*) CONF="${OPTARG#*=}" ;;
115				conf-path=*) CONF_PATH="${OPTARG#*=}" ;;
116				dir-git=*) GIT_REPOS="${OPTARG#*=}" ;;
117				disable-tsocks) NO_TSOCKS=true ;;
118				*) error "Invalid argument '$OPTARG'" ;;
119			esac
120			;;
121		h)
122			usage
123			exit 0
124			;;
125		u) UPGRADE=true ;;
126		i) INSTALL=true ;;
127		t) CONF="$OPTARG" ;;
128		c) CONF_PATH="$OPTARG" ;;
129		d) GIT_REPOS="$OPTARG" ;;
130		s) NO_TSOCKS=true ;;
131		*) error "Invalid argument '$OPTARG'" ;;
132	esac
133done
134
135if [[ -z $package_manager ]]; then
136	echo "Supported package manager not found. Script supports:"
137	printf " * %s\n" "${manager_scripts[@]##*/}"
138	exit 1
139fi
140
141if [[ -n $CONF_PATH ]]; then
142	if [[ ! -f $CONF_PATH ]]; then
143		error "Configuration file does not exist: '$CONF_PATH'"
144	fi
145	source "$CONF_PATH"
146fi
147
148if $UPGRADE; then
149	upgrade
150fi
151
152if $INSTALL; then
153	sudo "$rootdir/scripts/pkgdep.sh" --all
154	pre_install
155	install "${packages[@]}"
156fi
157
158install_sources
159