Files
spark/tools/deploy_data_stream_app.sh
T

171 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
print_help_and_exit() {
cat <<EOF
Usage: deploy_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.
EOF
exit "$1"
}
app_name=""
container_prefix=""
release=""
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 [ "$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
if [ "$release" == "" ]; then
# Use commit sha
release="$(git rev-parse --short HEAD)"
fi
if [ "$release" == "" ]; then
echo "Release not defined"
print_help_and_exit 1
fi
this_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
cd $this_dir/..
cfg_profile="AwsProd"
cfg_profiles_dir="src/DataStreamSuite/$app_name/Configurations"
cfg_profile_json="$cfg_profiles_dir/$cfg_profile.json"
if [ ! -e "$cfg_profile_json" ]; then
echo "Profile '${cfg_profile}' not found. Looked for file '${cfg_profile_json}'"
fi
release_dir=".tmp/$release"
mkdir -p "$release_dir"
release_bin="$release_dir/bin"
publish_sh="$this_dir/publish_data_stream.sh"
# Publish self-contained app(s)
echo "Publishing $release"
"$publish_sh" -o "$release_bin"
status=$?
if [ $status -ne 0 ]; then
echo "Publish exited with code $status. Full command below:"
echo "Command: $publish_sh -o $release_bin"
exit 1
fi
# Copy configuration file to release_bin
echo "Copying configuration to appsettings.json"
cp "$cfg_profile_json" "$release_bin/appsettings.json"
status=$?
if [ $status -ne 0 ]; then
echo "Copy configuration exited with code $status. Full command below:"
echo "cp $cfg_profile_json $release_bin/appsettings.json"
exit 1
fi
# Copy Dockerfile to release_dir
cp "docker/ubuntu-22.04/dotnet-7/Dockerfile" "$release_dir/"
status=$?
if [ $status -ne 0 ]; then
echo "Copy Dockerfile exited with code $status. Full command below:"
echo "cp docker/ubuntu-22.04/dotnet-7/Dockerfile $release_dir/"
exit 1
fi
cat <<EOF >> "$release_dir/Dockerfile"
COPY "bin" "$app_name/bin"
WORKDIR /home/bts/$app_name/bin
ENTRYPOINT [ "./$app_name" ]
EOF
# Compress
echo "Compressing $release"
cd .tmp
tar -czf "$release.tar.gz" "$release"
status=$?
if [ $status -ne 0 ]; then
echo "Compress exited with code $status. Full command below:"
echo "tar -czf $release.tar.gz $release"
exit 1
fi
# Clean
echo "Removing $release (uncompressed dir)"
rm -r "$release"
# Upload
remote=bts-cloud-streams
echo "Uploading $release.tar.gz to $remote"
scp "$release.tar.gz" $remote:~/DataStream/releases/
cat <<EOF | ssh $remote /usr/bin/env bash
rm -r /home/ubuntu/DataStream/releases/$release
/home/ubuntu/DataStream/scripts/bounce_data_stream_app.sh $app_name $container_prefix $release
EOF