Sunday, March 1, 2015

Automatic deployment and provisioning using Vagrant

Getting started

1. Download and install virtualbox
2. Install Ruby 
3. Download vagrant from https://www.vagrantup.com/downloads.html for your plattform
4. Install vagrant

Start deploying

mypc:Development myuser$ mkdir vagrant
mypc:Development myuser$ cd vagrant/
mypc:vagrant myuser$ vagrant init
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.


mypc:vagrant myuser$ ls -al
total 8
drwxr-xr-x   3 myuser  staff   102  1 Μαρ 19:03 .
drwxr-xr-x  83 myuser  staff  2822  1 Μαρ 19:03 ..
-rw-r--r--   1 myuser  staff  3016  1 Μαρ 19:03 Vagrantfile


mypc:vagrant myuser$ vagrant box add hashicorp/precise32
==> box: Loading metadata for box 'hashicorp/precise32'
    box: URL: https://atlas.hashicorp.com/hashicorp/precise32
==> box: Adding box 'hashicorp/precise32' (v1.0.0) for provider: virtualbox
    box: Downloading: https://atlas.hashicorp.com/hashicorp/boxes/precise32/versions/1.0.0/providers/virtualbox.box
==> box: Successfully added box 'hashicorp/precise32' (v1.0.0) for 'virtualbox'!

Change Vanguardfile to add vm.box type

mypc:vagrant myuser$ more Vagrantfile 
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "hashicorp/precise32"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

Deploy to virtualbox and startup

mypc:vagrant myuser$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/precise32'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Setting the name of the VM: vagrant_default_1425231325898_93253
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
    default: /vagrant => /Users/myuser/Development/vagrant
mypc:vagrant myuser$ ls -al
total 8
drwxr-xr-x   4 myuser  staff   136  1 Μαρ 19:20 .
drwxr-xr-x  83 myuser  staff  2822  1 Μαρ 19:03 ..
drwxr-xr-x   3 myuser  staff   102  1 Μαρ 19:34 .vagrant
-rw-r--r--   1 myuser  staff  3031  1 Μαρ 19:25 Vagrantfile
mypc:vagrant myuser$ ls .vagrant/
machines
mypc:vagrant myuser$ ls .vagrant/machines/
default
mypc:vagrant myuser$ ls .vagrant/machines/default/
virtualbox
mypc:vagrant myuser$ ls .vagrant/machines/default/virtualbox/
action_provision action_set_name  id               index_uuid       private_key      synced_folders
mypc:vagrant myuser$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/
New release '14.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2
vagrant@precise32:~$ uname
Linux
vagrant@precise32:~$ df
Filesystem                 1K-blocks      Used Available Use% Mounted on
/dev/mapper/precise32-root  82711212   2071888  76497040   3% /
udev                          181464         4    181460   1% /dev
tmpfs                          75600       272     75328   1% /run
none                            5120         0      5120   0% /run/lock
none                          189000         0    189000   0% /run/shm
/dev/sda1                     233191     24417    196333  12% /boot
vagrant                    487546976 309899044 177647932  64% /vagrant
vagrant@precise32:~$ pwd
/home/vagrant

Vagrant machine Shutdown

1. from inside

vagrant@precise32:/$ sudo shutdown -h now
vagrant@precise32:/$ 
Broadcast message from vagrant@precise32
  (/dev/pts/0) at 17:45 ...

The system is going down for halt NOW!
Connection to 127.0.0.1 closed by remote host.
Connection to 127.0.0.1 closed.

2. from hosting machine

mypc:vagrant myuser$ vagrant halt
==> default: Attempting graceful shutdown of VM...

Provisioning

Add the following script as bootstrap.sh in project folder:

#!/usr/bin/env bash

apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
  rm -rf /var/www
  ln -fs /vagrant /var/www
fi

Execute: vagrant reload --provision

mypc:vagrant myuser$ vagrant reload --provision
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
    default: /vagrant => /Users/myuser/Development/vagrant
==> default: Running provisioner: shell...
    default: Running: /var/folders/9b/2c8k0skx37d5kh67_vzp9r4m0000gn/T/vagrant-shell20150301-2327-1lgw9jp.sh
==> default: stdin: is not a tty
==> default: Ign http://security.ubuntu.com precise-security InRelease
==> default: Ign http://us.archive.ubuntu.com precise InRelease
==> default: Ign http://us.archive.ubuntu.com precise-updates InRelease
==> default: Ign http://us.archive.ubuntu.com precise-backports InRelease
==> default: Get:1 http://security.ubuntu.com precise-security Release.gpg [198 B]
==> default: Hit http://us.archive.ubuntu.com precise Release.gpg
==> default: Get:2 http://security.ubuntu.com precise-security Release [53.0 kB]
==> default: Get:3 http://us.archive.ubuntu.com precise-updates Release.gpg [198 B]
==> default: Get:4 http://us.archive.ubuntu.com precise-backports Release.gpg [198 B]
==> default: Hit http://us.archive.ubuntu.com precise Release
==> default: Get:5 http://us.archive.ubuntu.com precise-updates Release [194 kB]
==> default: Get:6 http://security.ubuntu.com precise-security/main Sources [124 kB]
==> default: Get:7 http://security.ubuntu.com precise-security/restricted Sources [3,759 B]
==> default: Get:8 http://security.ubuntu.com precise-security/universe Sources [33.8 kB]
==> default: Get:9 http://us.archive.ubuntu.com precise-backports Release [53.1 kB]
==> default: Get:10 http://security.ubuntu.com precise-security/multiverse Sources [1,819 B]
==> default: Get:11 http://security.ubuntu.com precise-security/main i386 Packages [527 kB]
==> default: Hit http://us.archive.ubuntu.com precise/main Sources
==> default: Hit http://us.archive.ubuntu.com precise/restricted Sources
==> default: Hit http://us.archive.ubuntu.com precise/universe Sources
==> default: Hit http://us.archive.ubuntu.com precise/multiverse Sources
==> default: Hit http://us.archive.ubuntu.com precise/main i386 Packages
==> default: Hit http://us.archive.ubuntu.com precise/restricted i386 Packages
==> default: Hit http://us.archive.ubuntu.com precise/universe i386 Packages
==> default: Hit http://us.archive.ubuntu.com precise/multiverse i386 Packages
==> default: Hit http://us.archive.ubuntu.com precise/main TranslationIndex
==> default: Hit http://us.archive.ubuntu.com precise/multiverse TranslationIndex
==> default: Hit http://us.archive.ubuntu.com precise/restricted TranslationIndex
==> default: Hit http://us.archive.ubuntu.com precise/universe TranslationIndex
==> default: Get:12 http://us.archive.ubuntu.com precise-updates/main Sources [485 kB]
==> default: Get:13 http://security.ubuntu.com precise-security/restricted i386 Packages [8,939 B]
==> default: Get:14 http://security.ubuntu.com precise-security/universe i386 Packages [114 kB]
==> default: Get:15 http://us.archive.ubuntu.com precise-updates/restricted Sources [7,981 B]
==> default: Get:16 http://us.archive.ubuntu.com precise-updates/universe Sources [112 kB]
==> default: Get:17 http://security.ubuntu.com precise-security/multiverse i386 Packages [2,651 B]
==> default: Get:18 http://security.ubuntu.com precise-security/main TranslationIndex [208 B]
==> default: Get:19 http://security.ubuntu.com precise-security/multiverse TranslationIndex [199 B]
==> default: Get:20 http://security.ubuntu.com precise-security/restricted TranslationIndex [202 B]
==> default: Get:21 http://security.ubuntu.com precise-security/universe TranslationIndex [205 B]
==> default: Get:22 http://us.archive.ubuntu.com precise-updates/multiverse Sources [9,390 B]
==> default: Get:23 http://us.archive.ubuntu.com precise-updates/main i386 Packages [914 kB]
==> default: Get:24 http://security.ubuntu.com precise-security/main Translation-en [216 kB]
==> default: Get:25 http://security.ubuntu.com precise-security/multiverse Translation-en [1,299 B]
==> default: Get:26 http://security.ubuntu.com precise-security/restricted Translation-en [2,066 B]
==> default: Get:27 http://security.ubuntu.com precise-security/universe Translation-en [65.3 kB]
==> default: Get:28 http://us.archive.ubuntu.com precise-updates/restricted i386 Packages [13.6 kB]
==> default: Get:29 http://us.archive.ubuntu.com precise-updates/universe i386 Packages [262 kB]
==> default: Get:30 http://us.archive.ubuntu.com precise-updates/multiverse i386 Packages [16.6 kB]
==> default: Get:31 http://us.archive.ubuntu.com precise-updates/main TranslationIndex [10.6 kB]
==> default: Get:32 http://us.archive.ubuntu.com precise-updates/multiverse TranslationIndex [7,613 B]
==> default: Get:33 http://us.archive.ubuntu.com precise-updates/restricted TranslationIndex [7,297 B]
==> default: Get:34 http://us.archive.ubuntu.com precise-updates/universe TranslationIndex [8,333 B]
==> default: Get:35 http://us.archive.ubuntu.com precise-backports/main Sources [5,411 B]
==> default: Get:36 http://us.archive.ubuntu.com precise-backports/restricted Sources [28 B]
==> default: Get:37 http://us.archive.ubuntu.com precise-backports/universe Sources [41.3 kB]
==> default: Get:38 http://us.archive.ubuntu.com precise-backports/multiverse Sources [5,750 B]
==> default: Get:39 http://us.archive.ubuntu.com precise-backports/main i386 Packages [5,484 B]
==> default: Get:40 http://us.archive.ubuntu.com precise-backports/restricted i386 Packages [28 B]
==> default: Get:41 http://us.archive.ubuntu.com precise-backports/universe i386 Packages [43.2 kB]
==> default: Get:42 http://us.archive.ubuntu.com precise-backports/multiverse i386 Packages [5,413 B]
==> default: Get:43 http://us.archive.ubuntu.com precise-backports/main TranslationIndex [202 B]
==> default: Get:44 http://us.archive.ubuntu.com precise-backports/multiverse TranslationIndex [202 B]
==> default: Get:45 http://us.archive.ubuntu.com precise-backports/restricted TranslationIndex [193 B]
==> default: Get:46 http://us.archive.ubuntu.com precise-backports/universe TranslationIndex [205 B]
==> default: Hit http://us.archive.ubuntu.com precise/main Translation-en
==> default: Hit http://us.archive.ubuntu.com precise/multiverse Translation-en
==> default: Hit http://us.archive.ubuntu.com precise/restricted Translation-en
==> default: Hit http://us.archive.ubuntu.com precise/universe Translation-en
==> default: Get:47 http://us.archive.ubuntu.com precise-updates/main Translation-en [385 kB]
==> default: Get:48 http://us.archive.ubuntu.com precise-updates/multiverse Translation-en [9,533 B]
==> default: Get:49 http://us.archive.ubuntu.com precise-updates/restricted Translation-en [2,982 B]
==> default: Get:50 http://us.archive.ubuntu.com precise-updates/universe Translation-en [148 kB]
==> default: Get:51 http://us.archive.ubuntu.com precise-backports/main Translation-en [4,911 B]
==> default: Get:52 http://us.archive.ubuntu.com precise-backports/multiverse Translation-en [4,838 B]
==> default: Hit http://us.archive.ubuntu.com precise-backports/restricted Translation-en
==> default: Get:53 http://us.archive.ubuntu.com precise-backports/universe Translation-en [34.4 kB]
==> default: Fetched 3,954 kB in 6s (611 kB/s)
==> default: Reading package lists...
==> default: Reading package lists...
==> default: Building dependency tree...
==> default: Reading state information...
==> default: The following extra packages will be installed:
==> default:   apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1
==> default:   libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert
==> default: Suggested packages:
==> default:   www-browser apache2-doc apache2-suexec apache2-suexec-custom
==> default:   openssl-blacklist
==> default: The following NEW packages will be installed:
==> default:   apache2 apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common
==> default:   libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert
==> default: 0 upgraded, 10 newly installed, 0 to remove and 179 not upgraded.
==> default: Need to get 1,845 kB of archives.
==> default: After this operation, 5,315 kB of additional disk space will be used.
==> default: Get:1 http://us.archive.ubuntu.com/ubuntu/ precise/main libapr1 i386 1.4.6-1 [91.3 kB]
==> default: Get:2 http://us.archive.ubuntu.com/ubuntu/ precise/main libaprutil1 i386 1.3.12+dfsg-3 [75.4 kB]
==> default: Get:3 http://us.archive.ubuntu.com/ubuntu/ precise/main libaprutil1-dbd-sqlite3 i386 1.3.12+dfsg-3 [10.2 kB]
==> default: Get:4 http://us.archive.ubuntu.com/ubuntu/ precise/main libaprutil1-ldap i386 1.3.12+dfsg-3 [7,962 B]
==> default: Get:5 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main apache2.2-bin i386 2.2.22-1ubuntu1.7 [1,329 kB]
==> default: Get:6 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main apache2-utils i386 2.2.22-1ubuntu1.7 [88.7 kB]
==> default: Get:7 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main apache2.2-common i386 2.2.22-1ubuntu1.7 [226 kB]
==> default: Get:8 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main apache2-mpm-worker i386 2.2.22-1ubuntu1.7 [2,304 B]
==> default: Get:9 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main apache2 i386 2.2.22-1ubuntu1.7 [1,488 B]
==> default: Get:10 http://us.archive.ubuntu.com/ubuntu/ precise-updates/main ssl-cert all 1.0.28ubuntu0.1 [12.3 kB]
==> default: dpkg-preconfigure: unable to re-open stdin: No such file or directory
==> default: Fetched 1,845 kB in 2s (840 kB/s)
==> default: Selecting previously unselected package libapr1.
==> default: (Reading database ... 
==> default: 29361 files and directories currently installed.)
==> default: Unpacking libapr1 (from .../libapr1_1.4.6-1_i386.deb) ...
==> default: Selecting previously unselected package libaprutil1.
==> default: Unpacking libaprutil1 (from .../libaprutil1_1.3.12+dfsg-3_i386.deb) ...
==> default: Selecting previously unselected package libaprutil1-dbd-sqlite3.
==> default: Unpacking libaprutil1-dbd-sqlite3 (from .../libaprutil1-dbd-sqlite3_1.3.12+dfsg-3_i386.deb) ...
==> default: Selecting previously unselected package libaprutil1-ldap.
==> default: Unpacking libaprutil1-ldap (from .../libaprutil1-ldap_1.3.12+dfsg-3_i386.deb) ...
==> default: Selecting previously unselected package apache2.2-bin.
==> default: Unpacking apache2.2-bin (from .../apache2.2-bin_2.2.22-1ubuntu1.7_i386.deb) ...
==> default: Selecting previously unselected package apache2-utils.
==> default: Unpacking apache2-utils (from .../apache2-utils_2.2.22-1ubuntu1.7_i386.deb) ...
==> default: Selecting previously unselected package apache2.2-common.
==> default: Unpacking apache2.2-common (from .../apache2.2-common_2.2.22-1ubuntu1.7_i386.deb) ...
==> default: Selecting previously unselected package apache2-mpm-worker.
==> default: Unpacking apache2-mpm-worker (from .../apache2-mpm-worker_2.2.22-1ubuntu1.7_i386.deb) ...
==> default: Selecting previously unselected package apache2.
==> default: Unpacking apache2 (from .../apache2_2.2.22-1ubuntu1.7_i386.deb) ...
==> default: Selecting previously unselected package ssl-cert.
==> default: Unpacking ssl-cert (from .../ssl-cert_1.0.28ubuntu0.1_all.deb) ...
==> default: Processing triggers for man-db ...
==> default: Processing triggers for ufw ...
==> default: Processing triggers for ureadahead ...
==> default: ureadahead will be reprofiled on next reboot
==> default: Setting up libapr1 (1.4.6-1) ...
==> default: Setting up libaprutil1 (1.3.12+dfsg-3) ...
==> default: Setting up libaprutil1-dbd-sqlite3 (1.3.12+dfsg-3) ...
==> default: Setting up libaprutil1-ldap (1.3.12+dfsg-3) ...
==> default: Setting up apache2.2-bin (2.2.22-1ubuntu1.7) ...
==> default: Setting up apache2-utils (2.2.22-1ubuntu1.7) ...
==> default: Setting up apache2.2-common (2.2.22-1ubuntu1.7) ...
==> default: Enabling site default.
==> default: Enabling module alias.
==> default: Enabling module autoindex.
==> default: Enabling module dir.
==> default: Enabling module env.
==> default: Enabling module mime.
==> default: Enabling module negotiation.
==> default: Enabling module setenvif.
==> default: Enabling module status.
==> default: Enabling module auth_basic.
==> default: Enabling module deflate.
==> default: Enabling module authz_default.
==> default: Enabling module authz_user.
==> default: Enabling module authz_groupfile.
==> default: Enabling module authn_file.
==> default: Enabling module authz_host.
==> default: Enabling module reqtimeout.
==> default: Setting up apache2-mpm-worker (2.2.22-1ubuntu1.7) ...
==> default:  * Starting web server apache2
==> default: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
==> default:    ...done.
==> default: Setting up apache2 (2.2.22-1ubuntu1.7) ...
==> default: Setting up ssl-cert (1.0.28ubuntu0.1) ...
==> default: Processing triggers for libc-bin ...
==> default: ldconfig deferred processing now taking place

