Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Sansbury committed Nov 29, 2012
0 parents commit a6bd90f
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.DS_Store*
ehthumbs.db
Thumbs.db
45 changes: 45 additions & 0 deletions copy_tables.php
@@ -0,0 +1,45 @@
<?php

$ignored = drush_shift();
$prefix = $dt_args['@prefix'] = drush_shift();

if (!$ignored) {
drush_set_error('NO_IGNORE_PREFIX', dt('You must specify the prefix for tables to ignore.'));
return;
}
if (!$prefix) {
drush_set_error('NO_DB_PREFIX', dt('You must specify a database prefix.'));
return;
}

$creds = drush_get_context('DRUSH_DB_CREDENTIALS');
$db_name = $creds['name'];

$sql = "SHOW TABLES WHERE tables_in_$db_name NOT LIKE '$ignored%'";
$tables = db_query($sql)->fetchCol();

if (empty($tables)) {
drush_set_error('NO_TABLES', dt('There were no database tables to clone.'));
return;
}

try {
foreach ($tables as $table) {
$new_table_name = "$prefix$table";
$dt_args['@new-table'] = $new_table_name;
$dt_args['@table'] = $table;
drush_log(dt('Dropping table @new-table.', $dt_args));
db_drop_table($new_table_name);
drush_log(dt('Creating table @new-table from @table.', $dt_args));
db_query("CREATE TABLE $new_table_name LIKE $table");
drush_log(dt('Copying data from @table to @new-table.', $dt_args));
db_query("INSERT INTO $new_table_name SELECT * FROM $table");
$dt_args['@successes']++;
}
}
catch (Exception $e) {
drush_set_error('EXCEPTION', (string) $e);
}

drush_log(dt('Cloned @successes database tables, prefixing with @prefix.', $dt_args), 'completed');

142 changes: 142 additions & 0 deletions pull_request_builder.sh
@@ -0,0 +1,142 @@
#!/usr/bin/env bash
set -e

usage()
{
cat << EOF
usage: $0 options
This script should be executed within Jenkins, and will fail otherwise.
WHAT DOES IT DO?
- Moves the checked out repository to a unique directory in the workspace
REQUIREMENTS:
- Drush
- A web accessible URL for the pull request. The location of the docroot for
this URL should be specified with the -l option.
- An existing Drupal 7 site with a site alias, and empty prefix line in the
database array in settings.php
- A Jenkins job that checks out the Pull Request to 'new_pull_request' directory
inside the job workspace.
OPTIONS:
-h Show this message
-l Location of the docroot where the site will be symlinked to. Note,
the Jenkins user must have write permissions to this directory.
-d Defaults to 'http://default'. The domain name the site will be set up
at. Note, the site will be in a subdirectory of this domain using the
Pull Request ID, so if the Pull Request ID is 234, and you pass
https://www.example.com/foo, The site will be at
https://www.example.com/foo/234.
-a The drush site alias for the source site to copy from.
-r The path to drush. Defaults to drush.
-v Verbose mode, passed to all drush commands.
EOF
}

DOCROOT=
DOMAIN="http://default"
ALIAS=
DRUSH="drush"
VERBOSE=""

while getopts “hl:d:a:r:v” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
l)
DOCROOT=$OPTARG
;;
d)
DOMAIN=$OPTARG
;;
a)
ALIAS=$OPTARG
;;
r)
DRUSH=$OPTARG
;;
v)
VERBOSE="--verbose"
;;
?)
usage
exit
;;
esac
done

# If we're missing some of these variables, show the usage and throw an error.
if [[ -z $DOCROOT ]] || [[ -z $ALIAS ]]; then
usage
exit 1
fi

if [[ -z $sha1 ]] || [[ -z $WORKSPACE ]]; then
echo "This script must be executed from within a proper Jenkins job."
exit 1
fi

DRUSH="$DRUSH $VERBOSE"
# Pull out the Pull Request ID from the origin.
GHPRID=`echo $sha1 | grep -o '[0-9]'`
# This is the directory of the checked out pull request, from Jenkins.
ORIGINAL_DIR="${WORKSPACE}/new_pull_request"
# The directory where the checked out pull request will reside.
ACTUAL_DIR="${WORKSPACE}/${GHPRID}-actual"
# The command will attempt to merge master with the pull request.
BRANCH="pull-request-$GHPRID"

# Check to make sure drush is working.
$DRUSH $ALIAS status --quiet

SETTINGS="`$DRUSH $ALIAS dd`/`$DRUSH $ALIAS status site_path --pipe`/settings.php"
SETTINGS_DIR=`echo $DOMAIN |
# Remove http:// or https:// from the beginning.
sed -e "s/https\?:\/\///" |
# Replace all dots or slashes with underscores.
sed -e "s/[\.|\/]/_/g" |
# Remove trailing new lines.
tr -d '\n'`
NEW_SETTINGS="${DOCROOT}/sites/${SETTINGS_DIR}/settings.php"
PREFIX="pr_"
DB_PREFIX="${PREFIX}${GHPRID}_"
DB_NAME="`$DRUSH $ALIAS status database_name --pipe`"

# Remove the existing .git dir if it exists.
rm -rf $ACTUAL_DIR/.git
# Copy the new_pull_request directory.
rsync -a ${ORIGINAL_DIR}/ $ACTUAL_DIR
# Now remove the new_pull_request directory.
rm -rf $ORIGINAL_DIR
# Create a symlink to the docroot.
ln -sf $ACTUAL_DIR/docroot $DOCROOT

# Copy the existing settings.php to the new site, but add a database prefix.
cat $SETTINGS |
# Sets the database prefix to the prefix line starting only with whitespace.
sed -e "s/^\(\s*\)'prefix'\ =>\ '',/\1'prefix'\ =>\ '$DB_PREFIX',/" \
# Then saves the settings.php file to the new location.
> $NEW_SETTINGS

echo "Copied $SETTINGS to $NEW_SETTINGS"

# Copy all the database tables, using the new prefix.
$DRUSH $ALIAS scr ${WORKSPACE}/copy_tables.php $PREFIX $DB_PREFIX

cd $ACTUAL_DIR
git checkout -b $BRANCH
git checkout master
git pull
git merge $BRANCH -m "Jenkins test merge into master."

echo "Checked out a new branch for this pull request, and merged it to master."

# Rsync the files over as well.
cd $DOCROOT
$DRUSH -y rsync $ALIAS:%files @self:%files
32 changes: 32 additions & 0 deletions remove_tables.php
@@ -0,0 +1,32 @@
<?php

$prefix = $dt_args['@prefix'] = drush_shift();

if (!$prefix) {
drush_set_error('NO_DB_PREFIX', dt('You must specify a database prefix.'));
return FALSE;
}

$creds = drush_get_context('DRUSH_DB_CREDENTIALS');
$db_name = $creds['name'];

$sql = "SHOW TABLES LIKE :prefix";
$tables = db_query($sql, array(':prefix' => "$prefix%"))->fetchCol();

if (empty($tables)) {
drush_log(dt('There were no database tables to remove.'), 'status');
return;
}

dlm($tables);

try {
array_walk($tables, 'db_drop_table');
}
catch (Exception $e) {
drush_set_error('EXCEPTION', (string) $e);
}

$dt_args['@count'] = count($tables);
drush_log(dt('Deleted @count database tables with prefix @prefix.', $dt_args), 'completed');

0 comments on commit a6bd90f

Please sign in to comment.