This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
cleanup.sh
executable file
·112 lines (101 loc) · 2.64 KB
/
cleanup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
set -e
# The directory this script is in.
REAL_PATH=`readlink -f "${BASH_SOURCE[0]}"`
SCRIPT_DIR=`dirname "$REAL_PATH"`
usage() {
cat $SCRIPT_DIR/README.md |
# Remove ticks and stars.
sed -e "s/[\`|\*]//g"
}
# Parse options.
DRUSH="drush"
WEBROOT=$WORKSPACE
VERBOSE=""
CLONE="table"
while getopts “hi:l:d:u:cvx” OPTION; do
case $OPTION in
h)
usage
exit 1
;;
i)
GHPRID=$OPTARG
;;
l)
WEBROOT=$OPTARG
;;
d)
DRUSH=$OPTARG
;;
u)
URIS=$OPTARG
;;
c)
CLONE="database"
;;
v)
VERBOSE="--verbose"
;;
x)
set -x
;;
?)
usage
exit
;;
esac
done
# Remove the switches we parsed above from the arguments.
shift `expr $OPTIND - 1`
# If we're missing some of these variables, show the usage and throw an error.
if [[ -z $WEBROOT ]] || [[ -z $GHPRID ]]; then
usage
exit 1
fi
# Put drush in verbose mode, if requested, and include our script dir so we have
# access to our custom drush commands.
DRUSH="$DRUSH $VERBOSE --include=$SCRIPT_DIR"
# The docroot of the new Drupal directory.
DOCROOT="$WEBROOT/$GHPRID"
# The real directory of the docroot.
REAL_DOCROOT=`readlink -f "$DOCROOT"`
# The path of the repository.
ACTUAL_PATH=`dirname "$REAL_DOCROOT"`
# The unique prefix to use for just this pull request.
DB_PREFIX="pr_${GHPRID}_"
# The drush options for the Drupal destination site. Eventually, we could open
# this up to allow users to specify a drush site alias, but for now, we'll just
# manually specify the root and uri options.
DESTINATION="--root=$DOCROOT"
# If $URIS is currently empty, attempt to load them from the sa command.
if [[ -z $URIS ]]; then
# Check to make sure drush is working properly, and can access the source site.
URIS=`eval $DRUSH $DESTINATION sa`
# Run this through grep separately, turning off error reporting in case of an
# empty string. TODO: figure out how to do this with bash pattern substitution.
set +e
URIS=`echo "$URIS" | grep -v ^@`
set -e
# If we didn't get any aliases, throw an error and quit.
if [[ -z $URIS ]]; then
echo "No sites found at $DOCROOT."
exit 1
fi
fi
# Delete all prefixed tables.
for URI in $URIS; do
DESTINATION="$DESTINATION --uri=$URI"
if [ "$CLONE" == "database" ]; then
DATABASE=`$DRUSH $DESTINATION status database_name --format=list`
$DRUSH $DESTINATION sqlq "DROP DATABASE $DATABASE"
else
$DRUSH $DESTINATION --yes drop-prefixed-tables $DB_PREFIX
fi
done;
# Remove the symlink.
rm $DOCROOT
echo "Removed the $DOCROOT symlink."
# Remove the repository.
rm -rf $ACTUAL_PATH
echo "Removed $ACTUAL_PATH"