2018-03-27 18:15:01 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# 3 options:
|
|
|
|
# - docker compose
|
|
|
|
# - docker "simple"
|
|
|
|
# - classic installation
|
|
|
|
# > macOS
|
2018-03-28 15:37:04 +02:00
|
|
|
# > debian/ubuntu
|
|
|
|
# > other linux
|
2018-03-27 18:15:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
config()
|
|
|
|
{
|
2018-03-29 12:41:00 +02:00
|
|
|
if ! command -v curl
|
|
|
|
then
|
|
|
|
echo "Please install curl before running this script."
|
|
|
|
echo "curl was not found, exiting..."
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-03-27 18:15:01 +02:00
|
|
|
FM_PATH=$(pwd)
|
2018-03-28 15:37:04 +02:00
|
|
|
TYPE="NOT-FOUND"
|
|
|
|
read -rp "Is fab-manager installed at \"$FM_PATH\"? (y/n) " confirm </dev/tty
|
2018-03-27 18:15:01 +02:00
|
|
|
if [ "$confirm" = "y" ]
|
|
|
|
then
|
2018-03-28 15:37:04 +02:00
|
|
|
if [ -f "$FM_PATH/config/application.yml" ]
|
|
|
|
then
|
|
|
|
ES_HOST=$(cat "$FM_PATH/config/application.yml" | grep ELASTICSEARCH_HOST | awk '{print $2}')
|
|
|
|
elif [ -f "$FM_PATH/config/env" ]
|
|
|
|
then
|
|
|
|
ES_HOST=$(cat "$FM_PATH/config/env" | grep ELASTICSEARCH_HOST | awk '{split($0,a,"="); print a[2]}')
|
|
|
|
fi
|
2018-03-27 18:15:01 +02:00
|
|
|
ES_IP=$(getent hosts "$ES_HOST" | awk '{ print $1 }')
|
|
|
|
else
|
|
|
|
echo "Please run this script from the fab-manager's installation folder"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
test_docker_compose()
|
|
|
|
{
|
2018-03-28 15:37:04 +02:00
|
|
|
if [[ -f "$FM_PATH/docker-compose.yml" ]]
|
2018-03-27 18:15:01 +02:00
|
|
|
then
|
|
|
|
docker-compose ps | grep elastic
|
2018-03-28 15:37:04 +02:00
|
|
|
if [[ $? = 0 ]]
|
|
|
|
then
|
|
|
|
TYPE="DOCKER-COMPOSE"
|
|
|
|
local container_id=$(docker-compose ps | grep elastic | awk '{print $1}')
|
|
|
|
ES_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$container_id")
|
|
|
|
fi
|
2018-03-27 18:15:01 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
test_docker()
|
|
|
|
{
|
|
|
|
docker ps | grep elasticsearch:1.7
|
|
|
|
if [[ $? = 0 ]]
|
|
|
|
then
|
|
|
|
local containers=$(docker ps | grep elasticsearch:1.7)
|
|
|
|
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(echo "$containers" | awk '{print $1}') | grep "$ES_IP"
|
2018-03-28 15:37:04 +02:00
|
|
|
if [[ $? = 0 ]]; then TYPE="DOCKER"; fi
|
2018-03-27 18:15:01 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
test_classic()
|
|
|
|
{
|
2018-03-28 15:37:04 +02:00
|
|
|
if [ "$ES_IP" = "127.0.0.1" ] || [ "$ES_IP" = "::1" ]
|
2018-03-27 18:15:01 +02:00
|
|
|
then
|
|
|
|
whereis -b elasticsearch | grep "/"
|
2018-03-28 15:37:04 +02:00
|
|
|
if [[ $? = 0 ]]; then TYPE="CLASSIC"; fi
|
2018-03-27 18:15:01 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
test_running()
|
|
|
|
{
|
2018-03-28 15:37:04 +02:00
|
|
|
local http_res=$(curl -I "$ES_IP:9200" 2>/dev/null | head -n 1 | cut -d$' ' -f2)
|
|
|
|
if [ "$http_res" = "200" ]
|
2018-03-27 18:15:01 +02:00
|
|
|
then
|
|
|
|
echo "ONLINE"
|
|
|
|
else
|
|
|
|
echo "OFFLINE"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-03-28 15:37:04 +02:00
|
|
|
test_version()
|
|
|
|
{
|
|
|
|
local version=$(curl "$ES_IP:9200" 2>/dev/null | grep number | awk '{print $3}')
|
|
|
|
if [[ "$version" = *\"1.7* ]]; then echo "1.7"
|
|
|
|
elif [[ "$version" = *\"2.4* ]]; then echo "2.4"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-03-27 18:15:01 +02:00
|
|
|
detect_installation()
|
|
|
|
{
|
|
|
|
echo "Detecting installation type..."
|
|
|
|
|
2018-03-28 15:37:04 +02:00
|
|
|
test_docker_compose
|
|
|
|
if [[ "$TYPE" = "DOCKER-COMPOSE" ]]
|
2018-03-27 18:15:01 +02:00
|
|
|
then
|
|
|
|
echo "Docker-compose installation detected."
|
|
|
|
else
|
2018-03-28 15:37:04 +02:00
|
|
|
test_docker
|
|
|
|
if [[ "$TYPE" = "DOCKER" ]]
|
2018-03-27 18:15:01 +02:00
|
|
|
then
|
|
|
|
echo "Classical docker installation detected."
|
|
|
|
else
|
2018-03-28 15:37:04 +02:00
|
|
|
test_classic
|
|
|
|
if [[ "$TYPE" = "CLASSIC" ]]
|
2018-03-27 18:15:01 +02:00
|
|
|
then
|
|
|
|
echo "Local installation detected on the host system."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$TYPE" = "NOT-FOUND" ]]
|
|
|
|
then
|
|
|
|
echo "ElasticSearch 1.7 was not found on the current system, exiting..."
|
|
|
|
exit 2
|
|
|
|
else
|
|
|
|
echo "Detecting online status..."
|
|
|
|
if [[ "$TYPE" != "NOT-FOUND" ]]
|
|
|
|
then
|
|
|
|
STATUS=$(test_running)
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
upgrade_compose()
|
|
|
|
{
|
2018-03-29 12:41:00 +02:00
|
|
|
local current=$1
|
|
|
|
local target=$2
|
2018-03-27 18:15:01 +02:00
|
|
|
echo "Upgrading docker-compose installation..."
|
|
|
|
docker-compose stop elasticsearch
|
|
|
|
docker-compose rm -f elasticsearch
|
2018-03-29 12:41:00 +02:00
|
|
|
sed -i.bak "s/image: elasticsearch:$current/image: elasticsearch:$target/g" "$FM_PATH/docker-compose.yml"
|
2018-03-27 18:15:01 +02:00
|
|
|
docker-compose pull
|
|
|
|
docker-compose up -d
|
|
|
|
sleep 10
|
|
|
|
STATUS=$(test_running)
|
2018-03-28 15:37:04 +02:00
|
|
|
local version=$(test_version)
|
2018-03-29 12:41:00 +02:00
|
|
|
if [ "$STATUS" = "ONLINE" ] && [ "$version" = "$target" ]; then
|
|
|
|
echo "Migration to elastic $target was successful."
|
2018-03-27 18:15:01 +02:00
|
|
|
else
|
2018-03-29 12:41:00 +02:00
|
|
|
echo "Unable to find an active ElasticSearch $target instance, something may have went wrong, exiting..."
|
2018-03-28 15:37:04 +02:00
|
|
|
echo "status: $STATUS, version: $version"
|
2018-03-27 18:15:01 +02:00
|
|
|
exit 4
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
upgrade_docker()
|
|
|
|
{
|
2018-03-29 12:41:00 +02:00
|
|
|
local current=$1
|
|
|
|
local target=$2
|
2018-03-27 18:15:01 +02:00
|
|
|
echo "Upgrading docker installation..."
|
2018-03-29 12:41:00 +02:00
|
|
|
local containers=$(docker ps | grep "elasticsearch:$current")
|
2018-03-27 18:15:01 +02:00
|
|
|
# get container id
|
|
|
|
local id=$(docker inspect -f '{{.Id}} {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(echo "$containers" | awk '{print $1}') | grep "$ES_IP" | awk '{print $1}')
|
|
|
|
# get container name
|
|
|
|
local name=$(docker inspect -f '{{.Name}}' "$id" | sed s:^/::g)
|
|
|
|
# get container network name
|
2018-03-28 10:45:04 +02:00
|
|
|
local network=$(docker inspect -f '{{.NetworkSettings.Networks}}' "$id" | sed 's/map\[\(.*\):0x[a-f0-9]*\]/\1/')
|
2018-03-27 18:15:01 +02:00
|
|
|
# get container mapping to data folder
|
2018-03-29 09:33:34 +02:00
|
|
|
local mounts=$(docker inspect -f '{{.Mounts}}' "$id" | sed 's/} {/\n/g' | sed 's/^\[\?{\?bind[[:blank:]]*\([^[:blank:]]*\)[[:blank:]]*\([^[:blank:]]*\)[[:blank:]]*true \(rprivate\)\?}\?]\?$/-v \1:\2/g' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g')
|
2018-03-29 12:41:00 +02:00
|
|
|
# stop current elastic
|
2018-03-28 10:45:04 +02:00
|
|
|
docker stop "$name"
|
|
|
|
docker rm -f "$name"
|
2018-03-29 12:41:00 +02:00
|
|
|
# run target elastic
|
|
|
|
docker pull "elasticsearch:$target"
|
|
|
|
echo docker run --restart=always -d --name="$name" --network="$network" --ip="$ES_IP" "$mounts" "elasticsearch:$target" | bash
|
2018-03-28 11:55:10 +02:00
|
|
|
# check status
|
2018-03-28 10:45:04 +02:00
|
|
|
sleep 10
|
|
|
|
STATUS=$(test_running)
|
2018-03-28 15:37:04 +02:00
|
|
|
local version=$(test_version)
|
2018-03-29 12:41:00 +02:00
|
|
|
if [ "$STATUS" = "ONLINE" ] && [ "$version" = "$target" ]; then
|
|
|
|
echo "Migration to elastic $target was successful."
|
2018-03-28 10:45:04 +02:00
|
|
|
else
|
2018-03-29 12:41:00 +02:00
|
|
|
echo "Unable to find an active ElasticSearch $target instance, something may have went wrong, exiting..."
|
2018-03-28 15:37:04 +02:00
|
|
|
echo "status: $STATUS, version: $version"
|
2018-03-28 10:45:04 +02:00
|
|
|
exit 4
|
|
|
|
fi
|
2018-03-27 18:15:01 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 12:41:00 +02:00
|
|
|
unsupported_message()
|
|
|
|
{
|
|
|
|
local version=$1
|
|
|
|
echo "Automated upgrade of your elasticSearch installation is not supported on your system."
|
|
|
|
echo "Please refer to your vendor's instructions to install ElasticSearch $version"
|
|
|
|
echo "For more informations: https://www.elastic.co/guide/en/elasticsearch/reference/$version/setup-upgrade.html"
|
|
|
|
exit 5
|
|
|
|
}
|
2018-03-27 18:15:01 +02:00
|
|
|
|
|
|
|
upgrade_classic()
|
|
|
|
{
|
2018-03-29 12:41:00 +02:00
|
|
|
local target=$1
|
2018-03-28 11:55:10 +02:00
|
|
|
local system=$(uname -s)
|
|
|
|
case "$system" in
|
|
|
|
Linux*)
|
|
|
|
if [ -f /etc/os-release ]
|
|
|
|
then
|
|
|
|
. /etc/os-release
|
2018-03-28 15:37:04 +02:00
|
|
|
if [ "$ID" = 'debian' ] || [[ "$ID_LIKE" = *'debian'* ]]
|
2018-03-28 11:55:10 +02:00
|
|
|
then
|
|
|
|
# Debian compatible
|
|
|
|
wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
|
2018-03-29 12:41:00 +02:00
|
|
|
case "$target" in
|
|
|
|
"2.4")
|
|
|
|
echo "deb http://packages.elastic.co/elasticsearch/2.x/debian stable main" | sudo tee /etc/apt/sources.list.d/elasticsearch-2.x.list
|
|
|
|
;;
|
|
|
|
esac
|
2018-03-29 10:06:03 +02:00
|
|
|
sudo apt-get update && sudo apt-get install --only-upgrade elasticsearch
|
2018-03-29 12:41:00 +02:00
|
|
|
else
|
|
|
|
unsupported_message
|
2018-03-28 11:55:10 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
Darwin*)
|
2018-03-29 12:41:00 +02:00
|
|
|
# Mac OS X
|
2018-03-28 11:55:10 +02:00
|
|
|
brew update
|
2018-03-29 12:41:00 +02:00
|
|
|
case "$target" in
|
|
|
|
"2.4")
|
|
|
|
brew install homebrew/versions/elasticsearch24
|
|
|
|
;;
|
|
|
|
esac
|
2018-03-28 11:55:10 +02:00
|
|
|
;;
|
|
|
|
*)
|
2018-03-29 12:41:00 +02:00
|
|
|
unsupported_message
|
2018-03-28 11:55:10 +02:00
|
|
|
;;
|
|
|
|
esac
|
2018-03-27 18:15:01 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 12:41:00 +02:00
|
|
|
reindex_indices()
|
|
|
|
{
|
|
|
|
local indices=$(curl "$ES_IP:9200/_cat/indices?v" 2>/dev/null | grep [[:digit:]] | awk '{print $3}')
|
|
|
|
for index in indices
|
|
|
|
do
|
|
|
|
local migration_index="$index""_$1"
|
|
|
|
curl -XPUT "http://$ES_IP:9200/$migration_index/" -d '{
|
|
|
|
"settings" : {
|
|
|
|
"index" : {
|
|
|
|
"number_of_shards" : 0,
|
|
|
|
"number_of_replicas" : 0,
|
|
|
|
"refresh_interval": -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
curl -XPOST "$ES_IP:9200/_reindex?pretty" -H 'Content-Type: application/json' -d "{
|
|
|
|
'source': {
|
|
|
|
'index': '$index'
|
|
|
|
},
|
|
|
|
'dest': {
|
|
|
|
'index': '$migration_index'
|
|
|
|
}
|
|
|
|
}"
|
|
|
|
done
|
|
|
|
echo "Reindex completed, deleting previous index..."
|
|
|
|
echo "Indices are reindexing, waiting to finish..."
|
|
|
|
local state=$(curl "$ES_IP:9200/_cat/indices?v" 2>/dev/null | grep [[:digit:]] | awk '{print $1}' | sort | uniq)
|
|
|
|
while [ "$state" = "green" ]
|
|
|
|
do
|
|
|
|
sleep 1
|
|
|
|
state=$(curl "$ES_IP:9200/_cat/indices?v" 2>/dev/null | grep [[:digit:]] | awk '{print $1}' | sort | uniq)
|
|
|
|
done
|
|
|
|
echo "Reindex completed, deleting previous index..."
|
|
|
|
for index in indices
|
|
|
|
do
|
|
|
|
curl -XDELETE "$ES_IP:9200/$index?pretty"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
reindex_final_indices()
|
|
|
|
{
|
|
|
|
local previous=$1
|
|
|
|
local indices=$(curl "$ES_IP:9200/_cat/indices?v" 2>/dev/null | grep [[:digit:]] | awk '{print $3}')
|
|
|
|
for index in indices
|
|
|
|
do
|
|
|
|
local final_index=$(echo "$index" | sed "s/\(.*\)_$previous$/\1/")
|
|
|
|
curl -XPUT "http://$ES_IP:9200/$final_index"
|
|
|
|
curl -XPOST "$ES_IP:9200/_reindex?pretty" -H 'Content-Type: application/json' -d "{
|
|
|
|
'source': {
|
|
|
|
'index': '$index'
|
|
|
|
},
|
|
|
|
'dest': {
|
|
|
|
'index': '$final_index'
|
|
|
|
}
|
|
|
|
}"
|
|
|
|
done
|
|
|
|
echo "Indices are reindexing, waiting to finish..."
|
|
|
|
local state=$(curl "$ES_IP:9200/_cat/indices?v" 2>/dev/null | grep [[:digit:]] | awk '{print $1}' | sort | uniq)
|
|
|
|
while [ "$state" != "green" ]
|
|
|
|
do
|
|
|
|
sleep 1
|
|
|
|
state=$(curl "$ES_IP:9200/_cat/indices?v" 2>/dev/null | grep [[:digit:]] | awk '{print $1}' | sort | uniq)
|
|
|
|
done
|
|
|
|
echo "Reindex completed, deleting previous index..."
|
|
|
|
for index in indices
|
|
|
|
do
|
|
|
|
curl -XDELETE "$ES_IP:9200/$index?pretty"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2018-03-27 18:15:01 +02:00
|
|
|
start_upgrade()
|
|
|
|
{
|
2018-03-29 12:41:00 +02:00
|
|
|
# $1: current version
|
|
|
|
# $2: target version
|
2018-03-27 18:15:01 +02:00
|
|
|
case "$TYPE" in
|
|
|
|
"DOCKER-COMPOSE")
|
2018-03-29 12:41:00 +02:00
|
|
|
upgrade_compose $1 $2
|
2018-03-27 18:15:01 +02:00
|
|
|
;;
|
|
|
|
"DOCKER")
|
2018-03-29 12:41:00 +02:00
|
|
|
upgrade_docker $1 $2
|
2018-03-27 18:15:01 +02:00
|
|
|
;;
|
|
|
|
"CLASSIC")
|
2018-03-29 12:41:00 +02:00
|
|
|
upgrade_classic $2
|
2018-03-27 18:15:01 +02:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unexpected ElasticSearch installation $TYPE"
|
|
|
|
exit 3
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
upgrade_elastic()
|
|
|
|
{
|
|
|
|
config
|
|
|
|
detect_installation
|
2018-03-29 12:41:00 +02:00
|
|
|
start_upgrade '1.7' '2.4'
|
|
|
|
reindex_indices '24'
|
|
|
|
start_upgrade '2.4' '5.6'
|
|
|
|
reindex_indices '56'
|
|
|
|
start_upgrade '5.6' '6.2'
|
|
|
|
reindex_final_indices '56'
|
2018-03-27 18:15:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
upgrade_elastic "$@"
|