2024-10-05 12:21:38 +02:00
#!/bin/bash
#-------------------------------------------------------------------------------
2024-11-26 17:44:43 +01:00
#-------------------------------------------------------------------------------
#
# DOCKER_TOOLS_DEBUG= TRUE | FALSE # Enable debugging (Default FALSE)
#
# DOCKER_TOOLS_VOLUME= SINGLE | MERGED | SPLITED # Default: SPLITED
# SINGLE: Merged volume mode + No service suffix
# MERGED: Merged volume mode + Service suffix
# SPLITED: By service volume mode + No service suffix
#
2024-12-23 00:00:27 +01:00
# DOCKER_TOOLS_EXEPTION_LIBS="module_name:lib_dir_name ..."
2024-11-26 17:44:43 +01:00
# Example: postgres:pgsq
#
#-------------------------------------------------------------------------------
2024-10-05 12:21:38 +02:00
set -o errexit
2024-12-14 08:40:32 +01:00
if [[ "${DOCKER_TOOLS_DEBUG}" == "TRUE" ]]
2024-11-23 10:50:07 +01:00
then
set -x
fi
2024-10-05 12:21:38 +02:00
APPL_HOME_DIR="$(dirname "$(dirname "$0")")"
2024-11-23 10:50:07 +01:00
DYNVAR_LIST="APPL_HOME_DIR"
2024-12-23 00:00:27 +01:00
CONFIG_FILE_LIST=""
2024-10-05 12:21:38 +02:00
version="$1"
2024-11-25 01:08:25 +01:00
#-------------------------------------------------------------------------------
2024-11-26 17:44:43 +01:00
function dynvar_export
{
var=$1
value=$2
eval export ${var}=\"${value}\"
DYNVAR_LIST+=" ${var}"
}
#-------------------------------------------------------------------------------
function psfix_cat
2024-11-25 01:08:25 +01:00
{
prefix=$1
separator=$2
2024-11-26 17:44:43 +01:00
suffix=$3
2024-11-25 01:08:25 +01:00
2024-11-26 17:44:43 +01:00
if [[ "${prefix}" == "" ]] || [[ "${suffix}" == "" ]]
2024-11-25 01:08:25 +01:00
then
2024-11-26 17:44:43 +01:00
separator=""
2024-11-25 01:08:25 +01:00
fi
2024-11-26 17:44:43 +01:00
echo "${prefix}${separator}${suffix}"
2024-11-25 01:08:25 +01:00
}
2024-11-03 17:16:33 +01:00
#-------------------------------------------------------------------------------
2024-11-26 17:44:43 +01:00
function module_lib_name_get
2024-11-03 17:16:33 +01:00
{
2024-11-26 17:44:43 +01:00
module=$1
2024-12-23 00:00:27 +01:00
for tup in ${DOCKER_TOOLS_EXEPTION_LIBS}
2024-11-26 17:44:43 +01:00
do
if [[ "${module}" == "${tup/:*}" ]]
then
echo ${tup/*:}
return
fi
done
echo "${module}"
2024-11-03 17:16:33 +01:00
}
#-------------------------------------------------------------------------------
function docker_service_add
2024-11-17 19:30:48 +01:00
{
varible_prefix="$1"
shift
2024-11-23 10:50:07 +01:00
for var in service_name target_url_prefix target_name target_name_full target_version from_url_prefix from_name from_version module_name module_version port_main_ext port_main_int port_admin_ext port_admin_int user_name_id group_name_id
2024-11-17 19:30:48 +01:00
do
eval ${var}=\"${1}\"
shift
done
2024-11-23 10:50:07 +01:00
2024-11-17 19:30:48 +01:00
2024-11-23 10:50:07 +01:00
# Service variables
dynvar_export "${varible_prefix}_SERVICE_NAME" "${service_name}"
2024-11-17 19:30:48 +01:00
# Global Image variables
2024-11-23 10:50:07 +01:00
if [[ "${target_name}" == "-" ]]
then
target_name="${service_name}"
fi
2024-11-17 19:30:48 +01:00
dynvar_export "${varible_prefix}_IMG_NAME" "${target_name}"
dynvar_export "${varible_prefix}_IMG_NAME_FULL" "${target_name_full}"
dynvar_export "${varible_prefix}_IMG_VERSION" "${target_version}"
dynvar_export "${varible_prefix}_IMG_MAINTAINER" "\${ORG_MAINTAINER}"
dynvar_export "${varible_prefix}_IMG_URL" "${target_url_prefix}/${target_name}:${target_version}"
2024-11-23 10:50:07 +01:00
if [[ "${service_name}" == "${APPL_NAME}" ]]
2024-11-17 19:30:48 +01:00
then
2024-11-23 10:50:07 +01:00
dynvar_export "${varible_prefix}_IMG_URL" "${target_url_prefix}:${target_version}"
2024-11-26 17:44:43 +01:00
dynvar_export "${varible_prefix}_HOST_NAME" "$( psfix_cat "${HOST_NAME_PREFIX}" "-" "webapp")"
2024-11-17 19:30:48 +01:00
else
2024-11-23 10:50:07 +01:00
dynvar_export "${varible_prefix}_IMG_URL" "${target_url_prefix}/${target_name}:${target_version}"
2024-11-26 17:44:43 +01:00
dynvar_export "${varible_prefix}_HOST_NAME" "$( psfix_cat "${HOST_NAME_PREFIX}" "-" "${service_name}")"
2024-11-17 19:30:48 +01:00
fi
2024-11-23 10:50:07 +01:00
# Building variables
2024-11-17 19:30:48 +01:00
2024-11-23 10:50:07 +01:00
if [[ "${module_name}" == "-" ]]
then
module_name="${target_name}"
fi
2024-11-17 19:30:48 +01:00
if [[ "${target_name_full}" != "-" ]]
then
# Build
dynvar_export "${varible_prefix}_MODULE_NAME" "${module_name}"
dynvar_export "${varible_prefix}_MODULE_PATH" "src/${module_name}"
dynvar_export "${varible_prefix}_MODULE_VERSION" "${module_version}"
dynvar_export "${varible_prefix}_IMG_FROM_URL" "${from_url_prefix}/${from_name}:${from_version}"
fi
# Volume Directories
if [[ "${APPL_NAME}" != "" ]]
then
2024-11-26 17:44:43 +01:00
dynvar_export "${varible_prefix}_CONTEXT_PATH" "/${APPL_NAME}/${service_name}/"
2024-11-17 19:30:48 +01:00
else
2024-11-26 17:44:43 +01:00
DOCKER_TOOLS_VOLUME="SINGLE"
dynvar_export "${varible_prefix}_CONTEXT_PATH" "/${service_name}/"
2024-11-17 19:30:48 +01:00
fi
2024-11-26 17:44:43 +01:00
if [[ "${DOCKER_TOOLS_VOLUME}" == "" ]]
then
DOCKER_TOOLS_VOLUME="SPLITED"
fi
if [[ "${DOCKER_TOOLS_VOLUME}" != "SPLITED" ]]
then
volume_prefix="."
else
volume_prefix="./mnt/${service_name}"
fi
if [[ "${DOCKER_TOOLS_VOLUME}" != "MERGED" ]]
then
volume_suffix_ext=""
else
volume_suffix_ext="${service_name}"
fi
2024-12-23 00:00:27 +01:00
2024-11-26 17:44:43 +01:00
module_lib_name="$( module_lib_name_get "${module_name}")"
2024-12-23 00:00:27 +01:00
if [[ "${module_lib_name}" != /* ]]
then
path_prefix="/var/lib/"
else
path_prefix=""
fi
2024-11-26 17:44:43 +01:00
2024-12-23 00:00:27 +01:00
module_lib_path=${path_prefix}${module_lib_name}
dynvar_export "${varible_prefix}_LIB_INT" "${module_lib_path}"
dynvar_export "${varible_prefix}_LIB_EXT" "$( psfix_cat "${volume_prefix}${module_lib_path}" "-" "${volume_suffix_ext}")"
# dynvar_export "${varible_prefix}_LIB_INT" "/var/lib/${module_lib_name}"
# dynvar_export "${varible_prefix}_LIB_EXT" "$( psfix_cat "${volume_prefix}/var/lib/${module_lib_name}" "-" "${volume_suffix_ext}")"
2024-11-17 19:30:48 +01:00
dynvar_export "${varible_prefix}_LOG_INIT_INT" "/var/log/init"
2024-11-26 17:44:43 +01:00
dynvar_export "${varible_prefix}_LOG_INIT_EXT" "$( psfix_cat "${volume_prefix}/var/log/init" "-" "${volume_suffix_ext}")"
2024-11-17 19:30:48 +01:00
dynvar_export "${varible_prefix}_LOG_SV_INT" "/var/log/supervisor"
2024-11-26 17:44:43 +01:00
dynvar_export "${varible_prefix}_LOG_SV_EXT" "$( psfix_cat "${volume_prefix}/var/log/supervisor" "-" "${volume_suffix_ext}")"
2024-11-17 19:30:48 +01:00
dynvar_export "${varible_prefix}_LOG_MAIN_INT" "/var/log/${module_name}"
2024-11-26 17:44:43 +01:00
dynvar_export "${varible_prefix}_LOG_MAIN_EXT" "$( psfix_cat "${volume_prefix}/var/log/${module_name}" "-" "${volume_suffix_ext}")"
2024-11-17 19:30:48 +01:00
# Ports
2024-11-23 10:50:07 +01:00
if [[ "${port_main_int}" != "-" ]]
then
dynvar_export "${varible_prefix}_PORT_MAIN_INT" "${port_main_int}"
dynvar_export "${varible_prefix}_PORT_MAIN_EXT" "\$(( ${port_main_ext} + ${PORT_ENV_OFFSET}))"
fi
2024-11-17 19:30:48 +01:00
2024-11-23 10:50:07 +01:00
if [[ "${PORT_SV_INT}" != "" ]]
then
dynvar_export "${varible_prefix}_PORT_SV_INT" "${PORT_SV_INT}"
dynvar_export "${varible_prefix}_PORT_SV_EXT" "\$(( \${${varible_prefix}_PORT_MAIN_EXT} + ${PORT_SV_OFFSET}))"
fi
2024-11-17 19:30:48 +01:00
if [[ "${port_admin_int}" != "-" ]]
then
dynvar_export "${varible_prefix}_PORT_ADMIN_INT" "${port_admin_int}"
dynvar_export "${varible_prefix}_PORT_ADMIN_EXT" "\$(( ${port_admin_ext} + ${PORT_ENV_OFFSET}))"
fi
# User & Group
if [[ "${user_name_id}" == "-" ]]
then
user_name="-"
user_id="-"
else
if [[ "${user_name_id}" =~ ^[0-9]+$ ]]
then
user_name="-"
user_id="${user_name_id}"
else
user_name="${user_name_id}"
user_id="$( id -u "${user_name}")"
fi
fi
dynvar_export "${varible_prefix}_USER_NAME" "${user_name}"
dynvar_export "${varible_prefix}_USER_ID" "${user_id}"
if [[ "${group_name_id}" == "-" ]]
then
group_name="-"
group_id="-"
else
if [[ "${group_name_id}" =~ ^[0-9]+$ ]]
then
group_name="-"
group_id="${group_name_id}"
else
group_name="${group_name_id}"
group_id="$( getent group "${group_name}" | cut -d: -f3)"
fi
fi
dynvar_export "${varible_prefix}_GROUP_NAME" "${group_name}"
dynvar_export "${varible_prefix}_GROUP_ID" "${group_id}"
}
#-------------------------------------------------------------------------------
2024-11-23 10:50:07 +01:00
function docker_service_add-12
2024-11-03 17:16:33 +01:00
{
varible_prefix="$1"
shift
2024-11-15 19:58:26 +01:00
for var in NAME NAME_FULL URL_PREFIX IMG_NAME APPL_NAME VERSION_EXT VERSION_INT PORT_MAIN_EXT PORT_MAIN_INT PORT_ADMIN_EXT PORT_ADMIN_INT USER_NAME GROUP_NAME
2024-11-03 17:16:33 +01:00
do
dynvar_export "${varible_prefix}_${var}" "$1"
shift
done
2024-11-15 19:58:26 +01:00
2024-11-17 19:30:48 +01:00
dynvar_export "${varible_prefix}_LOG_INIT_INT" "/var/log/init"
dynvar_export "${varible_prefix}_LOG_INIT_EXT" "./var/log/init-\${${varible_prefix}_NAME}"
dynvar_export "${varible_prefix}_LOG_SV_INT" "/var/log/supervisor"
dynvar_export "${varible_prefix}_LOG_SV_EXT" "./var/log/supervisor-\${${varible_prefix}_NAME}"
2024-11-15 19:58:26 +01:00
if [[ $( eval echo \${${varible_prefix}_APPL_NAME}) != "-" ]]
then
2024-11-17 19:30:48 +01:00
dynvar_export "${varible_prefix}_LIB_INT" "/var/lib/\${${varible_prefix}_APPL_NAME}"
dynvar_export "${varible_prefix}_LIB_EXT" "./var/lib/\${${varible_prefix}_APPL_NAME}"
dynvar_export "${varible_prefix}_LOG_MAIN_INT" "/var/log/\${${varible_prefix}_APPL_NAME}"
dynvar_export "${varible_prefix}_LOG_MAIN_EXT" "./var/log/\${${varible_prefix}_APPL_NAME}"
2024-11-15 19:58:26 +01:00
fi
2024-11-03 17:16:33 +01:00
if [[ $( eval echo \${${varible_prefix}_IMG_NAME}) != "-" ]]
then
# No Build
2024-11-15 19:58:26 +01:00
dynvar_export "${varible_prefix}_IMG_URL" "\${${varible_prefix}_URL_PREFIX}/\${${varible_prefix}_IMG_NAME}:\${${varible_prefix}_VERSION_EXT}"
2024-11-03 17:16:33 +01:00
else
# Build
2024-11-15 19:58:26 +01:00
dynvar_export "${varible_prefix}_IMG_URL" "\${${varible_prefix}_URL_PREFIX}/\${${varible_prefix}_NAME}:\${${varible_prefix}_VERSION_EXT}"
2024-11-03 17:16:33 +01:00
2024-11-15 19:58:26 +01:00
dynvar_export "${varible_prefix}_MODULE_NAME" "\${${varible_prefix}_NAME}"
dynvar_export "${varible_prefix}_MODULE_PATH" "src/\${${varible_prefix}_NAME}"
2024-11-03 17:16:33 +01:00
fi
2024-11-15 19:58:26 +01:00
dynvar_export "${varible_prefix}_CONTAINER_NAME" "${CONTAINER_NAME_PREFIX}-\${${varible_prefix}_NAME}"
dynvar_export "${varible_prefix}_HOST" "\${${varible_prefix}_CONTAINER_NAME}"
2024-11-03 17:16:33 +01:00
2024-11-15 19:58:26 +01:00
dynvar_export "${varible_prefix}_PORT_MAIN_EXT" "\$(( \${${varible_prefix}_PORT_MAIN_EXT} + ${PORT_ENV_OFFSET}))"
dynvar_export "${varible_prefix}_PORT_SV_EXT" "\$(( \${${varible_prefix}_PORT_MAIN_EXT} + ${PORT_SV_OFFSET}))"
dynvar_export "${varible_prefix}_PORT_SV_INT" "9001"
2024-11-03 17:16:33 +01:00
if [[ $( eval echo \${${varible_prefix}_PORT_ADMIN_EXT}) != "-" ]]
then
2024-11-15 19:58:26 +01:00
dynvar_export "${varible_prefix}_PORT_ADMIN_EXT" "\$(( \${${varible_prefix}_PORT_ADMIN_EXT} + ${PORT_ENV_OFFSET}))"
2024-11-03 17:16:33 +01:00
fi
if [[ $( eval echo \${${varible_prefix}_USER_NAME}) == "-" ]]
then
2024-11-15 19:58:26 +01:00
dynvar_export "${varible_prefix}_USER_ID" "-"
2024-11-03 17:16:33 +01:00
else
if [[ $( eval echo \${${varible_prefix}_USER_NAME}) =~ ^[0-9]+$ ]]
then
2024-11-15 19:58:26 +01:00
dynvar_export "${varible_prefix}_USER_ID" "\${${varible_prefix}_USER_NAME}"
2024-11-03 17:16:33 +01:00
else
2024-11-15 19:58:26 +01:00
dynvar_export "${varible_prefix}_USER_ID" "\$( id -u \${${varible_prefix}_USER_NAME})"
2024-11-03 17:16:33 +01:00
fi
fi
if [[ $( eval echo \${${varible_prefix}_GROUP_NAME}) == "-" ]]
then
dynvar_export "${varible_prefix}_GROUP_ID" "-"
else
if [[ $( eval echo \${${varible_prefix}_GROUP_NAME}) =~ ^[0-9]+$ ]]
then
dynvar_export "${varible_prefix}_GROUP_ID" "\${${varible_prefix}_GROUP_NAME}"
else
dynvar_export "${varible_prefix}_GROUP_ID" "\$( getent group \${${varible_prefix}_GROUP_NAME} | cut -d: -f3)"
fi
fi
dynvar_export "${varible_prefix}_CONTEXT_PATH" "/${APPL_NAME}/\${${varible_prefix}_NAME}/"
# dynvar_export "${varible_prefix}_" "${varible_prefix}_${}"
}
2024-12-23 00:00:27 +01:00
#-------------------------------------------------------------------------------
function docker_config_add
{
config_file_target="$1"
config_file_source="$2"
CONFIG_FILE_LIST+=" ${config_file_target}:${config_file_source}"
}
2024-11-03 17:16:33 +01:00
#-------------------------------------------------------------------------------
2024-10-05 12:21:38 +02:00
. ${APPL_HOME_DIR}/env
#-------------------------------------------------------------------------------
if [[ "${version}" != "" ]]
then
APPL_VERSION="$1"
fi
#-------------------------------------------------------------------------------
>${APPL_HOME_DIR}/.env
2024-11-03 17:16:33 +01:00
for var in $( ( grep "^ *[a-zA-Z0-9_-]*=" ${APPL_HOME_DIR}/env | sed -e "s/=.*//" -e "s/ *//"; echo ${DYNVAR_LIST} | sed -e 's/ /\n/g') | sort -u)
2024-10-05 12:21:38 +02:00
do
echo "${var}='${!var}'" >>${APPL_HOME_DIR}/.env
done
2024-12-23 00:00:27 +01:00
for tupple in ${CONFIG_FILE_LIST}
do
IFS=":"
set ${tupple}
config_file_target="$1"
config_file_source="$2"
IFS=" "
while read line
do
eval echo $(echo ${line} | sed -e 's/"/\\"/g' -e 's/</\\</g' -e 's/>/\\>/g';)
done <${config_file_source} >${config_file_target}
done