Custom entrypoint.sh for microservice

Product/components used and version/fix level:

Using Cumulocity IoT backend and UI version: 1016.0.376

Detailed explanation of the problem:

I’m wondering if one can deploy a microservice with a custom entrypoint.sh script to Cumulocity IoT platform? For example, default version (generated by microservice-package-maven-plugin) looks something like this:

#!/bin/sh
if [ -n "$MEMORY_LIMIT" ];
 then
  value=$(numfmt  --from=auto  --grouping $MEMORY_LIMIT)
  value=$(($value/1048576)) # convert to MB
  echo "MEMORY_LIMIT: ${value}MB"
  memory_left=$(awk "BEGIN { memory = int($value * 0.1); if (memory <50) {memory = 50} print memory} ")
  echo "${memory_left}MB is left for system"
  value=$(awk "BEGIN { print(int($value - $memory_left))}") # leave memory space for system
  echo "${value}MB is left for application"
  if [ $value -lt "128" ]; # if less then 128MB fail
  then
    echo "Memory left for application is to small must be at lest 128MB"
    exit 1;
   else
    metaspace=$(awk "BEGIN { memory= int($value * 0.1); if (memory >1024) {memory = 1024} else if ( memory < 64 ){ memory = 64 } print memory} ") # take 10% of available memory to metaspace
    heap=$(($value - $metaspace))
  fi

  jvm_heap="@package.jvm-heap@"
  jvm_metaspace="@package.jvm-meta@"
  jvm_variable_heap="-Xmx${heap}m"

  echo "Using JDK8+ memory settings"
  jvm_variable_metaspace="-XX:MaxMetaspaceSize=${metaspace}m"

  export JAVA_MEM="${jvm_heap:-`echo $jvm_variable_heap`} ${jvm_metaspace:-`echo $jvm_variable_metaspace`}"
  echo "Java Memory Settings: $JAVA_MEM, memory limit: $MEMORY_LIMIT"
fi

jvm_gc=${JAVA_GC:-"@package.jvm-gc@"}
jvm_mem=${JAVA_MEM:-"@package.jvm-heap@ @package.jvm-meta@"}
jvm_opts=${JAVA_OPTS:-"-server -XX:HeapDumpPath=/var/log/@package.directory@/heap-dump-%p.hprof"}
arguments=${ARGUMENTS:-"@package.arguments@ --package.name=@package.name@ --package.directory=@package.directory@"}

proxy_params=""
if [ -n "$PROXY_HTTP_HOST" ]; then proxy_params="-Dhttp.proxyHost=${PROXY_HTTP_HOST} -DproxyHost=${PROXY_HTTP_HOST}"; fi
if [ -n "$PROXY_HTTP_PORT" ]; then proxy_params="${proxy_params} -Dhttp.proxyPort=${PROXY_HTTP_PORT} -DproxyPort=${PROXY_HTTP_PORT}"; fi
if [ -n "$PROXY_HTTP_NON_PROXY_HOSTS" ]; then proxy_params="${proxy_params} -Dhttp.nonProxyHosts=\"${PROXY_HTTP_NON_PROXY_HOSTS}\""; fi
if [ -n "$PROXY_HTTPS_HOST" ]; then proxy_params="${proxy_params} -Dhttps.proxyHost=${PROXY_HTTPS_HOST}"; fi
if [ -n "$PROXY_HTTPS_PORT" ]; then proxy_params="${proxy_params} -Dhttps.proxyPort=${PROXY_HTTPS_PORT}"; fi
if [ -n "$PROXY_SOCKS_HOST" ]; then proxy_params="${proxy_params} -DsocksProxyHost=${PROXY_SOCKS_HOST}"; fi
if [ -n "$PROXY_SOCKS_PORT" ]; then proxy_params="${proxy_params} -DsocksProxyPort=${PROXY_SOCKS_PORT}"; fi


mkdir -p /var/log/@package.name@; echo "heap dumps  /var/log/@package.name@/heap-dump-<pid>.hprof"

java ${jvm_opts} ${jvm_gc} ${jvm_mem} ${proxy_params} -jar /data/@package.name@.jar ${arguments}

If I would, for whatever reason, want to echo a custom message like echo "My custom little message" right above echo "Using JDK8+ memory settings" so it looks like:

