0

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.)

Community
  • 1
  • 1
Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
  • Did you find a solution to this problem? It's okay to answer your own question. Please don't forget to mark an answer as correct! :) – sethvargo Jan 03 '14 at 16:54

1 Answers1

1

You could probably use /etc/rc.local for that:

file "/etc/rc.local" do
  content <<-EOC
    sudo tune-kernel-parameter
    curl http://something/ > /tmp/some-file
    sudo some-daemon --config /tmp/some-file
  EOC
  mode 0755
  notifies :run, "execute[rc.local]"
end

execute "rc.local"
  command "/etc/rc.local"
  action :nothing
end

The execute block only declares a resource but does :nothing with it. When file runs, if it modifies the file on disk, it then notifies the execute resource to :run.

cassianoleal
  • 2,556
  • 19
  • 22