Skip to content

Commit cf97de2

Browse files
committed
consolidate rebuild script
1 parent 2163b44 commit cf97de2

4 files changed

Lines changed: 334 additions & 2 deletions

File tree

local/bin/db-rebuild.sh

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#!/bin/bash
2+
3+
# Set the things
4+
HOST=${DB_HOST:-${MYSQL_HOST:-127.0.0.1}}
5+
USER=${DB_USER:-${MYSQL_USER:-root}}
6+
PASSWORD=${DB_PASSWORD:-${MYSQL_PASSWORD:-}}
7+
DATABASE=${DB_NAME:-${MYSQL_DATABASE:-database}}
8+
PORT=${DB_PORT:-${MYSQL_TCP_PORT:-3306}}
9+
WEBROOT=${LANDO_WEBROOT:-$(dirname $(dirname $(realpath $0)))}
10+
DRUSHTASK=${DRUSH_TASK:-deploy}
11+
TERMINUSENV=${TERMINUS_ENV:-dev}
12+
13+
# PARSE THE ARGZZ
14+
# TODO: compress the mostly duplicate code below?
15+
while (( "$#" )); do
16+
case "$1" in
17+
-h|--host|--host=*)
18+
if [ "${1##--host=}" != "$1" ]; then
19+
HOST="${1##--host=}"
20+
shift
21+
else
22+
HOST=$2
23+
shift 2
24+
fi
25+
;;
26+
-u|--user|--user=*)
27+
if [ "${1##--user=}" != "$1" ]; then
28+
USER="${1##--user=}"
29+
shift
30+
else
31+
USER=$2
32+
shift 2
33+
fi
34+
;;
35+
-p|--password|--password=*)
36+
if [ "${1##--password=}" != "$1" ]; then
37+
PASSWORD="${1##--password=}"
38+
shift
39+
else
40+
PASSWORD=$2
41+
shift 2
42+
fi
43+
;;
44+
-d|--database|--database=*)
45+
if [ "${1##--database=}" != "$1" ]; then
46+
DATABASE="${1##--database=}"
47+
shift
48+
else
49+
DATABASE=$2
50+
shift 2
51+
fi
52+
;;
53+
-P|--port|--port=*)
54+
if [ "${1##--port=}" != "$1" ]; then
55+
PORT="${1##--port=}"
56+
shift
57+
else
58+
PORT=$2
59+
shift 2
60+
fi
61+
;;
62+
-D|--drush-task|--drush-task=*)
63+
if [ "${1##--drush-task=}" != "$1" ]; then
64+
DRUSHTASK="${1##--drush-task=}"
65+
shift
66+
else
67+
DRUSHTASK=$2
68+
shift 2
69+
fi
70+
;;
71+
--)
72+
shift
73+
break
74+
;;
75+
-*|--*=)
76+
echo "Error: Unsupported flag $1" >&2
77+
exit 1
78+
;;
79+
*)
80+
FILE="$(pwd)/$1"
81+
shift
82+
;;
83+
esac
84+
done
85+
echo "$FILE"
86+
# Test if FILE is set if not download pantheon's production db to temp file.
87+
if [ -z "$FILE" ]; then
88+
# generate temp file
89+
FILE=$(mktemp) || exit 1
90+
# grab latest production db backup.
91+
curl `terminus backup:get --element=db myeap2.$TERMINUSENV` --output $FILE
92+
function cleanup {
93+
rm "$FILE"
94+
}
95+
trap cleanup EXIT
96+
fi
97+
98+
# Set positional arguments in their proper place
99+
eval set -- "$FILE"
100+
CMD="$FILE"
101+
PV=""
102+
103+
# Validate we have a file
104+
if [ ! -f "$FILE" ]; then
105+
echo "File $FILE not found!"
106+
exit 1;
107+
fi
108+
109+
# reset db back to default repo state
110+
echo "Dropping and re-creating $DATABASE @ $HOST:$PORT as $USER..."
111+
if [ ! -z "$PASSWORD" ]; then
112+
eval "mysqladmin -h$HOST -u$USER -p$PASSWORD drop $DATABASE -f"
113+
eval "mysqladmin -h$HOST -u$USER -p$PASSWORD create $DATABASE -f"
114+
else
115+
eval "mysqladmin -h$HOST -u$USER drop $DATABASE -f"
116+
eval "mysqladmin -h$HOST -u$USER create $DATABASE -f"
117+
fi
118+
119+
# Inform the user of things
120+
echo "Preparing to import $FILE into $DATABASE on $HOST:$PORT as $USER..."
121+
122+
# Check to see if we have any unzipping options or GUI needs
123+
if command -v gunzip >/dev/null 2>&1 && gunzip -t $FILE >/dev/null 2>&1; then
124+
echo "Gunzipped file detected!"
125+
if command -v pv >/dev/null 2>&1; then
126+
CMD="pv $CMD"
127+
else
128+
CMD="cat $CMD"
129+
fi
130+
CMD="$CMD | gunzip"
131+
elif command -v unzip >/dev/null 2>&1 && unzip -t $FILE >/dev/null 2>&1; then
132+
echo "Zipped file detected!"
133+
CMD="unzip -p $CMD"
134+
if command -v pv >/dev/null 2>&1; then
135+
CMD="$CMD | pv"
136+
fi
137+
else
138+
if command -v pv >/dev/null 2>&1; then
139+
CMD="pv $CMD"
140+
else
141+
CMD="cat $CMD"
142+
fi
143+
fi
144+
145+
# Put the pieces together
146+
CMD="$CMD | mysql -h $HOST -P $PORT --protocol tcp -u $USER"
147+
if [ ! -z "$PASSWORD" ]; then
148+
CMD="$CMD -p$PASSWORD $DATABASE"
149+
else
150+
CMD="$CMD $DATABASE"
151+
fi
152+
153+
# Import
154+
echo "Importing $FILE..."
155+
eval "$CMD"
156+
echo "Import completed with status code $?"
157+
158+
# update db with latest code/config changes
159+
echo "Running drush:$DRUSHTASK to update DB with latest configs and baseline migrations."
160+
eval "cd $WEBROOT && drush $DRUSHTASK"

local/bin/templater.sh

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#!/bin/bash
2+
#
3+
# Very simple templating system that replaces {{VAR}} by the value of $VAR.
4+
# Supports default values by writting {{VAR=value}} in the template.
5+
#
6+
# Copyright (c) 2017 Sébastien Lavoie
7+
# Copyright (c) 2017 Johan Haleby
8+
#
9+
# Permission is hereby granted, free of charge, to any person obtaining a copy
10+
# of this software and associated documentation files (the "Software"), to deal
11+
# in the Software without restriction, including without limitation the rights
12+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
# copies of the Software, and to permit persons to whom the Software is
14+
# furnished to do so, subject to the following conditions:
15+
#
16+
# The above copyright notice and this permission notice shall be included in all
17+
# copies or substantial portions of the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
# SOFTWARE.
26+
#
27+
# See: https://github.com/johanhaleby/bash-templater
28+
# Version: https://github.com/johanhaleby/bash-templater/commit/5ac655d554238ac70b08ee4361d699ea9954c941
29+
30+
# Replaces all {{VAR}} by the $VAR value in a template file and outputs it
31+
32+
readonly PROGNAME=$(basename $0)
33+
34+
config_file="<none>"
35+
print_only="false"
36+
silent="false"
37+
38+
usage="${PROGNAME} [-h] [-d] [-f] [-s] --
39+
40+
where:
41+
-h, --help
42+
Show this help text
43+
-p, --print
44+
Don't do anything, just print the result of the variable expansion(s)
45+
-f, --file
46+
Specify a file to read variables from
47+
-s, --silent
48+
Don't print warning messages (for example if no variables are found)
49+
50+
examples:
51+
VAR1=Something VAR2=1.2.3 ${PROGNAME} test.txt
52+
${PROGNAME} test.txt -f my-variables.txt
53+
${PROGNAME} test.txt -f my-variables.txt > new-test.txt"
54+
55+
if [ $# -eq 0 ]; then
56+
echo "$usage"
57+
exit 1
58+
fi
59+
60+
if [[ ! -f "${1}" ]]; then
61+
echo "You need to specify a template file" >&2
62+
echo "$usage"
63+
exit 1
64+
fi
65+
66+
template="${1}"
67+
68+
if [ "$#" -ne 0 ]; then
69+
while [ "$#" -gt 0 ]
70+
do
71+
case "$1" in
72+
-h|--help)
73+
echo "$usage"
74+
exit 0
75+
;;
76+
-p|--print)
77+
print_only="true"
78+
;;
79+
-f|--file)
80+
config_file="$2"
81+
;;
82+
-s|--silent)
83+
silent="true"
84+
;;
85+
--)
86+
break
87+
;;
88+
-*)
89+
echo "Invalid option '$1'. Use --help to see the valid options" >&2
90+
exit 1
91+
;;
92+
# an option argument, continue
93+
*) ;;
94+
esac
95+
shift
96+
done
97+
fi
98+
99+
vars=$(grep -oE '\{\{[A-Za-z0-9_]+\}\}' "${template}" | sort | uniq | sed -e 's/^{{//' -e 's/}}$//')
100+
101+
if [[ -z "$vars" ]]; then
102+
if [ "$silent" == "false" ]; then
103+
echo "Warning: No variable was found in ${template}, syntax is {{VAR}}" >&2
104+
fi
105+
fi
106+
107+
# Load variables from file if needed
108+
if [ "${config_file}" != "<none>" ]; then
109+
if [[ ! -f "${config_file}" ]]; then
110+
echo "The file ${config_file} does not exists" >&2
111+
echo "$usage"
112+
exit 1
113+
fi
114+
115+
# Create temp file where & and "space" is escaped
116+
tmpfile=`mktemp`
117+
sed -e "s;\&;\\\&;g" -e "s;\ ;\\\ ;g" "${config_file}" > $tmpfile
118+
source $tmpfile
119+
fi
120+
121+
var_value() {
122+
eval echo \$$1
123+
}
124+
125+
replaces=""
126+
127+
# Reads default values defined as {{VAR=value}} and delete those lines
128+
# There are evaluated, so you can do {{PATH=$HOME}} or {{PATH=`pwd`}}
129+
# You can even reference variables defined in the template before
130+
defaults=$(grep -oE '^\{\{[A-Za-z0-9_]+=.+\}\}' "${template}" | sed -e 's/^{{//' -e 's/}}$//')
131+
132+
for default in $defaults; do
133+
var=$(echo "$default" | grep -oE "^[A-Za-z0-9_]+")
134+
current=`var_value $var`
135+
136+
# Replace only if var is not set
137+
if [[ -z "$current" ]]; then
138+
eval $default
139+
fi
140+
141+
# remove define line
142+
replaces="-e '/^{{$var=/d' $replaces"
143+
vars="$vars
144+
$current"
145+
done
146+
147+
vars=$(echo $vars | sort | uniq)
148+
149+
if [[ "$print_only" == "true" ]]; then
150+
for var in $vars; do
151+
value=`var_value $var`
152+
echo "$var = $value"
153+
done
154+
exit 0
155+
fi
156+
157+
# Replace all {{VAR}} by $VAR value
158+
for var in $vars; do
159+
value=`var_value $var`
160+
if [[ -z "$value" ]]; then
161+
if [ $silent == "false" ]; then
162+
echo "Warning: $var is not defined and no default is set, replacing by empty" >&2
163+
fi
164+
fi
165+
166+
# Escape slashes
167+
value=$(echo "$value" | sed 's/\//\\\//g');
168+
replaces="-e 's/{{$var}}/${value}/g' $replaces"
169+
done
170+
171+
escaped_template_path=$(echo $template | sed 's/ /\\ /g')
172+
eval sed $replaces "$escaped_template_path"

local/etc/uceap.d/devcontainer_post_create.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function devcontainer_post_create() {
2121
mkdir -p drush
2222
export HTTP_ADDRESS
2323
export HTTP_PROTOCOL
24-
build/templater.sh /usr/local/share/drush/example.drush.yml > drush/drush.yml
24+
templater.sh /usr/local/share/drush/example.drush.yml > drush/drush.yml
2525

2626
# set httpd port to be publicly accessible
2727
if [[ -n "$CODESPACE_NAME" ]]; then

local/etc/uceap.d/refresh_content.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function refresh_content() {
1919
sudo find web/sites/default/files -type d -exec chmod g+ws {} +
2020
sudo find web/sites/default/files -type f -exec chmod g+w {} +
2121

22-
build/db-rebuild.sh $DATABASE_BACKUP
22+
db-rebuild.sh $DATABASE_BACKUP
2323

2424
rm $FILES_BACKUP
2525
rm $DATABASE_BACKUP

0 commit comments

Comments
 (0)