База знаний Скрипт создания контейнера Р-Управления (va-mn)

Скрипт создания контейнера Р-Управления (va-mn)

Задача

Заново создать контейнер Р-Управления с помощью шелл-скрипта — например, после его утраты.

Предусловия

Скрипт выполняется на хосте, где изначально создавались контейнеры управления.

Текст скрипта восстановлен из повреждённого экспорта старой базы знаний. Перед использованием сверьте его с рабочим экземпляром из дистрибутива (deploy-va-mn).

Использование

Usage: $0 [OPTIONS]
  -h                   Display this help message
  -l PATH              Override log file path
  -r PATH              Override base repository path
  -R PATH              Override release RPM path
  --skip-inet-check    Skip Internet connection checking
  --set-verbose-log    Increase verbosity of VA MN after installation

Скрипт

#!/bin/bash
#
# A script for installing VA management node on VzLinux/CentOS 7.
#
# Copyright (c) 2016-2017 R-Platforma LLC, All rights reserved.
#

version="1.7.3"

LOGFILE="/var/log/deploy-va-mn.log"
REPO_BASE="http://updates.rosplatforma.ru/r-management/va-mn"

function usage()
{
    echo "Usage: $0 [OPTIONS]" >&2
    echo "  -h                   Display this help message" >&2
    echo "  -l PATH              Override log file path" >&2
    echo "  -r PATH              Override base repository path" >&2
    echo "  -R PATH              Override release RPM path" >&2
    echo "  --skip-inet-check    Skip Internet connection checking" >&2
    echo "  --set-verbose-log    Increase verbosity of VA MN after installation" >&2
}

function get_options()
{
    while getopts ':hl:r:R:-:' opt; do
        case $opt in
        h)
            usage
            exit 0
            ;;
        l)
            LOGFILE=$OPTARG
            ;;
        r)
            REPO_BASE=$OPTARG
            ;;
        R)
            PACKAGE_OVR=$OPTARG
            ;;
        -)
            if [ "$OPTARG" == "skip-inet-check" ]; then
                SKIP_INET_CHECK=1
            elif [ "$OPTARG" == "set-verbose-log" ]; then
                SET_VERBOSE_LOG=1
            else
                echo "Unknown option --$OPTARG" >&2
                usage
                exit 3
            fi
            ;;
        \?)
            echo "Unknown option -$OPTARG" >&2
            usage
            exit 3
            ;;
        :)
            echo "Option -$OPTARG requires an argument" >&2
            usage
            exit 3
            ;;
        esac
    done
}

function get_os_id()
{
    local id="unknown"
    local src="/etc/os-release"

    if [ -e "$src" ]; then
        id=`. $src 2>/dev/null && echo $ID`
    fi

    echo $id
}

function get_os_version()
{
    local ver="0"
    local src="/etc/os-release"

    if [ -e "$src" ]; then
        ver=`. $src 2>/dev/null && echo $VERSION_ID | sed 's/\.//g'`
    fi

    echo $ver
}

get_options "$@"

os_id=$(get_os_id)
os_version=$(get_os_version)

product_lc=`echo R-Virtualization | tr A-Z a-z`

if [ "$os_id" = "rosplatforma" ]; then
    if [ "$os_version" -ne "7" -a "$os_version" -ne "70" ]; then
        echo "It looks that you are running unsupported version of CentOS"
        exit 2
    fi
elif [ "$os_id" = "$product_lc" ]; then
    if [ -e /etc/${product_lc}-release ]; then
        echo "Can't install VA MN on R-Virtualization host"
        exit 2
    elif [ "$os_version" -ne "7" -a "$os_version" -ne "70" ]; then
        echo "It looks that you are running unsupported version of R-Virtualization Linux"
        exit 2
    fi
else
    echo "'$os_id' distribution is not supported"
    exit 2
fi

# Try to install va-mn-release from updates repo, then from the release one
# Also let's be prepared for the case when this package has "noarch" suffix and then it doesn't have it
# We don't check rpm exit code since it returns 1 if package is already installed;
# let's check if package is present instead
echo "Installing va-mn-release..." 2>&1 | tee -a $LOGFILE
if [ -z "$PACKAGE_OVR" ]; then
    rpm -Uvh $REPO_BASE/updates/7.0/x86_64/os/Packages/v/va-mn-release.noarch.rpm >> $LOGFILE 2>&1
    rpm -Uvh $REPO_BASE/updates/7.0/x86_64/os/Packages/v/va-mn-release.rpm >> $LOGFILE 2>&1
    rpm -Uvh $REPO_BASE/releases/7.0/x86_64/os/Packages/v/va-mn-release.noarch.rpm >> $LOGFILE 2>&1
    rpm -Uvh $REPO_BASE/releases/7.0/x86_64/os/Packages/v/va-mn-release.rpm >> $LOGFILE 2>&1
else
    rpm -Uvh $PACKAGE_OVR >> $LOGFILE 2>&1
    REPO_OVR=`echo $PACKAGE_OVR | rev | cut -f4- -d/ | rev`
    yum install -y yum-utils >> $LOGFILE 2>&1
    yum-config-manager --disable va-mn-updates >> $LOGFILE 2>&1
    yum-config-manager --disable va-mn >> $LOGFILE 2>&1
    cat > /etc/yum.repos.d/va-mn-custom.repo <<EOF
[va-mn-custom]
name=R-Management Management Node - Custom Repo
baseurl=$REPO_OVR
enabled=1
gpgcheck=0
EOF
fi
# Die if package is not installed; note that rpm will issue a "package not installed" message for us
rpm -q va-mn-release || exit 1

# Check if we have access to centos mirrors
if ! ping -c 1 mirrorlist.centos.org && ! (($SKIP_INET_CHECK)); then
    echo "It looks like Internet connection is missing."
    echo "Access to public repositories of the distribution is required to install dependencies for VA packages."
    echo "If you think that the check is wrong or you have already installed all necessary dependencies, "
    echo "launch this script with --skip-inet-check parameter to skip this check."
    exit 1
fi

# Check if we have firewalld running; do this before installing VA packages
# since they can stop it
FIREWALLD_STATE_BEFORE=`systemctl show firewalld --property ActiveState`

yum -y groupinstall "VA Management Node" 2>&1 | tee -a $LOGFILE
if [ ${PIPESTATUS[0]} != 0 ]; then
    echo "Failed to install VA Management Node packages"
    exit 1
fi

yum -y groupinstall "VA Control Center" 2>&1 | tee -a $LOGFILE
if [ ${PIPESTATUS[0]} != 0 ]; then
    echo "Failed to install VA Control Center packages"
    exit 1
fi

if systemctl is-enabled iptables >/dev/null 2>&1; then
    echo "Setting up iptables rules for VA MN..." | tee -a $LOGFILE
    echo "Note that we don't override your own rules; in case you can't access VA MN Web UI, "
    echo "please check your iptables configs"
    # This rule is present by default in Vz7 templates for CentOS
    iptables -D INPUT -j REJECT --reject-with icmp-host-prohibited
    iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

    for PORT in 389 4533 4534 4648 4450 4452 80 443 1433 22; do
        echo "...port: $PORT..." | tee -a $LOGFILE
        iptables -A INPUT -p tcp -m tcp --dport ${PORT} -m conntrack --ctstate NEW -j ACCEPT
    done
elif systemctl is-enabled firewalld >/dev/null 2>&1; then
    echo "Checking firewalld config and state..." | tee -a $LOGFILE

    FIREWALLD_STATE_AFTER=`systemctl show firewalld --property ActiveState`
    if [ $FIREWALLD_STATE_BEFORE == 'ActiveState=active' -a $FIREWALLD_STATE_AFTER != 'ActiveState=active' ]; then
        echo "Restarting firewalld, it was stopped during VA packages install..." | tee -a $LOGFILE
        systemctl start firewalld 2>&1 | tee -a $LOGFILE
    fi

    FW_FORCE_START=0
    if [ `systemctl show firewalld --property ActiveState` != 'ActiveState=active' ]; then
        echo "You have firewalld enabled but not running."
        echo "We will start firewalld right now to add rules for VA MN."
        echo "The service will be stopped again just after the rules are added."
        FW_FORCE_START=1
        systemctl start firewalld 2>&1 | tee -a $LOGFILE
    fi

    echo "Setting up firewalld rules for VA MN..." | tee -a $LOGFILE
    echo "...ssh service..." | tee -a $LOGFILE
    firewall-cmd --permanent --add-service ssh 2>&1 | tee -a $LOGFILE

    for PORT in 389 4533 4534 4648 4450 4452 80 443 1433; do
        echo "...port: $PORT..." | tee -a $LOGFILE
        firewall-cmd --permanent --add-port ${PORT}/tcp 2>&1 | tee -a $LOGFILE
    done
    firewall-cmd --reload 2>&1 | tee -a $LOGFILE

    if [ $FW_FORCE_START = 1 ]; then
        systemctl stop firewalld 2>&1 | tee -a $LOGFILE
    fi
fi

if (($SET_VERBOSE_LOG)); then
    /opt/pva/mn/bin/vzlxmlman -s 6 -i data/system/configuration/log_level /var/opt/pva/mn/etc/vzagent.conf
    sed -i 's:<level>1</level>:<level>6</level>:' /etc/opt/pva/cc/pvacc.conf
    sed -i 's:<agent_level>3</agent_level>:<agent_level>6</agent_level>:' /etc/opt/pva/cc/pvacc.conf
fi

systemctl start va-mn 2>&1 | tee -a $LOGFILE
if [ ${PIPESTATUS[0]} != 0 ]; then
    echo "Failed to start va-mn service"
    exit 1
fi

systemctl start va-cc 2>&1 | tee -a $LOGFILE
if [ ${PIPESTATUS[0]} != 0 ]; then
    echo "Failed to start va-cc service"
    exit 1
fi

echo "VA Management Node has been successfully installed!"
echo "Web UI is now accessible at the following address(es): "
IPS=$(/sbin/ip -o addr show scope global | /usr/bin/awk '{gsub(/\/.*/, " ", $4); print $4}')
for IP in $IPS; do
    echo "  https://${IP}:4648"
done

echo "Please use root credentials from this machine to enter the Web UI".