blob: 80ba1e5e3ead31ef34316ccc051f9fcd23224fd7 [file] [log] [blame]
Hari Krishna74489792015-07-30 10:07:15 -07001#!/usr/bin/env bash
2
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07003# Copyright 2015 Open Networking Foundation (ONF)
4#
5# Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
6# the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
7# or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
8#
9# TestON is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 2 of the License, or
12# (at your option) any later version.
13#
14# TestON is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with TestON. If not, see <http://www.gnu.org/licenses/>.
21
Hari Krishna74489792015-07-30 10:07:15 -070022# TestON install script for Ubuntu (Debian and Fedora)
23
24# Fail on error
25set -e
26
27# Fail on unset var usage
28set -o nounset
29
Jon Hall9e8c7612016-07-18 17:40:06 -070030function init {
31 # Identify Linux release
32 DIST=Unknown
33 RELEASE=Unknown
34 CODENAME=Unknown
Hari Krishna74489792015-07-30 10:07:15 -070035
Jon Hall9e8c7612016-07-18 17:40:06 -070036 ARCH=`uname -m`
37 if [ "$ARCH" = "x86_64" ]; then ARCH="amd64"; fi
38 if [ "$ARCH" = "i686" ]; then ARCH="i386"; fi
Hari Krishna74489792015-07-30 10:07:15 -070039
Jon Hall9e8c7612016-07-18 17:40:06 -070040 if [ -e /etc/debian_version ]; then DIST="Debian"; fi
41 if [ -e /etc/fedora-release ]; then DIST="Debian"; fi
42 grep Ubuntu /etc/lsb-release &> /dev/null && DIST="Ubuntu"
Hari Krishna74489792015-07-30 10:07:15 -070043
Jon Hall9e8c7612016-07-18 17:40:06 -070044 if [ "$DIST" = "Ubuntu" ] || [ "$DIST" = "Debian" ]; then
45 install='sudo apt-get -y install'
46 remove='sudo apt-get -y remove'
47 pipinstall='sudo pip install'
48 # Prereqs for this script
49 if ! which lsb_release &> /dev/null; then
50 $install lsb-release
51 fi
Hari Krishna74489792015-07-30 10:07:15 -070052 fi
Hari Krishna74489792015-07-30 10:07:15 -070053
Jon Hall9e8c7612016-07-18 17:40:06 -070054 if [ "$DIST" = "Fedora" ]; then
55 install='sudo yum -y install'
56 remove='sudo yum -y erase'
57 pipinstall='sudo pip install'
58 # Prereqs for this script
59 if ! which lsb_release &> /dev/null; then
60 $install redhat-lsb-core
61 fi
Hari Krishna74489792015-07-30 10:07:15 -070062 fi
Jon Hall9e8c7612016-07-18 17:40:06 -070063 if which lsb_release &> /dev/null; then
64 DIST=`lsb_release -is`
65 RELEASE=`lsb_release -rs`
66 CODENAME=`lsb_release -cs`
67 fi
68 echo "Detected Linux distribution: $DIST $RELEASE $CODENAME $ARCH"
Hari Krishna74489792015-07-30 10:07:15 -070069
Jon Hall9e8c7612016-07-18 17:40:06 -070070 if ! echo $DIST | egrep 'Ubuntu|Debian|Fedora'; then
71 echo "Install.sh currently only supports Ubuntu, Debian and Fedora."
72 exit 1
73 fi
Hari Krishna74489792015-07-30 10:07:15 -070074
Jon Hall9e8c7612016-07-18 17:40:06 -070075 #Get location of TestON dir
76 SOURCE="${BASH_SOURCE[0]}"
77 while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
78 DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
79 SOURCE="$(readlink "$SOURCE")"
80 [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
81 done
82 DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
83 echo "Found TestON at $DIR"
84}
Hari Krishna74489792015-07-30 10:07:15 -070085
Jon Hall9e8c7612016-07-18 17:40:06 -070086function requirements {
87 system_reqs
88 python_reqs
89}
90
91function system_reqs {
92 # Specify a specific command with $1
93 set +o nounset
94 if [ -z $1 ]
95 then
96 cmd=$install
97 else
98 cmd=$1
99 fi
100 set -o nounset
101 # Install TestON dependencies
102 echo "Installing TestON dependencies"
103 if [ "$DIST" = "Fedora" ]; then
104 # Fedora may have vlan enabled by default. Still need to confirm and update later
Devin Lim1dd79312017-06-13 10:54:58 -0700105 $cmd python-pip build-essential python-dev pep8 python3-requests
Jon Hall9e8c7612016-07-18 17:40:06 -0700106 else
Devin Lim1dd79312017-06-13 10:54:58 -0700107 $cmd python-pip build-essential python-dev pep8 vlan python3-requests
Jon Hall9e8c7612016-07-18 17:40:06 -0700108 fi
109
110 # Some Distos have this already from another package
111 if which arping > /dev/null ; then
112 echo "Arping command detected, skipping package installation."
113 else
114 $cmd arping
115 fi
116}
117
118function python_reqs {
119 # Specify a specific pip command with $1
120 set +o nounset
121 if [ -z $1 ]
122 then
123 cmd=$pipinstall
124 else
125 cmd=$1' install'
126 fi
127 set -o nounset
128 $cmd -r requirements.txt
129}
130
131function symlinks {
132 set +e
133 # Add symbolic link to main TestON folder in Home dir
134 pushd ~
135 sudo ln -s $DIR && echo "Added symbolic link to TestON folder in HOME directory."
136 popd
137
138 # Add symbolic link to TestON cli in /usr/local/bin
139 pushd /usr/local/bin
140 sudo ln -s $DIR/bin/cli.py teston && echo "Added symbolic link to TestON CLI in /usr/local/bin."
141 popd
142
143 # Add symlink to get bash completion
144 pushd /etc/bash_completion.d
145 sudo cp $DIR/bin/.teston_completion teston_completion
146 echo "Bash completion will now be enabled for new shells"
147 popd
148 set -e
149}
150
151function git {
152 # OnosSystemTest git pre-commit hooks
153 pushd $DIR/..
154 cp .git/hooks/pre-commit.sample .git/hooks/pre-commit
155 popd
156}
157
158function get_pypy {
159 echo "Getting pypy"
160 pushd ~
161 if [ ! -e pypy2-v5.3.1-linux64.tar.bz2 ]
162 then
163 wget https://bitbucket.org/pypy/pypy/downloads/pypy2-v5.3.1-linux64.tar.bz2
164 fi
165 tar xf pypy2-v5.3.1-linux64.tar.bz2
166 python_impl=~/pypy2-v5.3.1-linux64/bin/pypy
167 popd
168 venv $python_impl "venv-teston-pypy"
169}
170
171# Optionally install in virtual env
172function venv {
173 # $1 is the path to python implementation, $2 is the venv-name
174 echo "Installing virtual env for TestON..."
175 pushd $DIR
176 set +o nounset
177 if [ -z "$2" ]
178 then
179 venv_name="venv-teston"
180 else
181 venv_name=$2
182 fi
183 pip install virtualenv
184
185 # Setup virtual env
186 if [ -z "$1" ]; then
187 echo "No python implementation specified for virtual env, using default."
188 virtualenv $venv_name
189 else
190 python_impl=$1
191 echo "Using $python_impl for python in virtual env."
192 virtualenv -p $python_impl $venv_name
193 fi
194 python_reqs $venv_name/bin/pip
195 set -o nounset
196 popd
197}
198
199
200function default {
201 requirements
202 symlinks
203 git
204}
205
206function finished {
207 echo ""
208 echo "Completed running install.sh script"
209 echo "Run TestON CLI by typing teston at bash prompt"
210 echo "Example: teston run <TestSuite Name>"
211}
212
213# TODO Add log rotation configuration for TestON logs here (place holder)
214# TODO Add bash tab completion script to this
215
216function usage {
217 printf "Usage: $(basename $0) [-dgprsv] \n"
218 printf "Usage: $(basename $0) -y [PATH] \n\n"
219 printf "This install script attempts to install deoendencies needed to run teston\n"
220 printf "and any tests included in the official repository. If a test still does \n"
221 printf "not run after running this script, you can try looking at the test's README\n"
222 printf "or the driver files used by the tests. There are some software components\n"
223 printf "such as software switches that this script does not attempt to install as\n"
224 printf "they are more complicated.\n\n"
225
226 printf "Options:\n"
227 printf "\t -d (default) requirements, symlinks and git hooks\n"
228 printf "\t -g install git hooks\n"
229 printf "\t -p install pypy in a virtual env\n"
230 printf "\t -r install requirements for TestON/tests\n"
231 printf "\t -s install symlinks\n"
232 printf "\t -v install a python virtual environment for teston using the default python implementation\n"
233 printf "\t -y <PATH> install a python virtual environment for testonusing a specific python implementation at PATH.\n"
234
235}
236
237if [ $# -eq 0 ]
238then
Jon Hallfa9973a2016-09-23 10:41:59 -0700239 init
Jon Hall9e8c7612016-07-18 17:40:06 -0700240 default
241elif [ $1 == "--help" ]
242then
243 usage
Hari Krishna74489792015-07-30 10:07:15 -0700244else
Jon Hall9e8c7612016-07-18 17:40:06 -0700245 init
246 while getopts 'dgprsvy:' OPTION
247 do
248 case $OPTION in
249 d) default;;
250 g) git;;
251 p) get_pypy;;
252 r) requirements;;
253 s) symlinks;;
254 v) venv;;
255 y) venv $OPTARG;;
256 ?) usage;;
257 esac
258 done
259 shift $(($OPTIND -1))
260 finished
Hari Krishna74489792015-07-30 10:07:15 -0700261fi