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

Provision falsely claim to add domain to hosts file #917

Closed
jonathan-dejong opened this issue Jun 7, 2016 · 7 comments
Closed

Provision falsely claim to add domain to hosts file #917

jonathan-dejong opened this issue Jun 7, 2016 · 7 comments

Comments

@jonathan-dejong
Copy link

jonathan-dejong commented Jun 7, 2016

Hey!

Been experimenting with dynamically creating the necessary files for nginx and hostsupdater.
Creating the nginx conf file works great!

#Create nginx file automatically
    cat <<-EOF >vvv-nginx.conf
    server {
        listen          80;
        listen          443 ssl;
        server_name     $SITE_URL;
        root            {vvv_path_to_folder}/htdocs;
        include         /etc/nginx/nginx-wp-common.conf;
    }
    EOF

However creating the vvv-hosts file causes trouble.

echo "Create hosts file automatically"
    cat <<-EOF >vvv-hosts
    $SITE_URL
    EOF

The file is created and it seems to be located BUT even tho the output claims that it's being added to the hosts file on my local mac it is not. I have to run vagrant reload for it to actually get written.
So while the output from doing a vagrant provision says:
==> default: * Added testing.wordpress.dev from /srv/www/projects/testing/vvv-hosts
it does not. If I immediately after the provision run a vagrant reload this is output:
==> default: adding to (/etc/hosts) : 192.168.50.4 testing.wordpress.dev # VAGRANT: fc2950b8c4cf1501bfc5aecbd3f2c632 (default) / 58fec294-5584-4dfd-bf5a-9878f76900b8
and it actually gets added.

I figure this must be something in either VVV or the hostsupdater plugin? To me it sounds like the actual update of /etc/hosts happens before the file is created and then the output to the terminal happens after the file is created?

Anyone with any ideas on this? Would love to be able to create the vvv-hosts file dynamically as well.. then it would just be the .sh file (and our own composer file) for each new project.

thanks!

@jonathan-dejong
Copy link
Author

So did a bit more digging and found that the output from vagrant provision is on row 766 in vvv/provision/provision.sh (if it's not been moved around.. not sure I run the latest version).
I'm not well versed in bash etc. and not sure wether it actually adds anything but it looks to me like it just echoes to terminal..?

# Parse any vvv-hosts file located in www/ or subdirectories of www/
  # for domains to be added to the virtual machine's host file so that it is
  # self aware.
  #
  # Domains should be entered on new lines.
  echo "Cleaning the virtual machine's /etc/hosts file..."
  sed -n '/# vvv-auto$/!p' /etc/hosts > /tmp/hosts
  mv /tmp/hosts /etc/hosts
  echo "Adding domains to the virtual machine's /etc/hosts file..."
  find /srv/www/ -maxdepth 5 -name 'vvv-hosts' | \
  while read hostfile; do
    while IFS='' read -r line || [ -n "$line" ]; do
      if [[ "#" != ${line:0:1} ]]; then
        if [[ -z "$(grep -q "^127.0.0.1 $line$" /etc/hosts)" ]]; then
          echo "127.0.0.1 $line # vvv-auto" >> "/etc/hosts"
          echo " * Added $line from $hostfile"
        fi
      fi
    done < "$hostfile"
  done

@lkwdwrd
Copy link
Contributor

lkwdwrd commented Jun 7, 2016

The hosts logic in the provisioning script adds hosts to the internal hosts file (the guest). The hostsupdater plugin, which adds hosts entries to the external (the host) machine, runs prior to provisioning. The only way to add hosts via the hostsupdater if you create the vvv-hosts file during provisioning is to run vagrant reload or re-running vagrant up.

This is generally why there are tools like vv or my generator-vvv which work to create those files outside the VM before running vagrant commands or provisioning scripts.

@jonathan-dejong
Copy link
Author

jonathan-dejong commented Jun 7, 2016

@lkwdwrd Thanks for some clarification..
So when I have a vvv-hosts file present it works because the hostsupdater runs on vagrant provision but before our provision script? Because if there is a manually created vvv-hosts file it works.

Is there a reason why it would need to run before the provision script? If it's only task is to add entries to the external machine it seems it could just as well run after the provision script.

I'm not really keen on piling on more tools to a workflow that already works great for us just for this caveat. We've also customized the provisioning to work for us with composer loading in private repos etc.

@lkwdwrd
Copy link
Contributor

lkwdwrd commented Jun 7, 2016

I don't think it's necessarily a requirement, but hostsupdater is a vagrant plugin and I'm not much of a rubyist, nor have I dug into the depths of vagrant to see if there is a later hook available. The hostsupdater plugin is also somewhat abandoned at this point, one of the reasons I'd like to see us replace it with Vagrant Ghost - a more updated fork of the plugin. That still runs on the same hook, though, not after provisioning.

@lkwdwrd
Copy link
Contributor

lkwdwrd commented Jun 7, 2016

Also, to be it clear -- Vagrant Hostsupdater and Vagrant Ghost are separate projects from VVV, so it's not really something this project can change.

@jonathan-dejong
Copy link
Author

Yeah. Sorry if I sounded snarky :) I know they're separate projects but like you I'm not much of a rubyist so I figured I throw out the question. Now I got your two cents and it sounds like I'll be keeping the vvv-hosts file. No biggie!

@lock
Copy link

lock bot commented Feb 23, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Feb 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants