Skip to content

Commit 838baed

Browse files
authored
fix makemysql so that easywp might work (#154)
This commit consists 18 commits in total, earliest one dating back to May 2020, intended to fix chain reactions caused by #129. This commit: 1. Changed `makemysql-real`, allowing it to output only the password and nothing else if a specific argument is given 2. Changed `makemysql`, allowing it to correctly fetch password printed by `makemysql-real`, allowing it to be silent if no wordpress installation is found, allowing it to not change wordpress password if a specific argument is given, and allowing it to output only the password and nothing else if a specific argument is given. 3. Changed `easywp` so that it is compatible with the updated `makemysql`. Hopefully, this will not break ocf infra. 18 commits: * rewrote so that it might work * idk wat autopep8 changed/suggested i followed its advice * forgot to add the messages * Added argument parsing and non-human-friendly output * Squashed two commits redirected some stuff to stderr fix stupid mistakes for makemysql-real * updated all three scripts so that they support some silent arguments and make things fancy but they might not work as I didn't test it * fix stupid bugs made in 3cac9d4 * fix pre-commit problem made in 3cac9d4 * Wrapper for if silent * Fixed a stupid logical mistake and added some stuff in bash scripts; did not run pre commit yet * Applied @kpengboy's suggestions 1. Changed --silent to --quiet 2. Disable `set -e` at places where error-handling exists 3. Added some more instructions 4. Removed some redundant stuff, but idk if this will blow stuff up * Bug: if quite is specified, do not ask if user wants to proceed. * Indentation Errors * Fix, silent should be global variable * Fixed some bugs in easywp * fix comment * fix so precommit pass * more to squash
1 parent a37c932 commit 838baed

File tree

3 files changed

+104
-39
lines changed

3 files changed

+104
-39
lines changed

makeservices/easywp

+9-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,18 @@ fi
5757

5858
# Get SQL password
5959
echo "Resetting database password..."
60-
sqlpass=$(echo "yes" | makemysql | tail -n 1 | grep -Po '(?<=: )([0-9a-zA-Z]){24,}$')
61-
if [[ -z "$sqlpass" ]]; then
60+
# Do not exit immediately if makemysql fails, as we need to display some error message...
61+
set +e
62+
sqlpass=$(makemysql --quiet)
63+
# I don't know if this will work... Just make sure no abnormal exit code first!
64+
if [ $? ] || [[ -z "$sqlpass" ]]; then
6265
echo -e "\\033[31mError:\\033[00m Could not retrieve database password. Run makemysql to see the issue."
6366
exit 1
6467
fi
68+
# Only have the aforementioned behaviour when running makemysql
69+
# Note also technically if should be below this set statement
70+
# but set messes up with $? as well...
71+
set -e
6572
echo "SQL set up and the password can be found in wp-config.php ..."
6673

6774
# Install WordPress and create config file

makeservices/makemysql

+48-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,52 @@ if [[ "$(hostname)" != "tsunami" && "$(hostname)" != "dev-tsunami" ]]; then
44
exit 1
55
fi
66

7-
PASS=$(sudo -u mysql /opt/share/utils/makeservices/makemysql-real | tee /dev/tty | tail -n 1 | grep -Po '(?<=: )([0-9a-zA-Z]){24,}$')
7+
IGNORE_WP=false
8+
QUIET=false
89

9-
echo 'Changing WordPress database password'
10-
cd ~/public_html/
11-
wp config set DB_PASSWORD "$PASS" > /dev/null
10+
# Parse arguments down below
11+
while (( "$#" )); do
12+
case "$1" in
13+
-i|--ignore-wp)
14+
IGNORE_WP=true
15+
shift
16+
;;
17+
-q|--quiet)
18+
QUIET=true
19+
shift
20+
;;
21+
-*)
22+
# Output to stderr
23+
echo "Error: Unsupported flag $1" >&2
24+
exit 1
25+
;;
26+
esac
27+
done
28+
29+
# The correctness of this line relies on the fact that
30+
# makemysql-real will output the new password ONLY on the
31+
# last line. The --quiet will also make sure it only output the password...
32+
PASS=$(sudo -u mysql /opt/share/utils/makeservices/makemysql-real --quiet)
33+
34+
if [ $? ] ; then
35+
echo 'makemysql-real did not exit properly.'
36+
echo 'Run "sudo -u mysql /opt/share/utils/makeservices/makemysql-real" for a more verbose output.'
37+
echo 'Additionally, you may contact staff members for help. This script will stop.'
38+
exit 1
39+
fi
40+
41+
# Check if wp is installed, wp-cli cannot be used as it verifies DB password as well
42+
# And change password if installed with easywp
43+
# Use --ignore-wp flag to skip this process
44+
if ! $IGNORE_WP && [ -f "$HOME/public_html/wp-config.php" ] ; then
45+
if ! $QUIET; then
46+
echo "WordPress installation detected, changing WordPress mysql password for you."
47+
fi
48+
wp config set DB_PASSWORD "$PASS" --path="$HOME/public_html" > /dev/null 2>&1
49+
fi
50+
51+
if $QUIET; then
52+
echo "$PASS"
53+
else
54+
echo "Your MySQL database password is: $PASS"
55+
fi

makeservices/makemysql-real

+47-33
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ MYSQL_HOST = 'mysql.ocf.berkeley.edu'
3636

3737
PW_LENGTH = 24
3838

39+
quiet = False
40+
3941

4042
def read_config():
4143
"""Fetches the MySQL hostname and root password from the config in
@@ -47,6 +49,11 @@ def read_config():
4749
return mysql_host, mysql_root_pw
4850

4951

52+
def print_if_not_quiet(*args, **kwargs):
53+
if not quiet:
54+
print(*args, **kwargs)
55+
56+
5057
def intro_prompt():
5158
print(dedent(
5259
"""
@@ -69,6 +76,8 @@ def intro_prompt():
6976

7077

7178
def main():
79+
# Without this quiet below will be local variable
80+
global quiet
7281
try:
7382
username = os.environ.get('SUDO_USER')
7483

@@ -78,43 +87,45 @@ def main():
7887
# Read config file.
7988
mysql_host, mysql_root_pw = read_config()
8089

90+
# Added a simple and stupid argument parsing so that other scripts can use it without tunneling in yes.
91+
if len(sys.argv) > 1 and sys.argv[1] in ['-q', '--quiet']:
92+
quiet = True
8193
# Check whether the script should proceed.
82-
if not intro_prompt():
83-
print('>>> Aborted by user request.')
84-
return
85-
94+
if not quiet:
95+
if not intro_prompt():
96+
print_if_not_quiet('>>> Aborted by user request.', file=sys.stderr)
97+
return
8698
# Connect to the MySQL server.
8799
try:
88-
print('>>> Connecting to MySQL database server...')
100+
print_if_not_quiet('>>> Connecting to MySQL database server...')
89101
connection = MySQLdb.connect(host=mysql_host,
90102
user='root',
91103
passwd=mysql_root_pw)
92104
except MySQLdb.MySQLError:
93-
print('>>> Error: Failed to connect to MySQL server.')
94-
raise
105+
raise ConnectionError('>>> Error: Failed to connect to MySQL server.')
95106

96107
# Check if the database already exists.
97108
try:
98-
print(">>> Checking if database '{}' already exists...".format(username))
109+
print_if_not_quiet(">>> Checking if database '{}' already exists...".format(username))
99110
connection.select_db(username)
100111

101112
# The database already exists, so skip the creation step.
102-
print('yes.')
113+
print_if_not_quiet('yes.')
103114
db_create = False
104-
print(dedent("""
105-
The MySQL database '{}' already exists.
106-
The database password will be reset.
115+
print_if_not_quiet(dedent("""
116+
The MySQL database '{}' already exists.
117+
The database password will be reset.
107118
108-
If you are unsure how to access or use your database, please visit
119+
If you are unsure how to access or use your database, please visit
109120
110-
https://www.ocf.berkeley.edu/docs/services/mysql/
121+
https://www.ocf.berkeley.edu/docs/services/mysql/
111122
112-
If you run into trouble trying to use your database, contact us at
123+
If you run into trouble trying to use your database, contact us at
113124
114-
help@ocf.berkeley.edu
115-
""").format(username))
125+
help@ocf.berkeley.edu
126+
""").format(username))
116127
except MySQLdb.OperationalError:
117-
print('no.')
128+
print_if_not_quiet('no.')
118129
db_create = True
119130

120131
# Add or update user database privileges.
@@ -128,37 +139,39 @@ def main():
128139
# Result should be "Query OK, 0 rows affected",
129140
# but we'll assume no exception means success.
130141
except MySQLdb.MySQLError:
131-
print('>>> Error: Failed to grant database privileges.')
132-
raise
142+
raise ConnectionError('>>> Error: Failed to grant database privileges.')
133143

134144
# Create new database, if necessary.
135145
if db_create:
136146
try:
137-
print(">>> Creating new database '{}'...".format(username))
147+
print_if_not_quiet(">>> Creating new database '{}'...".format(username))
138148
query = CREATE_QUERY.format(username)
139149
connection.query(query)
140150
connection.store_result()
141151
# Result should be "Query OK, 1 row affected",
142152
# but we'll assume no exception means success.
143153
except MySQLdb.MySQLError:
144-
print('>>> Error: Failed to create database.')
145-
raise
154+
raise IOError('>>> Error: Failed to create database.')
146155

147156
# Database is ready for use.
148157
if db_create:
149-
print(dedent("""
150-
Your MySQL database has been created.
158+
print_if_not_quiet(dedent("""
159+
Your MySQL database has been created.
160+
161+
For instructions on accessing and using your database, please visit
151162
152-
For instructions on accessing and using your database, please visit
163+
https://www.ocf.berkeley.edu/docs/services/mysql/
153164
154-
https://www.ocf.berkeley.edu/docs/services/mysql/
165+
If you run into trouble trying to use your database, contact us at
155166
156-
If you run into trouble trying to use your database, contact us at
167+
help@ocf.berkeley.edu
168+
"""))
157169

158-
help@ocf.berkeley.edu
159-
"""))
170+
print_if_not_quiet('>>> Your MySQL database password is: ')
160171

161-
print('>>> Your MySQL database password is: {}'.format(userpass))
172+
# This line to be printed, no matter quiet or not.
173+
# The userpass will always be on the last line.
174+
print(userpass)
162175
except Exception as ex:
163176
send_problem_report(dedent(
164177
"""\
@@ -171,8 +184,9 @@ def main():
171184
"""
172185
A fatal error was encountered during program execution.
173186
OCF staff have been notified of the problem.
174-
"""
175-
))
187+
Error for staff: {}: {}.
188+
""".format(ex.__class__.__name__, ex)
189+
), file=sys.stderr)
176190
sys.exit(1)
177191

178192

0 commit comments

Comments
 (0)