dockerizing

This commit is contained in:
Alfredo Oliviero 2024-05-16 18:00:32 +02:00
parent b4a6cb5539
commit 24a65c548e
13 changed files with 317 additions and 73 deletions

View File

@ -3,7 +3,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
# Changelog for Hello World Service
## [v1.0.0-SNAPSHOT]
## [v0.0.1-SNAPSHOT]
- First Version

View File

@ -1,13 +1,15 @@
#FROM d4science/smartgears-distribution:4.0.0-SNAPSHOT-java11-tomcat9
FROM smartgears-distribution:4.0.0-java11-tomcat9
ARG JAVA_VERSION=11
ARG SMARTGEARS_VERSION=4.0.1-SNAPSHOT
ARG REPOUSER=admin
ARG REPOPWD=admin
#FROM d4science/smartgears-distribution:${SMARTGEARS_VERSION}-java${JAVA_VERSION}-tomcat10.1.19
#FROM hub.dev.d4science.org/gcube/smartgears-distribution:${SMARTGEARS_VERSION}-java${JAVA_VERSION}-tomcat10.1.19
FROM smartgears-distribution:${SMARTGEARS_VERSION}-java${JAVA_VERSION}-tomcat10.1.19
ARG CONTAINER_INI="./docker/container.ini"
COPY ./target/social-service.war /usr/local/tomcat/webapps/
COPY ./docker/logback.xml /etc/
COPY ./docker/*.gcubekey /tomcat/lib
COPY ./target/social-service.war /tomcat/webapps/
COPY ${CONTAINER_INI} /etc/container.ini
EXPOSE 8080
COPY ./docker/logback.xml /etc/
COPY ./docker/container.ini /etc/
RUN unzip /usr/local/tomcat/webapps/social-service.war -d /usr/local/tomcat/webapps/social-service
RUN rm /usr/local/tomcat/webapps/social-service.war
EXPOSE 8080

178
buildImageAndStart.sh Executable file
View File

@ -0,0 +1,178 @@
#!/bin/bash
# set -x # uncomment to debug script
BUILD_VERSION=0.0.1-SNAPSHOT
SMARTGEARS_VERSION=4.0.1-SNAPSHOT
ACCEPTED_JAVA_VERSIONs=(11 17)
PORT=8080
DEBUG_PORT=5005
DEBUG=false
EXECUTE=false
TEST=false
COMPILE=true
JAVA_VERSION=11
NAME=social-service
CONTAINER_INI="./docker/container.ini"
IMAGE_VERSION=${BUILD_VERSION}-java${JAVA_VERSION}-smartgears${SMARTGEARS_VERSION}
PUSH_DOCKER=false
PUSH_HARBOR=false
LOGIN_HARBOR=false
BUILD_NAME=$NAME:$IMAGE_VERSION
SMARTGEAR_IMAGE=hub.dev.d4science.org/gcube/smartgears-distribution:${SMARTGEARS_VERSION}-java${JAVA_VERSION}-tomcat10.1.19
DOCKER_IMAGE_NAME=d4science/$BUILD_NAME
HARBOR_IMAGE_NAME=hub.dev.d4science.org/gcube/$BUILD_NAME
echo "BUILD_NAME=$BUILD_NAME"
################################################################################
# Help #
################################################################################
Help() {
# Display Help
echo "build, create and run in docker the identity manager service"
echo
echo "Syntax: buildDistribution [-n arg] [-p arg] [-j arg] [-d arg?] [-h]"
echo "options:"
echo "-s skip maven package"
echo "-t exec also maven tests"
echo "-n arg specifies the docker image name (default is identity-manager)."
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 $JAVA_VERSION)"
echo " accepted version are: ${ACCEPTED_JAVA_VERSIONs[@]}"
echo "-e execute the image"
echo "-d arg? enable java debug mode for execution"
echo " arg is the debug port (default is $DEBUG_PORT)"
echo "-r push image to d4science harbo[r] (with login already done, or -l to login)"
echo "-l [l]ogin to d4science harbor"
echo "-u p[u]sh image to dockerhub (with docker login already done)"
echo "-c arg path of the file to deploy as container.ini (default ./docker/container.ini)"
echo "-h Print this Help."
echo
echo "to compile and push to harbor registry with a custom container.ini file: "
echo " ./buildImageAndStart.sh -r -m -l -c \"./docker/container-XXX.ini\" "
echo
echo "to debug locally: "
echo " ./buildImageAndStart.sh -d "
}
################################################################################
################################################################################
# Main program #
################################################################################
################################################################################
set -e
#OPTSTRING=":sn:p:d:j:?h"
OPTSTRING=":c:n:p:d:?jsmulrteh"
while getopts $OPTSTRING opt; do
# echo "Option -${opt} was triggered, Argument: ${OPTARG}"
case "${opt}" in
s) COMPILE=false && echo "compile $COMPILE" ;;
c)
CONTAINER_INI=${OPTARG}
echo "CONTAINER_INI: $CONTAINER_INI"
;;
m) MULTI_PLATFORM=true ;;
n) NAME=${OPTARG} ;;
p) PORT=${OPTARG} ;;
u) PUSH_DOCKER=true ;;
l) LOGIN_HARBOR=true ;;
r) PUSH_HARBOR=true ;;
t) TEST=true ;;
e) EXECUTE=true ;;
d)
DEBUG=true
DEBUG_PORT=${OPTARG}
echo "debug enabled, port $DEBUG_PORT, execute $EXECUTE"
;;
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 ;;
:)
# matched when an option that is expected to have an argument is passed without one
if [ ${OPTARG} = "d" ]; then
DEBUG=true
EXECUTE=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
done
if [ $COMPILE = true ]; then
if [ $TEST = false ]; then
mvn clean package -Dmaven.test.skip
else
mvn clean package
fi
else
echo "skipping mvn package"
fi
docker pull $SMARTGEAR_IMAGE
if [ $MULTI_PLATFORM ]; then
echo "build multiplatform"
docker build -t $BUILD_NAME --build-arg="CONTAINER_INI=$CONTAINER_INI" --build-arg="JAVA_VERSION=${JAVA_VERSION}" --build-arg="SMARTGEARS_VERSION=${SMARTGEARS_VERSION}" --platform=linux/amd64,linux/arm64,linux/arm/v7 .
else
echo "build single platform"
docker build -t $BUILD_NAME --build-arg="CONTAINER_INI=$CONTAINER_INI" --build-arg="JAVA_VERSION=${JAVA_VERSION}" --build-arg="SMARTGEARS_VERSION=${SMARTGEARS_VERSION}" .
# docker manifest create hub.dev.d4science.org/gcube/$BUILD_NAME \
# hub.dev.d4science.org/gcube/$NAME-amd64-linux:$IMAGE_VERSION \
# hub.dev.d4science.org/gcube/$NAME-arm-linux:$IMAGE_VERSION \
# hub.dev.d4science.org/gcube/$NAME-arm-linux:$IMAGE_VERSION
fi
if [ ${PUSH_DOCKER} = true ]; then
docker tag $BUILD_NAME $DOCKER_IMAGE_NAME./
docker push $DOCKER_IMAGE_NAME
echo ">>> pushed on dockerhub the image $DOCKER_IMAGE_NAME"
fi
if [ ${LOGIN_HARBOR} = true ]; then
./loginHarborHub.sh
fi
if [ $PUSH_HARBOR = true ]; then
echo ">>> PUSHING on hub.dev.d4science.org the image $HARBOR_IMAGE_NAME"
docker tag $BUILD_NAME $HARBOR_IMAGE_NAME
echo ">>> docker push $HARBOR_IMAGE_NAME"
docker push $HARBOR_IMAGE_NAME
echo ">>> pushed on hub.dev.d4science.org the image $HARBOR_IMAGE_NAME"
fi
if [ ${EXECUTE} = true ]; then
if [ $DEBUG = true ]; then
docker run -p $PORT:8080 -p $DEBUG_PORT:5005 -e JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,address=*:5005,server=y,suspend=y" $BUILD_NAME
else
docker run -p $PORT:8080 $BUILD_NAME
fi
fi

3
docker/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
container*.ini
!container.default.ini
*.gcubekey

View File

@ -0,0 +1,25 @@
[node]
mode = offline
hostname = localhost
protocol= http
port = 8080
infrastructure = /gcube
authorizeChildrenContext = true
publicationFrequencyInSeconds = 60
[properties]
SmartGearsDistribution = 4.0.1-SNAPSHOT
SmartGearsDistributionBundle = UnBundled
[site]
country = it
location = pisa
[authorization]
factory = org.gcube.smartgears.security.defaults.DefaultAuthorizationProviderFactory
factory.endpoint = https://accounts.dev.d4science.org/auth/realms/d4science/protocol/openid-connect/token
credentials.class = org.gcube.smartgears.security.SimpleCredentials
credentials.clientID =
credentials.secret =

View File

@ -1,28 +0,0 @@
[node]
mode = offline
hostname = socialservice
protocol= http
port = 8080
infrastructure = gcube
authorizeChildrenContext = true
publicationFrequencyInSeconds = 60
[properties]
SmartGearsDistribution = 4.0.0-SNAPSHOT
SmartGearsDistributionBundle = UnBundled
[site]
country = it
location = pisa
[authorization]
factory = org.gcube.smartgears.security.defaults.DefaultAuthorizationProviderFactory
factory.endpoint = https://accounts.dev.d4science.org/auth/realms/d4science/protocol/openid-connect/token
credentials.class = org.gcube.smartgears.security.SimpleCredentials
//for smartgears
credentials.clientID = social-service-hosting-node-client
credentials.secret = 979bd3bc-5cc4-11ec-bf63-0242ac130002
//for interacting with alfredo's service
socialservice
yqMpmqwlcu9o2mZTWMyDq0om8QMcjUEb

1
docker/container.ini Symbolic link
View File

@ -0,0 +1 @@
container.ahmed.ini

28
docker/logback copy.xml Normal file
View File

@ -0,0 +1,28 @@
<configuration scan="true" debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>Ï <pattern>%-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.gcube.service.idm" level="DEBUG" />
<logger name="org.gcube.service.rest" level="DEBUG" />
<logger name="org.gcube.smartgears" level="DEBUG" />
<!--
<logger name="org.gcube" level="DEBUG" />
<logger name="org.gcube.smartgears" level="TRACE" />
<logger name="org.gcube.smartgears.handlers" level="TRACE" />
<logger name="org.gcube.common.events" level="WARN" />
<logger name="org.gcube.data.publishing" level="ERROR" />
<logger name="org.gcube.documentstore" level="ERROR" />
<logger name="org.gcube.common.core.publisher.is.legacy" level="TRACE" />
<logger name="org.gcube.data.access" level="TRACE" />
<logger name="org.gcube.data.access.storagehub.handlers" level="DEBUG" />
-->
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

28
docker/logback.ori.xml Normal file
View File

@ -0,0 +1,28 @@
<configuration scan="true" debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>Ï <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.gcube.service.idm" level="DEBUG" />
<logger name="org.gcube.service.rest" level="DEBUG" />
<logger name="org.gcube.smartgears" level="DEBUG" />
<!--
<logger name="org.gcube" level="DEBUG" />
<logger name="org.gcube.smartgears" level="TRACE" />
<logger name="org.gcube.smartgears.handlers" level="TRACE" />
<logger name="org.gcube.common.events" level="WARN" />
<logger name="org.gcube.data.publishing" level="ERROR" />
<logger name="org.gcube.documentstore" level="ERROR" />
<logger name="org.gcube.common.core.publisher.is.legacy" level="TRACE" />
<logger name="org.gcube.data.access" level="TRACE" />
<logger name="org.gcube.data.access.storagehub.handlers" level="DEBUG" />
-->
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -4,4 +4,5 @@ version: ${version}
description: ${description}
excludes:
- path: /docs.*
- path: /api-docs.*
- path: /api-docs.*
- path: /guest

View File

@ -1,5 +0,0 @@
mvn clean package
docker build -t idm .
docker compose up -d --build --force-recreate
#docker run -it -d -p 9090:8080 --name idm idm

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<application mode='online'>
<name>${project.artifactId}</name>
<group>${project.groupId}</group>
<version>${project.version}</version>
<description>${project.description}</description>
<exclude>/api-docs.*</exclude>
<exclude>/docs.*</exclude>
</application>

View File

@ -1,20 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<context-param>
<param-name>admin-username</param-name>
<param-value>{{adminId}}</param-value>
</context-param>
<context-param>
<param-name>admin-pwd</param-name>
<param-value>{{adminPwd}}</param-value>
</context-param>
<servlet>
<servlet-name>org.gcube.social_networking.SocialService</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>org.gcube.social_networking.SocialService</servlet-name>
<url-pattern>/social-service/*</url-pattern>
</servlet-mapping>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Social Service</display-name>
<description>
Gcube Social Service
</description>
<servlet>
<servlet-name>social-service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.gcube.social_networking.rest</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
org.gcube.social_networking.mappers
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>social-service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

13
loginHarborHub.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
REGISTRY_URL="hub.dev.d4science.org"
#USERNAME="alfredo.oliviero"
echo "to obtain Harbor username and CLI secret:"
echo "https://hub.dev.d4science.org/ -> user profile -> CLI secret"
read -p "username:" USERNAME
echo ""
read -s -p "CLI secret:" ACCESS_TOKEN
echo "$ACCESS_TOKEN" | docker login $REGISTRY_URL -u $USERNAME --password-stdin
unset ACCESS_TOKEN