Running shell after first /vagrant mount

676 views
Skip to first unread message

valentin bancarel

unread,
Feb 20, 2016, 10:23:59 AM2/20/16
to Vagrant
Hi there, I'm new to vagrant, but already love it !! I just configured a MongoDB, nodejs server in no time. On vagrant up, I install node and mongo via a bootstrap file, then my /vagrant folder is mount and synchronized, that's perfect. But what I want now, after that done, is to run a command ("npm install") from that directory to install node_modules given on a package.json automatically synced from my host machine.

I saw some option with the upstart event, and I think I'll be using that, but I'm not a shell expert, and found this script on Github:

#!upstart
description "Restart nginx after synced folders mounted"
author      ".silent"

start on vagrant-mounted

# Automatically restart process if crashed
respawn

# Essentially lets upstart know the process will detach itself to the background
expect fork

# Run before process
pre-start script
end script

# Start the process
script
    service nginx restart
end script
 I suppose I have to replace the 'service nginx restart' line by 'cd /vagrant; npm installservice ' but I have no idea about what the others commands are intended to, nor how to execute that script... So any help would be really appreciate. Thanks

Alvaro Miranda Aguilera

unread,
Feb 20, 2016, 10:32:48 PM2/20/16
to vagra...@googlegroups.com
Hello.

If I understand corrsctly, you want to run npm install from /vagrant.

If thats the case, then you need to have an scrip. say:

provision.sh

with a context like this:

#!/bin/bash
pushd /vagrant
npm install


And tell vagrant to run it with a provisioning line.

config.vm.provision "shell", path: "provision.sh"


Is that what you are after?


Alvaro.


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/7e393032-8a8f-40e2-ab95-3a4914531bf0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

valentin bancarel

unread,
Feb 21, 2016, 9:26:39 AM2/21/16
to Vagrant
It's exactly what I want except that, this is intended to be run at first "vagrant up" and when the provision script is run the first time, the /vagrant folder isn't mounted yet.

Alvaro Miranda Aguilera

unread,
Feb 21, 2016, 8:20:05 PM2/21/16
to vagra...@googlegroups.com
Hello,

Well, that is the default user experience, so at somepoint this is not working.

Can you share your Vagrantfile?

And also a gist (gist.githun.cob) of the the following output:

vagrant destroy (this will delete the box)
vagrant up

Thanks!
Alvaro.


On Mon, Feb 22, 2016 at 3:26 AM, valentin bancarel <bancarel...@gmail.com> wrote:
It's exactly what I want except that, this is intended to be run at first "vagrant up" and when the provision script is run the first time, the /vagrant folder isn't mounted yet.
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.

valentin bancarel

unread,
Feb 22, 2016, 7:20:47 AM2/22/16
to Vagrant
Hi Alvaro, here is the outputs of the two commands you asked, and also the content of my Vagrantfile and the provisioning script I'm using: https://gist.github.com/BancarelValentin/67ab99557821c91ce294

What I need now, is to run the commands in the "Extra scripts to be run after folder mount" after that folder is created

Alvaro Miranda Aguilera

unread,
Feb 22, 2016, 10:00:38 AM2/22/16
to vagra...@googlegroups.com
Hello,

Seems those commands are not part of the main script.

So you could create a new script. ie extra.sh
and add a new line to the Vagrantfile

ie:

config.vm.provision :shell, :path => "extra.sh"

Alvaro.


valentin bancarel

unread,
Feb 22, 2016, 10:15:24 AM2/22/16
to Vagrant
I tried that, but as I explain in my first message, on the vagrant up, when vagrant execute the provision script, /vagrant isn't created yet.

Alvaro Miranda Aguilera

unread,
Feb 22, 2016, 7:06:48 PM2/22/16
to vagra...@googlegroups.com
Hello

Can you unzip the attached, run `vagrant up` and share the last 20 lines?

It should be something like this:

==> default: Running provisioner: shell...

    default: Running: /var/folders/vh/plvhbdqx35ncp68sk1jvs0s80000gp/T/vagrant-shell20160222-39958-1sbyxsl.sh

==> default: stdin: is not a tty

==> default: /vagrant

==> default: will run npm install

==> default: supervisor server.js

z2-3:t kikitux$ 


Thanks

Alvaro.


extra.zip
extra.zip

valentin bancarel

unread,
Feb 23, 2016, 12:39:12 PM2/23/16
to Vagrant

==> default: Running provisioner: shell...

    default: Running: /var/folders/b3/ndmwgg0d12d1wt10z6pg2sxw0000gq/T/vagrant-shell20160223-3619-zzkjqe.sh

==> default: stdin: is not a tty

==> default: /vagrant

==> default: will run npm install

==> default: supervisor server.js


Alvaro Miranda Aguilera

unread,
Feb 23, 2016, 9:17:57 PM2/23/16
to vagra...@googlegroups.com
So, it works.

its able to change into /vagrant
and its able to run the extra script.

Now you need to be able to put some code, and edit the extra.sh

you can do like this:

vagrant ssh
cd /vagrant
bash extra.sh

So you will be just using vagrant to run the script.

Errors you may hace are:

- Symlink fails, This is because virtualbox fs is not a real fs. Workaround will be use Git, and the script cloning or copying the code to a a folder in a real FS
- Webserver fails to read updated files. This is because Virtualbox fs doesn't implement all the posix calls and stuff that tells the application there has been a change

I happy to help, just we need to see where the error is coming from.

If you can put the Vagrantfile and scripts on a github, and you hit errors, issues, feel free to share logs, and I can check from the repo.

Thanks
alvaro.


Reply all
Reply to author
Forward
0 new messages