1

I am using vagrant driver and virtualbox provider for testing serverspec. I need to execute some application related script before executing kitchen converge.

I have tried using vagrant provisioning, but I am trying to keep application configuration separately from vagrant configuration.

Is there any way where we can pass some commands to .kitchen.yml file in kitchen? to execute some commands before converging cookbooks.

Deepali
  • 11
  • 3

5 Answers5

1

If you are trying to run the command on the host system there is pre_create_command. If you mean run something on the guest, that isn't supported. You would be best off making a test fixture recipe or cookbook and adding it to the guest node's run list.

coderanger
  • 52,400
  • 4
  • 52
  • 75
1

As suggested by Andy, I would insert a pre_converge lifecycle hook in the kitchen.yml file, this example helps me to have updated packages deployed for my Ubuntu kitchen VMS:

lifecycle:
  pre_converge:
    remote: sudo apt-get update
Steve Baroti
  • 188
  • 7
0

@coderanger's suggestion is probably the best approach.

Alternatively, you could try one of the following:

Custom Vagrant box

If the application script is always the same, you could consider creating your own custom Vagrant box were the script has already been executed.

Abuse chef_omnibus_url

.kitchen.yml:

driver:
  name: vagrant

provisioner:
  name: chef_zero
  chef_omnibus_url: "uri_to_your_script/script.sh"
...

script.sh:

#!/bin/sh

# Your script goes here

curl https://www.chef.io/chef/install.sh | sh

(https://www.chef.io/chef/install.sh is the default value for chef_omnibus_url)

Fabrice Devaux
  • 356
  • 1
  • 3
  • I liked this suggestion, but it won't work in recent version: `chef_omnibus_ulr` is deprecated and would be removed – Lukino Jun 21 '19 at 14:19
0

I like both answers, but they didn't fit my case as well as yours: * 1st deprecated * 2nd not what you need, but VERY good point

However, my solution might help you.

As this is - I would guess now - for TESTING only, then I suggeste to your create a new cookbook (e.g. test_kitchen_support), which you run 1st in run list. You can install all you need there.

This way I found it very niet and it is just testing, so production would use only cookbook.

My problem was that I was sick of creating own vagrant boxes and most that I found didn't suits me well (e.g. they have some hardcodings, or they were very tiny OS version with some default packages missing)

I hope this help somebody.

Lukino
  • 1,407
  • 13
  • 14
0

The following Vagrantfile (placed in the root of my cookbook's folder) worked for me with Chef's test-kitchen:

###########################################################################################################
# The in-line script that will be used to grow our disk partition to the size specified in our Vagrantfile.
###########################################################################################################
$script = <<-SCRIPT
#!/bin/bash
ROOT_DISK_DEVICE="/dev/sda"
ROOT_DISK_DEVICE_PART="/dev/sda5"
LV_PATH=`sudo lvdisplay -c | sed -n 1p | awk -F ":" '{print $1;}'`
FS_PATH=`df / | sed -n 2p | awk '{print $1;}'`
ROOT_FS_SIZE=`df -h / | sed -n 2p | awk '{print $2;}'`
echo "The root file system (/) has a size of $ROOT_FS_SIZE"
echo "> Increasing disk size of $ROOT_DISK_DEVICE to available maximum"
sudo parted /dev/sda resizepart 2 100%
sudo parted /dev/sda resizepart 5 100%
sudo pvresize $ROOT_DISK_DEVICE_PART
sudo lvextend -l +100%FREE $LV_PATH
sudo resize2fs -p $FS_PATH
ROOT_FS_SIZE=`df -h / | sed -n 2p | awk '{print $2;}'`
echo "The root file system (/) has a size of $ROOT_FS_SIZE"
exit 0
SCRIPT

# Fail if the vagrant-disksize plugin is not installed
unless Vagrant.has_plugin?("vagrant-disksize")
  raise 'vagrant-disksize is not installed!'
end

Vagrant.configure("2") do |config|
  config.disksize.size = "150GB"
  config.vm.provision "shell", inline: $script
end

Along with the following little snippet added to my kitchen.yml:

  vagrantfiles:
    - Vagrantfile
  provision: True

Place that directly beneath:

driver:
  name: vagrant

Replace the script value with whatever you want... this one was used to extend the filesystem of an Ubuntu 20.04 virtualbox image.