Thursday, January 22, 2015

Vagrant Setup with Chef Solo

Architecture:
a)   Host Machine: Windows7
      Guest Machine: CentOS 7.0
b) Directory hierarchy on Host machine:
D:.
├───mount
├───vagrant
│   ├───.vagrant
│   │   └───machines
│   │       └───default
│   │           └───virtualbox
│   └───cookbooks
│       └───test
│           ├───attributes
│           ├───definitions
│           ├───files
│           │   └───default
│           ├───recipes
│           ├───spec
│           ├───templates
└───default
├───vbox
└───vfiles
    └───ssh


Prerequisite: 
A. create a vm "devbox" and install all the base softwares post OS install:
1. create user vagrant and key, add to wheel group for sudo access.
2. check in wheel group has "NOPASSWD" in sudoer file.
3. copy vagrant private key to your local and remove from VM
4. copy public key to "authorized_key" and "chmod 600 authorized_keys".
3.  $ cd D:\tasks\vagrant\vagrant
       $ vagrant plugin install vagrant-vbguest
# Above command is just to avoid - "Failed to mount folders in Linux guest. This is usually because the "vboxsf" file system is not available. Please verify that the guest additions are properly installed in the guest and can work properly. The command attempted was:       mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant"
4. Then package the running VM to ".vbox":
$ vagrant package --base devbox

Working: 
A) Create a new file "Vagrantfile" under "D:\tasks\vagrant\vagrant":
    $ cd D:\tasks\vagrant\vagrant
$ vi Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
  config.vm.box = "vbase64"
  config.vm.box_url = "file:///D:/tasks/vagrant/vbox/centos7_64.box"
  config.vm.network "private_network", ip: "172.16.16.16"
  config.vm.provider "virtualbox" do |vb|
vb.name = "vbase64"
vb.memory = "512"
  end

  # Enable provisioning with a shell script and chef solo
  config.vm.provision "shell", inline: <<-SHELL
     touch /tmp/justtotest
  SHELL
  config.vm.provision "chef_solo" do |chef|
chef.cookbooks_path = "cookbooks"
    chef.add_recipe "test"
  end

  # config.ssh.port = "22"
  config.ssh.username = "vagrant"
  config.ssh.private_key_path = "D:\\tasks\\vagrant\\vfiles\\ssh\\vagrant.key"
  config.vm.synced_folder "../mount", "/vagrant", disabled: false
  config.ssh.pty= true
end


B) Now do a vagrant up, this kicks off vagrant guest machine:
$ vagrant up

C) SSH to vagrant gues machine:
$ vagrant ssh   or
$ ssh 172.16.16.16

D) Run Chef Solo manually on guest(on centos):
$ sudo chef-solo -c /tmp/vagrant-chef/solo.rb -j /tmp/recipe.json
$  cat /tmp/recipe.json
{ "run_list": [  "recipe[base]",  "recipe[foo]",  "recipe[bar]",  "role[webserver]" ] }

E) Destroy VM:
$ vagrant destroy
F)  made some changes to vagrant file? just provision the VM  again, no need to "vagrant reload":
$ vagrant provision
Note: 
- nat interface would listen on 2222 and hostonly would listen on 22
- mentioned private key should be openssh compatible.

------------------------------------------------------

Troubleshooting:

# Before re-packaging some VM, make sure you removed the already provisioned box:
$ vagrant box list
$ vagrant box remove vbase64

# sudo: sorry, you must have a tty to run sudo
config.ssh.pty= true

# dont want to mount(in vagrantfile)
config.vm.synced_folder ".", "/vagrant", disabled: true

# vagrant destroy failed and causing issues with next up (VERR_ALREADY_EXISTS)
$ vagrant destroy -f

# check the ssh bit used:
vagrant ssh-config
# Make the instance name static:
 config.vm.provider "virtualbox" do |vb|
vb.name = "vbase64"
vb.memory = "512"
 end

---------------------------------------------------------------------
##############################################################################

Junk:
=> "vagrant up" output:
D:\tasks\vagrant\vagrant>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'vbase64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vbase64
==> 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: Running 'pre-boot' VM customizations...
==> 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!
No installation found.
Loaded plugins: fastestmirror, langpacks
base                                                     | 3.6 kB     00:00
extras                                                   | 3.4 kB     00:00
updates                                                  | 3.4 kB     00:00
updates/7/x86_64/primary_db    FAILED
updates/7/x86_64/primary_d 0% [                 ]  0.0 B/s |    0 B   --:-- ETA
http://mirror.digistar.vn/centos/7.0.1406/updates/x86_64/repodata/df859f8e88574d
f31f3d30c0847fb9156492a8c63af3f61fc21f9ae0798f8421-primary.sqlite.bz2: [Errno 12
] Timeout on http://mirror.digistar.vn/centos/7.0.1406/updates/x86_64/repodata/d
f859f8e88574df31f3d30c0847fb9156492a8c63af3f61fc21f9ae0798f8421-primary.sqlite.b
z2: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 s
econds')
Trying other mirror.
updates/7/x86_64/primary_d 0% [                 ]  0.0 B/s |    0 B   --:-- ETA
updates/7/x86_64/primary_d 2% [                 ]  0.0 B/s | 178 kB   --:-- ETA
updates/7/x86_64/primary_d 11% [=-              ] 880 kB/s | 741 kB   00:06 ETA
updates/7/x86_64/primary_d 22% [===-            ] 958 kB/s | 1.4 MB   00:04 ETA
updates/7/x86_64/primary_d 33% [=====           ] 1.0 MB/s | 2.0 MB   00:04 ETA
updates/7/x86_64/primary_d 43% [=======         ] 1.1 MB/s | 2.6 MB   00:03 ETA
updates/7/x86_64/primary_d 54% [========-       ] 1.1 MB/s | 3.3 MB   00:02 ETA
updates/7/x86_64/primary_d 64% [==========      ] 1.2 MB/s | 3.9 MB   00:01 ETA
updates/7/x86_64/primary_d 74% [===========-    ] 1.2 MB/s | 4.5 MB   00:01 ETA
updates/7/x86_64/primary_d 82% [=============   ] 1.3 MB/s | 5.0 MB   00:00 ETA
updates/7/x86_64/primary_d 92% [==============- ] 1.3 MB/s | 5.6 MB   00:00 ETA
updates/7/x86_64/primary_db                                | 6.0 MB   00:08
Determining fastest mirrors
 * base: mirror.nbrc.ac.in
 * extras: mirror.digistar.vn
 * updates: mirrors.viethosting.vn
Package kernel-devel-3.10.0-123.el7.x86_64 already installed and latest version
Package gcc-4.8.2-16.2.el7_0.x86_64 already installed and latest version
Package 1:make-3.82-21.el7.x86_64 already installed and latest version
Package 4:perl-5.16.3-283.el7.x86_64 already installed and latest version
Nothing to do
Copy iso file C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso into the
 box /tmp/VBoxGuestAdditions.iso
mount: /dev/loop0 is write-protected, mounting read-only
Installing Virtualbox Guest Additions 4.3.20 - guest version is
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.20 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.3.20 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module                   [  OK  ]
Building the shared folder support module                  [  OK  ]
Building the OpenGL support module                         [  OK  ]
Doing non-kernel setup of the Guest Additions              [  OK  ]
Starting the VirtualBox Guest Additions                    [  OK  ]
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.
An error occurred during installation of VirtualBox Guest Additions 4.3.20. Some
 functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => D:/tasks/vagrant/vagrant/vfiles
    default: /tmp/vagrant-chef/952de30bae9c91c5205bd436b3b8899d/cookbooks => D:/
tasks/vagrant/vagrant/cookbooks
==> default: Running provisioner: chef_solo...
    default: Installing Chef (latest)...
Generating chef JSON and uploading...
==> default: Running chef-solo...
==> default: [2015-01-22T20:13:17+05:30] INFO: Forking chef instance to converge
...
==> default: Starting Chef Client, version 12.0.3
==> default: [2015-01-22T20:13:17+05:30] INFO: *** Chef 12.0.3 ***
==> default: [2015-01-22T20:13:17+05:30] INFO: Chef-client pid: 10152
==> default: [2015-01-22T20:13:23+05:30] INFO: Setting the run_list to ["recipe[
test]"] from CLI options
==> default: [2015-01-22T20:13:23+05:30] INFO: Run List is [recipe[test]]
==> default: [2015-01-22T20:13:23+05:30] INFO: Run List expands to [test]
==> default: [2015-01-22T20:13:23+05:30] INFO: Starting Chef Run for localhost
==> default: [2015-01-22T20:13:23+05:30] INFO: Running start handlers
==> default: [2015-01-22T20:13:23+05:30] INFO: Start handlers complete.
==> default: Compiling Cookbooks...
==> default: Converging 1 resources
==> default: Recipe: test::default
==> default:   * directory[/tmp/just/shutup] action create[2015-01-22T20:13:23+0
5:30] INFO: directory[/tmp/just/shutup] created directory /tmp/just/shutup
==> default:
==> default:     - create new directory /tmp/just/shutup
==> default: [2015-01-22T20:13:23+05:30] INFO: Chef Run complete in 0.05791181 s
econds
==> default: [2015-01-22T20:13:23+05:30] INFO: Skipping removal of unused files
from the cache
==> default:
==> default: Running handlers:
==> default: [2015-01-22T20:13:23+05:30] INFO: Running report handlers
==> default: Running handlers complete
==> default: [2015-01-22T20:13:23+05:30] INFO: Report handlers complete
==> default: Chef Client finished, 1/1 resources updated in 6.168898464 seconds
D:\tasks\vagrant\vagrant>vagrant ssh
Last login: Thu Jan 22 20:13:16 2015 from 10.0.2.2
[vagrant@localhost ~]$ logout
Connection to 127.0.0.1 closed.
D:\tasks\vagrant\vagrant>
----------------------------------------------------- END ---------------------------------------------------------------