Anton Zhdanov
Anton Zhdanov

Reputation: 175

Share one Vagrant instance between different directories

I have a few directories with different Mercurial histories that I am working on in parallel. They all have the same Vagrantfile so it would be natural to use just one instance for all of them. But when I run "vagrant up" in a new directory, it starts from linking the existent VM, setting up the environment, and so on. How do I share the Vagrant instance between different directories?

UPDATE: my directory structure:

\
 Vagrantfile
 puppet
  *.pp
 support
  nginx.conf
  uwsgi.development.ini
 other_repo_related_files_and_dirs

Upvotes: 4

Views: 1640

Answers (2)

Robert
Robert

Reputation: 10943

Well, if you want to share some directories with the same Vagrant's instance, you can configure the Vagrantfile.

This is an example with two VM (app and web), using the same box (ubuntu-12.04) and the same Vagrantfile. Each instance have two folders (one folder by VM).

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.define 'app' do |app_config|
    app_config.vm.box = 'ubuntu-12.04'
    app_config.vm.host_name = 'app'
    app_config.vm.network "private_network", ip: "192.168.33.33"
    app_config.vm.synced_folder "app_config", "/app_config"    
  end
  config.vm.define 'web' do |web_config|
    web_config.vm.box = 'ubuntu-12.04'
    web_config.vm.host_name = 'web'
    web_config.vm.network "private_network", ip: "192.168.33.34"
    web_config.vm.synced_folder "web_config", "/web_config"
  end  
end

The app machine has an app_config folder and the web machine have a web_config folder(these folders are in the same level of the Vagrantfile file).

When you enter to each VM with the vagrant ssh command you can see each folder. This is into app machine.

roberto@rcisla-pc:~/Desktop/multiple$ vagrant ssh app
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Mon Jan 27 13:46:36 2014 from 10.0.2.2
vagrant@app:~$ cd /app_config/
vagrant@app:/app_config$ ls
app_config_file

This is into web machine.

roberto@rcisla-pc:~/Desktop/multiple$ vagrant ssh web
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Mon Jan 27 13:47:12 2014 from 10.0.2.2
vagrant@web:~$ cd /web_config/
vagrant@web:/web_config$ ls
web_config_file
vagrant@web:/web_config$

And this is the structure for my directory.

.
├── **app_config**
│   └── *app_config_file*
├── attributes
├── Berksfile
├── Berksfile.lock
├── chefignore
├── definitions
├── files
│   └── default
├── Gemfile
├── libraries
├── LICENSE
├── metadata.rb
├── providers
├── README.md
├── recipes
│   └── default.rb
├── resources
├── templates
│   └── default
├── test
│   └── integration
│       └── default
├── Thorfile
├── Vagrantfile
├── Vagrantfile~
└── **web_config**
    └── *web_config_file*

I hope this help you.

Upvotes: 6

cocheese
cocheese

Reputation: 512

Just thinking out loud here. Not sure if it's a solution that meets your demands.

If you set-up a directory structure like this

/Main
  /projects
    /mercurial_history_1 
    /mercurial_history_2
    /mercurial_history_3
  /puppet
  /modules
  /manifests
    default.pp
  Vagrantfile

I'm not sure what kind of projects you are running, but if you are running a apache webserver for example. You could specify a separate vhost for every mercurial project inside the VM. So you can point the DocumentRoot to the specific mercurial project.

For this solution you have to add the following line in the Vagrantfile

config.vm.network "private_network", ip: "22.22.22.11" <- Just an example IP

Then on you host machine you can update the hosts file with the IP and corresponding vhostname servername. It's a little bit more work, but you can add vhosts using a provisioner to make life easier ;)

This way you only have one VM running that runs al your mercurial projects

Upvotes: 1

Related Questions