Skip to content

Commit

Permalink
Add PHP profiles for DSM 6
Browse files Browse the repository at this point in the history
  • Loading branch information
mreid-tt committed Jan 27, 2024
1 parent cca1f4e commit d5b22ec
Showing 1 changed file with 154 additions and 35 deletions.
189 changes: 154 additions & 35 deletions spk/fengoffice/src/service-setup.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

# Package
SC_DNAME="Feng Office"
SC_PKG_PREFIX="com-synocommunity-packages-"
SC_PKG_NAME="${SC_PKG_PREFIX}${SYNOPKG_PKGNAME}"
SVC_KEEP_LOG=y
SVC_BACKGROUND=y
SVC_WRITE_PID=y
Expand All @@ -18,10 +21,11 @@ else
WEB_USER="http"
WEB_GROUP="http"
fi
WEB_ROOT="${WEB_DIR}/${SYNOPKG_PKGNAME}"

service_prestart ()
{
FENGOFFICE="${WEB_DIR}/${SYNOPKG_PKGNAME}/cron.php"
FENGOFFICE="${WEB_ROOT}/cron.php"
COMMAND="${PHP} ${FENGOFFICE}"
SLEEP_TIME="600"
# Main loop
Expand All @@ -42,6 +46,18 @@ service_prestart ()

validate_preinst ()
{
# Check for modification to PHP template defaults on DSM 6
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
WS_TMPL_DIR="/var/packages/WebStation/target/misc"
WS_TMPL_FILE="php74_fpm.mustache"
WS_TMPL_PATH="${WS_TMPL_DIR}/${WS_TMPL_FILE}"
# Check for PHP template defaults
if ! grep -q -E '^user = http$' "${WS_TMPL_PATH}" || ! grep -q -E '^listen\.owner = http$' "${WS_TMPL_PATH}"; then
echo "PHP template defaults have been modified. Installation is not supported."
exit 1
fi
fi

if [ "${SYNOPKG_PKG_STATUS}" = "INSTALL" ]; then
if ! ${MYSQL} -u root -p"${wizard_mysql_password_root}" -e quit > /dev/null 2>&1; then
echo "Incorrect MySQL root password"
Expand All @@ -60,9 +76,68 @@ validate_preinst ()

service_postinst ()
{
# Install the web interface
# Web interface setup for DSM 6 -- used by INSTALL and UPGRADE
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
cp -pR ${SYNOPKG_PKGDEST}/share/${SYNOPKG_PKGNAME} ${WEB_DIR}
# Install the web interface
echo "Installing web interface"
${MKDIR} ${WEB_ROOT}
rsync -aX ${SYNOPKG_PKGDEST}/share/${SYNOPKG_PKGNAME}/ ${WEB_ROOT} 2>&1

# Install web configurations
TEMPDIR="${SYNOPKG_PKGTMP}/web"
${MKDIR} ${TEMPDIR}
WS_CFG_DIR="/usr/syno/etc/packages/WebStation"
WS_CFG_FILE="WebStation.json"
WS_CFG_PATH="${WS_CFG_DIR}/${WS_CFG_FILE}"
TMP_WS_CFG_PATH="${TEMPDIR}/${WS_CFG_FILE}"
PHP_CFG_FILE="PHPSettings.json"
PHP_CFG_PATH="${WS_CFG_DIR}/${PHP_CFG_FILE}"
TMP_PHP_CFG_PATH="${TEMPDIR}/${PHP_CFG_FILE}"
PHP_PROF_NAME="Default PHP 7.4 Profile"
WS_BACKEND="$(jq -r '.default.backend' ${WS_CFG_PATH})"
WS_PHP="$(jq -r '.default.php' ${WS_CFG_PATH})"
RESTART_APACHE="no"
RSYNC_ARCH_ARGS="--backup --suffix=.bak --remove-source-files"
# Check if Apache is the selected back-end
if [ ! "$WS_BACKEND" = "2" ]; then
echo "Set Apache as the back-end server"
jq '.default.backend = 2' ${WS_CFG_PATH} > ${TMP_WS_CFG_PATH}
rsync -aX ${RSYNC_ARCH_ARGS} ${TMP_WS_CFG_PATH} ${WS_CFG_DIR}/ 2>&1
RESTART_APACHE="yes"
fi
# Check if default PHP profile is selected
if [ -z "$WS_PHP" ] || [ "$WS_PHP" = "null" ]; then
echo "Enable default PHP profile"
# Locate default PHP profile
PHP_PROF_ID="$(jq -r '. | to_entries[] | select(.value | type == "object" and .profile_desc == "'"$PHP_PROF_NAME"'") | .key' "${PHP_CFG_PATH}")"
jq ".default.php = \"$PHP_PROF_ID\"" "${WS_CFG_PATH}" > ${TMP_WS_CFG_PATH}
rsync -aX ${RSYNC_ARCH_ARGS} ${TMP_WS_CFG_PATH} ${WS_CFG_DIR}/ 2>&1
RESTART_APACHE="yes"
fi
# Check for PHP profile
if ! jq -e ".[\"${SC_PKG_NAME}\"]" "${PHP_CFG_PATH}" >/dev/null; then
echo "Add PHP profile for ${SC_DNAME}"
jq --slurpfile newProfile ${SYNOPKG_PKGDEST}/web/${SYNOPKG_PKGNAME}.json '.["'"${SC_PKG_NAME}"'"] = $newProfile[0]' ${PHP_CFG_PATH} > ${TMP_PHP_CFG_PATH}
rsync -aX ${RSYNC_ARCH_ARGS} ${TMP_PHP_CFG_PATH} ${WS_CFG_DIR}/ 2>&1
RESTART_APACHE="yes"
fi
# Check for Apache config
if [ ! -f "/usr/local/etc/apache24/sites-enabled/${SYNOPKG_PKGNAME}.conf" ]; then
echo "Add Apache config for ${SC_DNAME}"
rsync -aX ${SYNOPKG_PKGDEST}/web/${SYNOPKG_PKGNAME}.conf /usr/local/etc/apache24/sites-enabled/ 2>&1
RESTART_APACHE="yes"
fi
# Restart Apache if configs have changed
if [ "$RESTART_APACHE" = "yes" ]; then
if jq -e 'to_entries | map(select((.key | startswith("'"${SC_PKG_PREFIX}"'")) and .key != "'"${SC_PKG_NAME}"'")) | length > 0' "${PHP_CFG_PATH}" >/dev/null; then
echo " [WARNING] Multiple PHP profiles detected, will require restart of DSM to load new configs"
else
echo "Restart Apache to load new configs"
${SYNOSVC} --restart pkgctl-Apache2.4
fi
fi
# Clean-up temporary files
${RM} ${TEMPDIR}
fi

# Run installer
Expand All @@ -87,7 +162,7 @@ script_installer_storage[database_type]=mysqli\
&submited=submited"
# Prepare environment
COMMAND="${PHP} ${PHP_EXTENSIONS} install_helper.php"
cd ${WEB_DIR}/${SYNOPKG_PKGNAME}/public/install/ || exit 1
cd ${WEB_ROOT}/public/install/ || exit 1
# Execute based on DSM version
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
/bin/su "$WEB_USER" -s /bin/sh -c "${COMMAND}" > /dev/null
Expand All @@ -98,10 +173,10 @@ script_installer_storage[database_type]=mysqli\

# Fix permissions
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
chown -R ${WEB_USER}:${WEB_GROUP} ${WEB_DIR}/${SYNOPKG_PKGNAME}/config
chown -R ${WEB_USER}:${WEB_GROUP} ${WEB_DIR}/${SYNOPKG_PKGNAME}/cache
chown -R ${WEB_USER}:${WEB_GROUP} ${WEB_DIR}/${SYNOPKG_PKGNAME}/upload
chown -R ${WEB_USER}:${WEB_GROUP} ${WEB_DIR}/${SYNOPKG_PKGNAME}/tmp
chown -R ${WEB_USER}:${WEB_GROUP} ${WEB_ROOT}/config
chown -R ${WEB_USER}:${WEB_GROUP} ${WEB_ROOT}/cache
chown -R ${WEB_USER}:${WEB_GROUP} ${WEB_ROOT}/upload
chown -R ${WEB_USER}:${WEB_GROUP} ${WEB_ROOT}/tmp
fi
}

Expand All @@ -113,40 +188,84 @@ service_preuninst ()
exit 1
fi

# Check database export location
if [ "${SYNOPKG_PKG_STATUS}" = "UNINSTALL" ] && [ -n "${wizard_dbexport_path}" ]; then
if [ -f "${wizard_dbexport_path}" ] || [ -e "${wizard_dbexport_path}/${MYSQL_DATABASE}.sql" ]; then
echo "File ${wizard_dbexport_path}/${MYSQL_DATABASE}.sql already exists. Please remove or choose a different location"
exit 1
fi
fi
}

service_postuninst ()
{
# Export database
if [ "${SYNOPKG_PKG_STATUS}" = "UNINSTALL" ]; then
# Check export directory
if [ -n "${wizard_dbexport_path}" ]; then
mkdir -p ${wizard_dbexport_path}
if [ ! -d "${wizard_dbexport_path}" ]; then
# If the export path directory does not exist, create it
${MKDIR} "${wizard_dbexport_path}" || {
# If mkdir fails, print an error message and exit
echo "Error: Unable to create directory ${wizard_dbexport_path}. Check permissions."
exit 1
}
elif [ ! -w "${wizard_dbexport_path}" ]; then
# If the export path directory is not writable, print an error message and exit
echo "Error: Unable to write to directory ${wizard_dbexport_path}. Check permissions."
exit 1
elif [ -e "${wizard_dbexport_path}/${MYSQL_DATABASE}.sql" ]; then
echo "File ${wizard_dbexport_path}/${MYSQL_DATABASE}.sql already exists. Please remove or choose a different location"
exit 1
fi
# Export database
${MYSQLDUMP} -u root -p"${wizard_mysql_password_root}" ${MYSQL_DATABASE} > ${wizard_dbexport_path}/${MYSQL_DATABASE}.sql
fi
fi
}

# Remove the web interface
service_postuninst ()
{
# Web interface removal for DSM 6 -- used by UNINSTALL and UPGRADE
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
rm -fr ${WEB_DIR}/${SYNOPKG_PKGNAME}
# Remove the web interface
echo "Removing web interface"
${RM} ${WEB_ROOT}

# Remove web configurations
TEMPDIR="${SYNOPKG_PKGTMP}/web"
${MKDIR} ${TEMPDIR}
WS_CFG_DIR="/usr/syno/etc/packages/WebStation"
PHP_CFG_FILE="PHPSettings.json"
PHP_CFG_PATH="${WS_CFG_DIR}/${PHP_CFG_FILE}"
TMP_PHP_CFG_PATH="${TEMPDIR}/${PHP_CFG_FILE}"
RESTART_APACHE="no"
RSYNC_ARCH_ARGS="--backup --suffix=.bak --remove-source-files"
# Check for PHP profile
if jq -e ".[\"${SC_PKG_NAME}\"]" "${PHP_CFG_PATH}" >/dev/null; then
echo "Removing PHP profile for ${SC_DNAME}"
jq 'del(.["'"${SC_PKG_NAME}"'"])' ${PHP_CFG_PATH} > ${TMP_PHP_CFG_PATH}
rsync -aX ${RSYNC_ARCH_ARGS} ${TMP_PHP_CFG_PATH} ${WS_CFG_DIR}/ 2>&1
${RM} "${WS_CFG_DIR}/php_profile/${SC_PKG_NAME}"
RESTART_APACHE="yes"
fi
# Check for Apache config
if [ -f "/usr/local/etc/apache24/sites-enabled/${SYNOPKG_PKGNAME}.conf" ]; then
echo "Removing Apache config for ${SC_DNAME}"
${RM} /usr/local/etc/apache24/sites-enabled/${SYNOPKG_PKGNAME}.conf
RESTART_APACHE="yes"
fi
# Restart Apache if configs have changed
if [ "$RESTART_APACHE" = "yes" ]; then
if jq -e 'to_entries | map(select((.key | startswith("'"${SC_PKG_PREFIX}"'")) and .key != "'"${SC_PKG_NAME}"'")) | length > 0' "${PHP_CFG_PATH}" >/dev/null; then
echo " [WARNING] Multiple PHP profiles detected, will require restart of DSM to load new configs"
else
echo "Restart Apache to load new configs"
${SYNOSVC} --restart pkgctl-Apache2.4
fi
fi
# Clean-up temporary files
${RM} ${TEMPDIR}
fi
}

service_save ()
{
# Save configuration and files
rm -fr ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}
[ -d ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME} ] && ${RM} ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}
mkdir -p ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}
mv ${WEB_DIR}/${SYNOPKG_PKGNAME}/config/config.php ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/
mv ${WEB_DIR}/${SYNOPKG_PKGNAME}/config/installed_version.php ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/
mv ${WEB_ROOT}/config/config.php ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/
mv ${WEB_ROOT}/config/installed_version.php ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/
mkdir ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/upload/
cp -r ${WEB_DIR}/${SYNOPKG_PKGNAME}/upload/*/ ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/upload/
cp -r ${WEB_ROOT}/upload/*/ ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/upload/
}

