Skip to content

Instantly share code, notes, and snippets.

@dcinzona
Last active August 29, 2015 14:15
Show Gist options
  • Save dcinzona/b586f16e85df0a63f5af to your computer and use it in GitHub Desktop.
Save dcinzona/b586f16e85df0a63f5af to your computer and use it in GitHub Desktop.
ElasticSearch deployment script with nginx ready for Kibana using Azure File Service for config/web/data storage
#!/usr/bin/env bash
tput setaf 8
##DEFAULT environment variables
AZURESTORAGEACCOUNT='null'
AZURESTORAGEPASSWORD='null'
AZUREFILESHARE='elasticsearch'
ELASTICSEARCH_VERSION=1.4.3
KIBANA_VERSION=3.1.2 #Unused currently
ESCONFIG='esconfig' #you can use this to separate between QA and Production configs
RUN='false' # defaults to false, so that running the script will preview out ENV variables
#Set Script Name variable
SCRIPT=`basename ${BASH_SOURCE[0]}`
#Set fonts for Help.
NORM=`tput sgr0`
BOLD=`tput bold`
REV=`tput smso`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
RED=`tput setaf 1`
BLUE=`tput setaf 4`
RESET=`tput setaf 8`
WHITE=`tput setaf 7`
#Help function
function HELP {
echo -e \\n"Help documentation for ${BOLD}${SCRIPT}.${NORM}"\\n
echo -e "${REV}Basic usage:${NORM} ${BOLD}$SCRIPT -u account -p key -e 1.4.3 -r 1${NORM}"\\n
echo "Command line switches are optional. The following switches are recognized."
echo "${REV}-u${NORM} --Sets the value for option ${BOLD}AZURESTORAGEACCOUNT${NORM}. Default is ${BOLD}nflpaelasticsearchdata${NORM}."
echo "${REV}-p${NORM} --Sets the value for option ${BOLD}AZURESTORAGEPASSWORD${NORM}. Default is ${BOLD}null${NORM}."
echo "${REV}-f${NORM} --Sets the value for option ${BOLD}AZUREFILESHARE${NORM}. Default is ${BOLD}elasticsearch${NORM}."
echo "${REV}-e${NORM} --Sets the version to download for elasticsearch. Default is ${BOLD}1.4.3${NORM}."
echo "${REV}-k${NORM} --Sets the version to download for kibana. Default is ${BOLD}3.1.2${NORM}."
echo "${REV}-r${NORM} --Runs the script. Default is ${BOLD}false${NORM}."
echo -e "${REV}-h${NORM} --Displays this help message. No further functions are performed."\\n
exit 1
}
function SHOWSETTINGS {
echo -e \\n"${YELLOW}Settings >>>${NORM}"\\n
echo "${NORM}Azure storage account: ${BOLD}${YELLOW}"$AZURESTORAGEACCOUNT
echo "${NORM}Azure storage key: ${BOLD}${YELLOW}"$AZURESTORAGEPASSWORD
echo "${NORM}Azure storage file share: ${BOLD}${YELLOW}"$AZUREFILESHARE
echo "${NORM}ElasticSearch Version: ${BOLD}${YELLOW}"$ELASTICSEARCH_VERSION
echo "${NORM}Kibana Version: ${BOLD}${YELLOW}"$KIBANA_VERSION
echo "${NORM}ESConfig: ${BOLD}${YELLOW}"$ESCONFIG"${NORM}"
echo -e " "
read -p "${BOLD}${WHITE}Continue?${NORM} [y/N] "
echo "${NORM}" # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy] ]]
then
exit 1
fi
}
#Check the number of arguments. If none are passed, print help and exit.
NUMARGS=$#
if [ $NUMARGS -eq 0 ]; then
HELP
fi
while getopts ":u:p:f:e:k:r:h" opt; do
case $opt in
u)
AZURESTORAGEACCOUNT=$OPTARG
;;
p)
AZURESTORAGEPASSWORD=$OPTARG
;;
f)
AZUREFILESHARE=$OPTARG
;;
e)
ELASTICSEARCH_VERSION=$OPTARG
;;
k)
KIBANA_VERSION=$OPTARG
;;
r)
RUN=$OPTARG
;;
h)
HELP
exit 1
;;
\?) #unrecognized option - show help
echo -e \\n"${RED}Option -${BOLD}$OPTARG${NORM}${RED} not allowed."
HELP
;;
:)
echo "${RED}Option -$OPTARG requires an argument.${NORM}" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
SHOWSETTINGS
if [ $AZURESTORAGEACCOUNT == 'null' ]; then
echo -e "${RED}ERROR: ${BOLD}Azure storage account required.\n${NORM}You can set it using the ${BOLD}${WHITE}-u${NORM} parameter.${NORM}\n"
exit 1
fi
if [ $AZURESTORAGEPASSWORD == 'null' ]; then
echo -e "${RED}ERROR: ${BOLD}Azure storage key required.\n${NORM}You can set it using the ${BOLD}${WHITE}-p${NORM} parameter.${NORM}\n"
exit 1
fi
echo "${YELLOW}>>> Updating hostname in /etc/hosts${NORM}"
sudo su -c "echo -e '\n#The following was inserted by the NFLPA Provisioning script\n127.0.0.1\t"$HOSTNAME"' >> /etc/hosts"
echo "${YELLOW}>>> Provisioning...${NORM}"
##Update apt
sudo apt-get update
##install cifs for azure file storage
sudo apt-get install cifs-utils -y
#install ElasticSearch latest
cd ~
echo "${YELLOW}>>> Installing Java{NORM}"
sudo apt-get install openjdk-7-jre-headless -y
sudo apt-get install wget -y
echo "${YELLOW}>>> Downloading and Installing Elastic Search $ELASTICSEARCH_VERSION{NORM}"
wget --quiet https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-$ELASTICSEARCH_VERSION.deb
sudo dpkg -i elasticsearch-$ELASTICSEARCH_VERSION.deb
rm elasticsearch-$ELASTICSEARCH_VERSION.deb
sudo update-rc.d elasticsearch defaults 95 10
echo "${YELLOW}>>> Installing NGINX for Kibana${NORM}"
sudo apt-get install nginx-full apache2-utils -y
echo "${YELLOW}>>> Mounting Azure File Service Shares${NORM}"
sudo mkdir /usr/share/azure-files
#Add to fstab so they automount on restart
##Azure File Storage link for ElasticSearch Configs (This can be done using ln -s as well)
sudo su -c "echo -e '//"$AZURESTORAGEACCOUNT".file.core.windows.net/"$AZUREFILESHARE"/"$ESCONFIG"\t/etc/elasticsearch\tcifs\tvers=2.1,dir_mode=0777,file_mode=0777,username="$AZURESTORAGEACCOUNT",password="$AZURESTORAGEPASSWORD"' >> /etc/fstab"
##Azure File Storage root share
sudo su -c "echo -e '//"$AZURESTORAGEACCOUNT".file.core.windows.net/"$AZUREFILESHARE"\t/usr/share/azure-files\tcifs\tvers=2.1,dir_mode=0777,file_mode=0777,username="$AZURESTORAGEACCOUNT",password="$AZURESTORAGEPASSWORD"' >> /etc/fstab"
##AFS mount for nginx configs
sudo su -c "echo -e '//"$AZURESTORAGEACCOUNT".file.core.windows.net/"$AZUREFILESHARE"/nginx\t/etc/nginx\tcifs\tvers=2.1,dir_mode=0777,file_mode=0777,username="$AZURESTORAGEACCOUNT",password="$AZURESTORAGEPASSWORD"' >> /etc/fstab"
#remount
sudo mount -a
echo "${YELLOW}>>> STARTING SERVICES${NORM}"
#start elasticsearch
sudo service elasticsearch start
##Kibana files are on shared azure storage so no need to download. This can be centrally managed
sudo service nginx start
sudo service nginx reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment