I have a Vagrant project which defines a couple of VMs. On one of them I need to run some commands to tune kernel parameters, download a file from a remote location, and start a daemon. Currently I define in a cookbooks/*/recipes/default.rb
:
execute "sudo tune-kernel-parameter"
execute "curl http://something/ > /tmp/some-file"
execute "sudo some-daemon --config /tmp/some-file"
which all works fine when I first vagrant up
. But of course if the machine reboots, none of these commands are run again, since they are not part of any init script.
I suppose I could always run vagrant reload
to reboot a VM, but I want it to just be able to reboot directly. In other words, have a persisted “service”.
Now some of the commands given above (kernel tuning) could be replaced by modifications to files in /etc
, if I wanted to spend the time to look up the right syntax for those files (after I have already done the work of verifying that the immediate commands do what I want). And for others (starting a daemon) I may be able to use a predefined service, i.e. an Ubuntu package defining a service that I can configure in /etc
and start using the service
resource.
But then there are idiosyncratic commands like the download mentioned above (which must be done before the daemon starts). Presumably I could use the template
resource to define a script in /etc/init.d/
and then use service
to start it upon provisioning (though Chef, how to run a template that creates a init.d script before the service is created implies that this is even more work than it sounds).
But is there no standard way of simply asking for a short inline script to be run both during provisioning and also upon subsequent reboots? Something in between execute
and service
, e.g.
init_script <<-END
sudo tune-kernel-parameter
curl http://something/ > /tmp/some-file
sudo some-daemon --config /tmp/some-file
END
(I am assuming that the script is idempotent, i.e. it is OK for a vagrant provision
to run a modified version again in the same VM session; if I need to killall some-daemon
first, that is easy.)