Network setup

1. For port forwarding, add the following in Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise32"
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network :forwarded_port, host: 4567, guest: 80
end

2. Run: vagrant reload

mypc:vagrant myuser$ vagrant reload 
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 80 => 4567 (adapter 1)
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
    default: /vagrant => /Users/myuser/Development/vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run.

3. Private network
Create a private network, which allows host-only access to the machine using a specific IP.
In Vagrantfile uncomment or add:
  # config.vm.network "private_network", ip: "192.168.33.10"

4. Public network
Create a public network, which generally matched to bridged network. Bridged networks make the machine appear as another physical device on your network.
  # config.vm.network "public_network"

After vagrant reload:
mypc:vagrant myuser$ vagrant reload 
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Available bridged network interfaces:
1) en0: Ethernet
2) en1: Wi-Fi (AirPort)
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.
    default: Which interface should the network bridge to? 1
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: bridged
==> default: Forwarding ports...
    default: 80 => 4567 (adapter 1)
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/myuser/Development/vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run.

Use a default network interface - bypass question

If more than one network interface is available on the host machine, Vagrant will ask you to choose which interface the virtual machine should bridge to. A default interface can be specified by adding a :bridge clause to the network definition.

config.vm.network "public_network", bridge: 'en0: Ethernet'

mypc:vagrant myuser$ vagrant ssh
ifconfigWelcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/
New release '14.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2
ifconfig
vagrant@precise32:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9060 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4899 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:6366102 (6.3 MB)  TX bytes:344879 (344.8 KB)

eth1      Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx  
          inet addr:192.168.1.13  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:53 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:3832 (3.8 KB)  TX bytes:1152 (1.1 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)


Destroy box deployment and cleanup

Run destroy to stop the running machine Vagrant is managing and destroys all resources that were created during the machine creation process. After running this command, your computer should be left at a clean state, as if you never created the guest machine in the first place.
The Vagrantfile is not deleted so you may run later vagrant up to redeploy!

$ vagrant destroy

mypc:vagrant myuser$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
==> default: Running cleanup tasks for 'shell' provisioner...

Vagrant rebuild

kmypc:vagrant myuser$ vagrant up --provision
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/precise32'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Setting the name of the VM: vagrant_default_1425249187078_92773
==> default: Clearing any previously set network interfaces...
==> default: Available bridged network interfaces:
1) en0: Ethernet
2) en1: Wi-Fi (AirPort)
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.
    default: Which interface should the network bridge to? 1
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: bridged
==> default: Forwarding ports...
    default: 80 => 4567 (adapter 1)
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/myuser/Development/vagrant
==> default: Running provisioner: shell...
    default: Running: /var/folders/9b/2c8k0skx37d5kh67_vzp9r4m0000gn/T/vagrant-shell20150302-3021-pko1q3.sh
==> default: stdin: is not a tty
==> default: Ign http://security.ubuntu.com precise-security InRelease
...
==> default: Setting up apache2-mpm-worker (2.2.22-1ubuntu1.7) ...
==> default:  * Starting web server apache2
==> default: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
==> default:    ...done.
==> default: Setting up apache2 (2.2.22-1ubuntu1.7) ...
==> default: Setting up ssl-cert (1.0.28ubuntu0.1) ...
==> default: Processing triggers for libc-bin ...
==> default: ldconfig deferred processing now taking place

Removing a box

$ vagrant box remove hashicorp/precise32

Create second project

/Users/myuser/Development/vagrant-projects

mypc:vagrant-projects myuser$ mkdir centos65
mypc:vagrant-projects myuser$ mv centos65/ v-centos65-was8553
mypc:vagrant-projects myuser$ cd v-centos65-was8553/
mypc:v-centos65-was8553 myuser$ vargant init
-bash: vargant: command not found
mypc:v-centos65-was8553 myuser$ vagrant init
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.
mypc:v-centos65-was8553 myuser$ vagrant box add chef/centos-6.5
==> box: Loading metadata for box 'chef/centos-6.5'
    box: URL: https://atlas.hashicorp.com/chef/centos-6.5
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) virtualbox
2) vmware_desktop

Enter your choice: 1
==> box: Adding box 'chef/centos-6.5' (v1.0.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/chef/boxes/centos-6.5/versions/1.0.0/providers/virtualbox.box
    box: Progress: 33% (Rate: 117k/s, Estimated time remaining: 0:22:24)

    ==> box: Successfully added box 'chef/centos-6.5' (v1.0.0) for 'virtualbox'!

*** CHANGE Vagrantfile for chef/centos-6.5

mypc:v-centos65-was8553 myuser$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'chef/centos-6.5'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'chef/centos-6.5' is up to date...
==> default: Setting the name of the VM: v-centos65-was8553_default_1425238923568_97858
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2200 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/myuser/Development/vagrant-projects/v-centos65-was8553

Connect

mypc:v-centos65-was8553 myuser$ vagrant ssh
Last login: Fri Mar  7 16:57:20 2014 from 10.0.2.2

Vagrant box List

mypc:vagrant myuser$ vagrant box list
chef/centos-6.5     (virtualbox, 1.0.0)
hashicorp/precise32 (virtualbox, 1.0.0)



*** boxes are stored at ~/.vagrant.d/boxes

REFERENCES

No comments:

Post a Comment