#!/bin/sh
if [ -n "$MEMORY_LIMIT" ];
 then
  value=$(numfmt  --from=auto  --grouping $MEMORY_LIMIT)
  value=$(($value/1048576)) # convert to MB
  echo "MEMORY_LIMIT: ${value}MB"
  memory_left=$(awk "BEGIN { memory = int($value * 0.1); if (memory <50) {memory = 50} print memory} ")
  echo "${memory_left}MB is left for system"
  value=$(awk "BEGIN { print(int($value - $memory_left))}") # leave memory space for system
  echo "${value}MB is left for application"
  if [ $value -lt "128" ]; # if less then 128MB fail
  then
    echo "Memory left for application is to small must be at lest 128MB"
    exit 1;
   else
    metaspace=$(awk "BEGIN { memory= int($value * 0.1); if (memory >1024) {memory = 1024} else if ( memory < 64 ){ memory = 64 } print memory} ") # take 10% of available memory to metaspace
    heap=$(($value - $metaspace))
  fi

  jvm_heap="@package.jvm-heap@"
  jvm_metaspace="@package.jvm-meta@"
  jvm_variable_heap="-Xmx${heap}m"

  echo "My custom little message"
  echo "Using JDK8+ memory settings"
  jvm_variable_metaspace="-XX:MaxMetaspaceSize=${metaspace}m"

  export JAVA_MEM="${jvm_heap:-`echo $jvm_variable_heap`} ${jvm_metaspace:-`echo $jvm_variable_metaspace`}"
  echo "Java Memory Settings: $JAVA_MEM, memory limit: $MEMORY_LIMIT"
fi

jvm_gc=${JAVA_GC:-"@package.jvm-gc@"}
jvm_mem=${JAVA_MEM:-"@package.jvm-heap@ @package.jvm-meta@"}
jvm_opts=${JAVA_OPTS:-"-server -XX:HeapDumpPath=/var/log/@package.directory@/heap-dump-%p.hprof"}
arguments=${ARGUMENTS:-"@package.arguments@ --package.name=@package.name@ --package.directory=@package.directory@"}

proxy_params=""
if [ -n "$PROXY_HTTP_HOST" ]; then proxy_params="-Dhttp.proxyHost=${PROXY_HTTP_HOST} -DproxyHost=${PROXY_HTTP_HOST}"; fi
if [ -n "$PROXY_HTTP_PORT" ]; then proxy_params="${proxy_params} -Dhttp.proxyPort=${PROXY_HTTP_PORT} -DproxyPort=${PROXY_HTTP_PORT}"; fi
if [ -n "$PROXY_HTTP_NON_PROXY_HOSTS" ]; then proxy_params="${proxy_params} -Dhttp.nonProxyHosts=\"${PROXY_HTTP_NON_PROXY_HOSTS}\""; fi
if [ -n "$PROXY_HTTPS_HOST" ]; then proxy_params="${proxy_params} -Dhttps.proxyHost=${PROXY_HTTPS_HOST}"; fi
if [ -n "$PROXY_HTTPS_PORT" ]; then proxy_params="${proxy_params} -Dhttps.proxyPort=${PROXY_HTTPS_PORT}"; fi
if [ -n "$PROXY_SOCKS_HOST" ]; then proxy_params="${proxy_params} -DsocksProxyHost=${PROXY_SOCKS_HOST}"; fi
if [ -n "$PROXY_SOCKS_PORT" ]; then proxy_params="${proxy_params} -DsocksProxyPort=${PROXY_SOCKS_PORT}"; fi


mkdir -p /var/log/@package.name@; echo "heap dumps  /var/log/@package.name@/heap-dump-<pid>.hprof"

java ${jvm_opts} ${jvm_gc} ${jvm_mem} ${proxy_params} -jar /data/@package.name@.jar ${arguments}

how would I proceed in doing that? I’ve tried changing entrypoint.sh inside of the generated .zip file before uploading microservice. However, with or without those changes, this is the output after subscribing to microservice:

MEMORY_LIMIT: 488MB
50MB is left for system
438MB is left for application
Using JDK8+ memory settings
Java Memory Settings: -Xmx374m -XX:MaxMetaspaceSize=64m, memory limit: 512M
...

As you can see, my custom message does not get echoed/printed. Is there some obvious mistake I’m making? Maybe it’s not even possible? Or is it up to Tenant settings, where admin can override to always use default entrypoint script?

Thank you for your time

@zkristic In your microservice project structure, you should place the generated file entrypoint.sh in the highlighted folder structure. Add your custom message to the entrypoint.sh file, which will override the generated one. This way, you will see your message when deploying the generated zip file into your tenant.

2 Likes

Thank you for the suggestion. Looks like you are right, adding custom entrypoint.sh to the specified folder does result in generating .zip file with my own message. However, after uploading to tenant, the result is still the same. It’s not echoed, as if it ignores whatever entrypoint.sh is in provided .zip and uses a default one regardless. Do you know what could be the cause of it?

@Vachaspati_Diwevedi2 suggestion should work. We are also using it e.g. in the microservice templates. I suspect you might not have increased the version number of the microservice. You need to increase it or use a snapshot version. Otherwise an older image with the same version number may be used:

2 Likes

That was it… After adding -SNAPSHOT to the version, my message gets logged. Thank you both @Vachaspati_Diwevedi2 and @Harald_Meyer

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.