devopsgroup-io / vagrant-hostmanager

:pencil: A Vagrant plugin that manages hosts files within a multi-machine environment.
Mozilla Public License 2.0
1.46k stars 148 forks source link

Looking for work around for duplicate hostnames; Multiple VM's from the same Vagrantfile. #245

Open henry911 opened 6 years ago

henry911 commented 6 years ago

I haven't been able to find a work around for this yet.

Issue: The same Vagrantfile may be used to start multiple instances on the same machine. While vagrant-hostmanager works great at putting in the proper IP addresses, the issue is, if there's already a VM running with the hostname of 'vagrant-test', hostmanager will just add another entry in the hosts file with the same hostname. Which leaves me looking for a solution that will concatenate a prefix or suffix to the hostname, if one already exists.

Example:

## vagrant-hostmanager-start id: 2e62976e-026b-4457-9195-f4f11800946a
172.28.128.3    vagrant-test
## vagrant-hostmanager-end

## vagrant-hostmanager-start id: 361c642a-9fb7-492a-aa85-fb51d2d1b01f
172.28.128.4    vagrant-test
## vagrant-hostmanager-end

So far my best idea is to detect the host OS, the based on the host OS, read the host file, checking for exists entries of the defined hostname in the vagrant file. If found add an incrementing prefix/suffix. Which should result in something like:

## vagrant-hostmanager-start id: 2e62976e-026b-4457-9195-f4f11800946a
172.28.128.3    vagrant-test
## vagrant-hostmanager-end

## vagrant-hostmanager-start id: 361c642a-9fb7-492a-aa85-fb51d2d1b01f
172.28.128.4    vagrant-test2
## vagrant-hostmanager-end

Just wondering if I'm completely missing a simple solution to do this, as I was considering modifying the hostmanager plugin locally to attempt to do this. Obviously I can easily do something like communicating to the host machine and running num=$(cat /etc/hosts | grep "emulator-api" | wc -l | sed -e 's/[\t ]//g;/^$/d') && sum=$(($num + 1)) && echo $sum (if the host machine is linux/osx) which brings me to the second issue I need this to work on both *nix OS's and Windows, I'm sure I can make a Windows equivalent, but again, there might be an easier way to accomplish this.

What do you all suggest?