captaincore/app/update
2024-05-02 21:28:26 -04:00

81 lines
3.3 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Runs themes, plugins and core updates on one or more sites.
#
# `captaincore update`
#
# [<site>...]
# One or more sites to update.
#
# [@<target>]
# Target groups of sites like @all @production or @staging. Use `captaincore update @production.updates-on` to target production sites marked for automatic updates.
#
# [--exclude-themes=<themes>]
# Passed onto `wp theme update --exclude=<theme-names>`
#
# [--exclude-plugins=<plugins>]
# Passed onto `wp plugin update --exclude=<theme-names>`
#
if [ ${#@} -ne 1 ]; then
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Please specify a <site>."
fi
while read config; do
if [[ "$config" == "Error:"* ]]; then
continue
fi
declare "$config"
done <<< "$(php ${CAPTAINCORE_PATH}/lib/local-scripts/configs.php fetch)"
site=$1
run_command() {
# Extract environment
if [[ "$site" == *"-staging"* ]]; then
environment=staging
else
environment=production
fi
# Load site configs
IFS=$'\n'$'\r'; for line in $(captaincore site get $site --bash --captain-id=$CAPTAIN_ID); do declare "$line"; done
rclone_config_file="$path/${site}_${site_id}/rclone.conf"
ftp_output=$( { rclone lsd ${environment}:$home_directory --config="$rclone_config_file" ; } 2>&1 )
ftp_search_for_wordpress=$( echo "$ftp_output" | perl -wnE'say for /wp-admin/g' )
if [[ $ftp_search_for_wordpress != "wp-admin"* ]]; then
echo "Error: WordPress not found $site-$environment"
return
fi
cd ${CAPTAINCORE_PATH}/data
captaincore quicksave generate $site-$environment --captain-id=$CAPTAIN_ID
quicksave_id_before=$( captaincore quicksave latest $site-$environment --field=hash --captain-id=$CAPTAIN_ID )
plugin_count_before=$( captaincore ssh $site-$environment --command="wp plugin list --format=csv --fields=name --skip-plugins --skip-themes | tail -n +2 | wc -l" --captain-id=$CAPTAIN_ID )
wp eval-file ../lib/local-scripts/site-run-updates.php site=$site environment=$environment updates_enabled=$updates_enabled updates_exclude_themes=$updates_exclude_themes updates_exclude_plugins=$updates_exclude_plugins
captaincore quicksave generate $site-$environment --captain-id=$CAPTAIN_ID
quicksave_id_after=$( captaincore quicksave latest $site-$environment --field=hash --captain-id=$CAPTAIN_ID )
plugin_count_after=$( captaincore ssh $site-$environment --command="wp plugin list --format=csv --fields=name --skip-plugins --skip-themes | tail -n +2 | wc -l" --captain-id=$CAPTAIN_ID )
if [[ $plugin_count_before != $plugin_count_after ]]; then
echo "Sending Email"
echo "Updating plugins on $home_url ($site-$environment) resulted in a mismatch count.<br /><br />Before updating there were $plugin_count_before plugins.<br />After updating there were $plugin_count_after plugins." | mutt -e 'set content_type=text/html' -s "Plugin update mismatch ($plugin_count_before:$plugin_count_after) on $home_url" -- $captaincore_admin_email
fi
if [[ $quicksave_id_before != $quicksave_id_after ]]; then
captaincore update-log generate $site-$environment $quicksave_id_before $quicksave_id_after --captain-id=$CAPTAIN_ID
captaincore update-log list-generate $site-$environment --captain-id=$CAPTAIN_ID
fi
if [ -f "${path}/process-${process_id}-progress.log" ]; then
echo -n "." >> ${path}/process-${process_id}-progress.log
fi
}
run_command