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

wp-env: override generated file directory with environment variable #20253

Merged
merged 11 commits into from
Feb 19, 2020

Conversation

noahtallen
Copy link
Member

@noahtallen noahtallen commented Feb 15, 2020

Description

  • Adds a non-hidden default directory for Linux (~/wp-env)
  • Allows a user to override the directory in which wp-env creates generated files with the WP_ENV_HOME environment variable.

Work towards #20180, but will need to continue solving it in a follow-up.

How has this been tested?

I used the environment variable locally and verified generated files were placed there. I also checked that the default location is still used if the environment variable is not specified.

I also tested that, if the container is running, the start command works successfully after deleting the wp-env home directory.

Types of changes

New Feature

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@noahtallen noahtallen added [Type] Enhancement A suggestion for improvement. [Tool] Env /packages/env labels Feb 15, 2020
@noahtallen noahtallen self-assigned this Feb 15, 2020
packages/env/CHANGELOG.md Outdated Show resolved Hide resolved
packages/env/README.md Outdated Show resolved Hide resolved
packages/env/lib/config.js Outdated Show resolved Hide resolved
@noisysocks
Copy link
Member

noisysocks commented Feb 17, 2020

In the code for wp-env we call the ~/.wp-env directory the "work directory". I'm unsure if that's the best name (I struggled with choosing one!), but nonetheless thinking that we should be consistent and name this new variable something like WP_ENV_WORK_DIR.

edit: Actually, looking through it again, maybe it makes sense to call ~/.wp-env the home directory and ~/.wp-env/{hash} the work directory. Either way let's make the environment variable name match the code so that we're consistent with the terminology 🙂

@ockham
Copy link
Contributor

ockham commented Feb 17, 2020

Thanks Noah! This allows me to run wp-env at least once! 😅

~/src/gutenberg$ WP_ENV_SOURCE=~/wpenv npx wp-env start
> ✔ WordPress started. (in 296s 268ms)
~/src/gutenberg$ WP_ENV_SOURCE=~/wpenv npx wp-env stop
✔ Stopped WordPress. (in 4s 979ms)
> ~/src/gutenberg$ WP_ENV_SOURCE=~/wpenv npx wp-env start
✖ EACCES: permission denied, open '~/wpenv/6ac6b03bd54e5e922f0d180f6d7ff25d/tests-WordPress/wp-config.php'
[Error: EACCES: permission denied, open '~/wpenv/6ac6b03bd54e5e922f0d180f6d7ff25d/tests-WordPress/wp-config.php'] {
  errno: -13,
  code: 'EACCES',
  syscall: 'open',
  path: '~/wpenv/6ac6b03bd54e5e922f0d180f6d7ff25d/tests-WordPress/wp-config.php'
}

FWIW:

-rw-r--r-- 1 www-data www-data 3187 Feb 17 14:19 ~/wpenv/6ac6b03bd54e5e922f0d180f6d7ff25d/tests-WordPress/wp-config.php

whereas the other files in that directory have my user and group as their owner.

@epiqueras
Copy link
Contributor

I guess we need to set the owner and permissions of the file explicitly for Linux.

@noahtallen noahtallen force-pushed the add/wp-env-support-for-source-override branch 2 times, most recently from b4d8fd1 to 66aa627 Compare February 17, 2020 22:08
@noahtallen
Copy link
Member Author

noahtallen commented Feb 17, 2020

Per the permissions issue, it looks as if the basic stuff is working well. So I'm not sure it's a node filesystem issue since we use nodegit to download the sourcecode which has this permissions issue. (Here's where we use it.)

Do we need to run chmod and/or chown on every downloaded file?

noahtallen and others added 2 commits February 17, 2020 14:50
Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>
@noahtallen noahtallen force-pushed the add/wp-env-support-for-source-override branch from 66aa627 to 7d4f948 Compare February 17, 2020 22:50
@noahtallen
Copy link
Member Author

Actually, scratch that last comment.

I think the install command, which is run by docker-compose, is just generating the file under that web server user.

So, should we chmod/chown on the wp-config file for any WordPress source?

@noahtallen
Copy link
Member Author

noahtallen commented Feb 17, 2020

Hm, maybe it's something else entirely. On this branch:

~/source/gutenberg(add/wp-env-support-for-source-override) » packages/env/bin/wp-env start
✖ Error while running docker-compose command.
Starting 31911d623e75f345e9ed328b9f48cff6_mysql_1 ... done
Starting 31911d623e75f345e9ed328b9f48cff6_wordpress_1 ... done
Error: 'wp-config.php' not found.
Either create one manually or use `wp config create`.

I get this on a fresh install (after deleting the wp-env directory for the project) on this branch even on macOS. lol. I've been testing in a separate plugin, which does not specify a GitHub source for WordPress, so it may just be that using a GitHub source for WordPress (like Gutenberg does) has been broken for a while... 🤔

@epiqueras
Copy link
Contributor

I don't get that error.

packages/env/README.md Outdated Show resolved Hide resolved
packages/env/lib/config.js Outdated Show resolved Hide resolved
packages/env/lib/config.js Outdated Show resolved Hide resolved
packages/env/lib/config.js Outdated Show resolved Hide resolved
@noahtallen
Copy link
Member Author

The old command relied on this script to properly initialize the config file. I'm a bit out of my depth here; @noisysocks do you have opinions about how to handle this?

@epiqueras
Copy link
Contributor

Doesn't wp core install create it for you?

noahtallen and others added 2 commits February 17, 2020 15:33
Co-Authored-By: Enrique Piqueras <epiqueras@users.noreply.github.com>
@noahtallen noahtallen force-pushed the add/wp-env-support-for-source-override branch from 6388c07 to bb183bf Compare February 17, 2020 23:33
@noahtallen
Copy link
Member Author

Doesn't wp core install create it for you?

That's what I expected to happen as well, but if I run these commands on this branch, I consistently get this issue.

~/source/gutenberg(add/wp-env-support-for-source-override) » rm -rf ~/.wp-env
~/source/gutenberg(add/wp-env-support-for-source-override) » packages/env/bin/wp-env start
✖ Error while running docker-compose command.
Starting 31911d623e75f345e9ed328b9f48cff6_mysql_1 ... done
Starting 31911d623e75f345e9ed328b9f48cff6_wordpress_1 ... done
Error: 'wp-config.php' not found.
Either create one manually or use `wp config create`.

@noahtallen
Copy link
Member Author

For that matter, I also get the error on master, so we could resolve this in a follow up.

@epiqueras
Copy link
Contributor

Add logs to figure out on what line that throws. It's probably related to the new order of commands.

@noahtallen
Copy link
Member Author

Ah, apparently this condition happens when:

  • you already have the docker instance started
  • you delete the ~/.wp-env directory
  • you run wp-env start again

It looks like it can be fixed by stopping the docker instance before running wp-env start.

I think we should handle this case gracefully, but it definitely does not appear to be the reported issue.

So we're back at #20253 (comment), which I think means we just need to set the correct permissions on the wp-config file.

@epiqueras
Copy link
Contributor

Any ideas @ockham?

@ockham
Copy link
Contributor

ockham commented Feb 18, 2020

Just retested this (after deleting my previous wp-env directory). Unfortunately, now the first attempt is also failing:

$ npx wp-env start
✖ EPERM: operation not permitted, chown '/home/me/wp-env/6ac6b03bd54e5e922f0d180f6d7ff25d/WordPress/wp-config.php'
[Error: EPERM: operation not permitted, chown '/home/me/wp-env/6ac6b03bd54e5e922f0d180f6d7ff25d/WordPress/wp-config.php'] {
  errno: -1,
  code: 'EPERM',
  syscall: 'chown',
  path: '/home/me/wp-env/6ac6b03bd54e5e922f0d180f6d7ff25d/WordPress/wp-config.php'
}

Looks like the chown, while a good idea, isn't working 🙁

Let's go back to the state of this PR before the chown I guess? Working at least once is better than not working 😅

@ockham
Copy link
Contributor

ockham commented Feb 18, 2020

Any ideas @ockham?

Just saw this -- unfortunately, I don't have any ideas; I'm seeing different errors.

@sainthkh I'm using the 'Snap' package for Docker that Ubuntu provides (in its Software Center) -- have you tried that? (You'd probably need to remoive your current Docker setup 😅 )

Copy link
Contributor

@ockham ockham left a comment

Choose a reason for hiding this comment

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

Blocking merge b/c of #20253 (comment)

@ockham
Copy link
Contributor

ockham commented Feb 18, 2020

As a follow-up to this PR, we might consider generating the wp-config.php as suggested by @noisysocks here (#20253 (comment)):

We should test if generating a wp-config.php ourselves using wp config generate causes the config file to have correct permissions.

(Maybe this stands a better chance of success since it's called from within the container. Bit of a hand-waving argument here 😅 )

Suggesting to do this as a follow-up, since all of this is a massive can of worms, and I think it makes sense to divide and conquer: Make this PR only about the env variable (and its Linux default), and tackle wp-config permissions separately.

@epiqueras
Copy link
Contributor

Suggesting to do this as a follow-up, since all of this is a massive can of worms, and I think it makes sense to divide and conquer: Make this PR only about the env variable (and its Linux default), and tackle wp-config permissions separately.

Agreed, unless we can fix chown somehow.

@ockham
Copy link
Contributor

ockham commented Feb 18, 2020

Suggesting to do this as a follow-up, since all of this is a massive can of worms, and I think it makes sense to divide and conquer: Make this PR only about the env variable (and its Linux default), and tackle wp-config permissions separately.

Agreed, unless we can fix chown somehow.

My gut says that the chown issue is actually the same as the previous one with accessing the wp-config: When done from outside the container, we plainly lack permissions, no matter what we try to do with that file. (But yeah, this is clearly not my domain of expertise, so I might be wrong.)

@epiqueras
Copy link
Contributor

It makes sense, you need permissions to change permissions. We would need to sudo somehow.

@noahtallen
Copy link
Member Author

Sad to hear that it did not work. I've removed chown from this branch now; I agree that it makes sense to split this up and continue trying to fix that issue after merging the other stuff here :)

@noahtallen
Copy link
Member Author

mysqlcheck: Got error: 2002: Can't connect to MySQL server on 'mysql' (115) when trying to connect

@sainthkh Thanks for the test! Interestingly, I also saw a similar error yesterday when running docker-compose run cli wp config create.

@noahtallen
Copy link
Member Author

cc @ockham and @noisysocks for ✅

Copy link
Member

@noisysocks noisysocks left a comment

Choose a reason for hiding this comment

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

Looks good! :shipit:

@noahtallen noahtallen requested a review from ockham February 19, 2020 04:14
@sainthkh
Copy link
Contributor

I changed to snap docker. But there was no difference.

✖ Error while running docker-compose command.
Starting 841be7b26bd583f294009fefb90de98d_mysql_1 ... done
Starting 841be7b26bd583f294009fefb90de98d_wordpress_1 ... done
mysqlcheck: Got error: 1049: Unknown database 'wordpress' when selecting the database

I either get no 'wordpress' or cannot connect error.

Copy link
Contributor

@ockham ockham left a comment

Choose a reason for hiding this comment

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

Thanks Noah! We're back to this behavior -- which, as discussed, is an improvement over the previous state of things!

@noahtallen
Copy link
Member Author

@ockham & @sainthkh Thanks for the continued testing. I can keep working towards #20180, but it is a bit tough since I don't have a way to test that the changes work. :) The two things I'm thinking of for next steps are:

  1. Try generating the wp-config.php file ourselves
  2. Look up mysql connection bugs. I wonder if we need to define a network for the different Docker services?

@noahtallen noahtallen merged commit 57533e8 into master Feb 19, 2020
@noahtallen noahtallen deleted the add/wp-env-support-for-source-override branch February 19, 2020 17:59
@github-actions github-actions bot added this to the Gutenberg 7.6 milestone Feb 19, 2020
@epiqueras
Copy link
Contributor

Has anyone reproduced the connection issue? It might be a third party problem.

@noahtallen
Copy link
Member Author

I did see a similar error locally when I was playing around with generating the config file, so I'll keep an eye out for it

@gglnx
Copy link

gglnx commented Feb 28, 2020

Starting 8dd17dfdf919942794ccb8bf7b39ce80_mysql_1 ... done
Starting 8dd17dfdf919942794ccb8bf7b39ce80_wordpress_1 ... done
mysqlcheck: Got error: 1049: Unknown database 'wordpress' when selecting the database

Starting 8dd17dfdf919942794ccb8bf7b39ce80_mysql_1 ... done
Starting 8dd17dfdf919942794ccb8bf7b39ce80_wordpress_1 ... done
mysqlcheck: Got error: 1049: Unknown database 'wordpress' when selecting the database

I've got the same problem. Tried it with sudo and without it. Using the latest Docker Desktop (Mac, 2.2.0.3).

@jorgefilipecosta
Copy link
Member

Hi @noahtallen should this PR be backported into WordPress 5.4? If yes, feel free to add the label backport to wp core.

@noahtallen
Copy link
Member Author

Hi @noahtallen should this PR be backported into WordPress 5.4?

If the other PRs for wp-env are being backported, I don't see why not :)

@noahtallen noahtallen added the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Feb 28, 2020
jorgefilipecosta pushed a commit that referenced this pull request Mar 2, 2020
- Uses a non-hidden default directory for Linux (~/wp-env)
- Allows a user to override the directory in which wp-env
  creates generated files with the WP_ENV_HOME environment
  variable.

Work towards #20180, but will need to continue in a follow-up.
jorgefilipecosta pushed a commit that referenced this pull request Mar 2, 2020
- Uses a non-hidden default directory for Linux (~/wp-env)
- Allows a user to override the directory in which wp-env
  creates generated files with the WP_ENV_HOME environment
  variable.

Work towards #20180, but will need to continue in a follow-up.
@jorgefilipecosta jorgefilipecosta removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Tool] Env /packages/env [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants