Files
spark/tools/bounce_data_stream_app.sh

184 lines
4.4 KiB
Bash

#!/usr/bin/env bash
print_help_and_exit() {
cat <<EOF
Usage: bounce_data_stream_app.sh [OPTIONS] <app> <container_prefix> <release>
Positionals:
app The application to bounce [PubSubServer | DataStreamPublisher | Bts.DataStreamWebApp.Server]
container_prefix The container prefix to use. Existing dockers with this prefix will be terminated.
release The release id. This will be appended to the container prefix to form the new container name.
Options:
--app The application to bounce [PubSubServer | DataStreamPublisher | Bts.DataStreamWebApp.Server]
--container-prefix The container prefix to use. Existing dockers with this prefix will be terminated.
--release The release id. This will be appended to the container prefix to form the new container name.
Examples:
Deploy PubSubServer : deploy_data_stream_app.sh PubSubServer pubsub 00d0d6ade
Deploy DataStreamPublisher : deploy_data_stream_app.sh DataStreamPublisher ds-publisher 00d0d6ade
EOF
exit "$1"
}
stop_containers() {
if [ $# -ne 1 ]; then
echo "stop_containers expects exactly 1 arg (the container prefix). Received $#: '$@'"
exit 1
fi
declare -a containers="$(docker ps -q --filter "name=^$1-[a-z0-9]{9}\$")"
for c in ${containers[@]}; do
echo "Removing old container $c"
docker stop "$c"
status=$?
if [ $status -ne 0 ]; then
echo "Failed to remove container $c"
exit 1
fi
done
}
remove_containers() {
if [ $# -ne 1 ]; then
echo "remove_containers expects exactly 1 arg (the container prefix). Received $#: '$@'"
exit 1
fi
declare -a containers="$(docker container ls --all -q --filter "name=^$1-[a-z0-9]{9}\$")"
for c in ${containers[@]}; do
echo "Removing old container $c"
docker rm "$c"
status=$?
if [ $status -ne 0 ]; then
echo "Failed to remove container $c"
exit 1
fi
done
}
kill_containers() {
stop_containers $1
remove_containers $1
}
app_name=$1
container_prefix=$2
release=$3
while [[ $# -gt 0 ]]; do
case "$1" in
--app)
if [[ -z "$2" ]]; then
echo "App (--app) must be non-empty"
print_help_and_exit 1
fi
app_name="$2"
shift 2
;;
--container-prefix)
if [[ -z "$2" ]]; then
echo "Container prefix (--container-prefix) must be non-empty"
print_help_and_exit 1
fi
container_prefix="$2"
shift 2
;;
--release)
if [[ -z "$2" ]]; then
echo "Release (--release) must be non-empty"
print_help_and_exit 1
fi
release="$2"
shift 2
;;
-h)
;&
--help)
print_help_and_exit 0
;;
-*)
echo "Unexpected option '$1'"
print_help_and_exit 1
;;
*)
if [[ -z "$1" ]]; then
echo "Invalid position argument '$1'"
print_help_and_exit 1
elif [[ -z "$app_name" ]]; then
app_name="$1"
elif [[ -z "$container_prefix" ]]; then
container_prefix="$1"
elif [[ -z "$release" ]]; then
release="$1"
fi
shift 1
;;
esac
done
# Validate args
if [ "$release" == "" ]; then
echo "Release not defined"
print_help_and_exit 1
fi
if [ "$app_name" == "" ]; then
echo "App name not defined"
print_help_and_exit 1
fi
if [ "$container_prefix" == "" ]; then
echo "Container prefix not defined"
print_help_and_exit 1
fi
container="$container_prefix-$release"
image="$container_prefix-$release"
cat <<EOF
Bouncing with the following parameters:
Application : $app_name
Container Prefix : $container_prefix
Container : $container
Image : $image
Release : $release
EOF
cd $HOME/DataStream/releases
if [ ! -e $release ]; then
if [ -e $release.tar.gz ]; then
tar -xf $release.tar.gz
status=$?
if [ $status -ne 0 ]; then
echo "Failed to extract $release.tar.gz"
exit 1
fi
fi
if [ ! -e $release ]; then
echo "Did not find release '$release'"
exit 1
fi
fi
cd $release
echo "Building new image"
docker build -t "$image" .
status=$?
if [ $status -ne 0 ]; then
echo "Build image failed. Full command below:"
echo "docker build -t $image ."
exit 1
fi
cd ..
echo "Killing existing $app_name containers (matching prefix '$container_prefix')"
kill_containers "$container_prefix"
echo "Starting container $container"
docker run \
--name "$container" \
--net host \
-w "/home/bts/$app_name/bin" \
--shm-size=4024m \
-d \
"$image"