Skip to content

Instantly share code, notes, and snippets.

@devkinetic
Created April 30, 2019 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devkinetic/7055409f86c75c5939e17211624b77a9 to your computer and use it in GitHub Desktop.
Save devkinetic/7055409f86c75c5939e17211624b77a9 to your computer and use it in GitHub Desktop.
Migration to tput support
#!/bin/bash
if [ -t 1 ] && command -v tput > /dev/null; then
# see if it supports colors
ncolors=$(tput colors);
if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
bold="$(tput bold || echo)";
normal="$(tput sgr0 || echo)";
black="$(tput setaf 0 || echo)";
red="$(tput setaf 1 || echo)";
red_bg="$(tput setab 1 || echo)";
green="$(tput setaf 2 || echo)";
yellow="$(tput setaf 3 || echo)";
blue="$(tput setaf 4 || echo)";
magenta="$(tput setaf 5 || echo)";
cyan="$(tput setaf 6 || echo)";
white="$(tput setaf 7 || echo)";
orange_bg="$(tput setab 214 || echo)";
fi
fi
say() {
printf "%b\n" "${normal:-} $1" >&3
}
say_announce() {
printf "%b\n" "${blue:-}$1${normal:-}" >&3
}
say_guide() {
printf "%b\n" "${orange_bg:-}${black:-}$1${normal:-}" >&3
}
say_action() {
printf "%b\n" "${green:-}$1${normal:-}" >&3
}
say_err() {
printf "%b\n" "${red_bg:-}${white:-}Error: $1${normal:-}" >&2
}
function clear_stdin(){
while read -e -t 1; do : ; done
}
BASEDIR=$(pwd);
ERRORLOG=${BASEDIR}/.update-error-log;
DRUPALPATH='docroot';
if [ -d "${BASEDIR}/web" ]; then
DRUPALPATH='web';
fi
DRUSH="drush";
clear;
echo "${blue:-}";
cat << "EOF"
__ __ _______ ______ __ __ _______
| | | || _ || _ | | | | || |
| |_| || |_| || | || | | | || _ |
| || || |_||_ ___ | |_| || |_| |
| || || __ | |___| | || ___|
| | | _ || | | | | || |
|___| |__| |__||___| |_| |_______||___|
EOF
echo "${normal}";
say_announce "Varbase Updater";
clear_stdin;
say "Please choose your Drupal installation folder. Type the folder name or hit enter to choose the default one: ($DRUPALPATH): ";
read drupalfolder;
if [ "$drupalfolder" ] ;then
DRUPALPATH=$drupalfolder;
fi;
backup () {
cd ${BASEDIR};
rm -rf ${BASEDIR}/update_backups;
mkdir -p ${BASEDIR}/update_backups
tar --exclude='sites' -zcf ./update_backups/${DRUPALPATH}.tgz ./${DRUPALPATH}
tar -zcf ./update_backups/vendor.tgz ./vendor
cp ${BASEDIR}/composer.json ${BASEDIR}/update_backups/composer.json;
cd ${BASEDIR}/${DRUPALPATH};
${DRUSH} sql-dump --result-file=${BASEDIR}/update_backups/db.sql 1> >(tee -a ${ERRORLOG} >&1) 2> >(tee -a ${ERRORLOG} >&2);
result="$?";
if [ "$result" -ne 0 ]; then
say_err "Error in creating a backup, exiting update process! Please check ${ERRORLOG} for more info.";
cd ${BASEDIR};
exit;
fi
cd ${BASEDIR};
}
backup_skipped_modules () {
if [ -f ${BASEDIR}/vendor/vardot/varbase-updater/config/.skip-update ]; then
mkdir -p ${BASEDIR}/update_backups/skip
while read p; do
if [ -d "${BASEDIR}/${DRUPALPATH}/modules/contrib/${p}" ]; then
cp -r ${BASEDIR}/${DRUPALPATH}/modules/contrib/${p} ${BASEDIR}/update_backups/skip/;
fi
done < ${BASEDIR}/vendor/vardot/varbase-updater/config/.skip-update
fi
}
revert_backup () {
cd ${BASEDIR}
rm -rf mv ${BASEDIR}/update_backups/sites
mv ${BASEDIR}/${DRUPALPATH}/sites ${BASEDIR}/update_backups/
rm -rf ${BASEDIR}/${DRUPALPATH}
tar -xf ./update_backups/${DRUPALPATH}.tgz
mv ${BASEDIR}/update_backups/sites ${BASEDIR}/${DRUPALPATH}/
rm -rf ${BASEDIR}/vendor
tar -xf ./update_backups/vendor.tgz
cp ${BASEDIR}/update_backups/composer.json ${BASEDIR}/composer.json;
cd ${BASEDIR}/${DRUPALPATH};
$DRUSH sql-drop --yes 1> >(tee -a ${ERRORLOG} >&1) 2> >(tee -a ${ERRORLOG} >&2);
$DRUSH sql-cli < ${BASEDIR}/update_backups/db.sql --yes 1> >(tee -a ${ERRORLOG} >&1) 2> >(tee -a ${ERRORLOG} >&2);
result="$?";
if [ "$result" -ne 0 ]; then
say_err "Failed to restore the backup. Please check ${ERRORLOG} for more info. You can find the backup to restore it manually in ${BASEDIR}/update_backups";
exit;
fi
cd ${BASEDIR};
rm -rf ${BASEDIR}/update_backups;
}
exit_and_revert(){
clear_stdin;
if [ -d ${BASEDIR}/update_backups ]; then
say "Would you like to abort the update process and restore the backup? (no): ";
else
say "Would you like to abort the update process? (no): ";
fi
read answer </dev/tty;
if [ "$answer" != "${answer#[Yy]}" ] ;then
if [ -d ${BASEDIR}/update_backups ]; then
say_announce "Going back in time and restoring the snapshot before the update process!";
revert_backup;
exit;
else
say_announce "Mission aborted.";
exit;
fi
fi
}
cleanup(){
if [ -d ${BASEDIR}/vendor/drupal-composer/drupal-scaffold ]; then
rm -rf ${BASEDIR}/vendor/drupal-composer/drupal-scaffold;
fi
composer varbase-version-check composer-patches;
result="$?";
if [ "$result" -ne 0 ]; then
if [ -d ${BASEDIR}/vendor/cweagans/composer-patches ]; then
rm -rf ${BASEDIR}/vendor/cweagans/composer-patches;
fi
fi
if [ -d ${BASEDIR}/${DRUPALPATH}/vendor ]; then
rm -rf ${BASEDIR}/${DRUPALPATH}/vendor;
fi
if [ -f ${BASEDIR}/${DRUPALPATH}/composer.json ]; then
rm -rf ${BASEDIR}/${DRUPALPATH}/composer.json;
fi
if [ -f ${BASEDIR}/${DRUPALPATH}/composer.lock ]; then
rm -rf ${BASEDIR}/${DRUPALPATH}/composer.lock;
fi
if [ -f ${BASEDIR}/scripts/composer/ScriptHandler.php ]; then
rm -rf ${BASEDIR}/scripts/composer/ScriptHandler.php;
fi
if [ -f ${BASEDIR}/vendor/vardot/varbase-updater/config/.download-before-update ]; then
rm -rf ${BASEDIR}/vendor/vardot/varbase-updater/config/.download-before-update;
fi
chmod -R gu+rwx ${BASEDIR}/${DRUPALPATH};
composer dump-autoload;
}
download_before_update(){
if [ -f ${BASEDIR}/vendor/vardot/varbase-updater/config/.download-before-update ]; then
while read p; do
say_action "Downloading $p.";
say_action "Downloading $p." >> ${ERRORLOG};
$DRUSH up $p --pm-force --yes --strict=0 1> >(tee -a ${ERRORLOG} >&1) 2> >(tee -a ${ERRORLOG} >&2);
result="$?";
if [ "$result" -ne 0 ]; then
say_err "Error while downloading $p. Please check ${ERRORLOG} for more info.";
exit_and_revert;
fi
done < ${BASEDIR}/vendor/vardot/varbase-updater/config/.download-before-update
fi
}
copy_after_update(){
if [ -f ${BASEDIR}/vendor/vardot/varbase-updater/config/.skip-update ]; then
while read p; do
if [ -d "${BASEDIR}/update_backups/skip/${p}" ]; then
cp -r ${BASEDIR}/update_backups/skip/${p} ${BASEDIR}/${DRUPALPATH}/modules/contrib/;
fi
done < ${BASEDIR}/vendor/vardot/varbase-updater/config/.skip-update
fi
}
enable_after_update(){
if [ -f ${BASEDIR}/vendor/vardot/varbase-updater/config/.enable-after-update ]; then
while read p; do
$DRUSH en $p --yes --strict=0 1> >(tee -a ${ERRORLOG} >&1) 2> >(tee -a ${ERRORLOG} >&2);
result="$?";
if [ "$result" -ne 0 ]; then
say_err "Error while enabling $p. Please check ${ERRORLOG} for more info.";
exit_and_revert;
fi
done < ${BASEDIR}/vendor/vardot/varbase-updater/config/.enable-after-update
fi
}
echo "${green:-}";
composer varbase-version-check current-message;
say "";
say_announce "This command will guide you to update your Varbase project.";
say "";
say_guide "The update process will go through several tasks to update your Drupal core and modules. Please run this script on a development environment.";
say "";
say_action "The command will go through the following steps:";
say_action " \t 1. Backup your current installation (code and database)";
say_action " \t 2. Cleanup and update your composer.json to prepare for Varbase updates";
say_action " \t 3. Update Varbase using (composer update)";
say_action " \t 4. Enable some required modules before running Drupal database updates";
say_action " \t 5. Update Drupal database for latest changes (drush updatedb)";
say_action " \t 6. Write log files and perform some cleanups";
say "";
say_guide "The update process will go through several tasks to update your Drupal core and modules. Please run this script on a development environment.";
clear_stdin;
if [ -d ${BASEDIR}/update_backups ]; then
say_action "What would you like to do?:";
say "- (u|update) To start the update process (default).";
say "- (r|revert) To revert the previews backup.";
say "- (e|exit) To exit.";
else
say_action "Do you want to start the update process? (yes): ";
fi
read answer;
answer=${answer:-Yes}
if [ "$answer" != "${answer#[NnEe]}" ] ;then
say_action "Mission aborted.";
exit;
elif [ "$answer" != "${answer#[Rr]}" ] ; then
if [ -d ${BASEDIR}/update_backups ]; then
say_action "Reverting backup.";
revert_backup;
exit;
else
say_err "Sorry there is no backup to revert!";
exit;
fi
elif [ "$answer" != "${answer#[YyUu]}" ] ; then
touch ${ERRORLOG};
echo > ${ERRORLOG};
clear_stdin;
say_action "Do you want to create a backup snapshot before starting the update process? (yes):";
read answer;
answer=${answer:-Yes}
if [ "$answer" != "${answer#[Yy]}" ] ;then
say_action "Preparing a backup snapshot before performing updates...";
backup;
else
say_action "Backup snapshot skipped...";
rm -rf ${BASEDIR}/update_backups;
fi
say_action "Preparing composer.json for Varbase updates...";
say_action "Preparing composer.json for Varbase updates..." >> ${ERRORLOG};
cleanup;
composer varbase-refactor-composer ${BASEDIR}/composer.new.json ${DRUPALPATH};
result="$?";
if [ "$result" -ne 0 ]; then
say_err "There was an error while preparing composer.json for Varbase updates. Please check ${ERRORLOG} for more information.";
say_err "If you are running Varbase 8.x-4.x or 8.x-5.x version, make sure to update varbase-project using the update command:";
say_action "composer require vardot/varbase-updater";
exit_and_revert;
fi
backup_skipped_modules;
mv ${BASEDIR}/composer.new.json ${BASEDIR}/composer.json;
clear_stdin;
say_announce "composer.json has been updated. Now is your chance to perform any manual changes. Please do your changes (if any) then press enter to continue... ";
read answer;
say_action "Updating Varbase...";
say_action "Updating Varbase..." >> ${ERRORLOG};
composer update 1> >(tee -a ${ERRORLOG} >&1) 2> >(tee -a ${ERRORLOG} >&2);
result="$?";
if [ "$result" -ne 0 ]; then
say_err "There was an error while updating Varbase to the latest version. Please check ${ERRORLOG} for more information.";
exit_and_revert;
fi
if [ -f ${BASEDIR}/failed-patches.txt ]; then
say_action "Log of all failed patches has been created, please check failed-patches.txt after the update process finishes...";
fi
copy_after_update;
cd ${BASEDIR}/${DRUPALPATH};
$DRUSH cr --strict=0 1> >(tee -a ${ERRORLOG} >&1) 2> >(tee -a ${ERRORLOG} >&2);
result="$?";
if [ "$result" -ne 0 ]; then
say_err "Something went wrong while rebuilding the cache (drush cr), this might cause the update to fail.";
exit_and_revert;
fi
say_action "Enabling new required modules for the latest Varbase version...";
say_action "Enabling new required modules for the latest Varbase version..." >> ${ERRORLOG};
enable_after_update;
say_action "Updating the database for latest changes.";
say_action "Updating the database for latest changes." >> ${ERRORLOG};
$DRUSH updb --yes --strict=0 1> >(tee -a ${ERRORLOG} >&1) 2> >(tee -a ${ERRORLOG} >&2);
result="$?";
if [ "$result" -ne 0 ]; then
say_err "There was an error while updating Drupal core. Please check ${ERRORLOG} for more information.";
exit_and_revert;
fi
if [ -f ${BASEDIR}/vendor/vardot/varbase-updater/config/.skip-update ]; then
rm -rf ${BASEDIR}/vendor/vardot/varbase-updater/config/.skip-update;
fi
if [ -f ${BASEDIR}/vendor/vardot/varbase-updater/config/.enable-after-update ]; then
rm -rf ${BASEDIR}/vendor/vardot/varbase-updater/config/.enable-after-update;
fi
cd ${BASEDIR};
say_action "Hoya! Updates are now done. We will add a link in the near future for here to link to common issues appearing after updates and how to fix them.";
say_action "Hoya! Updates are now done. We will add a link in the near future for here to link to common issues appearing after updates and how to fix them." >> ${ERRORLOG};
composer varbase-version-check next-message;
else
say_err "Unrecognized option, exiting...";
exit;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment