Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring entrypoint scripts #87

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions 2.9.2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ RUN ln -sf /proc/self/fd/1 /var/log/apache2/access.log && \
ln -sf /proc/self/fd/2 /var/log/apache2/error.log

COPY conf/ /tmp/conf
COPY ./scripts/docker-entrypoint.sh /usr/bin/docker-entrypoint.sh

COPY ./docker-entrypoint.sh /docker-entrypoint.sh
COPY ./docker-entrypoint.d /docker-entrypoint.d

EXPOSE 80

# https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#entrypoint
ENTRYPOINT ["/usr/bin/docker-entrypoint.sh"]
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["/usr/sbin/apache2", "-DFOREGROUND"]
17 changes: 17 additions & 0 deletions 2.9.2/docker-entrypoint.d/01-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

echo "+----------------------------------------------------------+"
echo "| |"
echo "| Welcome to OCS Inventory NG Management Docker! |"
echo "| |"
echo "+----------------------------------------------------------+"

# Create all directories
mkdir -p $OCS_LOG_DIR
mkdir -p $OCS_PERLEXT_DIR/Apache/Ocsinventory/Plugins
mkdir -p $OCS_PLUGINSEXT_DIR
mkdir -p $OCS_VARLIB_DIR/download
mkdir -p $OCS_VARLIB_DIR/ipd
mkdir -p $OCS_VARLIB_DIR/logs
mkdir -p $OCS_VARLIB_DIR/scripts
mkdir -p $OCS_VARLIB_DIR/snmp
48 changes: 48 additions & 0 deletions 2.9.2/docker-entrypoint.d/10-com-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

SRV_CONF_FILE="/etc/apache2/conf-available/z-ocsinventory-server.conf"

if [ ! -z "${OCS_DISABLE_COM_MODE}" ]; then
echo "+---------------------------------------------------------------------------+"
echo "| Warning: OCS_DISABLE_COM_MODE environment variable is set! |"
echo "| Communication server, which handles HTTP communications between database |"
echo "| server and agents (Apache, perl and mod_perl) will be DISABLED! |"
echo "+---------------------------------------------------------------------------+"
exit 0
fi

# Configure z-ocsinventory-server file
if [ ! -f ${SRV_CONF_FILE} ]; then
cp /tmp/conf/ocsinventory-server.conf ${SRV_CONF_FILE}
sed -i 's/VERSION_MP/2/g' ${SRV_CONF_FILE}
sed -i 's/DATABASE_SERVER/'"$OCS_DB_SERVER"'/g' ${SRV_CONF_FILE}
sed -i 's/DATABASE_PORT/'"$OCS_DB_PORT"'/g' ${SRV_CONF_FILE}
sed -i 's/DATABASE_NAME/'"$OCS_DB_NAME"'/g' ${SRV_CONF_FILE}
sed -i 's/DATABASE_USER/'"$OCS_DB_USER"'/g' ${SRV_CONF_FILE}
sed -i 's/DATABASE_PASSWD/'"$OCS_DB_PASS"'/g' ${SRV_CONF_FILE}
sed -i 's/"PATH_TO_LOG_DIRECTORY"/'"${OCS_LOG_DIR//\//\\/}"'/g' ${SRV_CONF_FILE}
sed -i 's/"PATH_TO_PLUGINS_PERL_DIRECTORY"/'"${OCS_PERLEXT_DIR//\//\\/}"'/g' ${SRV_CONF_FILE}
sed -i 's/"PATH_TO_PLUGINS_CONFIG_DIRECTORY"/'"${OCS_PLUGINSEXT_DIR//\//\\/}"'/g' ${SRV_CONF_FILE}
fi

# Replace Variables
if [ -f ${SRV_CONF_FILE} ]; then
echo "+-----------------------------------------------+"
echo "| Customizing from environment variables... |"
echo "+-----------------------------------------------+"
echo
# Get all env vars starting with 'OCS_'
for var in $(env | cut -f1 -d= | grep -i OCS_); do
# Check that the current var is not commented out in conf file
if grep -q "^\s*PerlSetEnv ${var^^}" ${SRV_CONF_FILE}; then
echo "Applying Config ${var^^}=${!var} from environment variable"
sed -i "s,^\(\s*PerlSetEnv ${var^^}\).*$,\1 ${!var},g" ${SRV_CONF_FILE}
fi
done
fi

# Permissions
chown -R $APACHE_RUN_USER: $OCS_LOG_DIR

# Enable conf
a2enconf z-ocsinventory-server
57 changes: 57 additions & 0 deletions 2.9.2/docker-entrypoint.d/20-web-console.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

REPORTS_CONF_FILE="/etc/apache2/conf-available/ocsinventory-reports.conf"
DB_CONFIG_INC_FILE="${OCS_WEBCONSOLE_DIR}/ocsreports/dbconfig.inc.php"

if [ ! -z "${OCS_DISABLE_WEB_MODE}" ]; then
echo "+---------------------------------------------------------------------------+"
echo "| Warning: OCS_DISABLE_WEB_MODE environment variable is set! |"
echo "| Administration console, which allows administrators to query the database |"
echo "| server using their favorite browser (Apache, php) will be DISABLED! |"
echo "+---------------------------------------------------------------------------+"
exit 0
fi

if [ ! -f $OCS_WEBCONSOLE_DIR/ocsreports/var.php ]; then
cp -r /tmp/OCSNG_UNIX_SERVER-${OCS_VERSION}/ocsreports/ ${OCS_WEBCONSOLE_DIR}
rm -rf ${DB_CONFIG_INC_FILE}
fi

# Configure ocsinventory-reports file
if [ ! -f ${REPORTS_CONF_FILE} ]; then
cp /tmp/conf/ocsinventory-reports.conf ${REPORTS_CONF_FILE}
sed -i 's/OCSREPORTS_ALIAS/\/ocsreports/g' ${REPORTS_CONF_FILE}
sed -i 's/PATH_TO_OCSREPORTS_DIR/'"${OCS_WEBCONSOLE_DIR//\//\\/}"'\/ocsreports/g' ${REPORTS_CONF_FILE}
sed -i 's/PACKAGES_ALIAS/\/download/g' ${REPORTS_CONF_FILE}
sed -i 's/PATH_TO_PACKAGES_DIR/'"${OCS_VARLIB_DIR//\//\\/}"'download/g' ${REPORTS_CONF_FILE}
sed -i 's/SNMP_ALIAS/\/snmp/g' ${REPORTS_CONF_FILE}
sed -i 's/PATH_TO_SNMP_DIR/'"${OCS_VARLIB_DIR//\//\\/}"'snmp/g' ${REPORTS_CONF_FILE}
fi

# Generate dbconfig.inc.php
if [ ! -f ${DB_CONFIG_INC_FILE} ]; then
cp /tmp/conf/dbconfig.inc.php $OCS_WEBCONSOLE_DIR/ocsreports
sed -i 's/OCS_DB_NAME/'"$OCS_DB_NAME"'/g' ${DB_CONFIG_INC_FILE}
sed -i 's/OCS_READ_NAME/'"$OCS_DB_SERVER"'/g' ${DB_CONFIG_INC_FILE}
sed -i 's/OCS_WRITE_NAME/'"$OCS_DB_SERVER"'/g' ${DB_CONFIG_INC_FILE}
sed -i 's/OCS_DB_PORT/'"$OCS_DB_PORT"'/g' ${DB_CONFIG_INC_FILE}
sed -i 's/OCS_DB_USER/'"$OCS_DB_USER"'/g' ${DB_CONFIG_INC_FILE}
sed -i 's/OCS_DB_PASS/'"$OCS_DB_PASS"'/g' ${DB_CONFIG_INC_FILE}
sed -i 's/OCS_SSL_ENABLED/'"$OCS_SSL_ENABLED"'/g' ${DB_CONFIG_INC_FILE}
sed -i 's/OCS_SSL_WEB_MODE/'"$OCS_SSL_WEB_MODE"'/g' ${DB_CONFIG_INC_FILE}
sed -i 's/OCS_SSL_KEY/'"${OCS_SSL_KEY//\//\\/}"'/g' ${DB_CONFIG_INC_FILE}
sed -i 's/OCS_SSL_CERT/'"${OCS_SSL_CERT//\//\\/}"'/g' ${DB_CONFIG_INC_FILE}
sed -i 's/OCS_SSL_CA/'"${OCS_SSL_CA//\//\\/}"'/g' ${DB_CONFIG_INC_FILE}
fi

# Permissions
chown -R $APACHE_RUN_USER: $OCS_VARLIB_DIR
chown -R $APACHE_RUN_USER: $OCS_WEBCONSOLE_DIR

# rm install.php
if [ -f $OCS_WEBCONSOLE_DIR/ocsreports/install.php ]; then
rm $OCS_WEBCONSOLE_DIR/ocsreports/install.php
fi

# Enable conf
a2enconf ocsinventory-reports
32 changes: 32 additions & 0 deletions 2.9.2/docker-entrypoint.d/30-api-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

if [ ! -z "${OCS_DISABLE_API_MODE}" ] || [ ! -z "${OCS_DISABLE_COM_MODE}" ]; then
echo "+---------------------------------------------------------------------------+"
echo "| Warning: OCS_DISABLE_API_MODE or OCS_DISABLE_COM_MODE environment |"
echo "| variable is set! |"
echo "| REST API will be DISABLED |"
echo "+---------------------------------------------------------------------------+"
exit 0
fi

API_CONF_FILE="/etc/apache2/conf-available/zz-ocsinventory-restapi.conf"
API_ROUTE=$(perl -e "print \"@INC[2]\"")
API_ROUTE_LOADER="${API_ROUTE}/Api/Ocsinventory/Restapi/Loader.pm"

cp -R /tmp/OCSNG_UNIX_SERVER-${OCS_VERSION}/Api/ ${API_ROUTE}

# Configure zz-ocsinventory-restapi file
if [ ! -f ${API_CONF_FILE} ]; then
cp /tmp/conf/ocsinventory-restapi.conf ${API_CONF_FILE}
sed -i 's/DATABASE_SERVER/'"$OCS_DB_SERVER"'/g' ${API_CONF_FILE}
sed -i 's/DATABASE_PORT/'"$OCS_DB_PORT"'/g' ${API_CONF_FILE}
sed -i 's/DATABASE_NAME/'"$OCS_DB_NAME"'/g' ${API_CONF_FILE}
sed -i 's/DATABASE_USER/'"$OCS_DB_USER"'/g' ${API_CONF_FILE}
sed -i 's/DATABASE_PASSWD/'"$OCS_DB_PASS"'/g' ${API_CONF_FILE}
sed -i 's/OCS_SSL_ENABLED/'"$OCS_SSL_ENABLED"'/g' ${API_CONF_FILE}
sed -i 's/REST_API_PATH/'"${API_ROUTE//\//\\/}"'/g' ${API_CONF_FILE}
sed -i 's/REST_API_LOADER_PATH/'"${API_ROUTE_LOADER//\//\\/}"'/g' ${API_CONF_FILE}
fi

# Enable conf
a2enconf zz-ocsinventory-restapi
35 changes: 35 additions & 0 deletions 2.9.2/docker-entrypoint.d/99-finish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

echo "+----------------------------------------------------------+"
echo "| Setting Apache Server Name to '${APACHE_SERVER_NAME:-localhost}'"
echo "+----------------------------------------------------------+"
echo

sed -ri -e "s!^#(ServerName)\s+\S+!\1 ${APACHE_SERVER_NAME:-localhost}:80!g" \
"/etc/apache2/apache2.conf"

# Remove temp files
echo "+--------------------------------+"
echo "| Removing not used files... |"
echo "+--------------------------------+"
echo
cd /tmp
shopt -s extglob
rm -rf !("conf")

# Apache start
if [ ! -d "$APACHE_RUN_DIR" ]; then
mkdir "$APACHE_RUN_DIR"
chown $APACHE_RUN_USER:$APACHE_RUN_GROUP "$APACHE_RUN_DIR"
fi

if [ -f "$APACHE_PID_FILE" ]; then
rm "$APACHE_PID_FILE"
fi

echo "+----------------------------------------------------------+"
echo "| OK, prepare finshed ;-) |"
echo "| |"
echo "| Starting OCS Inventory NG Management Docker... |"
echo "+----------------------------------------------------------+"
echo
35 changes: 35 additions & 0 deletions 2.9.2/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
# vim:sw=4:ts=4:et

set -e

if [ -z "${OCS_ENTRYPOINT_QUIET_LOGS:-}" ]; then
exec 3>&1
else
exec 3>/dev/null
fi

if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -maxdepth 1 -type f -print -quit 2>/dev/null | read v; then
echo >&3 "$0: /docker-entrypoint.d/ is not empty, will attempt to perform configuration"

echo >&3 "$0: Looking for shell scripts in /docker-entrypoint.d/"
find "/docker-entrypoint.d/" -follow -type f -print | sort -V | while read -r f; do
case "$f" in
*.sh)
if ! [ -x "$f" ]; then
chmod +x $f
fi

echo -e >&3 "\n$0: Launching ${f}\n"
"$f"
;;
*) echo -e >&3 "\n$0: Ignoring ${f}\n" ;;
esac
done

echo >&3 "$0: Configuration complete; ready for start up"
else
echo >&3 "$0: No files found in /docker-entrypoint.d/, skipping configuration"
fi

exec "$@"
Loading