-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
104 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,8 @@ MYSQL_HOST = 'mysql.ocf.berkeley.edu' | |
|
||
PW_LENGTH = 24 | ||
|
||
quiet = False | ||
|
||
|
||
def read_config(): | ||
"""Fetches the MySQL hostname and root password from the config in | ||
|
@@ -47,6 +49,11 @@ def read_config(): | |
return mysql_host, mysql_root_pw | ||
|
||
|
||
def print_if_not_quiet(*args, **kwargs): | ||
if not quiet: | ||
print(*args, **kwargs) | ||
|
||
|
||
def intro_prompt(): | ||
print(dedent( | ||
""" | ||
|
@@ -69,6 +76,8 @@ def intro_prompt(): | |
|
||
|
||
def main(): | ||
# Without this quiet below will be local variable | ||
global quiet | ||
try: | ||
username = os.environ.get('SUDO_USER') | ||
|
||
|
@@ -78,43 +87,45 @@ def main(): | |
# Read config file. | ||
mysql_host, mysql_root_pw = read_config() | ||
|
||
# Added a simple and stupid argument parsing so that other scripts can use it without tunneling in yes. | ||
if len(sys.argv) > 1 and sys.argv[1] in ['-q', '--quiet']: | ||
quiet = True | ||
# Check whether the script should proceed. | ||
if not intro_prompt(): | ||
print('>>> Aborted by user request.') | ||
return | ||
|
||
if not quiet: | ||
if not intro_prompt(): | ||
print_if_not_quiet('>>> Aborted by user request.', file=sys.stderr) | ||
return | ||
# Connect to the MySQL server. | ||
try: | ||
print('>>> Connecting to MySQL database server...') | ||
print_if_not_quiet('>>> Connecting to MySQL database server...') | ||
connection = MySQLdb.connect(host=mysql_host, | ||
user='root', | ||
passwd=mysql_root_pw) | ||
except MySQLdb.MySQLError: | ||
print('>>> Error: Failed to connect to MySQL server.') | ||
raise | ||
raise ConnectionError('>>> Error: Failed to connect to MySQL server.') | ||
|
||
# Check if the database already exists. | ||
try: | ||
print(">>> Checking if database '{}' already exists...".format(username)) | ||
print_if_not_quiet(">>> Checking if database '{}' already exists...".format(username)) | ||
connection.select_db(username) | ||
|
||
# The database already exists, so skip the creation step. | ||
print('yes.') | ||
print_if_not_quiet('yes.') | ||
db_create = False | ||
print(dedent(""" | ||
The MySQL database '{}' already exists. | ||
The database password will be reset. | ||
print_if_not_quiet(dedent(""" | ||
The MySQL database '{}' already exists. | ||
The database password will be reset. | ||
If you are unsure how to access or use your database, please visit | ||
If you are unsure how to access or use your database, please visit | ||
https://www.ocf.berkeley.edu/docs/services/mysql/ | ||
https://www.ocf.berkeley.edu/docs/services/mysql/ | ||
If you run into trouble trying to use your database, contact us at | ||
If you run into trouble trying to use your database, contact us at | ||
[email protected] | ||
""").format(username)) | ||
[email protected] | ||
""").format(username)) | ||
except MySQLdb.OperationalError: | ||
print('no.') | ||
print_if_not_quiet('no.') | ||
db_create = True | ||
|
||
# Add or update user database privileges. | ||
|
@@ -128,37 +139,39 @@ def main(): | |
# Result should be "Query OK, 0 rows affected", | ||
# but we'll assume no exception means success. | ||
except MySQLdb.MySQLError: | ||
print('>>> Error: Failed to grant database privileges.') | ||
raise | ||
raise ConnectionError('>>> Error: Failed to grant database privileges.') | ||
|
||
# Create new database, if necessary. | ||
if db_create: | ||
try: | ||
print(">>> Creating new database '{}'...".format(username)) | ||
print_if_not_quiet(">>> Creating new database '{}'...".format(username)) | ||
query = CREATE_QUERY.format(username) | ||
connection.query(query) | ||
connection.store_result() | ||
# Result should be "Query OK, 1 row affected", | ||
# but we'll assume no exception means success. | ||
except MySQLdb.MySQLError: | ||
print('>>> Error: Failed to create database.') | ||
raise | ||
raise IOError('>>> Error: Failed to create database.') | ||
|
||
# Database is ready for use. | ||
if db_create: | ||
print(dedent(""" | ||
Your MySQL database has been created. | ||
print_if_not_quiet(dedent(""" | ||
Your MySQL database has been created. | ||
For instructions on accessing and using your database, please visit | ||
For instructions on accessing and using your database, please visit | ||
https://www.ocf.berkeley.edu/docs/services/mysql/ | ||
https://www.ocf.berkeley.edu/docs/services/mysql/ | ||
If you run into trouble trying to use your database, contact us at | ||
If you run into trouble trying to use your database, contact us at | ||
[email protected] | ||
""")) | ||
|
||
[email protected] | ||
""")) | ||
print_if_not_quiet('>>> Your MySQL database password is: ') | ||
|
||
print('>>> Your MySQL database password is: {}'.format(userpass)) | ||
# This line to be printed, no matter quiet or not. | ||
# The userpass will always be on the last line. | ||
print(userpass) | ||
except Exception as ex: | ||
send_problem_report(dedent( | ||
"""\ | ||
|
@@ -171,8 +184,9 @@ def main(): | |
""" | ||
A fatal error was encountered during program execution. | ||
OCF staff have been notified of the problem. | ||
""" | ||
)) | ||
Error for staff: {}: {}. | ||
""".format(ex.__class__.__name__, ex) | ||
), file=sys.stderr) | ||
sys.exit(1) | ||
|
||
|
||
|