batch-shipyard/install.sh

348 строки
9.9 KiB
Bash
Исходник Обычный вид История

2016-10-30 00:37:28 +03:00
#!/usr/bin/env bash
set -e
set -o pipefail
# vars
PYTHON=python3
PIP=pip3
2017-06-27 20:12:56 +03:00
SUDO=sudo
VENV_NAME=
# process options
while getopts "h?23ce:" opt; do
case "$opt" in
h|\?)
echo "install.sh parameters"
echo ""
echo "-2 install for Python 2.7"
echo "-3 install for Python 3.4+ [default]"
echo "-c install for Cloud Shell (via Dockerfile)"
echo "-e [environment name] install to a virtual environment"
echo ""
exit 1
;;
2)
PYTHON=python
PIP=pip
;;
3)
PYTHON=python3
PIP=pip3
;;
2017-06-27 20:12:56 +03:00
c)
PYTHON=python3
PIP=pip3
VENV_NAME=cloudshell
SUDO=
;;
e)
VENV_NAME=$OPTARG
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
2017-06-27 20:12:56 +03:00
# non-cloud shell environment checks
if [ ! -z $SUDO ]; then
# check to ensure this is not being run directly as root
if [ $(id -u) -eq 0 ]; then
echo "Installation cannot be performed as root or via sudo."
echo "Please install as a regular user."
exit 1
fi
# check for sudo
if hash sudo 2> /dev/null; then
echo "sudo found."
else
echo "sudo not found. Please install sudo first before proceeding."
exit 1
fi
2016-10-30 00:37:28 +03:00
fi
# check that shipyard.py is in cwd
if [ ! -f $PWD/shipyard.py ]; then
echo "shipyard.py not found in $PWD."
echo "Please run install.sh from the same directory as shipyard.py."
exit 1
fi
# check for python
if hash $PYTHON 2> /dev/null; then
echo "Installing for $PYTHON."
else
echo "$PYTHON not found, please install $PYTHON first with your system software installer."
exit 1
fi
# check for anaconda
set +e
ANACONDA=0
$PYTHON -c "from __future__ import print_function; import sys; print(sys.version)" | grep -Ei 'anaconda|continuum|conda-forge'
if [ $? -eq 0 ]; then
# check for conda
if hash conda 2> /dev/null; then
echo "Anaconda environment detected."
else
echo "Anaconda environment detected, but conda command not found."
exit 1
fi
if [ -z $VENV_NAME ]; then
echo "Virtual environment name must be supplied for Anaconda installations."
exit 1
fi
ANACONDA=1
PIP=pip
fi
set -e
# perform some virtual env parameter checks
INSTALL_VENV_BIN=0
if [ ! -z $VENV_NAME ]; then
# check if virtual env, env is not named shipyard
if [ "$VENV_NAME" == "shipyard" ]; then
echo "Virtual environment name cannot be shipyard. Please use a different virtual environment name."
exit 1
fi
# check for virtualenv executable
if [ $ANACONDA -eq 0 ]; then
if hash virtualenv 2> /dev/null; then
echo "virtualenv found."
else
echo "virtualenv not found."
INSTALL_VENV_BIN=1
fi
fi
fi
2016-10-30 00:37:28 +03:00
# try to get /etc/lsb-release
if [ -e /etc/lsb-release ]; then
. /etc/lsb-release
else
if [ -e /etc/os-release ]; then
. /etc/os-release
DISTRIB_ID=$ID
DISTRIB_RELEASE=$VERSION_ID
fi
2017-06-29 17:33:52 +03:00
# check for OS X
if [ -z ${DISTRIB_ID+x} ] && [ "$(uname)" == "Darwin" ]; then
DISTRIB_ID=$(uname)
DISTRIB_RELEASE=$(uname -a | cut -d' ' -f3)
fi
2016-10-30 00:37:28 +03:00
fi
if [ -z ${DISTRIB_ID+x} ] || [ -z ${DISTRIB_RELEASE+x} ]; then
echo "Unknown DISTRIB_ID or DISTRIB_RELEASE."
echo "Please refer to the Installation documentation for manual installation steps."
exit 1
fi
# lowercase vars
2017-06-29 17:33:52 +03:00
if [ $DISTRIB_ID != "Darwin" ]; then
DISTRIB_ID=${DISTRIB_ID,,}
DISTRIB_RELEASE=${DISTRIB_RELEASE,,}
fi
2016-10-30 00:37:28 +03:00
2018-03-01 06:50:30 +03:00
echo "Detected OS: $DISTRIB_ID $DISTRIB_RELEASE"
2016-10-30 00:37:28 +03:00
# install requisite packages from distro repo
2017-06-27 20:12:56 +03:00
if [ ! -z $SUDO ] || [ $(id -u) -eq 0 ]; then
if [ $DISTRIB_ID == "ubuntu" ] || [ $DISTRIB_ID == "debian" ]; then
$SUDO apt-get update
2018-03-01 06:50:30 +03:00
if [ $ANACONDA -eq 1 ]; then
PYTHON_PKGS=
2017-06-27 20:12:56 +03:00
else
2018-03-01 06:50:30 +03:00
if [ $PYTHON == "python" ]; then
PYTHON_PKGS="libpython-dev python-dev"
if [ $ANACONDA -eq 0 ]; then
PYTHON_PKGS="$PYTHON_PKGS python-pip"
fi
else
PYTHON_PKGS="libpython3-dev python3-dev"
if [ $ANACONDA -eq 0 ]; then
PYTHON_PKGS="$PYTHON_PKGS python3-pip"
fi
2017-06-27 20:12:56 +03:00
fi
fi
2017-06-27 20:12:56 +03:00
$SUDO apt-get install -y --no-install-recommends \
build-essential libssl-dev libffi-dev openssl \
openssh-client rsync $PYTHON_PKGS
elif [ $DISTRIB_ID == "centos" ] || [ $DISTRIB_ID == "rhel" ]; then
2018-03-01 06:50:30 +03:00
$SUDO yum makecache fast
if [ $ANACONDA -eq 1 ]; then
PYTHON_PKGS=
2017-06-27 20:12:56 +03:00
else
2018-03-01 06:50:30 +03:00
if [ $PYTHON == "python" ]; then
PYTHON_PKGS="python-devel"
else
yum list installed epel-release
if [ $? -ne 0 ]; then
echo "epel-release package not installed."
echo "Please install the epel-release package or refer to the Installation documentation for manual installation steps".
exit 1
fi
yum list installed python34
if [ $? -ne 0 ]; then
echo "python34 epel package not installed."
echo "Please install the python34 epel package or refer to the Installation documentation for manual installation steps."
exit 1
fi
PYTHON_PKGS="python34-devel"
2017-06-27 20:12:56 +03:00
fi
fi
$SUDO yum install -y gcc openssl-devel libffi-devel openssl \
openssh-clients rsync $PYTHON_PKGS
if [ $ANACONDA -eq 0 ]; then
2018-02-18 00:22:38 +03:00
curl -fSsL --tlsv1 https://bootstrap.pypa.io/get-pip.py | $SUDO $PYTHON
fi
2017-06-27 20:12:56 +03:00
elif [ $DISTRIB_ID == "opensuse" ] || [ $DISTRIB_ID == "sles" ]; then
$SUDO zypper ref
2018-03-01 06:50:30 +03:00
if [ $ANACONDA -eq 1 ]; then
PYTHON_PKGS=
2017-06-27 20:12:56 +03:00
else
2018-03-01 06:50:30 +03:00
if [ $PYTHON == "python" ]; then
PYTHON_PKGS="python-devel"
else
PYTHON_PKGS="python3-devel"
fi
fi
2017-06-27 20:12:56 +03:00
$SUDO zypper -n in gcc libopenssl-devel libffi48-devel openssl \
openssh rsync $PYTHON_PKGS
if [ $ANACONDA -eq 0 ]; then
2018-02-18 00:22:38 +03:00
curl -fSsL --tlsv1 https://bootstrap.pypa.io/get-pip.py | $SUDO $PYTHON
fi
2017-06-29 17:33:52 +03:00
elif [ $DISTRIB_ID == "Darwin" ]; then
# check for pip, otherwise install it
if hash $PIP 2> /dev/null; then
echo "$PIP detected."
else
echo "$PIP not found, installing for Python"
$SUDO $PYTHON -m ensurepip
fi
2016-10-30 00:37:28 +03:00
else
2017-06-27 20:12:56 +03:00
echo "Unsupported distribution."
echo "Please refer to the Installation documentation for manual installation steps."
exit 1
fi
2016-10-30 00:37:28 +03:00
fi
# create virtual env if required and install required python packages
if [ ! -z $VENV_NAME ]; then
# install virtual env if required
if [ $INSTALL_VENV_BIN -eq 1 ]; then
2017-06-27 20:12:56 +03:00
if [ ! -z $SUDO ] || [ $(id -u) -eq 0 ]; then
$SUDO $PIP install virtualenv
else
$PIP install --user virtualenv
fi
fi
if [ $ANACONDA -eq 0 ]; then
2017-04-01 07:57:41 +03:00
# create venv if it doesn't exist
2017-06-27 20:12:56 +03:00
if [ ! -z $SUDO ] || [ $(id -u) -eq 0 ]; then
virtualenv -p $PYTHON $VENV_NAME
else
$HOME/.local/bin/virtualenv -p $PYTHON $VENV_NAME
fi
source $VENV_NAME/bin/activate
$PIP install --upgrade pip setuptools
2017-11-08 20:47:03 +03:00
set +e
$PIP uninstall -y azure-storage
set -e
$PIP install --upgrade -r requirements.txt
$PIP install --upgrade --no-deps -r req_nodeps.txt
deactivate
else
2018-03-01 06:50:30 +03:00
# set python version
pyver=$($PYTHON -c "import sys;a=sys.version_info;print('{}.{}'.format(a[0],a[1]));")
echo "Creating conda env for Python $pyver"
# create conda env
2017-04-01 07:57:41 +03:00
set +e
2018-03-01 06:50:30 +03:00
conda create --yes --name $VENV_NAME python=$pyver
2017-04-01 07:57:41 +03:00
set -e
source activate $VENV_NAME
conda install --yes pip
# temporary workaround with pip requirements upgrading setuptools and
# conda pip failing to reference the old setuptools version
set +e
$PIP install --upgrade setuptools
2017-11-08 20:47:03 +03:00
$PIP uninstall -y azure-storage
set -e
$PIP install --upgrade -r requirements.txt
$PIP install --upgrade --no-deps -r req_nodeps.txt
source deactivate $VENV_NAME
fi
else
$SUDO $PIP install --upgrade pip
$PIP install --upgrade --user setuptools
2017-11-08 20:47:03 +03:00
set +e
$PIP uninstall -y azure-storage
set -e
$PIP install --upgrade --user -r requirements.txt
$PIP install --upgrade --no-deps --user -r req_nodeps.txt
fi
2016-10-30 00:37:28 +03:00
# create shipyard script
cat > shipyard << EOF
#!/usr/bin/env bash
set -e
set -f
BATCH_SHIPYARD_ROOT_DIR=$PWD
VENV_NAME=$VENV_NAME
2016-10-30 00:37:28 +03:00
EOF
cat >> shipyard << 'EOF'
if [ -z $BATCH_SHIPYARD_ROOT_DIR ]; then
echo Batch Shipyard root directory not set.
echo Please rerun the install.sh script.
exit 1
fi
2016-11-02 21:20:37 +03:00
EOF
if [ ! -z $VENV_NAME ]; then
if [ $ANACONDA -eq 0 ]; then
cat >> shipyard << 'EOF'
source $BATCH_SHIPYARD_ROOT_DIR/$VENV_NAME/bin/activate
EOF
else
cat >> shipyard << 'EOF'
source activate $VENV_NAME
EOF
fi
fi
if [ $PYTHON == "python" ]; then
2016-11-02 21:20:37 +03:00
cat >> shipyard << 'EOF'
2016-10-31 20:27:39 +03:00
python $BATCH_SHIPYARD_ROOT_DIR/shipyard.py $*
2016-10-30 00:37:28 +03:00
EOF
else
2016-10-31 20:27:39 +03:00
cat >> shipyard << 'EOF'
python3 $BATCH_SHIPYARD_ROOT_DIR/shipyard.py $*
EOF
2016-10-30 00:37:28 +03:00
fi
if [ ! -z $VENV_NAME ]; then
if [ $ANACONDA -eq 0 ]; then
cat >> shipyard << 'EOF'
deactivate
EOF
else
cat >> shipyard << 'EOF'
source deactivate $VENV_NAME
EOF
fi
fi
chmod 755 shipyard
echo ""
if [ -z $VENV_NAME ]; then
echo '>> Please add $HOME/.local/bin to your $PATH. You can do this '
echo '>> permanently in your shell rc script, e.g., .bashrc for bash shells.'
echo ""
fi
2017-04-01 07:57:41 +03:00
echo ">> Install complete for $PYTHON. Please run Batch Shipyard as: $PWD/shipyard"