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

Make ddev_drush_settings.php actually work on host, fixes #454 #1036

Merged
merged 9 commits into from
Aug 14, 2018

Conversation

JeremySkinner
Copy link
Contributor

@JeremySkinner JeremySkinner commented Aug 7, 2018

OP was from @JeremySkinner

The Problem/Issue/Bug:

drush.settings.php never did what we actually wanted it to do, see OP #454. We wanted it to work on the host so drush commands could be executed on the host. Here's hoping that @JeremySkinner isn't the only person in history that actually wanted/needed this.

How this PR Solves The Problem:

  • ddev_drush_settings.php is generated at ddev start time for drupal and backdrop. It's automatically included if on the host and the file is available.
  • Since it's generated at start time, the db host and port can be gathered and used, so the info in there is actually correct.
  • docs mention this and how to use it.

Manual Testing Instructions:

On a drupal6/7/8 project,

  • use ddev config to regenerate settings
  • use ddev start to start it (and generate the ddev_drush_settings.php)
  • Verify the content and existence of ddev_drush_settings.php
  • Use drush to do something, for example drush sql-cli or drush ws

Automated Testing Overview:

  • TestWriteDrushConfig() was completely redone.

Related Issue Link(s):

OP #454

Thanks to @JeremySkinner for making sure we finally actually got this to work.

This is currently slated for v1.1.0 but if we can't get it reviewed before that it can come in v1.2.0

@CLAassistant
Copy link

CLAassistant commented Aug 7, 2018

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

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

This is actually the issue in #454 I think and I suspect you've fixed it properly, but it should be removed from the docs and actually fixed in the generated settings.ddev.php (which is created in templates.go)

Thanks for chasing this, I think you've actually got it! The requirement is that it try to work both inside the container and on the host though. There may be a more elegant way of determining whether we're on the host. This has been untouched for like 2 years :)

@JeremySkinner
Copy link
Contributor Author

Thanks @rfay, I didn't realise this was auto-generated as part of settings.ddev.php, just assumed it was an incorrect example. I've gone ahead and modified the templates and removed the snippet from the docs page and replaced it with some new wording.

I haven't changed the use of the DEPLOY_NAME as the way of checking we're outside the container...it seems good enough for now, and at least drush.settings.php is then usable.

Let me know if there's anything else you'd like changed.

@JeremySkinner JeremySkinner changed the title Docs: Fix example path to drush.settings.php #454: Fix path to drush.settings.php Aug 7, 2018
@rfay rfay changed the title #454: Fix path to drush.settings.php Fix usage path to drush.settings.php, fixes #454 Aug 7, 2018
@rfay
Copy link
Member

rfay commented Aug 7, 2018

This probably needs an automated test, but it's nontrivial to check the host-side drush usage on every platform, where we don't currently have drush installed. I think there are drush usage tests sprinkled around that would make sure it's working inside the web container.

@JeremySkinner
Copy link
Contributor Author

I'm happy to try and add some tests for drush usage on the host side, although I'm not familiar with go so might take me a bit of time.

@rfay
Copy link
Member

rfay commented Aug 7, 2018

That seems like a lot to ask. When we test this we'll see if we can do some tests, if you'll allow push by rfay and andrewfrench to your fork (if you haven't already).

@JeremySkinner
Copy link
Contributor Author

Thanks - sounds good. Yes you should have access to push to the branch.

Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

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

I think the biggest problem here is that the db port is transient, which will annoy people no end, as in #941 - I'm not sure this is really even work working on if we can't fix #941 because people will just stumble on broken drush all the time.

@@ -53,7 +53,7 @@ The port referenced is unique per running project, and randomly chosen from avai
**Note:** The host database port is likely to change any time a project is stopped/removed and then later started again.

### Using Drush Installation on Host Machine
If you have Drush installed on your host system, you can use it to interact with a ddev project by defining a `drush.settings.php` file at the docroot of your code base, and referencing it from your `settings.php` file. The `drush.settings.php` file should look similar to below, using the host port information for your project retrieved from `ddev describe`:
If you have Drush installed on your host system, you can use it to interact with a ddev project by defining a `drush.settings.php` file at the docroot of your code base. This will be automatically referenced from the generated `settings.ddev.php` file which is located under `web/sites/default`. The `drush.settings.php` file should look similar to below, using the host port information for your project retrieved from `ddev describe`:
Copy link
Member

Choose a reason for hiding this comment

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

Despite my statement to the contrary, I don't see ddev generating this automatically on d8. Also, the path listed "web/sites/default" is highly variable, I assume that should be <docroot>/sites/default

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 changed to <docroot>/sites/default. I tested this with D8 and the include statement is actually automatically generated in settings.ddev.php (not setttings.php)

@@ -163,7 +163,7 @@ if (empty($config_directories[CONFIG_SYNC_DIRECTORY])) {
// This determines whether or not drush should include a custom settings file which allows
// it to work both within a docker container and natively on the host system.
if (!empty($_SERVER["argv"]) && strpos($_SERVER["argv"][0], "drush") && empty($_ENV['DEPLOY_NAME'])) {
include __DIR__ . '../../../drush.settings.php';
include __DIR__ . '/../../../drush.settings.php';
Copy link
Member

Choose a reason for hiding this comment

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

This works inside the container (I think) but I don't think there's any guaranteed way it can work on the host.

If we're assuming that the drush.settings.php is to be in sites/default/drush.settings.php, wouldn't we just use

$drush_settings = dirname(__FILE__) . '/drush.settings.php';
if (is_readable($drush_settings)) {
  require $ddev_settings;
}

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'd assumed there was a reason why it was referencing the doc root, rather than looking in sites/default, but if there isn't then this seems better as all the config files are then in one place.

I assume this should still check the environment variable and only do the include if we're not inside the container? Is there any reason why we'd even want this to work inside the container, if really the reason for it is to be able to access the db from the host? If so, then should we have 2 files, eg drush.settings.php and drush.host.settings.php?

@JeremySkinner
Copy link
Contributor Author

JeremySkinner commented Aug 8, 2018

Re #941 and transient ports, personally I'd much prefer to have a single db container exposed on a single port which can then be used by all the dev sites. I know that's not the 'docker way' and others probably wouldn't like this, but would fit my workflow better.

At the moment I'm just running ddev describe and updating the port in drush.settings.php manually whenever I start the project. Would it be feasible to automate that following a successful ddev start?

@rfay rfay changed the title Fix usage path to drush.settings.php, fixes #454 Make ddev_drush_settings.php actually work on host, fixes #454 Aug 10, 2018
Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

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

Well wow, I didn't intend to actually go to town with this, but thanks for the inspiration @JeremySkinner - I think this is now what it always wanted to be. The ddev_drush_settings.php is actually generated during ddev start and it has the right port number for db; also fixed that it wouldn't have had the right host on docker toolbox. The docs are now much shorter because people don't have to manage this themselves.

I'm going to take the liberty of updating the OP testing instructions etc.

Thanks so much for the inspiration, and I hope you can test it out a bit @JeremySkinner .

@rfay rfay self-assigned this Aug 10, 2018
@rfay rfay added this to the v1.1.0 milestone Aug 10, 2018
@JeremySkinner
Copy link
Contributor Author

Thanks @rfay, that looks good and will be much more useful. Looking forward to playing with it. Thanks again for getting this in so quickly 👍👍

@rfay
Copy link
Member

rfay commented Aug 10, 2018

Artifacts from this PR can be downloaded for testing from https://circleci.com/gh/drud/ddev/3225#artifacts/containers/0 for testing - there are builds for macOS, Linux, and Windows there.

@JeremySkinner
Copy link
Contributor Author

I've been using this today and it works nicely with no issues.

Copy link
Contributor

@andrewfrench andrewfrench left a comment

Choose a reason for hiding this comment

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

Code changes look good and I was able to manually confirm that ddev_drush_settings.php were correctly generated on ddev start.

@rfay rfay force-pushed the patch-1 branch 3 times, most recently from c761007 to e1d04bd Compare August 13, 2018 17:24
@rfay
Copy link
Member

rfay commented Aug 13, 2018

@andrewfrench there are probably a couple of things I slipped in here that you'll want to look at again and make sure you're good with them:

  • Settings directory gitignore creation happens in app.Init() instead of in post-config, so we always write our own in there. It's the same for every CMS type.
  • The settings.php or main settings file is not included in the gitignore. So many people actually check in a settings.php (without db credentials), that I thought it shouldn't be in there. Of course... maybe i added that and then reconsidered.

@@ -133,6 +133,10 @@ func (app *DdevApp) CreateSettingsFile() (string, error) {
if err != nil {
util.Warning("Unable to create settings file: %v", err)
}
if err := CreateGitIgnore(filepath.Dir(app.SiteSettingsPath), filepath.Base(app.SiteLocalSettingsPath), "ddev_drush_settings.php"); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

This may be my CMS ignorance showing, but isn't this very Drupal-centric? Do TYPO3 and Wordpress projects have a ddev_drush_settings.php file?

I suppose there's no harm in ignoring something that may not exist, but I think this block could be flagged for a CMS-specific refactor at some point.

Copy link
Member

Choose a reason for hiding this comment

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

My assumption is that it's ok to ignore something that doesn't exist to make the code simpler. Drush is for Drupal only, but I guess it shouldn't be offensive to people to find a non-their-CMS file gitignored.

This is actually undoing the previous CMS-specific approach, which involved 4x duplicated code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha, I see that now. That's fine with me.

Copy link
Contributor

@andrewfrench andrewfrench left a comment

Choose a reason for hiding this comment

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

Re-approved after changes

@rfay
Copy link
Member

rfay commented Aug 14, 2018

This one fails with the same #fail as master on TestDdevRestoreSnapshot, but only there, and only on Windows 10 Home. We'll definitely have to find out what's going on there, hopefully tomorrow, but it's not related to this PR. I'm going to go ahead and pull it.

@rfay rfay merged commit a6171c0 into ddev:master Aug 14, 2018
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.

None yet

4 participants