Skip to content

Instantly share code, notes, and snippets.

@jas8522
Created November 3, 2021 18:45
Show Gist options
  • Save jas8522/0490d6b4cf9b0981c4e32d604d4c5a88 to your computer and use it in GitHub Desktop.
Save jas8522/0490d6b4cf9b0981c4e32d604d4c5a88 to your computer and use it in GitHub Desktop.
This script updates Installatron apps when someone changes a domain in Plesk. It's triggered by Plesk change domain/subdomain hooks
#!/bin/bash
#For debugging
LOGFILE=/tmp/plesk_event_handler.log
if [ "${OLD_SUBDOMAIN_NAME}" != "${NEW_SUBDOMAIN_NAME}" ]; then
# This is a subdomain change
OLD_DOMAIN_NAME="${OLD_SUBDOMAIN_NAME}.${OLD_DOMAIN_NAME}"
NEW_DOMAIN_NAME="${NEW_SUBDOMAIN_NAME}.${NEW_DOMAIN_NAME}"
#echo "$PRIMARY_DOMAIN | $OLD_DOMAIN_NAME | $NEW_DOMAIN_NAME" >> $LOGFILE
fi
# Start assuming the domain is the primary domain
PRIMARY_DOMAIN=$NEW_DOMAIN_NAME
if [ "$OLD_DOMAIN_NAME" == "$NEW_DOMAIN_NAME" ]; then
exit 1
fi
if [ "$OLD_DOMAIN_NAME" == "" ] || [ "$NEW_DOMAIN_NAME" == ""]; then
echo "Missing at least one input variable." >> $LOGFILE
exit 1
fi
#echo "Changing ${OLD_DOMAIN_NAME} to ${NEW_DOMAIN_NAME}" >> $LOGFILE
# When addon domain, get the primary
ALL_PRIMARY_DOMAINS=$(plesk bin subscription --list)
if ! echo $ALL_PRIMARY_DOMAINS | grep -q $NEW_DOMAIN_NAME; then
PRIMARY_DOMAIN=$(plesk db -Ne "SELECT name FROM domains WHERE id=(SELECT webspace_id FROM domains WHERE name='$NEW_DOMAIN_NAME')")
fi
if [ "$PRIMARY_DOMAIN" == "" ]; then
echo "Primary domain not found." >> $LOGFILE
exit 1
fi
# Inform Installatron of the domain change
DOM_ID=$(plesk db -Ne "SELECT id from domains where name='$NEW_DOMAIN_NAME'")
/usr/local/installatron/installatron --edit --user domain$DOM_ID --id :all --replace-domain $OLD_DOMAIN_NAME=$NEW_DOMAIN_NAME
####
## Additional Fixes
####
# Fix for WP Super Cache config
app_paths=$(/usr/local/installatron/installatron --installs --user domain$DOM_ID | grep path | tr -d '\",\\' | awk '{print $2}')
# Loop in case there's multiple installs on this domain or subdomain
for path in $app_paths; do
wpconfig=$path/wp-config.php
sed -i "/^define.*WPCACHEHOME/s/$OLD_DOMAIN_NAME/$NEW_DOMAIN_NAME/" $wpconfig
done
# Loop all installatron app config files and replace old domain with new one
pushd /var/www/vhosts/$PRIMARY_DOMAIN/.appdata/current > /dev/null
for app_config_file in $(ls .); do
# If the domain being changed does not exist in the app config, skip it.
if ! grep -q $OLD_DOMAIN_NAME $app_config_file; then
continue
fi
#echo "Changing ${OLD_DOMAIN_NAME} to ${NEW_DOMAIN_NAME} in $app_config_file" >> $LOGFILE
# Installatron does not update the path value, so we do it:
if [ "$PRIMARY_DOMAIN" == "$NEW_DOMAIN_NAME" ]; then
sed -i "s#^path=/var/www/vhosts/$OLD_DOMAIN_NAME#path=/var/www/vhosts/$NEW_DOMAIN_NAME#" $app_config_file
fi
#Do app specific stuff
webapp=$(sed -nr 's/^installer=(.*)/\1/p' $app_config_file)
if [ "$webapp" == 'wordpress' ]; then
dbhost=$(sed -nr 's/^db-host=(.*)/\1/p' $app_config_file)
#dbtype=$(sed -nr 's/^db-type=(.*)/\1/p' $app_config_file)
dbname=$(sed -nr 's/^db-name=(.*)/\1/p' $app_config_file)
dbuser=$(sed -nr 's/^db-user=(.*)/\1/p' $app_config_file)
dbpass=$(sed -nr 's/^db-pass=(.*)/\1/p' $app_config_file)
dbprefix=$(sed -nr 's/^db-prefix=(.*)/\1/p' $app_config_file)
#Fix for upload path: https://clients.websavers.ca/whmcs/knowledgebase/233/You-receive-a-repeated-error-in-the-logs-about-an-openbasedir-restriction-in-effect.html
MYSQL_PWD=$dbpass /usr/bin/mysql -B -u$dbuser $dbname -Ne "DELETE FROM ${dbprefix}options WHERE option_name='upload_path';"
fi
done
popd > /dev/null
# Apply new SSL cert to new domain if DNS points to this server
ALL_IPS=$(plesk bin ipmanage --ip_list)
LIVE_IP=$(host $NEW_DOMAIN_NAME 8.8.8.8 | grep "$NEW_DOMAIN_NAME has address" | grep -Po "\d+.\d+.\d+.\d+")
if [[ $ALL_IPS == *"${LIVE_IP}"* ]]; then #generate SSL cert
CLIENT_EMAIL=$(plesk db -Ne "SELECT clients.email FROM clients INNER JOIN domains ON clients.id = domains.cl_id WHERE name='$NEW_DOMAIN_NAME'")
plesk bin extension --exec letsencrypt cli.php -d $NEW_DOMAIN_NAME -m $CLIENT_EMAIL
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment