Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # TestON install script for Ubuntu (Debian and Fedora) |
| 4 | |
| 5 | # Fail on error |
| 6 | set -e |
| 7 | |
| 8 | # Fail on unset var usage |
| 9 | set -o nounset |
| 10 | |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 11 | function init { |
| 12 | # Identify Linux release |
| 13 | DIST=Unknown |
| 14 | RELEASE=Unknown |
| 15 | CODENAME=Unknown |
Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 16 | |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 17 | ARCH=`uname -m` |
| 18 | if [ "$ARCH" = "x86_64" ]; then ARCH="amd64"; fi |
| 19 | if [ "$ARCH" = "i686" ]; then ARCH="i386"; fi |
Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 20 | |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 21 | if [ -e /etc/debian_version ]; then DIST="Debian"; fi |
| 22 | if [ -e /etc/fedora-release ]; then DIST="Debian"; fi |
| 23 | grep Ubuntu /etc/lsb-release &> /dev/null && DIST="Ubuntu" |
Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 24 | |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 25 | if [ "$DIST" = "Ubuntu" ] || [ "$DIST" = "Debian" ]; then |
| 26 | install='sudo apt-get -y install' |
| 27 | remove='sudo apt-get -y remove' |
| 28 | pipinstall='sudo pip install' |
| 29 | # Prereqs for this script |
| 30 | if ! which lsb_release &> /dev/null; then |
| 31 | $install lsb-release |
| 32 | fi |
Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 33 | fi |
Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 34 | |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 35 | if [ "$DIST" = "Fedora" ]; then |
| 36 | install='sudo yum -y install' |
| 37 | remove='sudo yum -y erase' |
| 38 | pipinstall='sudo pip install' |
| 39 | # Prereqs for this script |
| 40 | if ! which lsb_release &> /dev/null; then |
| 41 | $install redhat-lsb-core |
| 42 | fi |
Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 43 | fi |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 44 | if which lsb_release &> /dev/null; then |
| 45 | DIST=`lsb_release -is` |
| 46 | RELEASE=`lsb_release -rs` |
| 47 | CODENAME=`lsb_release -cs` |
| 48 | fi |
| 49 | echo "Detected Linux distribution: $DIST $RELEASE $CODENAME $ARCH" |
Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 50 | |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 51 | if ! echo $DIST | egrep 'Ubuntu|Debian|Fedora'; then |
| 52 | echo "Install.sh currently only supports Ubuntu, Debian and Fedora." |
| 53 | exit 1 |
| 54 | fi |
Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 55 | |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 56 | #Get location of TestON dir |
| 57 | SOURCE="${BASH_SOURCE[0]}" |
| 58 | while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink |
| 59 | DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" |
| 60 | SOURCE="$(readlink "$SOURCE")" |
| 61 | [[ $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 |
| 62 | done |
| 63 | DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" |
| 64 | echo "Found TestON at $DIR" |
| 65 | } |
Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 66 | |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 67 | function requirements { |
| 68 | system_reqs |
| 69 | python_reqs |
| 70 | } |
| 71 | |
| 72 | function system_reqs { |
| 73 | # Specify a specific command with $1 |
| 74 | set +o nounset |
| 75 | if [ -z $1 ] |
| 76 | then |
| 77 | cmd=$install |
| 78 | else |
| 79 | cmd=$1 |
| 80 | fi |
| 81 | set -o nounset |
| 82 | # Install TestON dependencies |
| 83 | echo "Installing TestON dependencies" |
| 84 | if [ "$DIST" = "Fedora" ]; then |
| 85 | # Fedora may have vlan enabled by default. Still need to confirm and update later |
Devin Lim | 1dd7931 | 2017-06-13 10:54:58 -0700 | [diff] [blame] | 86 | $cmd python-pip build-essential python-dev pep8 python3-requests |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 87 | else |
Devin Lim | 1dd7931 | 2017-06-13 10:54:58 -0700 | [diff] [blame] | 88 | $cmd python-pip build-essential python-dev pep8 vlan python3-requests |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 89 | fi |
| 90 | |
| 91 | # Some Distos have this already from another package |
| 92 | if which arping > /dev/null ; then |
| 93 | echo "Arping command detected, skipping package installation." |
| 94 | else |
| 95 | $cmd arping |
| 96 | fi |
| 97 | } |
| 98 | |
| 99 | function python_reqs { |
| 100 | # Specify a specific pip command with $1 |
| 101 | set +o nounset |
| 102 | if [ -z $1 ] |
| 103 | then |
| 104 | cmd=$pipinstall |
| 105 | else |
| 106 | cmd=$1' install' |
| 107 | fi |
| 108 | set -o nounset |
| 109 | $cmd -r requirements.txt |
| 110 | } |
| 111 | |
| 112 | function symlinks { |
| 113 | set +e |
| 114 | # Add symbolic link to main TestON folder in Home dir |
| 115 | pushd ~ |
| 116 | sudo ln -s $DIR && echo "Added symbolic link to TestON folder in HOME directory." |
| 117 | popd |
| 118 | |
| 119 | # Add symbolic link to TestON cli in /usr/local/bin |
| 120 | pushd /usr/local/bin |
| 121 | sudo ln -s $DIR/bin/cli.py teston && echo "Added symbolic link to TestON CLI in /usr/local/bin." |
| 122 | popd |
| 123 | |
| 124 | # Add symlink to get bash completion |
| 125 | pushd /etc/bash_completion.d |
| 126 | sudo cp $DIR/bin/.teston_completion teston_completion |
| 127 | echo "Bash completion will now be enabled for new shells" |
| 128 | popd |
| 129 | set -e |
| 130 | } |
| 131 | |
| 132 | function git { |
| 133 | # OnosSystemTest git pre-commit hooks |
| 134 | pushd $DIR/.. |
| 135 | cp .git/hooks/pre-commit.sample .git/hooks/pre-commit |
| 136 | popd |
| 137 | } |
| 138 | |
| 139 | function get_pypy { |
| 140 | echo "Getting pypy" |
| 141 | pushd ~ |
| 142 | if [ ! -e pypy2-v5.3.1-linux64.tar.bz2 ] |
| 143 | then |
| 144 | wget https://bitbucket.org/pypy/pypy/downloads/pypy2-v5.3.1-linux64.tar.bz2 |
| 145 | fi |
| 146 | tar xf pypy2-v5.3.1-linux64.tar.bz2 |
| 147 | python_impl=~/pypy2-v5.3.1-linux64/bin/pypy |
| 148 | popd |
| 149 | venv $python_impl "venv-teston-pypy" |
| 150 | } |
| 151 | |
| 152 | # Optionally install in virtual env |
| 153 | function venv { |
| 154 | # $1 is the path to python implementation, $2 is the venv-name |
| 155 | echo "Installing virtual env for TestON..." |
| 156 | pushd $DIR |
| 157 | set +o nounset |
| 158 | if [ -z "$2" ] |
| 159 | then |
| 160 | venv_name="venv-teston" |
| 161 | else |
| 162 | venv_name=$2 |
| 163 | fi |
| 164 | pip install virtualenv |
| 165 | |
| 166 | # Setup virtual env |
| 167 | if [ -z "$1" ]; then |
| 168 | echo "No python implementation specified for virtual env, using default." |
| 169 | virtualenv $venv_name |
| 170 | else |
| 171 | python_impl=$1 |
| 172 | echo "Using $python_impl for python in virtual env." |
| 173 | virtualenv -p $python_impl $venv_name |
| 174 | fi |
| 175 | python_reqs $venv_name/bin/pip |
| 176 | set -o nounset |
| 177 | popd |
| 178 | } |
| 179 | |
| 180 | |
| 181 | function default { |
| 182 | requirements |
| 183 | symlinks |
| 184 | git |
| 185 | } |
| 186 | |
| 187 | function finished { |
| 188 | echo "" |
| 189 | echo "Completed running install.sh script" |
| 190 | echo "Run TestON CLI by typing teston at bash prompt" |
| 191 | echo "Example: teston run <TestSuite Name>" |
| 192 | } |
| 193 | |
| 194 | # TODO Add log rotation configuration for TestON logs here (place holder) |
| 195 | # TODO Add bash tab completion script to this |
| 196 | |
| 197 | function usage { |
| 198 | printf "Usage: $(basename $0) [-dgprsv] \n" |
| 199 | printf "Usage: $(basename $0) -y [PATH] \n\n" |
| 200 | printf "This install script attempts to install deoendencies needed to run teston\n" |
| 201 | printf "and any tests included in the official repository. If a test still does \n" |
| 202 | printf "not run after running this script, you can try looking at the test's README\n" |
| 203 | printf "or the driver files used by the tests. There are some software components\n" |
| 204 | printf "such as software switches that this script does not attempt to install as\n" |
| 205 | printf "they are more complicated.\n\n" |
| 206 | |
| 207 | printf "Options:\n" |
| 208 | printf "\t -d (default) requirements, symlinks and git hooks\n" |
| 209 | printf "\t -g install git hooks\n" |
| 210 | printf "\t -p install pypy in a virtual env\n" |
| 211 | printf "\t -r install requirements for TestON/tests\n" |
| 212 | printf "\t -s install symlinks\n" |
| 213 | printf "\t -v install a python virtual environment for teston using the default python implementation\n" |
| 214 | printf "\t -y <PATH> install a python virtual environment for testonusing a specific python implementation at PATH.\n" |
| 215 | |
| 216 | } |
| 217 | |
| 218 | if [ $# -eq 0 ] |
| 219 | then |
Jon Hall | fa9973a | 2016-09-23 10:41:59 -0700 | [diff] [blame] | 220 | init |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 221 | default |
| 222 | elif [ $1 == "--help" ] |
| 223 | then |
| 224 | usage |
Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 225 | else |
Jon Hall | 9e8c761 | 2016-07-18 17:40:06 -0700 | [diff] [blame] | 226 | init |
| 227 | while getopts 'dgprsvy:' OPTION |
| 228 | do |
| 229 | case $OPTION in |
| 230 | d) default;; |
| 231 | g) git;; |
| 232 | p) get_pypy;; |
| 233 | r) requirements;; |
| 234 | s) symlinks;; |
| 235 | v) venv;; |
| 236 | y) venv $OPTARG;; |
| 237 | ?) usage;; |
| 238 | esac |
| 239 | done |
| 240 | shift $(($OPTIND -1)) |
| 241 | finished |
Hari Krishna | 7448979 | 2015-07-30 10:07:15 -0700 | [diff] [blame] | 242 | fi |