-
-
Notifications
You must be signed in to change notification settings - Fork 579
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
Clean up hostnames when removing project data, closes #831 #1017
Clean up hostnames when removing project data, closes #831 #1017
Conversation
Quick glance looks fantastic. How about a --all which would only remove those that match our default domain (and maybe that aren't currently active? or could only be executed when nothing is running?) |
Wow... as a side comment, thank you for such thoroughness on the coverage as well as the testing instructions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preparation
git checkout 2de58d64
make darwin
ddev version
=> v1.0.0-8-g2de58d64
Testing
- Manual Add/Remove
- Add:
- No sudo: "Could not write hosts file: open /etc/hosts: permission denied"
sudo ddev hostname test.host 1.2.3.4
- Verfied new line "1.2.3.4 test.host" in /etc/hosts
sudo ddev hostname test2.host 1.2.3.4
- Verfied line updated to "1.2.3.4 test.host test2.host" in /etc/hosts
sudo ddev hostname test.host 1.2.3.4
- Receive message "Hostname already exists in hosts file"
- Remove:
- No sudo: "Could not write hosts file: open /etc/hosts: permission denied"
- Note this one doesn't appear as an error while the "add" variation is colored red.
sudo ddev hostname test2.host 1.2.3.4 --remove
- Verfied edit line "1.2.3.4 test.host" in /etc/hosts
sudo ddev hostname test2.host 1.2.3.4 --remove
- Verfied no more entries for "1.2.3.4"
- No sudo: "Could not write hosts file: open /etc/hosts: permission denied"
- Add:
- Automatic with Site Start/Stop/Remove
- Deleted all *.ddev.local 127.0.0.1 entries in /etc/hosts
- Normal ddev config w/stock D7.59 site
- Verified base entry to /etc/hosts
- Uncommented the stock additional hostname and FQDN entries in config.yml
- Verified additional entries: "127.0.0.1 drupal-7.59.ddev.local example.com sub1.example.com somename.ddev.local someothername.ddev.local"
- Created another site and verified it's entry into /etc/hosts
ddev rm --all
- No modification to /etc/hosts
ddev rm --remove-data --all
- This doesn't seem happy, resulting in "Illegal option combination: --all and --remove-data" with
ddev rm
- This doesn't seem happy, resulting in "Illegal option combination: --all and --remove-data" with
ddev rm --remove-data
- Verified the removal of /etc/host entries for the project
Recommendations
- We probably want to mention in
ddev hostname --help
that this has security implications and requires sudo/root access. - I'm not sure why "Illegal option combination: --all and --remove-data" with
ddev rm
- I'm a fairly avid
ddev rm --remove-data
user and think that all the sudo requests and worry that all the /etc/hosts sudo requests will become annoying. Also thinking of DDEV-UI as having to get prompted there as well. I'm curious to release this as is and see what the feedback is, but I'm guessing a flag might be less invasive.
|
Thanks for the follow-ups. Everything you said makes sense and I'm +1 |
@rickmanelius, a failed host name removal is now logged as an error and returns 1, just like a failed addition. @rfay, I added the |
;TLDR:
First thing I wanted to do was use --fire-bazooka (and thanks for adding that). But:
Note that it shows many successes, but nothing actually worked (as it tells us at the very end) I'd hate to have Also, remember that this is a really serious problem for windows users, so we may have to help them along the way on it. FInally, really important: If we don't have a reason to believe ddev put the hostname in there, we shouldn't remove it (That's why I was suggesting filtering on ddev.local - or the configured TLD.) I saw this:
and realized that ddev could have cleaned up some things it absolutely didn't own. |
Good points @rfay, this will now only remove |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took this for a spin and people will love it. I had minor, minor request(s) on user prompts, but the code looks good and it's a great improvement. I guess we should make sure we're sure that ddev rm
shouldn't remove the hosts entry. On Windows, with capacity for only 10 entries, that might be important. OTOH, we can probably let them fend for themselves with the ddev hostname -A
command. Can we add an alias "--fire-bazooka"? (drush registry-rebuild
still has a --fire-bazooka to do everything it can think of to do :) )
cmd/ddev/cmd/hostname.go
Outdated
// Attempt to write the hosts file first to catch any permissions issues early | ||
if err := hosts.Flush(); err != nil { | ||
rawResult := make(map[string]interface{}) | ||
detail := fmt.Sprintf("Could not write hosts file: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just prompt them with what to do about it,
Please use "sudo hostname " or execute with administrative privileges.
}, | ||
} | ||
|
||
// addHostname encapsulates the logic of adding a hostname to the system's hosts file. | ||
func addHostname(hosts goodhosts.Hosts, ip, hostname string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a relic of long ago that all this has remained in cmd instead of getting refactored into pkg. OK with me, and this is a very, very isolated command.
@@ -887,13 +887,18 @@ func (app *DdevApp) Down(removeData bool) error { | |||
return fmt.Errorf("failed to remove %s: %v", app.GetName(), err) | |||
} | |||
|
|||
// Remove data/database if we need to. | |||
// Remove data/database/hostname if we need to. | |||
if removeData { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth just a small conversation on whether we should just do this on regular rm as well. I guess the argument against is that people hit sudo too often.
bd135db
to
683eaae
Compare
Still looks great to me. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @andrewfrench. Thanks for your work on this. I confirmed that the bad combination of "Illegal option combination: --all and --remove-data", although I'm still not sure it's an invalid use case. Still, we can handle that in a following PR as that is not necessary here. I can confirm the behavior of a previous review as well as the --remove-inactive
removing inactive *.ddev.local. It's worth noting that FQDN entries will not get purged this way, but that's not really possible until we have a metadata storage for project info.
Looks great! Let's get merged in.
The Problem/Issue/Bug:
#831: The user's hosts file eventually becomes polluted after having many project hostnames added to it. Currently, ddev makes no effort to remove a hostname entry after it has been added.
How this PR Solves The Problem:
Add the ability to remove a hostname via the
ddev hostname
command:Remove hostname entries from the user's hosts file on
ddev remove --remove-data
. Removing a hostname onddev remove --remove-data
andddev hostname --remove
mimic the output and structure of existing hostname addition code.User-facing output in
ddev hostname
has been improved.Manual Testing Instructions:
ddev hostname test.host 1.2.3.4
and ensure the hostname has been added to the hosts fileddev hostname test.host 1.2.3.4 --remove
and ensure the hostname has been removed from the hosts fileTest the above steps with and without
sudo
and the--json-output
to inspect output and error logging.ddev start
on the project and ensure the new hostnames have been added to the hosts filesddev remove --remove-data
on the project and ensure the project's hostnames have been removed from the hosts fileTest the above steps with and without
DRUD_NONINTERACTIVE
to see non-interactive instruction logging.Automated Testing Overview:
Related Issue Link(s):
Release/Deployment notes: