Thursday, March 08, 2012

Basic Vagrant Usage

Doing a 'vagrant box add' creates a .vagrant.d/ directory in your home directory. This directory stores base boxes installed. Doing a 'vagrant init' in some other directory creates a Vagrantfile inside of that directory which specifies which base box to use and provides any other provisioning information. 

List vagrant base boxes installed:
vagrant box list
# returns list of boxnames and type
mybox (virtualbox)

Add a new vagrant base box named "my_box" based on box image:
vagrant box add my_box

Remove a base box:
vagrant box remove my_box type
# for a virtualbox use virtualbox as the type

Location of vagrant base boxes:
cd ~/.vagrant.d/

Create a directory where customizations off the of the base box should take place.  then run the following: 
cd ~/Sites/VM/test/
vagrant init
vagrant init lucid32  # as an alternative to explicitly indicate which base box to use

Example Vagrant file in ~/Sites/VM/test/Vagrantfile do |config| = "lucid32"  # specify which base box to use

Run a virtual machine.  Builds a virtual machine using box and Vagrantfile and runs it.  You must be inside the given vagrant directory (~/Sites/VM/test) in order for this to work.
vagrant up

Destroy a virtual machine. Destroy's the virtual machine:
vagrant destroy

SSH into a virtual machine:
vagrant ssh

Running a packaged virtual machine
vagrant box add my_box /path/to/the/
vagrant init my_box
vagrant up

Suspend a virtual machine (save state to disk):
vagrant suspend

Resume a suspended virtual machine:
vagrant resume

Some Customizations to the default config (Pre-vagrant 1.1): do |config|
    # this is basically a way to programmatically invoke the
    # 'VBoxManage modifyvm' command from within Vagrant 

    # NOTE: this requires a fairly new version of vagrant 
    # to use this syntax. 
    config.vm.customize ["modifyvm", :id, "--memory", 1024]

    # By default vagrant boots boxes headless.  This enables the 
    # Virtualbox GUI
    config.vm.boot_mode = :gui

    # Path to ssh private key used to log into box with 
    # vagrant ssh. If it's a relative path, it will be relative 
    # to the dir holding the Vagrantfile
    config.ssh.private_key_path = 'vagrant_id_dsa'

    # Creates a shared folder on host called myshared in the 
    # same directory as the Vagrantfile.  On the guest, it 
    # mounts the shared folder as /my/shared.  The :create 
    # setting creates the directory on the host if it doesn't
    # exist.   
    config.vm.share_folder 'my-shared', '/my/shared/', 'myshared', {:create, true}


Some Customizations to the default config (Post-vagrant 1.1): 

Vagrant.configure("2") do |config|
   # setup port forwarding :forwarded_port, guest: 8000, host: 18000, auto_correct: true :forwarded_port, guest: 5432, host: 15432, auto_correct: true

  config.vm.provider :virtualbox do |vb|
      # Don't boot with headless mode
      vb.gui = true

      # Use VBoxManage to customize the VM. For example to change memory:
      vb.customize ["modifyvm", :id, "--memory", "1024"]

     # Create a shared folder   (host path, guest path)
     config.vm.synced_folder "../data", "/vagrant_data"

Turn on Debugging
export VAGRANT_LOG=debug

List of vagrant base boxes:

Get SSH configuration for machines:

vagrant ssh-config
Host swarm_manager
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/jjasinski/Sites/test_docker_swarm/.vagrant/machines/swarm_manager/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Host swarm_node1
  User vagrant
  Port 2200
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/jjasinski/Sites/test_docker_swarm/.vagrant/machines/swarm_node1/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

No comments: