diff --git a/jboss/container/wildfly/launch-config/config/added/launch/launch.sh b/jboss/container/wildfly/launch-config/config/added/launch/launch.sh index 2cabcf7a..fb3f1371 100644 --- a/jboss/container/wildfly/launch-config/config/added/launch/launch.sh +++ b/jboss/container/wildfly/launch-config/config/added/launch/launch.sh @@ -126,7 +126,7 @@ function launchServer() { local imgVersion=${JBOSS_IMAGE_VERSION:-$IMAGE_VERSION} log_info "Running $imgName image, version $imgVersion" - ${cmd} ${JAVA_PROXY_OPTIONS} ${JBOSS_HA_ARGS} ${JBOSS_MESSAGING_ARGS} ${CLI_EXECUTION_OPTS} & + ${cmd} -bmanagement ${SERVER_BIND_ALL_ADDR} ${JAVA_PROXY_OPTIONS} ${JBOSS_HA_ARGS} ${JBOSS_MESSAGING_ARGS} ${CLI_EXECUTION_OPTS} & local pid=$! diff --git a/jboss/container/wildfly/launch-config/os/added/launch/launch-common.sh b/jboss/container/wildfly/launch-config/os/added/launch/launch-common.sh index 16efc657..df2a1a51 100644 --- a/jboss/container/wildfly/launch-config/os/added/launch/launch-common.sh +++ b/jboss/container/wildfly/launch-config/os/added/launch/launch-common.sh @@ -115,4 +115,106 @@ function splitAttributesStringIntoLines() { local temp temp=$(echo $input | sed "s|\" ${attribute_name}=\"|\" \n${attribute_name}=\"|g" | awk -F "\"" '{print $2}') echo "${temp}" +} + +# retrieves the first IP v6 address +function get_host_ipv6() { + unset -v "$1" || echo "Invalid identifier: $1" >&2 + + local input="/proc/net/if_inet6" + + if [ ! -f "$input" ]; then + log_error "$input file doesn't exist. Can't discover ip v6 address." + exit 1 + fi + local count=0 + while IFS= read -r line + do + arr=($line) + address=${arr[0]} + # Skip loopback and link local addresses + if [ $address != "00000000000000000000000000000001" ] && [[ $address != fe80* ]]; then + if [ -z "$ipv6" ]; then + local ipv6="${address:0:4}:${address:4:4}:${address:8:4}:${address:12:4}:${address:16:4}:${address:20:4}:${address:24:4}:${address:28:4}" + fi + count=$((count+1)) + fi + done < "$input" + + if [[ "$count" == "0" ]]; then + log_error "No IP v6 address found. Can't configure IPv6" + exit 1 + fi + + if [[ "$count" != "1" ]]; then + log_warning "get_host_ipv6() returned $count ipv6 addresses, only the first address $ipv6 will be used. To use different address please set $JBOSS_HA_IP and $JBOSS_MESSAGING_HOST." + fi + + printf -v "$1" '%s' "${ipv6}" +} + +# +# Find the first ipv4 address of the host +# The host could have 1 or more ipv4 addresses +# For this function we need to return a single ipv4 address +# +# /proc/net/fib_tree contains the Forwarding Information Base table +# +# awk is using a block-pattern to filter lines with 32 or host +# +# python or other languages can not be used and it must be /bin/sh compatible +# +# depends on the following tools: +# sh, awk, sort, uniq, grep, wc, head +function get_host_ipv4() { + unset -v "$1" || echo "Invalid identifier: $1" >&2 + local input="/proc/net/fib_trie" + + if [ ! -f "$input" ]; then + log_error "$input file doesn't exist. Can't discover ip v4 address." + exit 1 + fi + local allIPs=$(awk '/32 host/ { print f } {f=$2}' <<< "$(<$input)" | sort -n | uniq | grep -v '127.0.0.') + local count=$(echo "$allIPs" | wc -l) + + local ipv4=$(echo "$allIPs" | head -n1) + + if [[ "$count" == "0" ]]; then + log_error "No IP v4 address found." + exit 1 + fi + if [[ "$count" != "1" ]]; then + log_warning "get_host_ipv4() returned $count ipv4 addresses, only the first address $ipv4 will be used. To use different address please set \$JBOSS_HA_IP and \$JBOSS_MESSAGING_HOST." + fi + printf -v "$1" '%s' "${ipv4}" +} + +# Retrieves the ip v4 (the default) or ip v6 (if SERVER_USE_IPV6 env variable is set). +# The passed argument is a name of a variable that will be set by this function +# Usage: +# local ip= +# get_host_ip_address "ip" +# echo $ip +function get_host_ip_address() { + if [ "xxx$SERVER_USE_IPV6" == "xxxtrue" ]; then + get_host_ipv6 "$1" + else + get_host_ipv4 "$1" + fi +} + +function get_bind_all_address() { + if [ "xxx$SERVER_USE_IPV6" == "xxxtrue" ]; then + echo "::" + else + echo "0.0.0.0" + fi +} + +function get_loopback_address() { + if [ "xxx$SERVER_USE_IPV6" == "xxxtrue" ]; then + echo "::1" + else + echo "127.0.0.1" + fi } \ No newline at end of file diff --git a/jboss/container/wildfly/launch/ip-address/added/ip.sh b/jboss/container/wildfly/launch/ip-address/added/ip.sh new file mode 100755 index 00000000..6e019d64 --- /dev/null +++ b/jboss/container/wildfly/launch/ip-address/added/ip.sh @@ -0,0 +1,36 @@ +#!/bin/sh +# only processes a single environment as the placeholder is not preserved + +source $JBOSS_HOME/bin/launch/logging.sh + +function prepareEnv() { + unset SERVER_USE_IPV6 +} + +function configure() { + configure_ip +} + +function configureEnv() { + configure +} + +function configure_ip() { + SERVER_IP_ADDR= + get_host_ip_address "SERVER_IP_ADDR" + export SERVER_IP_ADDR + SERVER_BIND_ALL_ADDR=$(get_bind_all_address) + export SERVER_BIND_ALL_ADDR + SERVER_LOOPBACK_ADDRESS=$(get_loopback_address) + export SERVER_LOOPBACK_ADDRESS + log_info "Server IP address $SERVER_IP_ADDR, bindAll adress $SERVER_BIND_ALL_ADDR" + if [ "xxx$SERVER_USE_IPV6" == "xxxtrue" ]; then + JAVA_OPTS_APPEND="-Djava.net.preferIPv4Stack=false -Djava.net.preferIPv6Addresses=true $JAVA_OPTS_APPEND" + export JAVA_OPTS_APPEND + cat << EOF >> "${CLI_SCRIPT_FILE}" + if (outcome == success && result != undefined) of /interface=bindall:read-resource + /interface=bindall:write-attribute(name=inet-address,value="$SERVER_BIND_ALL_ADDR") + end-if +EOF + fi +} \ No newline at end of file diff --git a/jboss/container/wildfly/launch/ip-address/configure.sh b/jboss/container/wildfly/launch/ip-address/configure.sh new file mode 100644 index 00000000..2a53355f --- /dev/null +++ b/jboss/container/wildfly/launch/ip-address/configure.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e + +SCRIPT_DIR=$(dirname $0) +ADDED_DIR=${SCRIPT_DIR}/added + +mkdir -p ${JBOSS_HOME}/bin/launch/ +cp -p ${ADDED_DIR}/ip.sh ${JBOSS_HOME}/bin/launch/ diff --git a/jboss/container/wildfly/launch/ip-address/module.yaml b/jboss/container/wildfly/launch/ip-address/module.yaml new file mode 100644 index 00000000..c3255a7b --- /dev/null +++ b/jboss/container/wildfly/launch/ip-address/module.yaml @@ -0,0 +1,10 @@ +schema_version: 1 +name: jboss.container.wildfly.launch.ip-address +version: '1.0' +description: Configure ip address. +execute: + - script: configure.sh + user: '185' +envs: + - name: "SERVER_USE_IPV6" + description: By default IP v4 is used. Set this env variable to true to enable IP v6. diff --git a/jboss/container/wildfly/launch/jgroups/added/launch/ha.sh b/jboss/container/wildfly/launch/jgroups/added/launch/ha.sh index 946c607c..d3802c45 100644 --- a/jboss/container/wildfly/launch/jgroups/added/launch/ha.sh +++ b/jboss/container/wildfly/launch/jgroups/added/launch/ha.sh @@ -289,8 +289,7 @@ generate_dns_ping_config() { configure_ha_args() { # Set HA args - IP_ADDR=`hostname -i` - JBOSS_HA_ARGS="-b ${JBOSS_HA_IP:-${IP_ADDR}} -bprivate ${JBOSS_HA_IP:-${IP_ADDR}}" + JBOSS_HA_ARGS="-b ${JBOSS_HA_IP:-${SERVER_IP_ADDR}} -bprivate ${JBOSS_HA_IP:-${SERVER_IP_ADDR}}" init_node_name diff --git a/jboss/container/wildfly/launch/keycloak/added/keycloak.sh b/jboss/container/wildfly/launch/keycloak/added/keycloak.sh index 29dd2082..c53dbc2c 100644 --- a/jboss/container/wildfly/launch/keycloak/added/keycloak.sh +++ b/jboss/container/wildfly/launch/keycloak/added/keycloak.sh @@ -1015,7 +1015,6 @@ function configure_client() { client_config="${client_config},\"attributes\":{\"saml.signing.certificate\":\"${pem}\"${server_signature}}" fi else - service_addr=$(hostname -i) client_config="{\"redirectUris\":[${redirects}]" if [ -n "$HOSTNAME_HTTP" ]; then diff --git a/jboss/container/wildfly/launch/messaging/added/launch/messaging.sh b/jboss/container/wildfly/launch/messaging/added/launch/messaging.sh index a82ef427..c8cdc457 100644 --- a/jboss/container/wildfly/launch/messaging/added/launch/messaging.sh +++ b/jboss/container/wildfly/launch/messaging/added/launch/messaging.sh @@ -77,7 +77,7 @@ function configure() { } function configure_artemis_address() { - IP_ADDR=${JBOSS_MESSAGING_HOST:-`hostname -i`} + IP_ADDR=${JBOSS_MESSAGING_HOST:-${SERVER_IP_ADDR}} JBOSS_MESSAGING_ARGS="${JBOSS_MESSAGING_ARGS} -Djboss.messaging.host=${IP_ADDR}" } # /subsystem=messaging-activemq/server=default/jms-queue=queue_name:add(entries=[])