Install Vagrant and free your Machine (more memory for Games!!)

Why is it necessary?

I am using Mac OS 10.7 for my development machine, and using was using Homebrew to install all Open Source Development needs. But there’s some drawback about installing those Open Source tools on Mac Os, those are:

  1. I install many backgrond-services to serve my development needs (nginx, mysql, pgsql, mongodb, redis). And in mac – to turn those off is not as easy as `sudo service nginx stop`
  2. Most Open Source Stack are developed for NIX*, so on some rare-case compiling source from scratch will need `some` manual hacks to be compiled on Mac environment
  3. On some rare case I broke Mac’s default PHP and Ruby (specially the basic gems that Mac use for their internal Server Tool on Mac Mini)
  4. Mac does not emulate my EC2 environment, and I want to match local and server environment as close as possible so I can do CI properly
  5. My team need whole day to setup a development machine to fit some project needs – and when they jump to another project – there’s many left-over. I need faster machine setup and clean environment
  6. Sometimes developers still need assistance in `setup` a perfect environment, not all developer familiar with #devops aspect :D

And there’s Vagrant to the rescue, combined with VirtualBox and become an awesome Development tools. The Pros are:

  1. My development tools are contained inside a box, I can turn on, suspend and stop a box easily
  2. Each box contain one OS, so I can install Ubuntu, Centos, AppScale, Heroku Celadon Cedar or even Windows Server 2008 R2 – Datacentre full (go here for Vagrant compatible base boxes)
  3. Vagrant support Chef and Puppet (and even simple bash script) to init my Box. So it’s all automated.
  4. I can tell developers to just clone a project and then `vagrant up`within minutes they will have the complete development tools on their machine (no more googling for tutorials or resolving issues)
  5. I can suspend a vagrant box anytime and resume it later, it will free my memories for a quick LAN game session (That is why the guys my office have Steam IDs / LOL IDs)
  6. I can jump from Mac Machine, to Windows and Linux easily since vagrant is available for those major OS platforms

Getting Started

Of course install lastest Vagrant and VirtualBox (or if VmWare is your thing.. there’s a section for Vagrant to run using VmWare.. it’s not free though).

Go to your project folder and run

> vagrant init ~/public_html/experimentation

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

It will create a Vagrantfile, basically Vagrantfile consists of configuration on how your machine-project will be handled by Vagrant.

Since most the server that I maintain are Ubuntu server, I’ll show you how to setup an Ubuntu box.

This is my standard VagrantFile

# environment
env = "local"

# Instance name prefix, so all site will be vhosted on "instanceprefix" + "appname"
instance = "vagrant.epm"

# default user:group used for www-data
user = "vagrant"
group = "vagrant"

# vm tweak
cpuexecutioncap = "100"
memorysize = "512"

Vagrant.configure("2") do |config|
 # the VM box and location (will download if not having any) - just add a box to your local
 config.vm.box = "precise64"
 config.vm.box_url = "http://files.vagrantup.com/precise64.box"

 config.vm.provider :virtualbox do |vb|
   vb.gui = false
   # cpu-execution
   vb.customize ["modifyvm", :id, "--cpuexecutioncap", cpuexecutioncap,"--memory",memorysize]
   # this is a fix for symlink in VirtualBoxFS
   vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
 end

# This is the direct IP so we can host-alias this machine
config.vm.network :private_network, ip: "10.10.11.1"

# port redirects
config.vm.network :forwarded_port, guest: 80 , host: 8080 # http - since hosting on port 80 will need ROOT
config.vm.network :forwarded_port, guest: 1000, host: 8010 # redis
config.vm.network :forwarded_port, guest: 3306, host: 8036 # mysql

# folder to share to vagrant machine
config.vm.synced_folder "./", "/var/www/appsindo-baseline"

# Chef Scripts
config.vm.provision :chef_solo do |chef|
  chef.cookbooks_path = "./provisioning/cookbooks"
  # nginx in vagrant use vagrant:vagrant user
  chef.json = {
    # core setup for file owners
    "instance" => instance,
    # base web user and group (change this to www-data on deployment/production)
    "www" => {
       "user" => user,
       "group" => group
    },

    # Webapp needed
    "webapp" => [
     {
      "server_name" => "#{instance}.business",
      "public_path" => "appsindo-baseline/business/public/",
      "log_path" => "appsindo-baseline/log/",
      "env" => env
     }
    ]
 }

 # the basic custom recipes
 chef.add_recipe("appsindo-ephemeral")
 chef.add_recipe("appsindo-ephemeral::nginx")
 chef.add_recipe("appsindo-ephemeral::node")
 chef.add_recipe("appsindo-ephemeral::redis")
 chef.add_recipe("appsindo-ephemeral::mysql")
 chef.add_recipe("appsindo-ephemeral::phpmyadmin")
 chef.add_recipe("appsindo-ephemeral::integration")
 chef.add_recipe("appsindo-ephemeral::finalize")
 end
end

The most important parts are

  # the VM box and location (will download if not having any) - just add a box to your local
  config.vm.box     = "precise64"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"

I want to install Ubuntu precise64. So I change `config.vm.box` to `precise64`. and `config.vm.box_url` to `http://files.vagrantup.com/precise64.box`. Box is a machine definition that will be used as a base-template. By using the `box` concept I can create few box and serve it on my company server (e.g. “precise64_with_nginx_and_nodejs”, “windows_server_plain”, “precise64_plain”) so the team can use appropriate boxes depend on the projects.

I also add

  config.vm.provider :virtualbox do |vb|
      vb.gui          = false
      # cpu-execution
      vb.customize ["modifyvm", :id, "--cpuexecutioncap", cpuexecutioncap,"--memory",memorysize]
      # this is a fix for symlink in VirtualBoxFS
      vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
  end

The `vb.customize.modifyvm` is to tweak the Virtual Machine CPU and Memory Cap. And the `vb.customize.setextradata` is some tweak for Symbolic Link issue within VirtualBox (due to Virtual File System limitation)

Make sure the network to have internal-ip (you can set it up as NAT or Bridged network.. see this)

  # This is the direct IP so we can host-alias this machine
  config.vm.network :private_network, ip: "10.10.11.1"

I set some port redirects from the guest-machine to host-machine. Not using port 80 since it will need root access on my host machine. Besides having
`http://vagrant.host:8080/` is useful as a constant reminded that I am on the local-development machine.

  # port redirects
  config.vm.network :forwarded_port, guest: 80  , host: 8080  # http
  config.vm.network :forwarded_port, guest: 1000, host: 8010  # redis
  config.vm.network :forwarded_port, guest: 3000, host: 8030  # kueue
  config.vm.network :forwarded_port, guest: 3306, host: 8036  # mysql

Then the folder to be synchronized/shared from host to guest machine.

  # folder to share to vagrant machine
  config.vm.synced_folder "./", "/var/www/appsindo-baseline"

Basically means – all changes on the current folder will be applied to `/var/www/appsindo-baseline` folder on the guest machine.

And finally – a Chef instruction to prepare my development machine, it’s a Custom Chef recipes written in Ruby to prepare my machine by installing `nginx`, `nodejs`, `redis`, `mysql`, `phpmyadmin` and also preparing an nginx site to be accessed from outside.

  # Chef Scripts
  config.vm.provision :chef_solo do |chef|
      chef.cookbooks_path = "./provisioning/cookbooks"
      # nginx in vagrant use vagrant:vagrant user
      chef.json = {
         # core setup for file owners
         "instance" => instance,
         # base web user and group (change this to www-data on deployment/production)
         "www" => {
            "user"  => user,
            "group" => group
         },
         # webapp needed
         "webapp" => [
          {
             "server_name" => "#{instance}.business",
             "public_path" => "appsindo-baseline/business/public/",
             "log_path"    => "appsindo-baseline/log/",
             "env"         => env
          }
         ]
      }

      # the basic recipes for PHP development
      chef.add_recipe("appsindo-ephemeral")
      chef.add_recipe("appsindo-ephemeral::nginx")
      chef.add_recipe("appsindo-ephemeral::node")
      chef.add_recipe("appsindo-ephemeral::redis")
      chef.add_recipe("appsindo-ephemeral::mysql")
      chef.add_recipe("appsindo-ephemeral::phpmyadmin")
      chef.add_recipe("appsindo-ephemeral::integration")
      chef.add_recipe("appsindo-ephemeral::finalize")
  end

That is all. All I need to do is typing `vagrant up` and let Chef do the installation works. It will spend some times – but it’s only for the initial works.
The next time I do `vagrant up` all the installation won’t be repeated.

If it’s a simple project, You can also use simple `bash script` to provision your machine, see the section about Using Shell to Provision Your Machine

About these ads

0 Responses to “Install Vagrant and free your Machine (more memory for Games!!)”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




About The Blog

Trying to share many settings or devops & programming problems that I find during my work-hours :)

The Writer

Erwin Maulana Saputra a Software Engineer who live in Indonesia, in love with this field since junior high-school while creating ASCII or 320x200, 256bit games :) and now somehow making money from web stuff *wink* Used to work at DotSeven and Mitrais, co-founding Kreavo and currently working for a cool-startup in Indonesia called Appsindo
LinkedIn

Follow

Get every new post delivered to your Inbox.

Join 858 other followers

%d bloggers like this: