buildImageAndStart.sh: \n * optional port for debug as d argument now works,* added -s for skip compile

This commit is contained in:
Alfredo Oliviero 2024-03-13 12:22:21 +01:00
parent 711af67199
commit fd2739690d
1 changed files with 62 additions and 42 deletions

View File

@ -1,24 +1,30 @@
#!/bin/bash #!/bin/bash
accepted_java_versions=(11 17) accepted_java_versions=(11 17)
NAME=smartgears-helloworld
PORT=8081
DEBUG_PORT=5005
DEBUG=false
COMPILE=true
java_version=11
################################################################################ ################################################################################
# Help # # Help #
################################################################################ ################################################################################
Help() Help() {
{
# Display Help # Display Help
echo "build, create and run in docker the helloworld service" echo "build, create and run in docker the helloworld service"
echo echo
echo "Syntax: buildDistribution [-n arg] [-p arg] [-j arg] [-d arg?] [-h]" echo "Syntax: buildDistribution [-n arg] [-p arg] [-j arg] [-d arg?] [-h]"
echo "options:" echo "options:"
echo "-s skip maven package"
echo "-n arg specifies the docker image name (default is smartgears-helloworld)." echo "-n arg specifies the docker image name (default is smartgears-helloworld)."
echo "-p arg specifies the port to be exposed for the docker container to access the service (default 8081)" echo "-p arg specifies the port to be exposed for the docker container to access the service (default $PORT)"
echo "-j arg specify java version (default is 11)" echo "-j arg specify java version (default is $java_version)"
echo " accepted version are: ${accepted_java_versions[@]}" echo " accepted version are: ${accepted_java_versions[@]}"
echo "-d arg? enable java debug mode" echo "-d arg? enable java debug mode"
echo " arg is the debug port (default is 5005)" echo " arg is the debug port (default is $DEBUG_PORT)"
echo "-h Print this Help." echo "-h Print this Help."
echo echo
} }
@ -31,38 +37,52 @@ Help()
set -e set -e
OPTSTRING=":sn:p:d:j:?h"
NAME=smartgears-helloworld while getopts $OPTSTRING opt; do
PORT=8081 # echo "Option -${opt} was triggered, Argument: ${OPTARG}"
DEBUG_PORT=5005 case "${opt}" in
debug=false s) COMPILE=false && echo "compile $COMPILE" ;;
compile=false
java_version=11
while getopts n:p:j:d?h flag
do
echo ${flag};
case "${flag}" in
n) NAME=${OPTARG} ;; n) NAME=${OPTARG} ;;
p) PORT=${OPTARG} ;; p) PORT=${OPTARG} ;;
d) debug=true && DEBUG_PORT=${OPTARG:-5005};; d) DEBUG=true
j) if [[ ${accepted_java_versions[@]} =~ ${OPTARG} ]] DEBUG_PORT=${OPTARG}
then java_version=${OPTARG}; echo "debug enabled, port $DEBUG_PORT"
else echo "Invalid java version" && echo "accepted version are: ${accepted_java_versions[@]}" && exit 1; ;;
fi;; j) if [[ ${accepted_java_versions[@]} =~ ${OPTARG} ]]; then
java_version=${OPTARG}
else
echo "Invalid java version" && echo "accepted version are: ${accepted_java_versions[@]}" && exit 1
fi
;;
h) Help && exit 0 ;; h) Help && exit 0 ;;
*) echo "Invalid option" && exit 1 ;; :) # matched when an option that is expected to have an argument is passed without one
if [ ${OPTARG} = "d" ]; then
DEBUG=true
echo "debug enabled, port $DEBUG_PORT"
else
# matched when an option that is expected to have an argument is passed without one
echo "Option -${OPTARG} requires an argument."
exit 1
fi
;;
?) # match any invalid option that is passed
echo "Invalid option: -${OPTARG}."
exit 1
;;
esac esac
done done
if [ $COMPILE = true ]; then
mvn clean package mvn clean package
else
echo "skipping mvn package"
fi
docker build -t $NAME --build-arg JAVA_VERSION=${java_version} . docker build -t $NAME --build-arg JAVA_VERSION=${java_version} .
if [ $debug = false ] ; then if [ $DEBUG = false ]; then
docker run -p $PORT:8080 $NAME docker run -p $PORT:8080 $NAME
else else
docker run -p $PORT:8080 -p $DEBUG_PORT:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=y" $NAME docker run -p $PORT:8080 -p $DEBUG_PORT:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=y" $NAME
fi fi