2022-11-30 15:28:39 +01:00
# This script installs and runs the project.
# For error-handling, we cannot use the "set -e" since: it has problems https://mywiki.wooledge.org/BashFAQ/105
# So we have our own function, for use when a single command fails.
handle_error ( ) {
2023-04-11 10:59:10 +02:00
echo -e " \n\n $1 \n\n " ; exit $2
2022-11-30 15:28:39 +01:00
}
# Change the working directory to the script's directory, when running from another location.
cd " ${ 0 %/* } " || handle_error "Could not change-dir to this script's dir!" 1
2021-09-09 14:56:37 +02:00
justInstall = 0
2022-02-04 14:49:56 +01:00
shouldRunInDocker = 0
2021-09-09 14:56:37 +02:00
if [ [ $# -eq 1 ] ] ; then
justInstall = $1
2022-02-04 14:49:56 +01:00
elif [ [ $# -eq 2 ] ] ; then
justInstall = $1
shouldRunInDocker = $2
elif [ [ $# -gt 2 ] ] ; then
2022-11-30 15:28:39 +01:00
echo -e " Wrong number of arguments given: ${# } \nPlease execute it like: script.sh <justInstall: 0 | 1> <shouldRunInDocker: 0 | 1> " ; exit 2
2022-02-04 14:49:56 +01:00
fi
if [ [ justInstall -eq 1 && shouldRunInDocker -eq 1 ] ] ; then
echo -e "Cannot run in docker without re-building the project (just to be safe). Setting \"justInstall\" to < 0 >"
justInstall = 0
2021-09-09 14:56:37 +02:00
fi
2023-04-22 15:50:33 +02:00
gradleVersion = "8.1.1"
2021-09-09 14:56:37 +02:00
if [ [ justInstall -eq 0 ] ] ; then
2021-10-14 01:46:33 +02:00
if [ [ ! -d /opt/gradle/gradle-${ gradleVersion } ] ] ; then
wget https://services.gradle.org/distributions/gradle-${ gradleVersion } -bin.zip
echo -e "\nAsking for sudo, in order to install 'gradle'..\n"
sudo mkdir /opt/gradle
sudo apt install -y unzip && sudo unzip -d /opt/gradle gradle-${ gradleVersion } -bin.zip
#ls /opt/gradle/gradle-${gradleVersion} # For debugging installation
fi
2021-09-09 14:56:37 +02:00
2022-01-21 14:45:12 +01:00
export PATH = /opt/gradle/gradle-${ gradleVersion } /bin:$PATH
2021-09-09 14:56:37 +02:00
gradle wrapper --gradle-version= ${ gradleVersion } --distribution-type= bin
#gradle tasks # For debugging installation
#gradle -v # For debugging installation
2022-02-04 14:49:56 +01:00
gradle clean build
if [ [ shouldRunInDocker -eq 1 ] ] ; then
2023-04-22 15:50:33 +02:00
2023-05-15 17:52:31 +02:00
runPrometheusAndGrafanaContainers = 0 # This is set here, not through cmd-args.
2023-04-22 15:50:33 +02:00
2023-03-21 15:46:33 +01:00
echo -e "\nBuilding the docker image and running the containers..\n"
2022-11-30 15:28:39 +01:00
sudo docker --version || handle_error "Docker was not found!" 3
2023-03-21 15:46:33 +01:00
( sudo mkdir -p " $HOME " /tmp/config && sudo cp ./src/main/resources/application.yml " $HOME " /tmp/config) || true # This also replaces an existing "application.yml".
sudo mkdir -p " $HOME " /logs || true
2023-04-22 15:50:33 +02:00
2023-05-15 17:52:31 +02:00
# Run in "detached mode" -d (in the background).
( sudo docker compose up --build -d && echo -e "\nThe Urls_Controller docker-container started running.\n" ) || handle_error "Could not build and/or run the 'urls_controller' docker container!" 4
2023-04-22 15:50:33 +02:00
if [ [ runPrometheusAndGrafanaContainers -eq 1 ] ] ; then
2023-05-15 17:52:31 +02:00
( sudo docker compose -f prometheus/docker-compose-prometheus.yml up --build -d && echo -e "\nThe Prometheus and Grafana docker-containers started running.\n" ) || handle_error "Could not build and/or run the 'prometheus' and/or 'grafana' docker containers!" 5
2023-04-22 15:50:33 +02:00
fi
2023-05-15 17:52:31 +02:00
echo -e "Waiting 65 seconds before getting the status..\n"
2023-04-10 21:28:53 +02:00
sleep 65
2023-05-15 17:52:31 +02:00
sudo docker ps -a || handle_error "Could not get the status of docker-containers!" 6 # Using -a to get the status of failed containers as well.
2023-04-11 10:59:10 +02:00
echo -e "\n\nGetting the logs of docker-container \"urlscontroller-urls_controller-1\":\n"
2023-05-15 17:52:31 +02:00
sudo docker logs " $( sudo docker ps -aqf " name=^urlscontroller-urls_controller-1 $" ) " || handle_error "Could not get the logs of docker-container \"urlscontroller-urls_controller-1\"!" 7 # Using "regex anchors" to avoid false-positives. Works even if the container is not running, thus showing the error-log.
2022-02-04 14:49:56 +01:00
fi
2021-09-09 14:56:37 +02:00
else
2022-01-21 14:45:12 +01:00
export PATH = /opt/gradle/gradle-${ gradleVersion } /bin:$PATH # Make sure the gradle is still accessible (it usually isn't without the "export").
2021-09-09 14:56:37 +02:00
fi
2022-02-04 14:49:56 +01:00
if [ [ shouldRunInDocker -ne 1 ] ] ; then
gradle bootRun
fi