#!/bin/bash set -e mirror_xivo="http://mirror.xivo.solutions" update='apt-get update' install='apt-get install --assume-yes' download='apt-get install --assume-yes --download-only' dcomp='/usr/bin/xivo-dcomp' get_system_architecture() { architecture=$(uname -m) } check_system() { local version_file='/etc/debian_version' if [ ! -f $version_file ]; then echo "You must install XiVO on a Debian $debian_version (\"$debian_name\") system" echo "You do not seem to be on a Debian system" exit 1 else version=$(cut -d '.' -f 1 "$version_file") fi if [ $version != $debian_version ]; then echo "You must install XiVO on a Debian $debian_version (\"$debian_name\") system" echo "You are currently on a Debian $version system" exit 1 fi } add_xivo_key() { wget $mirror_xivo/xivo_current.key -O - | apt-key add - } add_docker_key() { echo "Adding Docker GPG key..." apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 9DC858229FC7DD38854AE2D88D81803C0EBFCD88 } add_docker-engine_key() { echo "Adding Docker Engine GPG key..." apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D } list_packages_with_filter() { filter="$1" aptitude -q -F '%p' --disable-columns search "$filter" } xivo_package() { list_packages_with_filter "?installed?name(\"^xivo$|^xivo-base$|^pf-xivo$\")" | head -n1 } xivo_version_installed() { echo "$(LANG='C' apt-cache policy $(xivo_package) | grep Installed | grep -oE '[0-9]{2,4}\.[0-9]+(\.[0-9]+)?|1\.2\.[0-9]{1,2}' | head -n1)" } set_current_sources() { local xivo_package=$(xivo_package) xivo-dist "$distribution_prefix$(xivo_version_installed)" } add_mirror() { echo "Add mirrors informations" local mirror="deb $mirror_xivo/$repo $distribution main" apt_dir="/etc/apt" sources_list_dir="$apt_dir/sources.list.d" if ! grep -qr "$mirror" "$apt_dir"; then echo "$mirror" > $sources_list_dir/tmp-pf.sources.list fi add_xivo_key add_docker_key export DEBIAN_FRONTEND=noninteractive $update $install xivo-dist xivo-dist "$distribution" rm -f "$sources_list_dir/tmp-pf.sources.list" $update } install_dahdi_modules() { flavour=$(echo $(uname -r) | cut -d\- -f3) kernel_release=$(ls /lib/modules/ | grep ${flavour}) for kr in ${kernel_release}; do ${download} dahdi-linux-modules-${kr} ${install} dahdi-linux-modules-${kr} done } install_xivo () { wget -q -O - $mirror_xivo/d-i/$debian_name/pkg.cfg | debconf-set-selections kernel_release=$(uname -r) ${install} --purge postfix install_dahdi_modules ${download} xivo ${install} xivo if [ -f "${dcomp}" ]; then ${dcomp} pull fi xivo-service restart all if [ $? -eq 0 ]; then echo 'You must now finish the installation' xivo_ip=$(ip a s eth0 | grep -E 'inet.*eth0' | awk '{print $2}' | cut -d '/' -f 1 ) echo "open http://$xivo_ip to configure XiVO" fi } check_distribution_is_32bit() { if [ "$distribution" = "xivo-polaris" ] || [ "$distribution" = "xivo-five" ]; then return 0 else return 1 fi } propose_polaris_installation() { echo $not_installable_message read -p 'Would you like to install XiVO Polaris [Y/n]? ' answer answer="${answer:-Y}" if [ "$answer" != 'y' -a "$answer" != 'Y' ]; then exit 0 fi distribution="xivo-polaris" } check_system_is_64bit() { if [ "$architecture" != "x86_64" ]; then echo $not_installable_message exit 1 fi } check_archive_prefix() { if [ "${1::5}" = "xivo-" ]; then echo 'Archive version must be typed without the prefix "xivo-"' exit 1 fi } usage() { cat << EOF This script is used to install XiVO usage : $(basename $0) {-d|-r|-a version} whitout arg : install production version -r : install release candidate version -d : install development version -a version : install archived version (14.18 or later) EOF exit 1 } distribution_prefix='xivo-' distribution='xivo-polaris' dev_lts='aldebaran' repo='debian' debian_name='jessie' debian_version='8' not_installable_message='This version of XiVO is not installable on 32-bit systems.' get_system_architecture if [ -z $1 ] && [ "$architecture" != "x86_64" ]; then if ! check_distribution_is_32bit; then propose_polaris_installation fi else while getopts ':dra:' opt; do case ${opt} in d) check_system_is_64bit distribution="xivo-$dev_lts-dev" ;; r) distribution='xivo-rc' ;; a) check_archive_prefix $OPTARG distribution="xivo-$OPTARG" repo='archive' if [ "$OPTARG" \> "2018" ]; then check_system_is_64bit fi if [ "${OPTARG::7}" = "2018.02" ]; then add_docker-engine_key fi if [ "$OPTARG" \> "15.19" ]; then debian_version='8' debian_name='jessie' elif [ "$OPTARG" \> "14.17" ]; then debian_version='7' debian_name='wheezy' else # 14.17 and earlier don't have xivo-dist available echo "This script only supports installing XiVO 14.18 or later." exit 1 fi ;; *) usage ;; esac done fi check_system add_mirror install_xivo if [ "$distribution" != "xivo-rc" ] && [ "${distribution: -4}" != "-dev" ]; then set_current_sources fi