Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix makemysql so that easywp might work #154

Merged
merged 18 commits into from
Oct 19, 2021

Conversation

axmmisaka
Copy link
Contributor

@axmmisaka axmmisaka commented May 11, 2020

Why kuroko? bc the committer is shirai kuroko (use git log)
Judgement desu no
idk if i have access on this repo so i'll just use mine

#137
Problem is it used to just output sudo -u mysql /opt/share/utils/makeservices/makemysql-real, and after #129 it's nothing, #138 sometimes give a string so this will screw up, as easywp rely on some stuff extracted from tail -n 1 that look like Your MySQL database password is: aioeuwhgviujashfuiasb
This one just simulate if we were running sudo -u mysql /opt/share/utils/makeservices/makemysql-real and output the stuff in the last line so there are probably backwards compatibility

Another feature: use argument --ignore-wp to not reset in case you don't want to, and this shall remain a secret so that it is not abused

Potential problems:

  1. Does wp core is-installed --quiet --path=$HOME/public_html > /dev/null 2>&1 return true if wp exists and false otherwise
  2. Change password if installed with easywp is correct but change password only if installed with easywp is a wrong statement, idk if wp-cli will also handle this
    2.5 i didn't run make test for style check - ok i screwed up, wait one Now it worked
  3. idk if it works, i can't test

@axmmisaka
Copy link
Contributor Author

axmmisaka commented May 11, 2020

Remember to close #103 also after merging this (if this works)
Now CS170 time for me, damn

@axmmisaka
Copy link
Contributor Author

axmmisaka commented May 11, 2020

lets see if not running make test cause problems again
it doesn't!!!!! salud

@axmmisaka axmmisaka changed the title rewrote makemysql so that easywp might work fix makemysql so that easywp might work May 11, 2020
wp config set DB_PASSWORD "$PASS" > /dev/null 2>&1
fi
# Added for backward compatibility - the last line will be
# extracted by "tail -n 1" and the mysterious "grep"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may wish to mention which script is running these commands.

# extracted by "tail -n 1" and the mysterious "grep"
# will extract the password after the colon
# So that it simulate the same behaviour as the original makemysql
# which output the same thing as in makemysql-real
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# So that it simulate the same behaviour as the original makemysql
# which output the same thing as in makemysql-real

I would remove these comments; the comments in the previous lines about easywp already tell me everything I need to know.

@cg505
Copy link
Member

cg505 commented May 12, 2020

I feel like it would make more sense to update the whole stack here in one swipe:

  1. makemysql-real mostly prints to stderr, and only prints the password to stdout, which means we don't have to do the scary grep incantation that's currently in makemysql
  2. makemysql has an option to skip WP config, and also an option to only output the password (instead of the normal, nice message "Your MySQL database password is: ")
  3. easywp uses the options in Reorganize scripts, remove system-only scripts #2 to get the DB password without doing any weird text manipulation.

@axmmisaka
Copy link
Contributor Author

will do

@axmmisaka
Copy link
Contributor Author

3. `easywp` uses the options in #2 to get the DB password without doing any weird text manipulation.

Do u mean 2. here or PR #2...?

@cg505
Copy link
Member

cg505 commented May 13, 2020

Do u mean 2. here or PR #2...?
point 2. here, not the PR. it just autolinked.

Copy link
Member

@cg505 cg505 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking pretty good so far

@@ -57,7 +57,7 @@ fi

# Get SQL password
echo "Resetting database password..."
sqlpass=$(echo "yes" | makemysql | tail -n 1 | grep -Po '(?<=: )([0-9a-zA-Z]){24,}$')
sqlpass=$(makemysql --unreadable-output --silent)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like you should have --ignore-wp here too, since there won't be any wordpress installation yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--ignore-wp is for those who installed WordPress but don't want to change their pw, here the thing will be done by wp-cli in makemysql imo

YES="--silent"
shift
;;
# I've seen -*|--*=), but idk what does the = serve
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's for options like --key=value probably, but i wouldn't worry about it

esac
done

