Skip to content

Commit

Permalink
Applied @kpengboy's suggestions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
axmmisaka committed May 27, 2021
1 parent 5e9b52d commit 2798e6b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
6 changes: 6 additions & 0 deletions makeservices/easywp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ fi

# Get SQL password
echo "Resetting database password..."
# Do not exit immediately if makemysql fails, as we need to display some error message...
set +e
sqlpass=$(makemysql --silent)
# I don't know if this will work... Just make sure no abnormal exit code first!
if [ $? -ne 0 ] || [[ -z "$sqlpass" ]]; then
echo -e "\033[31mError:\033[00m Could not retrieve database password. Run makemysql to see the issue."
exit 1
fi
# Only have the aforementioned behaviour when running makemysql
# Note also technically if should be below this set statement
# but set messes up with $? as well...
set -e
echo "SQL set up and the password can be found in wp-config.php ..."

# Install WordPress and create config file
Expand Down
25 changes: 11 additions & 14 deletions makeservices/makemysql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if [[ "$(hostname)" != "tsunami" && "$(hostname)" != "dev-tsunami" ]]; then
fi

IGNORE_WP=false
SILENT=false
QUIET=false

# Parse arguments down below
while (( "$#" )); do
Expand All @@ -14,8 +14,8 @@ while (( "$#" )); do
IGNORE_WP=true
shift
;;
-s|--silent)
SILENT=true
-q|--quiet)
QUIET=true
shift
;;
-*|--*)
Expand All @@ -28,32 +28,29 @@ done

# The correctness of this line relies on the fact that
# makemysql-real will output the new password ONLY on the
# last line. The --silent will also make sure it only output the password...
PASS=$(sudo -u mysql /opt/share/utils/makeservices/makemysql-real --silent | tail -n 1)
# last line. The --quiet will also make sure it only output the password...
PASS=$(sudo -u mysql /opt/share/utils/makeservices/makemysql-real --quiet)

if [ $? -ne 0 ]; then
echo 'makemysql did not exit properly. This script will stop.'
echo 'makemysql-real did not exit properly.'
echo 'Run "sudo -u mysql /opt/share/utils/makeservices/makemysql-real" for a more verbose output.'
echo 'Additionally, you may contact staff members for help. This script will stop.'
exit 1
fi

# Check if wp is installed with wp-cli
# And change password if installed with easywp
# But change password only if installed with easywp is a wrong statement
# Use --ignore-wp flag to skip this process
if $IGNORE_WP && ! wp core is-installed --quiet --path="$HOME/public_html" > /dev/null 2>&1 ; then
if ! $SILENT; then
if ! $IGNORE_WP && wp core is-installed --quiet --path="$HOME/public_html" > /dev/null 2>&1 ; then
if ! $QUIET; then
echo "WordPress installation detected, changing WordPress mysql password for you."
fi
wp config set DB_PASSWORD "$PASS" > /dev/null 2>&1
fi

if $SILENT; then
if $QUIET; then
echo "$PASS"
else
# Added for backward compatibility - the last line will be
# extracted by "tail -n 1" and the mysterious "grep"
# will extract the password after the colon in legacy codes
# So that it simulate the same behaviour as the original makemysql
# which output the same thing as in makemysql-real
echo "Your MySQL database password is: $PASS"
fi
30 changes: 15 additions & 15 deletions makeservices/makemysql-real
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ MYSQL_HOST = 'mysql.ocf.berkeley.edu'

PW_LENGTH = 24

silent = False
quiet = False


def read_config():
Expand All @@ -48,8 +48,8 @@ def read_config():
mysql_root_pw = conf.get('makemysql', 'passwd')
return mysql_host, mysql_root_pw

def print_if_not_silent(*args, **kwargs):
if not silent:
def print_if_not_quiet(*args, **kwargs):
if not quiet:
print(*args, **kwargs)

def intro_prompt():
Expand Down Expand Up @@ -84,16 +84,16 @@ def main():
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 ['-s', '--silent']:
silent = True
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_if_not_silent('>>> Aborted by user request.', file=sys.stderr)
print_if_not_quiet('>>> Aborted by user request.', file=sys.stderr)
return

# Connect to the MySQL server.
try:
print_if_not_silent('>>> 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)
Expand All @@ -102,13 +102,13 @@ def main():

# Check if the database already exists.
try:
print_if_not_silent(">>> 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_if_not_silent('yes.')
print_if_not_quiet('yes.')
db_create = False
print_if_not_silent(dedent("""
print_if_not_quiet(dedent("""
The MySQL database '{}' already exists.
The database password will be reset.
Expand All @@ -121,7 +121,7 @@ def main():
help@ocf.berkeley.edu
""").format(username))
except MySQLdb.OperationalError:
print_if_not_silent('no.')
print_if_not_quiet('no.')
db_create = True

# Add or update user database privileges.
Expand All @@ -140,7 +140,7 @@ def main():
# Create new database, if necessary.
if db_create:
try:
print_if_not_silent(">>> 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()
Expand All @@ -151,7 +151,7 @@ def main():

# Database is ready for use.
if db_create:
print_if_not_silent(dedent("""
print_if_not_quiet(dedent("""
Your MySQL database has been created.
For instructions on accessing and using your database, please visit
Expand All @@ -163,9 +163,9 @@ def main():
help@ocf.berkeley.edu
"""))

print_if_not_silent('>>> Your MySQL database password is: ')
print_if_not_quiet('>>> Your MySQL database password is: ')

# This line to be printed, no matter silent or not.
# 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:
Expand Down

0 comments on commit 2798e6b

Please sign in to comment.