Skip to content

Update from ambimax #2

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "tschifftner/magento2-deployscripts",
"name": "ambimax/magento2-deployscripts",
"description": "Collection of scripts for Magento 2 builds and deployments",
"type": "magento2-component",
"version": "100.0.1",
Expand All @@ -11,11 +11,13 @@
"build.sh",
"cleanup.sh",
"deploy.sh",
"install.sh"
"install.sh",
"project_reset.sh",
"n98-magerun2.phar"
],
"authors":[
{
"name": "Tobias Schifftner"
}
]
}
}
48 changes: 47 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,53 @@ mkdir -p ${RELEASEFOLDER}/pub/static &&
chmod -R 774 ${RELEASEFOLDER}/pub/{media,static} &&
chmod -R 775 ${RELEASEFOLDER}/var || { echo "Cannot set permissions for directories" ; exit 1; }

########################################################################################################################
# Systemstorage
########################################################################################################################
echo
echo "Systemstorage"
echo "-------------"
if [[ -n ${SKIPIMPORTFROMSYSTEMSTORAGE} ]] && ${SKIPIMPORTFROMSYSTEMSTORAGE} ; then
echo "Skipping import system storage backup because parameter was set"
else

if [ -z "${MASTER_SYSTEM}" ] ; then
if [ -f "${RELEASEFOLDER}/config/mastersystem.txt" ] ; then
MASTER_SYSTEM=`head -n 1 "${RELEASEFOLDER}/config/mastersystem.txt" | sed "s/,/ /g" | sed "s/\r//"`
else
MASTER_SYSTEM="production"
fi
fi

if [[ " ${MASTER_SYSTEM} " =~ " ${ENVIRONMENT} " ]] ; then
echo "Current environment is the master environment. Skipping import."
else
echo "Current environment is not the master environment. Importing system storage..."

if [ -z "${PROJECT}" ] ; then
if [ ! -f "${RELEASEFOLDER}/config/project.txt" ] ; then error_exit "Could not find project.txt"; fi
PROJECT=`cat ${RELEASEFOLDER}/config/project.txt`
if [ -z "${PROJECT}" ] ; then error_exit "Error reading project name"; fi
fi

# Apply db settings
cd "${RELEASEFOLDER}/" || error_exit "Error while switching to htdocs directory"
if [ ! -f vendor/bin/zettr.phar ]; then
error_exit "Zettr.phar is missing"
fi
vendor/bin/zettr.phar apply --groups db ${ENVIRONMENT} config/settings.csv || error_exit "Error while applying settings"

if [ -z "${SYSTEM_STORAGE_ROOT_PATH}" ] ; then
SYSTEM_STORAGE_ROOT_PATH="/home/projectstorage/${PROJECT}/backup/${MASTER_SYSTEM}"
fi

# Import project storage
if [ -d "${SYSTEM_STORAGE_ROOT_PATH}" ]; then
vendor/bin/project_reset.sh -e ${ENVIRONMENT} -p "${RELEASEFOLDER}/" -s "${SYSTEM_STORAGE_ROOT_PATH}" || error_exit "Error while importing project storage"
fi
fi

fi
########################################################################################################################
# Apply configuration settings
########################################################################################################################
Expand All @@ -82,7 +129,6 @@ echo "Applying settings"
echo "-----------------"
cd "${RELEASEFOLDER}" || { echo "Error while switching to root directory" ; exit 1; }
if [ -f config/settings.csv ]; then
vendor/bin/zettr.phar apply ${ENVIRONMENT} config/settings.csv --groups db || { echo "Error while applying settings" ; exit 1; }
vendor/bin/zettr.phar apply ${ENVIRONMENT} config/settings.csv || { echo "Error while applying settings" ; exit 1; }
else
echo "No config/settings.csv found!"
Expand Down
146 changes: 146 additions & 0 deletions project_reset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/bin/bash -e

# Get absolute path to main directory
ABSPATH=$(cd "${0%/*}" 2>/dev/null; echo "${PWD}/${0##*/}")
SOURCE_DIR=`dirname "${ABSPATH}"`

if [ -a "${SOURCE_DIR}/../../.htaccess" ]; then
PROJECT_WEBROOT="${SOURCE_DIR}/../.."
fi

function usage {
echo "Usage:"
echo "$0 -e <environment> -p <projectWebRootPath> -s <systemStorageRootPath> [-a <awsCliProfile>] [-f]"
echo " -p <projectWebRootPath> Project web root path (htdocs)"
echo " -s <systemStorageRootPath> Systemstorage project root path"
echo " -f If set file will be skipped (database only)"
echo ""
echo "Example:"
echo " -p /var/www/projectname/deploy/htdocs -s /home/projectstorage/projectname/backup/deploy"
exit $1
}

function error_exit {
echo "$1" 1>&2
exit 1
}

function usage_exit {
echo "$1" 1>&2
usage 1
}


# Process options
while getopts 'e:p:s:a:f' OPTION ; do
case "${OPTION}" in
e) ENVIRONMENT="${OPTARG}";;
p) PROJECT_WEBROOT=`echo "${OPTARG}" | sed -e "s/\/*$//" `;; # delete last slash
s) SYSTEMSTORAGEPATH=`echo "${OPTARG}" | sed -e "s/\/*$//" `;; # delete last slash
a) AWSCLIPROFILE=${OPTARG};;
f) SKIPFILES=1;;
\?) echo; usage 1;;
esac
done

if [ ! -d "${PROJECT_WEBROOT}" ] ; then usage_exit "Could not find project root ${PROJECT_WEBROOT}"; fi
if [ ! -f "${PROJECT_WEBROOT}/index.php" ] ; then usage_exit "Invalid ${PROJECT_WEBROOT} (could not find index.php)"; fi

# Checking environment
VALID_ENVIRONMENTS=`head -n 1 "${PROJECT_WEBROOT}/config/settings.csv" | sed "s/^.*DEFAULT,//" | sed "s/,/ /g" | sed "s/\r//"`

if [ -z "${ENVIRONMENT}" ]; then error_exit "ERROR: Please provide an environment code (e.g. -e staging)"; fi
if [[ " ${VALID_ENVIRONMENTS} " =~ " ${ENVIRONMENT} " ]] ; then
echo "Environment: ${ENVIRONMENT}"
else
error_exit "ERROR: Illegal environment code ${ENVIRONMENT}"
fi


function cleanup {
echo "Removing temp dir ${TMPDIR}"
rm -rf "${SYSTEMSTORAGE_LOCAL}"
}

if [[ "${SYSTEMSTORAGEPATH}" =~ ^s3:// ]] ; then

SYSTEMSTORAGE_LOCAL=`mktemp -d`
trap cleanup EXIT

if [ -z "${AWSCLIPROFILE}" ] ; then usage_exit "No awsCliProfile given"; fi
echo "Downloading project storage from S3"
aws --profile ${AWSCLIPROFILE} s3 cp --recursive "${SYSTEMSTORAGEPATH}" "${SYSTEMSTORAGE_LOCAL}" || error_exit "Error while downloading package from S3"
else
SYSTEMSTORAGE_LOCAL=${SYSTEMSTORAGEPATH}
fi

if [ ! -d "${SYSTEMSTORAGE_LOCAL}" ] ; then usage_exit "Could not find project storage root $SYSTEMSTORAGE_LOCAL"; fi
if [ ! -d "${SYSTEMSTORAGE_LOCAL}/database" ] ; then error_exit "Invalid $SYSTEMSTORAGE_LOCAL (could not find database folder)"; fi
if [ ! -f "${SYSTEMSTORAGE_LOCAL}/database/combined_dump.sql.gz" ] ; then error_exit "Invalid $SYSTEMSTORAGE_LOCAL (could not find combined_dump.sql.gz)"; fi
if [ ! -f "${SYSTEMSTORAGE_LOCAL}/database/created.txt" ] ; then error_exit "Invalid $SYSTEMSTORAGE_LOCAL (created.txt)"; fi

if [ -z "${SKIPFILES}" ] ; then
if [ ! -d "${SYSTEMSTORAGE_LOCAL}/files" ] ; then usage_exit "Invalid $SYSTEMSTORAGE_LOCAL (could not find files folder)"; fi
fi


n98="/usr/bin/php -d apc.enable_cli=0 ${SOURCE_DIR}/n98-magerun2.phar --root-dir=${PROJECT_WEBROOT}"


# 1 day
echo "Checking age ..."
MAX_AGE=86400

NOW=`date +%s`


DB_CREATED=`cat ${SYSTEMSTORAGE_LOCAL}/database/created.txt`
AGE_DB=$((NOW-DB_CREATED))
if [ "$AGE_DB" -lt "$MAX_AGE" ] ; then echo "DB age ok (${AGE_DB} sec)" ; else error_exit "Age of the database dump is too old (1 day max)"; fi;


if [ -z "${SKIPFILES}" ] ; then
FILES_CREATED=`cat ${SYSTEMSTORAGE_LOCAL}/files/created.txt`
AGE_FILES=$((NOW-FILES_CREATED))
if [ "$AGE_FILES" -lt "$MAX_AGE" ] ; then echo "Files age ok (${AGE_FILES} sec)"; else error_exit "Age of the files dump is too old (1 day max)"; fi;
fi



# Importing database...
echo "Dropping all tables"
$n98 -q db:drop --tables --force || error_exit "Error while dropping all tables"


echo "Import database dump ${SYSTEMSTORAGE_LOCAL}/database/combined_dump.sql.gz"
$n98 -q db:import --compression=gzip "${SYSTEMSTORAGE_LOCAL}/database/combined_dump.sql.gz" || error_exit "Error while importing dump"

echo
echo "Applying settings"
echo "-----------------"
cd "${PROJECT_WEBROOT}" || error_exit "Error while switching to htdocs directory"
if [ ! -f vendor/aoepeople/zettr/zettr.phar ]; then
error_exit "Zettr.phar is missing"
fi
vendor/aoepeople/zettr/zettr.phar apply ${ENVIRONMENT} config/settings.csv || error_exit "Error while applying settings"
echo


# Importing files...
if [ -z "${SKIPFILES}" ] ; then
echo "Copy media folder"
rsync \
--archive \
--force \
--no-o --no-p --no-g \
--omit-dir-times \
--ignore-errors \
--partial \
--exclude=/catalog/product/cache/ \
--exclude=/tmp/ \
--exclude=.svn/ \
--exclude=*/.svn/ \
--exclude=.git/ \
--exclude=*/.git/ \
"${SYSTEMSTORAGE_LOCAL}/files/" "${PROJECT_WEBROOT}/pub/media/"
fi