PASS=$(sudo -u mysql /opt/share/utils/makeservices/makemysql-real $SILENT | tail -n 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what's the point of --silent, since we're always only going to see the last line (because of tail). Maybe we could make it so that makemysql-real prints its messages to stderr so they aren't affected but the subshell and tail?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is what I was expecting (only printing the password), but I do not know if other calls in makemysql-real print anything to stdout, so tail was added to make sure.
That being said, I was considering about what if makemysql-real failed, without grep PASS would be some weird stuffs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we should be adding the same option to control makemysql-real's output that we are adding to makemysql then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've actually done that, but I didn't test makemysql-real and the things it call might output some other mysterious stuff ig

if (wp core is-installed --quiet --path="$HOME/public_html" > /dev/null 2>&1) &&
([ $# -ne 1 ] || ( [ $# -eq 1 ] && [ ! "$1" = '--ignore-wp' ])); then
echo "WordPress installation detected, changing WordPress mysql password for you."
if (wp core is-installed --quiet --path="$HOME/public_html" > /dev/null 2>&1) && ! $IGNORE_WP ; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is kind of weird, it is literally running the commands true or false

Copy link
Contributor Author

@axmmisaka axmmisaka May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lemme do some research, my shell script sucks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@kpengboy kpengboy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this. I've left a couple comments.

esac
done

PASS=$(sudo -u mysql /opt/share/utils/makeservices/makemysql-real $SILENT | tail -n 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we should be adding the same option to control makemysql-real's output that we are adding to makemysql then.

if (wp core is-installed --quiet --path="$HOME/public_html" > /dev/null 2>&1) &&
([ $# -ne 1 ] || ( [ $# -eq 1 ] && [ ! "$1" = '--ignore-wp' ])); then
echo "WordPress installation detected, changing WordPress mysql password for you."
if (wp core is-installed --quiet --path="$HOME/public_html" > /dev/null 2>&1) && ! $IGNORE_WP ; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -78,43 +80,50 @@ 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 ['-s', '--silent']:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd use argparse.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's only one possible argument which I don't want to utilize a heavy parser


# Check if the database already exists.
try:
print(">>> Checking if database '{}' already exists...".format(username))
if not silent:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not silent:
    print(

Probably worth creating a wrapper for this...

@@ -57,7 +57,7 @@ fi

# Get SQL password
echo "Resetting database password..."
sqlpass=$(echo "yes" | makemysql | tail -n 1 | grep -Po '(?<=: )([0-9a-zA-Z]){24,}$')
sqlpass=$(makemysql --unreadable-output --silent)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe --machine-readable or similar?

@axmmisaka
Copy link
Contributor Author

axmmisaka commented Mar 26, 2021

Idk what happened to my local repo, but there seems to be a screwup

Updates:

  1. Found a stupid logical mistake I made in makemysql. --machine-readable does not make sense as it's doing the same thing as --silent does, and we always want to makemysql-real silently when calling it in the script - and even if we did not run it silently, it's in a subshell so nothing will be outputted...
  2. Added some exit code check after sub-shelling
  3. Applied cg505 and kpengboy's suggestions on making script more efficient

Can't believe I procrastinated on this for almost 1 year!

Comment on lines 62 to 67
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it stands, this code is not going to be executed at all if makemysql exits with a non-zero status due to the set -e at the top of the script.

To avoid this...I guess we can add a set +e right before the makemysql call and a set -e after the if block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -78,43 +83,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 ['-s', '--silent']:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call it --quiet if I were you. --silent sounds like no output would be printed at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed names


# 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 (wp core is-installed --quiet --path="$HOME/public_html" > /dev/null 2>&1) && ! $IGNORE_WP ; then
if $IGNORE_WP && ! wp core is-installed --quiet --path="$HOME/public_html" > /dev/null 2>&1 ; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be ! $IGNORE_WP?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was stupid!

Comment on lines 53 to 57
# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is out of date?

axmmisaka added 11 commits May 28, 2021 04:09
redirected some stuff to stderr

fix stupid mistakes for makemysql-real
…and make things fancy but they might not work as I didn't test it
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
@axmmisaka
Copy link
Contributor Author

oh no, i decided to rebase and force-push, seems like a very bad decision

Anyway, @kpengboy 's valuable suggestions are applied, and the only thing remaining is pre-commit and style check... Which I need to do on supernova

@emmatyping
Copy link
Member

Would you be interested in reviving this PR?

@axmmisaka
Copy link
Contributor Author

Yes, I think it's good tho?

@emmatyping
Copy link
Member

It appears CI is failing, could you look into that? Also wasn't sure if there were any review comments left to do

@kpengboy
Copy link
Member

It seems that you'll need to fix some shellcheck issues? Otherwise LGTM, assuming that you have tested this.

@axmmisaka
Copy link
Contributor Author

Remember to SQUASH if you reviewed and decided to merge please 😴

Copy link
Member

@kpengboy kpengboy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, assuming you have tested these changes thoroughly.

@axmmisaka axmmisaka merged commit 838baed into ocf:master Oct 19, 2021
@axmmisaka
Copy link
Contributor Author

As dev-tsunami does not exist, each of the scripts are tested individually with guser on tsunami.

@emmatyping emmatyping mentioned this pull request Oct 23, 2021
# 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 [ $? ] ; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these be if [[ $? -ne 0 ]]? Since we are checking if the return code was abnormal (0 is success)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you inverted this in 051e0387350c67f0fd6cf976da25ad8511ad46bb to make precommit pass. What was the issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why but pre-commit say I should use if [ $? ] instead of if [[ $? -ne 0 ]] so I accepted this recommendation

jyxzhang pushed a commit that referenced this pull request Oct 23, 2022
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants