You can find the files for this step of the workshop here:
The directory in the repository containts two vagrant setups. One is the cached variant and the other one is the uncached variant.
By now, we've learned how to setup a virtual host with the apache cookbook. As more services you install and cookbooks you use, the longer the provisioning will take. Implementing DevOps can loose acceptance very quickly when waiting times are to long. To prevent this, you should also have a look on the performance from the beginning.
One step, when you use linux or mac os as host system of your VM, you can use NFS for your Vagant shared folder.
The virtual box file system is slow. In addition vagrant has the possibility to include the shared folder with NFS:
config.vm.synced_folder ".", "/vagrant", nfs: true
NFS is only an option for linux of mac os host systems. When you want to use your VagrantFile on multiple plattforms, you can activate NFS dynamically based on the host system. The following snipped show how to activate NFS on MacOs and Linux only:
host = RbConfig::CONFIG['host_os'] # Use nfs when running on a unix system if host =~ /darwin/ config.vm.synced_folder ".", "/vagrant", nfs: true elsif host =~ /linux/ config.vm.synced_folder ".", "/vagrant", nfs: true else config.vm.synced_folder ".", "/vagrant", mount_options: ['dmode=777','fmode=666'] end
Only using NFS allready gives you a performance boost. This advantage can be increased by using NFS caching inside the box.
The first step is to activate the NFS Caching in your VagrantFile:
config.vm.synced_folder ".", "/vagrant", nfs: true, mount_options: ['rw', 'vers=3', 'tcp', 'fsc']
Inside you box you need to install the NFS caching service. This can be done like this in your recipe:
package "cachefilesd" do action :install end file "/etc/default/cachefilesd" do content <<-EOS RUN=yes EOS action :create mode 0755 end
You can use the resources of your development system in a optimal way, when you assign as much CPU and RAM as possible to your vagrant box. This can also be done automatically in the vagrant file, by getting the information from your host system.
Depending on your host system, you can read with shell command how many cpu cores and ram the system has. Depending on this information, you can assign a part of it, to your box.
In this example we assigned a quater of your memory and all cpu core to the virtual box system:
config.vm.provider :virtualbox do |v| if host =~ /darwin/ cpus = `sysctl -n hw.ncpu`.to_i ram = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / 4 elsif host =~ /linux/ cpus = `nproc`.to_i ram = `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024 / 4 else cpus = 2 ram = 4096 end v.customize ["modifyvm", :id, "--cpus", cpus] v.customize ["modifyvm", :id, "--memory", ram] v.gui = true end
DevOps helps us, to be up-to-date and to get provides the possiblity, to get the latest software with each "vagrant up". During the provisioning apt packets, tar.gz archives. git resources and others get downloaded from the internet.
Connections are pretty fast nowadays but i still makes sence to use some caching here to save bandwidth.
The vagrant cachier plugin hooks into vagrant and caches resources for a second installation. These caches can be used per box, or also across multiple boxes. Resourcen then only need to be downloaded once. The second download can be delivered from the lokal disk.
The first step is, to install the vagrant cachier plugin:
vagrant plugin install vagrant-cachier
The following change in the VagrantFile is needed to use the cachier plugin, if it is installed:
# Enable caching if plugin installed if Vagrant.has_plugin?("vagrant-cachier") config.cache.scope = :box end
To messure the run time, i did ten provisioning runs with and without cache:
And here as a visual chart, the average run time for one run in minutes: