diff --git a/composer.json b/composer.json index d7ee4a7..4a66d03 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ "phpunit/phpunit": "~4.1", "phpdocumentor/phpdocumentor": "~2.0", "squizlabs/php_codesniffer": "~2.0", + "loadsys/cakephp-config-read": "~3.0", "loadsys/loadsys_codesniffer": "~3.0" }, "config": { @@ -24,8 +25,10 @@ "coverage-ensure", "db-backup", "db-credentials", + "db-import", "db-login", "db-sample-data", + "deploy", "docs-generate", "email", "folder-delete-items-older-than-days", @@ -39,7 +42,6 @@ "owner-set", "rename-empty", "tests-run", - "update", "vagrant-exec" ] -} \ No newline at end of file +} diff --git a/db-backup b/db-backup index 7ac4d97..b810f21 100755 --- a/db-backup +++ b/db-backup @@ -65,7 +65,7 @@ try { echo "!! DB credentials are not available. Aborting."; exit(3); } - + ob_start(); // Capture the shebang line from db-credentials. $db = include($credentialsScript); ob_get_clean(); // Flush it. diff --git a/db-credentials b/db-credentials index 768ab7e..4b3a435 100755 --- a/db-credentials +++ b/db-credentials @@ -83,9 +83,7 @@ class DbConfig { */ public function __construct() { $dir = getcwd(); - $injectedCode = 'use Cake\Datasource\ConnectionManager; echo serialize(ConnectionManager::config("default")) . PHP_EOL; exit;'; // This must be on a single line to work with the REPL. - $cmd = "echo '{$injectedCode}' | " - . escapeshellcmd("{$dir}/bin/cake Console -q"); + $cmd = escapeshellcmd("{$dir}/bin/cake ConfigRead.ConfigRead -s Datasources.default"); $response = []; $code = 0; exec($cmd, $response, $code); diff --git a/db-import b/db-import new file mode 100755 index 0000000..ee82e92 --- /dev/null +++ b/db-import @@ -0,0 +1,83 @@ +#!/usr/bin/env php +0 = failure. + * @return void + */ +function usage($script = null, $exitCode = 0) { + $script = $script ?: basename(__FILE__); + $usage = <<getMessage(); + exit(4); + } +} catch (Exception $e) { + echo $e->getMessage(); + exit(1); +} + +// Create an appropriate password clause for the command line. +if (empty($db->password)) { + $passClause = ''; +} else { + $passClause = ' --password=' . escapeshellcmd($db->password); +} + +// Build the command to execute. +$importCmd = "mysql --host={$db->host} --user={$db->username} -p{$db->password} {$db->database} < {$dir}/{$sourceFile}"; + +// Execute the commands. +echo "## Importing file '{$sourceFile}'." . PHP_EOL; +system($importCmd); +echo "## File {$sourceFile} has been imported." . PHP_EOL; diff --git a/update b/deploy similarity index 70% rename from update rename to deploy index b382d4e..9250a83 100755 --- a/update +++ b/deploy @@ -14,17 +14,14 @@ ${0##*/} project root folder. Usage: - bin/${0##*/} [tag|branch|sha|ref] [dev|prod|staging] [user] [group] + bin/${0##*/} [tag|branch|sha|ref] [dev|prod|staging] \$1 = The new commit to which to update. Will be merged into the current branch/commit. - Default: (The remote tracking branch for the currently-checked-out branch.) + Default: (The remote tracking branch for the + currently-checked-out branch.) \$2 = The local config file "environment" to use. - Default: $APP_ENV if set, 'prod' if not. - \$3 = The local user to associate the project with. - Default: owner of ./ - \$4 = The local group to associate the project with. - Default: group of ./ + Default: \$APP_ENV if set, 'prod' if not. Environment Variables: APP_ENV = If set and \$2 is blank, will be used as the active @@ -46,26 +43,18 @@ echo "## Starting update process." # Set variables. DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )" BIN_DIR="$DIR/bin" -CONFIG_DIR="$DIR/Config" -MIGRATION_DIR="$CONFIG_DIR/Migration" +CONFIG_DIR="$DIR/config" +MIGRATION_DIR="$CONFIG_DIR/Migrations" SLEEP_TIME=10 -DEFAULT_DBUPDATES_FILE="$CONFIG_DIR/sql/db_updates.sql" -DEFAULT_ENVVARS_FILE="$CONFIG_DIR/env_vars.txt" EXISTINGCOMMIT=$( git rev-parse HEAD ) EXISTINGBRANCH=$( git rev-parse --abbrev-ref HEAD ) if [ -d "$MIGRATION_DIR" ] && [ "$(ls -A $MIGRATION_DIR)" ]; then DB_UPDATES_METHOD='migrations' -elif [ -e "$DEFAULT_DBUPDATES_FILE" ]; then - DB_UPDATES_METHOD='db_updates' else DB_UPDATES_METHOD='none' fi -if [ -r $DEFAULT_ENVVARS_FILE ]; then - source "${DEFAULT_ENVVARS_FILE}" -fi - # Input processing. if [ $1 ]; then @@ -83,18 +72,6 @@ else ARG_APPENV="prod" fi -if [ $3 ]; then - ARG_OWNER=$3 -else - ARG_OWNER="" -fi - -if [ $4 ]; then - ARG_GROUP=$4 -else - ARG_GROUP="" -fi - # Check for local changes that would cause a merge (without -f) to fail later. $BIN_DIR/git-localchanges @@ -128,22 +105,6 @@ git fetch origin refs/heads/*:refs/remotes/origin/* +refs/tags/*:refs/tags/* # Apply database updates, one way or another. if [ "$DB_UPDATES_METHOD" = 'migrations' ]; then echo "## Deferring database migrations until after merge." -elif [ "$DB_UPDATES_METHOD" = 'db_updates' ]; then - # Show manual pending DB updates to user. (Or use possibly use bin/db-apply-updates here to execute the SQL against the DB automatically, - # which should be fairly recoverable if it's wrong [which it can be] considering we have already made a backup.) - DB_CHANGES="$( $BIN_DIR/db-show-updates $EXISTINGCOMMIT $ARG_NEWCOMMIT)" - if [ -n "$DB_CHANGES" ]; then - echo "## There are database updates to apply!" - echo '---------' - echo '' - echo "$DB_CHANGES" - echo '' - echo '---------' - echo '' - read -p "Press [ENTER] to continue when these updates have been applied to the local DB." - else - echo "## No database updates to apply."; - fi elif [ "$DB_UPDATES_METHOD" = 'none' ]; then echo "## Could not automatically determine database change tracking system." read -p "## Make sure any pending DB changes have been made and press [ENTER] to continue." @@ -170,24 +131,13 @@ if [ "$DB_UPDATES_METHOD" = 'migrations' ]; then $BIN_DIR/migrations fi -# Copy the correct config files into place for this environment. -$BIN_DIR/set-configs $ARG_APPENV - # Make sure any external packages are installed and available. $BIN_DIR/deps-install # Purge cache folders. -$BIN_DIR/clear-cache - - -# Set file ownership. -$BIN_DIR/set-owner ${ARG_OWNER} ${ARG_GROUP} - - -# Set write permissions. -$BIN_DIR/writedirs +$BIN_DIR/cache-clear # Send an email report if the ENV var is set in Config/env_vars.txt @@ -198,6 +148,10 @@ if [ -n "${LOADSYS_EMAIL}" ]; then cat > "$MSG_BODY" <<-EOD A code push has been completed for ${LOADSYS_PROJECT_NAME}. + (Previous commit: ${EXISTINGCOMMIT}) + + ------------------------------------------------------- + ${GIT_LOG} EOD diff --git a/deps-install b/deps-install new file mode 100755 index 0000000..e55c68f --- /dev/null +++ b/deps-install @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +#--------------------------------------------------------------------- +usage () +{ + cat </dev/null 2>&1 && pwd )" +BINDIR="${DIR}/bin" +COMPOSER_CONFIG_FILE="$DIR/composer.json" +NPM_CONFIG_FILE="$DIR/package.json" +COMPOSER_NO_DEV_OPTION="--no-dev" +NPM_RUN=false +NPM_PRODUCTION_OPTION="--production" + +if [ "$1" = 'dev' ]; then + COMPOSER_NO_DEV_OPTION="" + NPM_PRODUCTION_OPTION="" + NPM_RUN=true +fi + +echo "## Running installs for all dependency management tools found."; + +# Install composer packages using versions specified in config/lock file. +if [ -e "$COMPOSER_CONFIG_FILE" ]; then + COMPOSER="$( which composer )" + if [ $? -gt 0 ]; then + echo "!! Found composer config file '$COMPOSER_CONFIG_FILE', but composer is not present on this system." + exit 2 + else + echo "## Found composer at: ${COMPOSER}" + "$COMPOSER" install --no-interaction $COMPOSER_NO_DEV_OPTION + "$COMPOSER" dumpautoload --optimize + fi +fi + +# Install npm modules using versions specified in config file. +if [ $NPM_RUN ] && [ -e "$NPM_CONFIG_FILE" ]; then + NPM="$( which npm )" + if [ $? -gt 0 ]; then + echo "!! Found npm config file '$NPM_CONFIG_FILE', but npm is not present on this system." + exit 3 + else + echo "## Found npm at: ${NPM}" + "$NPM" install $NPM_PRODUCTION_OPTION + fi +fi diff --git a/migrations b/migrations index f9f0f45..775f82d 100755 --- a/migrations +++ b/migrations @@ -30,7 +30,7 @@ DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )" if [ "$1" ]; then OPTION=$1; else - OPTION="run all"; + OPTION="migrate"; fi if [ "$2" ]; then @@ -47,5 +47,5 @@ else fi $DIR/bin/cache-clear -$DIR/bin/cake Migrations.migration $OPTION $PLUGIN +$DIR/bin/cake Migrations.migrations $OPTION $PLUGIN $DIR/bin/cache-clear