Created
December 27, 2017 22:37
-
-
Save rcbop/91651d9f814c25ed69d4a1ec15634825 to your computer and use it in GitHub Desktop.
try to detect linux distro and install ansible, at last if does not succeed tries to install using git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#/ Usage: ./install-ansible.sh | |
#/ Description: AWESOME INSTALL SCRIPT | |
#/ Author: Rogério Peixoto (rcbpeixoto@gmail.com) | |
#/ Options: | |
#/ --help: Display this help message | |
usage() { grep '^#/' "$0" | cut -c4- ; exit 0 ; } | |
expr "$*" : ".*--help" > /dev/null && usage | |
echo "Using $TERM terminal" | |
readonly LOG_FILE="/tmp/$(basename "$0").log" | |
STEP=0 | |
bump_step(){ | |
((STEP++)) | |
log "${BLU}[INFO] ($STEP) $1${NC}" | |
} | |
cleanup() { | |
debug | |
info "Cleanup script" | |
# debug "Cleaning ansible" | |
# [ -d ~/ansible ] && rmdir ansible | |
} | |
log() { echo -e "${BWHT}["$(date "+%Y%m%d${NC}T${BWHT}%H%M%S")"]${NC} $*" | tee -a "$LOG_FILE" >&2 ; } | |
separator() { SEP=$(printf '%*s' 105 | tr ' ' '#') && log "${GRN}[INFO] $SEP${NC}"; } | |
info() { log "${GRN}[INFO] $1${NC}"; } | |
warning() { log "${YEL}[WARN] $1${NC}"; } | |
error() { log "${RED}[ERROR] $1${NC}"; } | |
fatal() { log "${MAG}[FATAL] $1${NC}"; exit 1 ; } | |
debug() { if [ "${DEBUG}" == "true" ]; then log "${CYN}[DEBUG] :: ${FUNCNAME[1]} :: $1${NC}"; fi } | |
set_colors(){ | |
export RED="\033[0;31m" | |
export BLU="\033[0;34m" | |
export GRN="\033[0;32m" | |
export YEL="\033[33;m" | |
export CYN="\033[0;36m" | |
export MAG="\033[35m" | |
export BWHT="\033[1m" | |
export NC="\033[0m" | |
} | |
unset_colors(){ | |
export RED='' | |
export BLU='' | |
export GRN='' | |
export YEL='' | |
export CYN='' | |
export MAG='' | |
export BWHT='' | |
export NC='' | |
} | |
configure_colors(){ | |
debug | |
if test -t 1; then | |
number_colors=$(tput colors) | |
if test -n "${number_colors}" && test ${number_colors} -ge 6; then | |
set_colors | |
else | |
unset_colors | |
fi | |
fi | |
} | |
check_root(){ | |
debug | |
if [[ $EUID -ne 0 ]]; then | |
fatal "Please run this script as root my friend" 1>&2 | |
fi | |
} | |
# | |
# HTTP Proxy configs | |
# | |
[ -z "$HTTP_PROXY_ADDRESS" ] && HTTP_PROXY_ADDRESS=10.32.150.40 | |
[ -z "$HTTP_PROXY_PORT" ] && HTTP_PROXY_PORT=82 | |
[ -z "$HTTPS_PROXY_ADDRESS" ] && HTTPS_PROXY_ADDRESS=10.32.150.40 | |
[ -z "$HTTPS_PROXY_PORT" ] && HTTPS_PROXY_PORT=82 | |
# cd /usr/src | |
# git -c "http.proxy=http://$PROXY_USER:$PROXY_PASS@10.32.150.40:82" \ | |
# clone http://github.com/ansible/ansible.git -b stable-2.4 --recursive | |
install_ssh_ask_pass(){ | |
debug | |
info "Installing ssh ask pass" | |
yum -y install openssh-askpass | |
} | |
ask_proxy_creds(){ | |
debug | |
read -p "Enter proxy username: " PROXY_USER | |
read -s -p "Enter proxy password: " PROXY_PASS | |
} | |
check_proxy_credentials(){ | |
debug | |
if [ -z "$PROXY_USER" ] && [ -z "$PROXY_PASS" ]; then | |
ask_proxy_creds | |
fi | |
if [ ! -z "$PROXY_USER" ] && [ ! -z "$PROXY_PASS" ]; then | |
echo_blue "Setting proxy" | |
HTTP_PROXY=http://$PROXY_USER:$PROXY_PASS@$HTTP_PROXY_ADDRESS:$HTTP_PROXY_PORT/ | |
HTTPS_PROXY=https://$PROXY_USER:$PROXY_PASS@$HTTPS_PROXY_ADDRESS:$HTTPS_PROXY_PORT/ | |
export HTTP_PROXY=$HTTP_PROXY | |
export HTTPS_PROXY=$HTTPS_PROXY | |
else | |
echo | |
echo_red "No proxy" | |
fi | |
} | |
#!/bin/sh | |
#TODO.md | |
yum_makecache_retry() { | |
debug | |
tries=0 | |
until [ $tries -ge 5 ] | |
do | |
yum makecache && break | |
let tries++ | |
sleep 1 | |
done | |
} | |
set_debug_flags(){ | |
debug | |
if [ "x$KITCHEN_LOG" = "xDEBUG" -o "x$OMNIBUS_ANSIBLE_LOG" = "xDEBUG" ]; then | |
export PS4='(${BASH_SOURCE}:${LINENO}): - [${SHLVL},${BASH_SUBSHELL},$?] $ ' | |
set -x | |
fi | |
} | |
install_ansible_with_git(){ | |
debug | |
versionTag=${1:-'v2.4.2.0-1'} | |
ansibleHosts=${2:-'/etc/ansible/hosts'} | |
# Install Git | |
# The most stable way of getting the latest version of ansible to get it directly from the git repo | |
info "[Install Ansible] Install Git" | |
yum install -y git-core | |
# Install Ansible Dependencies | |
info '[Install Ansible] Install dependencies' | |
yum install -y python-setuptools | |
easy_install jinja2 | |
easy_install pyyaml | |
easy_install paramiko | |
# Install Ansible | |
info "[Install Ansible] Go to Home folder" | |
cd ~ | |
info '[Install Ansible] Git Clone Ansible' | |
# git clone https://github.com/ansible/ansible.git ansible | |
git -c "http.proxy=http://$PROXY_USER:$PROXY_PASS@10.32.150.40:82" clone http://github.com/ansible/ansible.git -b stable-2.4 --recursive ansible | |
cd ansible | |
info '[Install Ansible] Checkout to desired tag' | |
git checkout $versionTag | |
info '[Install Ansible] Make and Install' | |
make install | |
info '[Install Ansible] Clean up!' | |
cd .. | |
rm -rf ansible | |
info '[Install Ansible] Copy Ansible HOSTS' | |
mkdir -p /etc/ansible && cp -fr $ansibleHosts /etc/ansible/hosts | |
info '[Install Ansible] Update Permissions on hosts file' | |
# Giving exec permission causes issues when running ansible | |
chmod 644 /etc/ansible/hosts | |
info '[Install Ansible] Export ANSIBLE_HOST path' | |
export ANSIBLE_HOSTS=/etc/ansible/hosts | |
info '[Install Ansible] Done!' | |
} | |
# | |
# APT-GET | |
# | |
debian_ubuntu_distros_install(){ | |
debug | |
apt-get update | |
# Install via package | |
# apt-get update && \ | |
# apt-get install --no-install-recommends -y software-properties-common && \ | |
# apt-add-repository ppa:ansible/ansible && \ | |
# apt-get update && \ | |
# apt-get install -y ansible | |
# Install required Python libs and pip | |
apt-get install -y python-pip python-yaml python-jinja2 python-httplib2 python-paramiko python-pkg-resources | |
[ -n "$( apt-cache search python-keyczar )" ] && apt-get install -y python-keyczar | |
if ! apt-get install -y git ; then | |
apt-get install -y git-core | |
fi | |
# If python-pip install failed and setuptools exists, try that | |
if [ -z "$(which pip)" -a -z "$(which easy_install)" ]; then | |
apt-get -y install python-setuptools | |
easy_install pip | |
elif [ -z "$(which pip)" -a -n "$(which easy_install)" ]; then | |
easy_install pip | |
fi | |
# If python-keyczar apt package does not exist, use pip | |
[ -z "$( apt-cache search python-keyczar )" ] && sudo pip install python-keyczar | |
# Install passlib for encrypt | |
apt-get install -y build-essential | |
apt-get install -y python-all-dev python-mysqldb sshpass && pip install pyrax pysphere boto passlib dnspython | |
# Install Ansible module dependencies | |
apt-get install -y bzip2 file findutils git gzip mercurial procps subversion sudo tar debianutils unzip xz-utils zip python-selinux | |
} | |
# | |
# YUM DISTROS | |
# | |
yellow_dog_updater_modified_distros_install(){ | |
debug | |
# Install required Python libs and pip | |
# Fix EPEL Metalink SSL error | |
# - workaround: https://community.hpcloud.com/article/centos-63-instance-giving-cannot-retrieve-metalink-repository-epel-error | |
# - SSL secure solution: Update ca-certs!! | |
# - http://stackoverflow.com/q/26734777/645491#27667111 | |
# - http://serverfault.com/q/637549/77156 | |
# - http://unix.stackexchange.com/a/163368/7688 | |
yum -y install ca-certificates nss | |
yum clean all | |
rm -rf /var/cache/yum | |
yum_makecache_retry | |
yum -y install epel-release | |
# One more time with EPEL to avoid failures | |
yum_makecache_retry | |
yum -y install python-pip PyYAML python-jinja2 python-httplib2 python-keyczar python-paramiko git | |
# If python-pip install failed and setuptools exists, try that | |
if [ -z "$(which pip)" -a -z "$(which easy_install)" ]; then | |
yum -y install python-setuptools | |
easy_install pip | |
elif [ -z "$(which pip)" -a -n "$(which easy_install)" ]; then | |
easy_install pip | |
fi | |
# Install passlib for encrypt | |
yum -y groupinstall "Development tools" | |
yum -y install python-devel MySQL-python sshpass && pip install pyrax pysphere boto passlib dnspython | |
# Install Ansible module dependencies | |
yum -y install bzip2 file findutils git gzip hg svn sudo tar which unzip xz zip libselinux-python | |
[ -n "$(yum search procps-ng)" ] && yum -y install procps-ng || yum -y install procps | |
} | |
# ######################## | |
# MAIN INSTALLATION FLOW | |
# ######################## | |
install_ansible(){ | |
debug | |
if [ ! $(which ansible-playbook) ]; then | |
if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ] || [ -f /etc/system-release ] || grep -q 'Amazon Linux' /etc/system-release; then | |
yellow_dog_updater_modified_distros_install | |
elif [ -f /etc/debian_version ] || [ grep -qi ubuntu /etc/lsb-release ] || grep -qi ubuntu /etc/os-release; then | |
debian_ubuntu_distros_install | |
else | |
warning 'BOLLOCKS ROGÉRIO!!' | |
warning 'Could not detect distro or distro unsupported' | |
warning 'Trying to install ansible via pip without some dependencies' | |
warning 'Not all functionality of ansible may be available' | |
warning 'Gonna try installing ansible using git' | |
install_ansible_with_git | |
fi | |
mkdir /etc/ansible/ | |
echo -e '[local]\nlocalhost\n' > /etc/ansible/hosts | |
pip install ansible | |
if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ] || [ -f /etc/system-release ] || grep -q 'Amazon Linux' /etc/system-release; then | |
# Fix for pycrypto pip / yum issue | |
# https://github.com/ansible/ansible/issues/276 | |
if ansible --version 2>&1 | grep -q "AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'" ; then | |
warning 'Re-installing python-crypto package to workaround ansible/ansible#276' | |
warning 'https://github.com/ansible/ansible/issues/276' | |
pip uninstall -y pycrypto | |
yum erase -y python-crypto | |
yum install -y python-crypto python-paramiko | |
fi | |
fi | |
fi | |
} | |
if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then | |
trap cleanup EXIT | |
configure_colors | |
check_root | |
bump_step "Install ssh ask pass" | |
separator | |
install_ssh_ask_pass | |
bump_step "Configure HTTP proxy" | |
separator | |
[ -z "$NO_PROXY" ] && check_proxy_credentials || echo_green 'No proxy true' | |
bump_step "Set debug flags" | |
separator | |
set_debug_flags | |
bump_step "Install ansible" | |
separator | |
install_ansible | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment