Понедельник (09/12/16)

/dev/pts/0
10:52:49
#cat install
#!/bin/sh
hostname=`hostname`
uname -a | egrep -qi '(freebsd|darwin)' || hostname=`hostname -f`
###############################################################################
#
# Set this variables before installation:
lilalo_user=13-10-2014
lab=NT-LNet
install_l3bashrc_for_this_users=${users:-"root user"}  # users who will use l3agent and l3script
lilalo_context="/users/${lilalo_user}/${lab}/${hostname}"
#
###############################################################################
lilalo_rc=.l3rc
lilalo_home=.lilalo
url_lilalo="http://xgu.ru/lilalo"
url_l3bashrc="${url_lilalo}"/l3bashrc
url_l3agent="${url_lilalo}"/l3-agent
url_l3config_pm="${url_lilalo}"/l3config.pm
url_l3config="${url_lilalo}"/l3-config
url_l3prompt="${url_lilalo}"/l3prompt
url_perl_modules=${url_lilalo}/
perl_modules="Term-VT102 Text-Iconv"
apt_get_install_this="perl make libmodule-build-perl libc6-dev gcc"
wget=wget
uname -a | egrep -qi '(bsd|darwin)' && wget=fetch
normC='\033[0;39m'
whiteC='\033[1;37m'
redC='\033[0;31m'
greenC='\033[0;32m'
apt_get_install_deps()
{
    return 0
    if which apt-get >& /dev/null
    then
        apt-get install -y $apt_get_install_this
    else
        echo "Please install this dependencies manually:"
        echo $apt_get_install_this
        echo "Have you installed this already (y/n)?"
        echo y | read answer
        if echo $answer | grep -q ^[yY]
        then
            true
        else
            echo Please install the dependencies and rerun the script
            exit 1
        fi
    fi
}
step()
{
    msg="$1"
    shift
    printf "${whiteC}""$msg""...${normC}\n"
#    eval "$@" 2>&1 | sed 's/^/|\ \ \ /' && printf "Ok\n" || printf "Failed\n"
    eval "$@" 2>&1 > log 2>&1 && \
    {
          cat log | sed 's/^/|\ \ \ /'
          printf "${greenC}""Ok\n""${normC}"
    } || \
    {
        cat log | sed 's/^/|\ \ \ /'
        printf "${redC}""Failed\n""${normC}"
    }
}
get_user_home()
{
    uname -a | egrep -qi '(freebsd|darwin)' && pw user show "$@"| awk -F: '{print $9}' || getent passwd "$@"| awk -F: '{print $6}'
}
install_to_users_homes()
{
    . l3bashrc
    users="$@"
    set -x
    for user in $users
    do
        user_home=`get_user_home "$user"`
        mkdir -p ${user_home}/${lilalo_home}
        mkdir /etc/lilalo/
        cp l3config.pm /etc/lilalo/
        cp l3-agent /usr/local/bin
        cp l3-config /usr/local/bin
        ln -s `which bash` /usr/local/bin/l3script
        chmod 755 /usr/local/bin/l3-{agent,config}
        cp l3bashrc ${user_home}/${lilalo_home}
        cp l3prompt ${user_home}/${lilalo_home}
        chmod 755 ${user_home}/${lilalo_home}/l3prompt
        chown -R $user ${user_home}/${lilalo_home}
        echo l3cd=${lilalo_context}/$user > ${user_home}/${lilalo_rc}
        chown -R $user ${user_home}/${lilalo_rc}
    done
    set +x
}
install_to_users_bashrc()
{
    users="$@"
    for user in $users
    do
        user_home=`get_user_home "$user"`
        grep -q lilalo ${user_home}/.bashrc 2> /dev/null\
        || echo "[ \$0 == l3script ] && . ${user_home}/.lilalo/l3bashrc && _l3_start" >> ${user_home}/.bashrc; chown -R ${user} ${user_home}/.bashrc
    done
}
install_to_users_bash_profile()
{
    users="$@"
    for user in $users
    do
        user_home=`get_user_home "$user"`
        grep -q l3-agent ${user_home}/.bash_profile 2> /dev/null \
        || { echo >> ${user_home}/.bash_profile ; cat ${user_home}/.bash_profile | sed '1s/^/l3-agentX/' | tr X '\n' > /tmp/$$$$l3 ; mv /tmp/$$$$l3 ${user_home}/.bash_profile; chown -R ${user} ${user_home}/.bash_profile; }
    done
}
show_usage()
{
    cat <<USAGE
Usage:
    $0
USAGE
}
show_final_message()
{
    cat <<FINAL_MESSAGE
Installation is successfully completed.
Now restart your shell or relogin
to start script writing.
Your current lilalo context is ${lilalo_context}/USER
If you use xgu.ru backend, your labs will be available at
http://xgu.ru/l3/${lilalo_context}
Use commands
 $ l3cd ${lilalo_context%/*/*}/MY-NEW-CONTEXT/${hostname}/USER
 $ l3pwd
to change and to know your current context.
For further information see http://xgu.ru/lilalo/ (in Russian).
Thank you gor using LiLaLo.
Happy Labbing!
(don't forget to restart bash or relogin)
FINAL_MESSAGE
}
temp_dir=/tmp/lilalo-install-temp-$$
mkdir -p ${temp_dir}
cd ${temp_dir}
step "Installing dependencies" apt_get_install_deps
step "Downloading l3bashrc" ${wget} ${url_l3bashrc}
step "Downloading l3prompt" ${wget} ${url_l3prompt}
step "Downloading l3-agent" '${wget} ${url_l3agent}; ${wget} ${url_l3config_pm}; ${wget} ${url_l3config}'
step "Downloading perl modules for l3-agent" '{ for i in ${perl_modules}; do ${wget} ${url_perl_modules}/$i.tar.gz; done; }'
step "Installing perl modules for l3-agent" '{ for i in ${perl_modules}; do tar xvfz $i.tar.gz; cd $i*[^z]; perl Makefile.PL; make; make install; cd ..; done; }'
step "Installing l3bashrc to users home directories" install_to_users_homes $install_l3bashrc_for_this_users
step "Adding l3bashrc invocation to ~/.bashrc " install_to_users_bashrc $install_l3bashrc_for_this_users
step "Adding l3-agent invocation to ~/.bash_profile " install_to_users_bash_profile $install_l3bashrc_for_this_users
cd /
rm -rf ${temp_dir}
show_final_message