Search code examples
ubuntuvagrantbootshared-directory

vagrant + ubuntu + services + shared folders (nfs) + boot


I have a Vagrant box which setups/boots a Ubuntu 12.04 VM (Virtualbox) with a Tomcat app inside of it. The Tomcat app directory (result of a WAR file) really is a NFS shared folder in my project directory (on the host machine).

I'd like to have Tomcat start automatically on VM boot but only after NFS directory is available for to access.

Actually I got different services which should be booted in sequence (postgres -> tomcat -> apache) because on they rely on one another.

How can I achieve that?

I tried putting the following script in /etc/init.d/start_my_app:

#!/usr/bin/sh
service postgresql start
service tomcat7 start
service apache2 start

...and `chkconfig -s start_my_app on', but it doesn't seem to work. Services are not runnning.

Can anybody help me?

This question is related to this one.


Solution

  • I would recommend to use a configuration automation tool, such as Puppet for provisioning - I found it's declarative language easy to learn, and a lot more readable/maintainable than shell scripts.

    Here's my attempt at a puppet manifest to achieve what you described:

    mount { "/path/to/mountpoint":
        device  => "hostname:/path/to/nfs",
        fstype  => "nfs",
        ensure  => "mounted",
        options => "defaults",
        atboot  => true,
    }
    ->
    service { "postgresql":
        ensure => running,
    }
    ->
    service {'tomcat':
        name   => "tomcat7",
        ensure => running,
    }
    ->
    service {'apache2':
        ensure => running,
    }
    

    Four resources are described, the nfs mount and the services; the arrows (->) between them ensure that they are applied in the same order as they were written in the file (otherwise it is not guaranteed). For the details of the syntax, check the documentation, but I think this is simple enough to be self-explanatory.

    (of course it could be a lot more refined, by ensuring that the packages are installed for the services, or that configuration files are set up appropriatly, and so on)

    Vagrant has built-in support for puppet provisioning, so you could put the above code in a file called site.pp, and place it in a folder next to your Vagrantfile, e.g. manifests. Then add to the Vagrantfile:

    config.vm.provision "puppet" do |puppet|
        puppet.manifests_path = "manifests"
        puppet.manifest_file  = "site.pp"
    end
    

    I only used puppet as an example because that's what I have some experience with - some other examples of such automation tools are: Chef, Salt, Ansible