service_restore ()
Expand All @@ -157,21 +276,21 @@ service_restore ()
INSTALLED_VERSION=$(sed -n "s|return '\(.*\)';|\1|p" ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/installed_version.php | xargs)

# Restore configuration
mv ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/config.php ${WEB_DIR}/${SYNOPKG_PKGNAME}/config/
cp -r ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/upload/*/ ${WEB_DIR}/${SYNOPKG_PKGNAME}/upload/
rm -fr ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}
mv ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/config.php ${WEB_ROOT}/config/
cp -r ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/upload/*/ ${WEB_ROOT}/upload/
${RM} ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}

# Fix permissions
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
chown -R ${WEB_USER}:${WEB_GROUP} ${WEB_DIR}/${SYNOPKG_PKGNAME}/upload
chown -R ${WEB_USER}:${WEB_GROUP} ${WEB_ROOT}/upload
fi

# Run update scripts
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
/bin/su "$WEB_USER" -s /bin/sh -c "${PHP} ${WEB_DIR}/${SYNOPKG_PKGNAME}/public/upgrade/console.php ${INSTALLED_VERSION} ${PACKAGE_VERSION}" >> ${LOG_FILE} 2>&1
/bin/su "$WEB_USER" -s /bin/sh -c "${PHP} ${WEB_DIR}/${SYNOPKG_PKGNAME}/public/install/plugin-console.php update_all" >> ${LOG_FILE} 2>&1
/bin/su "$WEB_USER" -s /bin/sh -c "${PHP} ${WEB_ROOT}/public/upgrade/console.php ${INSTALLED_VERSION} ${PACKAGE_VERSION}" >> ${LOG_FILE} 2>&1
/bin/su "$WEB_USER" -s /bin/sh -c "${PHP} ${WEB_ROOT}/public/install/plugin-console.php update_all" >> ${LOG_FILE} 2>&1
else
${PHP} ${WEB_DIR}/${SYNOPKG_PKGNAME}/public/upgrade/console.php ${INSTALLED_VERSION} ${PACKAGE_VERSION} >> ${LOG_FILE} 2>&1
${PHP} ${WEB_DIR}/${SYNOPKG_PKGNAME}/public/install/plugin-console.php update_all >> ${LOG_FILE} 2>&1
${PHP} ${WEB_ROOT}/public/upgrade/console.php ${INSTALLED_VERSION} ${PACKAGE_VERSION} >> ${LOG_FILE} 2>&1
${PHP} ${WEB_ROOT}/public/install/plugin-console.php update_all >> ${LOG_FILE} 2>&1
fi
}

0 comments on commit d5b22ec

Please sign in to comment.